-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[V7] Add Encodable
protocol for PayPal Vault
#1492
base: paypal-checkout-encodable
Are you sure you want to change the base?
Changes from 13 commits
8006e18
d1c1cd1
14390fd
06fd294
6f0daad
428b0a4
2b072da
c7e226e
9543469
27c0a44
4a6e7d8
42af3f7
2200e5c
50c23ac
8e12bf8
8b18aeb
b903ba2
44d61f4
7f0a207
39ca19f
169ff3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -350,19 +350,21 @@ import BraintreeDataCollector | |||||
|
||||||
self.isConfigFromCache = configuration.isFromCache | ||||||
|
||||||
guard json["paypalEnabled"].isTrue else { | ||||||
guard configuration.isPayPalEnabled else { | ||||||
self.notifyFailure(with: BTPayPalError.disabled, completion: completion) | ||||||
return | ||||||
} | ||||||
|
||||||
self.payPalRequest = request | ||||||
|
||||||
guard let parameters = self.encodedPostBody(fromRequest: request, configuration: configuration) else { | ||||||
self.notifyFailure(with: BTPayPalError.failedToCreateEncodable, completion: completion) | ||||||
return | ||||||
} | ||||||
|
||||||
self.apiClient.post( | ||||||
request.hermesPath, | ||||||
parameters: request.parameters( | ||||||
with: configuration, | ||||||
universalLink: self.universalLink, | ||||||
isPayPalAppInstalled: self.application.isPayPalAppInstalled() | ||||||
) | ||||||
parameters: parameters | ||||||
) { body, _, error in | ||||||
if let error = error as? NSError { | ||||||
guard let jsonResponseBody = error.userInfo[BTCoreConstants.jsonResponseBodyKey] as? BTJSON else { | ||||||
|
@@ -401,6 +403,21 @@ import BraintreeDataCollector | |||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private func encodedPostBody(fromRequest payPalRequest: BTPayPalRequest, configuration: BTConfiguration) -> Encodable? { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if let checkoutRequest = payPalRequest as? BTPayPalCheckoutRequest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we instead do a switch here on |
||||||
return PayPalCheckoutPOSTBody(payPalRequest: checkoutRequest, configuration: configuration) | ||||||
} else if let vaultRequest = payPalRequest as? BTPayPalVaultRequest { | ||||||
return PayPalVaultPOSTBody( | ||||||
payPalRequest: vaultRequest, | ||||||
configuration: configuration, | ||||||
isPayPalAppInstalled: application.isPayPalAppInstalled(), | ||||||
universalLink: universalLink | ||||||
) | ||||||
} else { | ||||||
return nil | ||||||
} | ||||||
} | ||||||
|
||||||
private func launchPayPalApp( | ||||||
with payPalAppRedirectURL: URL, | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,6 +44,9 @@ public enum BTPayPalError: Error, CustomNSError, LocalizedError, Equatable { | |||||
|
||||||
/// 13. Missing PayPal Request | ||||||
case missingPayPalRequest | ||||||
|
||||||
/// 14. Unable to create Encodable | ||||||
case failedToCreateEncodable | ||||||
|
||||||
public static var errorDomain: String { | ||||||
"com.braintreepayments.BTPayPalErrorDomain" | ||||||
|
@@ -79,6 +82,8 @@ public enum BTPayPalError: Error, CustomNSError, LocalizedError, Equatable { | |||||
return 12 | ||||||
case .missingPayPalRequest: | ||||||
return 13 | ||||||
case .failedToCreateEncodable: | ||||||
return 14 | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -114,6 +119,8 @@ public enum BTPayPalError: Error, CustomNSError, LocalizedError, Equatable { | |||||
return "Missing BA Token for PayPal App Switch." | ||||||
case .missingPayPalRequest: | ||||||
return "The PayPal Request was missing or invalid." | ||||||
case .failedToCreateEncodable: | ||||||
return "Unable to create Encodable object" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may make it a bit more clear, but take it or leave it:
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Foundation | ||
|
||
#if canImport(BraintreeCore) | ||
import BraintreeCore | ||
#endif | ||
|
||
struct PayPalExperienceProfile: Encodable { | ||
|
||
// MARK: - Private Properties | ||
|
||
private let displayName: String? | ||
private let isShippingAddressRequired: Bool | ||
private let shippingAddressOverride: Bool | ||
|
||
private var landingPageType: String? | ||
private var localeCode: String? | ||
private var userAction: String? | ||
|
||
// MARK: - Initializer | ||
|
||
init(payPalRequest: BTPayPalCheckoutRequest, configuration: BTConfiguration) { | ||
self.displayName = payPalRequest.displayName != nil ? payPalRequest.displayName : configuration.displayName | ||
self.isShippingAddressRequired = !payPalRequest.isShippingAddressRequired | ||
|
||
if let landingPageType = payPalRequest.landingPageType?.stringValue { | ||
self.landingPageType = landingPageType | ||
} | ||
|
||
if let localeCode = payPalRequest.localeCode?.stringValue { | ||
self.localeCode = localeCode | ||
} | ||
|
||
self.shippingAddressOverride = payPalRequest.shippingAddressOverride != nil ? !payPalRequest.isShippingAddressEditable : false | ||
|
||
if payPalRequest.userAction != .none { | ||
self.userAction = payPalRequest.userAction.stringValue | ||
} | ||
} | ||
|
||
init(payPalRequest: BTPayPalVaultRequest, configuration: BTConfiguration) { | ||
self.displayName = payPalRequest.displayName != nil ? payPalRequest.displayName : configuration.displayName | ||
self.isShippingAddressRequired = !payPalRequest.isShippingAddressRequired | ||
|
||
if let landingPageType = payPalRequest.landingPageType?.stringValue { | ||
self.landingPageType = landingPageType | ||
} | ||
|
||
if let localeCode = payPalRequest.localeCode?.stringValue { | ||
self.localeCode = localeCode | ||
} | ||
|
||
self.shippingAddressOverride = payPalRequest.shippingAddressOverride != nil ? !payPalRequest.isShippingAddressEditable : false | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case isShippingAddressRequired = "no_shipping" | ||
case displayName = "brand_name" | ||
case landingPageType = "landing_page_type" | ||
case localeCode = "locale_code" | ||
case shippingAddressOverride = "address_override" | ||
case userAction = "user_action" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
#if canImport(BraintreeCore) | ||
import BraintreeCore | ||
#endif | ||
|
||
/// The POST body for v1/paypal_hermes/setup_billing_agreement | ||
struct PayPalVaultPOSTBody: Encodable { | ||
|
||
// MARK: - Private Properties | ||
|
||
private let userPhoneNumber: BTPayPalPhoneNumber? | ||
private let returnURL: String | ||
private let cancelURL: String | ||
private let experienceProfile: PayPalExperienceProfile | ||
|
||
private var billingAgreementDescription: String? | ||
private var enablePayPalAppSwitch: Bool? | ||
private var lineItems: [BTPayPalLineItem]? | ||
private var merchantAccountID: String? | ||
private var offerCredit = false | ||
private var osType: String? | ||
private var osVersion: String? | ||
private var recurringBillingPlanType: BTPayPalRecurringBillingPlanType? | ||
private var recurringBillingDetails: BTPayPalRecurringBillingDetails? | ||
private var riskCorrelationID: String? | ||
private var shippingAddressOverride: BTPostalAddress? | ||
private var universalLink: String? | ||
private var userAuthenticationEmail: String? | ||
|
||
// MARK: - Initializer | ||
|
||
init( | ||
payPalRequest: BTPayPalVaultRequest, | ||
configuration: BTConfiguration, | ||
isPayPalAppInstalled: Bool, | ||
universalLink: URL? | ||
) { | ||
if let merchantAccountID = payPalRequest.merchantAccountID { | ||
self.merchantAccountID = merchantAccountID | ||
} | ||
|
||
if let riskCorrelationID = payPalRequest.riskCorrelationID { | ||
self.riskCorrelationID = riskCorrelationID | ||
} | ||
|
||
if let lineItems = payPalRequest.lineItems, !lineItems.isEmpty { | ||
self.lineItems = lineItems | ||
} | ||
|
||
if let userAuthenticationEmail = payPalRequest.userAuthenticationEmail, !userAuthenticationEmail.isEmpty { | ||
self.userAuthenticationEmail = userAuthenticationEmail | ||
} | ||
|
||
self.userPhoneNumber = payPalRequest.userPhoneNumber | ||
self.returnURL = BTCoreConstants.callbackURLScheme + "://\(BTPayPalRequest.callbackURLHostAndPath)success" | ||
self.cancelURL = BTCoreConstants.callbackURLScheme + "://\(BTPayPalRequest.callbackURLHostAndPath)cancel" | ||
self.experienceProfile = PayPalExperienceProfile(payPalRequest: payPalRequest, configuration: configuration) | ||
|
||
if let universalLink, payPalRequest.enablePayPalAppSwitch, isPayPalAppInstalled { | ||
self.enablePayPalAppSwitch = payPalRequest.enablePayPalAppSwitch | ||
self.osType = UIDevice.current.systemName | ||
self.osVersion = UIDevice.current.systemVersion | ||
self.universalLink = universalLink.absoluteString | ||
return | ||
} | ||
|
||
if let recurringBillingPlanType = payPalRequest.recurringBillingPlanType { | ||
self.recurringBillingPlanType = recurringBillingPlanType | ||
} | ||
|
||
if let recurringBillingDetails = payPalRequest.recurringBillingDetails { | ||
self.recurringBillingDetails = recurringBillingDetails | ||
} | ||
|
||
self.offerCredit = payPalRequest.offerCredit | ||
|
||
if let billingAgreementDescription = payPalRequest.billingAgreementDescription { | ||
self.billingAgreementDescription = billingAgreementDescription | ||
} | ||
|
||
if let shippingAddressOverride = payPalRequest.shippingAddressOverride { | ||
self.shippingAddressOverride = shippingAddressOverride | ||
} | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case billingAgreementDescription = "description" | ||
case cancelURL = "cancel_url" | ||
case enablePayPalAppSwitch = "launch_paypal_app" | ||
case experienceProfile = "experience_profile" | ||
case lineItems = "line_items" | ||
case merchantAccountID = "merchant_account_id" | ||
case offerCredit = "offer_paypal_credit" | ||
case osType = "os_type" | ||
case osVersion = "os_version" | ||
case recurringBillingDetails = "plan_metadata" | ||
case recurringBillingPlanType = "plan_type" | ||
case returnURL = "return_url" | ||
case riskCorrelationID = "correlation_id" | ||
case shippingAddressOverride = "shipping_address" | ||
case universalLink = "merchant_app_return_url" | ||
case userAuthenticationEmail = "payer_email" | ||
case userPhoneNumber = "phone_number" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can also remove
let json = configuration.json
above on line 346