Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
agedd committed Feb 19, 2025
1 parent 48aa825 commit 4b22a6d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Sources/BraintreePayPal/BTPayPalCheckoutRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import BraintreeCore

/// Optional: Contact information of the recipient for the order
public var contactInformation: BTContactInformation?

/// Optional: Server side shipping callback URL to be notified when a customer updates their shipping address or options. A callback request will be sent to the merchant server at this URL.
public var shippingCallbackURL: URL?

Expand Down
4 changes: 2 additions & 2 deletions Sources/BraintreePayPal/BTPayPalError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public enum BTPayPalError: Error, CustomNSError, LocalizedError, Equatable {

/// 11. App Switch could not complete
case appSwitchFailed

/// 12. Missing BA Token for App Switch
case missingBAToken

/// 13. Missing PayPal Request
case missingPayPalRequest

Expand Down
80 changes: 39 additions & 41 deletions Sources/BraintreePayPal/BTPayPalRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import BraintreeCore
#endif

@objc public enum BTPayPalPaymentType: Int {

/// Checkout
case checkout

Expand All @@ -24,24 +23,23 @@ import BraintreeCore

/// Use this option to specify the PayPal page to display when a user lands on the PayPal site to complete the payment.
@objc public enum BTPayPalRequestLandingPageType: Int {

/// Default
case none // Obj-C enums cannot be nil; this default option is used to make `landingPageType` optional for merchants

/// Login
case login

/// Billing
case billing

var stringValue: String? {
switch self {
case .login:
return "login"

case .billing:
return "billing"

default:
return nil
}
Expand All @@ -51,62 +49,62 @@ import BraintreeCore
/// Base options for PayPal Checkout and PayPal Vault flows.
/// - Note: Do not instantiate this class directly. Instead, use BTPayPalCheckoutRequest or BTPayPalVaultRequest.
@objcMembers open class BTPayPalRequest: NSObject {

// MARK: - Public Properties

/// Defaults to false. When set to true, the shipping address selector will be displayed.
public var isShippingAddressRequired: Bool

/// Defaults to false. Set to true to enable user editing of the shipping address.
/// - Note: Only applies when `shippingAddressOverride` is set.
public var isShippingAddressEditable: Bool

/// Optional: A locale code to use for the transaction.
public var localeCode: BTPayPalLocaleCode

/// Optional: A valid shipping address to be displayed in the transaction flow. An error will occur if this address is not valid.
public var shippingAddressOverride: BTPostalAddress?

/// Optional: Landing page type. Defaults to `.none`.
/// - Note: Setting the BTPayPalRequest's landingPageType changes the PayPal page to display when a user lands on the PayPal site to complete the payment.
/// `.login` specifies a PayPal account login page is used.
/// `.billing` specifies a non-PayPal account landing page is used.
public var landingPageType: BTPayPalRequestLandingPageType

/// Optional: The merchant name displayed inside of the PayPal flow; defaults to the company name on your Braintree account
public var displayName: String?

/// Optional: A non-default merchant account to use for tokenization.
public var merchantAccountID: String?

/// Optional: The line items for this transaction. It can include up to 249 line items.
public var lineItems: [BTPayPalLineItem]?

/// Optional: Display a custom description to the user for a billing agreement. For Checkout with Vault flows, you must also set
/// `requestBillingAgreement` to `true` on your `BTPayPalCheckoutRequest`.
public var billingAgreementDescription: String?

/// Optional: A risk correlation ID created with Set Transaction Context on your server.
public var riskCorrelationID: String?

/// :nodoc: Exposed publicly for use by PayPal Native Checkout module. This property is not covered by semantic versioning.
@_documentation(visibility: private)
public var hermesPath: String

/// :nodoc: Exposed publicly for use by PayPal Native Checkout module. This property is not covered by semantic versioning.
@_documentation(visibility: private)
public var paymentType: BTPayPalPaymentType

/// Optional: A user's phone number to initiate a quicker authentication flow in the scenario where the user has a PayPal account
/// identified with the same phone number.
public var userPhoneNumber: BTPayPalPhoneNumber?

/// Optional: User email to initiate a quicker authentication flow in cases where the user has a PayPal Account with the same email.
public var userAuthenticationEmail: String?

/// Optional: The shopper session ID returned from your shopper insights server SDK integration.
public var shopperSessionID: String?

// MARK: - Internal Properties

/// Optional: Used to determine if the customer will use the PayPal app switch flow. Defaults to `false`.
Expand All @@ -116,9 +114,9 @@ import BraintreeCore
// MARK: - Static Properties

static let callbackURLHostAndPath: String = "onetouch/v1/"

// MARK: - Initializer

init(
hermesPath: String,
paymentType: BTPayPalPaymentType,
Expand Down Expand Up @@ -154,9 +152,9 @@ import BraintreeCore
self.enablePayPalAppSwitch = enablePayPalAppSwitch
self.shopperSessionID = shopperSessionID
}

// MARK: Public Methods

/// :nodoc: Exposed publicly for use by PayPal Native Checkout module. This method is not covered by semantic versioning.
@_documentation(visibility: private)
public func parameters(
Expand All @@ -165,51 +163,51 @@ import BraintreeCore
isPayPalAppInstalled: Bool = false
) -> [String: Any] {
var experienceProfile: [String: Any] = [:]

experienceProfile["no_shipping"] = !isShippingAddressRequired
experienceProfile["brand_name"] = displayName != nil ? displayName : configuration.json?["paypal"]["displayName"].asString()

if landingPageType.stringValue != nil {
experienceProfile["landing_page_type"] = landingPageType.stringValue
}

if localeCode.stringValue != nil {
experienceProfile["locale_code"] = localeCode.stringValue
}

experienceProfile["address_override"] = shippingAddressOverride != nil ? !isShippingAddressEditable : false

var parameters: [String: Any] = [:]

if merchantAccountID != nil {
parameters["merchant_account_id"] = merchantAccountID
}

if riskCorrelationID != nil {
parameters["correlation_id"] = riskCorrelationID
}

if let lineItems, !lineItems.isEmpty {
let lineItemsArray = lineItems.compactMap { $0.requestParameters() }
parameters["line_items"] = lineItemsArray
}

if let userPhoneNumberDictionary = try? userPhoneNumber?.toDictionary() {
parameters["phone_number"] = userPhoneNumberDictionary
}

if let userAuthenticationEmail, !userAuthenticationEmail.isEmpty {
parameters["payer_email"] = userAuthenticationEmail
}

if let shopperSessionID {
parameters["shopper_session_id"] = shopperSessionID
}

parameters["return_url"] = BTCoreConstants.callbackURLScheme + "://\(BTPayPalRequest.callbackURLHostAndPath)success"
parameters["cancel_url"] = BTCoreConstants.callbackURLScheme + "://\(BTPayPalRequest.callbackURLHostAndPath)cancel"
parameters["experience_profile"] = experienceProfile

if let universalLink, enablePayPalAppSwitch, isPayPalAppInstalled {
let appSwitchParameters: [String: Any] = [
"launch_paypal_app": enablePayPalAppSwitch,
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraintreePayPal/BTPayPalVaultBaseRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import BraintreeCore
public var offerCredit: Bool

// MARK: - Initializer

/// Initializes a PayPal Native Vault request
/// - Parameters:
/// - offerCredit: Optional: Offers PayPal Credit if the customer qualifies. Defaults to `false`.
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraintreePayPal/BTPayPalVaultRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import BraintreeCore
)
super.enablePayPalAppSwitch = enablePayPalAppSwitch
}

/// Initializes a PayPal Vault request
/// - Parameters:
/// - offerCredit: Optional: Offers PayPal Credit if the customer qualifies. Defaults to `false`.
Expand Down
10 changes: 5 additions & 5 deletions UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -785,29 +785,29 @@ class BTPayPalClient_Tests: XCTestCase {
func testTokenizeVaultAccount_whenPayPalAppApprovalURLMissingBAToken_returnsError() {
let fakeApplication = FakeApplication()
payPalClient.application = fakeApplication

mockAPIClient.cannedResponseBody = BTJSON(value: [
"agreementSetup": [
"paypalAppApprovalUrl": "https://www.some-url.com/some-path?token=value1"
]
])

let vaultRequest = BTPayPalVaultRequest(
userAuthenticationEmail: "[email protected]",
enablePayPalAppSwitch: true
)

let expectation = expectation(description: "completion block called")
payPalClient.tokenize(vaultRequest) { nonce, error in
XCTAssertNil(nonce)

guard let error = error as NSError? else { XCTFail(); return }
XCTAssertEqual(error.code, 12)
XCTAssertEqual(error.localizedDescription, "Missing BA Token for PayPal App Switch.")
XCTAssertEqual(error.domain, "com.braintreepayments.BTPayPalErrorDomain")
expectation.fulfill()
}

waitForExpectations(timeout: 1)
}

Expand Down

0 comments on commit 4b22a6d

Please sign in to comment.