Skip to content
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

Api client update apple pay #1530

Draft
wants to merge 7 commits into
base: v7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* Update PayPal app URL query scheme from `paypal-app-switch-checkout` to `paypal`
* BraintreeAmericanExpress
* Update initializer to `BTAmericanExpressClient(authorization:)`
* BraintreeApplePay
* Update initializer to `BTApplePayClient(authorization:)`

## 6.27.0 (2025-01-23)
* BraintreePayPal
Expand Down
2 changes: 1 addition & 1 deletion Demo/Application/Features/ApplePayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PassKit

class ApplePayViewController: PaymentButtonBaseViewController {

lazy var applePayClient = BTApplePayClient(apiClient: apiClient)
lazy var applePayClient = BTApplePayClient(authorization: authorization)

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
8 changes: 4 additions & 4 deletions Sources/BraintreeApplePay/BTApplePayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import BraintreeCore
// MARK: - Initializer

/// Creates an Apple Pay client
/// - Parameter apiClient: An API client
@objc(initWithAPIClient:)
public init(apiClient: BTAPIClient) {
self.apiClient = apiClient
/// - Parameter authorization: A client token or tokenization key
@objc(initWithAuthorization:)
public init(authorization: String) {
self.apiClient = BTAPIClient(newAuthorization: authorization)
}

// MARK: - Public Methods
Expand Down
79 changes: 60 additions & 19 deletions UnitTests/BraintreeApplePayTests/BTApplePay_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ class BTApplePay_Tests: XCTestCase {
// MARK: - Payment Request

func testPaymentRequest_whenConfiguredOff_callsBackWithError() {
let applePayClient = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "off"
]
])
let applePayClient = BTApplePayClient(apiClient: mockClient)

applePayClient.apiClient = mockClient

let expectation = self.expectation(description: "Callback invoked")
applePayClient.makePaymentRequest { (paymentRequest, error) in
Expand All @@ -35,8 +38,10 @@ class BTApplePay_Tests: XCTestCase {
}

func testPaymentRequest_whenConfigurationIsMissingApplePayStatus_callsBackWithError() {
let applePayClient = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")
mockClient.cannedConfigurationResponseBody = BTJSON(value: [:] as [AnyHashable: Any?])
let applePayClient = BTApplePayClient(apiClient: mockClient)

applePayClient.apiClient = mockClient

let expectation = self.expectation(description: "Callback invoked")
applePayClient.makePaymentRequest { (paymentRequest, error) in
Expand All @@ -51,6 +56,8 @@ class BTApplePay_Tests: XCTestCase {
}

func testPaymentRequest_returnsPaymentRequestUsingConfiguration() {
let applePayClient = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "production",
Expand All @@ -60,7 +67,8 @@ class BTApplePay_Tests: XCTestCase {
"supportedNetworks": ["visa", "mastercard", "amex"]
] as [String: Any]
])
let applePayClient = BTApplePayClient(apiClient: mockClient)

applePayClient.apiClient = mockClient

let expectation = self.expectation(description: "Callback invoked")
applePayClient.makePaymentRequest { (paymentRequest, error) in
Expand All @@ -81,12 +89,15 @@ class BTApplePay_Tests: XCTestCase {
}

func testPaymentRequest_whenConfigurationIsMissingValues_returnsPaymentRequestWithValuesUndefined() {
let applePayClient = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "production"
]
])
let applePayClient = BTApplePayClient(apiClient: mockClient)

applePayClient.apiClient = mockClient

let expectation = self.expectation(description: "Callback invoked")
applePayClient.makePaymentRequest { (paymentRequest, error) in
Expand All @@ -109,14 +120,18 @@ class BTApplePay_Tests: XCTestCase {
// MARK: - Tokenization

func testTokenization_whenConfiguredOff_callsBackWithError() {
let expectation = self.expectation(description: "Unsuccessful tokenization")

let client = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "off"
]
])
let expectation = self.expectation(description: "Unsuccessful tokenization")

let client = BTApplePayClient(apiClient: mockClient)

client.apiClient = mockClient

let payment = MockPKPayment()
client.tokenize(payment) { (tokenizedPayment, error) -> Void in
guard let error = error as NSError? else {return}
Expand All @@ -130,10 +145,14 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenization_whenConfigurationIsMissingApplePayStatus_callsBackWithError() {
mockClient.cannedConfigurationResponseBody = BTJSON(value: [:] as [AnyHashable: Any])
let expectation = self.expectation(description: "Unsuccessful tokenization")

let client = BTApplePayClient(apiClient: mockClient)
let client = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [:] as [AnyHashable: Any])

client.apiClient = mockClient

let payment = MockPKPayment()
client.tokenize(payment) { (tokenizedPayment, error) -> Void in
guard let error = error as NSError? else {return}
Expand All @@ -147,8 +166,10 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenization_whenConfigurationFetchErrorOccurs_callsBackWithError() {
let client = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")
mockClient.cannedConfigurationResponseError = NSError(domain: "MyError", code: 1, userInfo: nil)
let client = BTApplePayClient(apiClient: mockClient)
client.apiClient = mockClient

let payment = MockPKPayment()
let expectation = self.expectation(description: "tokenization error")

Expand All @@ -163,14 +184,18 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenization_whenTokenizationErrorOccurs_callsBackWithError() {
let client = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "production"
]
])
mockClient.cannedHTTPURLResponse = HTTPURLResponse(url: URL(string: "any")!, statusCode: 503, httpVersion: nil, headerFields: nil)
mockClient.cannedResponseError = NSError(domain: "foo", code: 100, userInfo: nil)
let client = BTApplePayClient(apiClient: mockClient)

client.apiClient = mockClient

let payment = MockPKPayment()
let expectation = self.expectation(description: "tokenization failure")

Expand All @@ -183,13 +208,17 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenization_whenTokenizationFailureOccurs_callsBackWithError() {
let client = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "production"
]
])
mockClient.cannedResponseError = NSError(domain: "MyError", code: 1, userInfo: nil)
let client = BTApplePayClient(apiClient: mockClient)

client.apiClient = mockClient

let payment = MockPKPayment()
let expectation = self.expectation(description: "tokenization failure")

Expand All @@ -204,6 +233,9 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenization_whenSuccessfulTokenizationInProduction_callsBackWithTokenizedPayment() {
let expectation = self.expectation(description: "successful tokenization")

let client = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")
mockClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "production"
Expand All @@ -230,9 +262,8 @@ class BTApplePay_Tests: XCTestCase {
] as [[String: Any]]
]
)
let expectation = self.expectation(description: "successful tokenization")

let client = BTApplePayClient(apiClient: mockClient)

client.apiClient = mockClient
let payment = MockPKPayment()
client.tokenize(payment) { (tokenizedPayment, error) -> Void in
XCTAssertNil(error)
Expand All @@ -256,10 +287,13 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenize_whenBodyIsMissingData_returnsError() {
let applePayClient = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: ["applePay" : ["status" : "production"]])
mockClient.cannedResponseBody = nil

let applePayClient = BTApplePayClient(apiClient: mockClient)

applePayClient.apiClient = mockClient

let payment = MockPKPayment()
let expectation = expectation(description: "Callback invoked")

Expand All @@ -277,10 +311,13 @@ class BTApplePay_Tests: XCTestCase {
}

func testTokenize_bodyDoesNotContainApplePayCards_returnsError() {
let applePayClient = BTApplePayClient(authorization: "sandbox_9dbg82cq_dcpspy2brwdjr3qn")

mockClient.cannedConfigurationResponseBody = BTJSON(value: ["applePay" : ["status" : "production"]])
mockClient.cannedResponseBody = BTJSON(value: ["notApplePayCards": "badData"])

let applePayClient = BTApplePayClient(apiClient: mockClient)
applePayClient.apiClient = mockClient

let payment = MockPKPayment()
let expectation = expectation(description: "Callback invoked")

Expand All @@ -300,13 +337,17 @@ class BTApplePay_Tests: XCTestCase {
// MARK: - Metadata

func testMetaParameter_whenTokenizationIsSuccessful_isPOSTedToServer() {
let applePayClient = BTApplePayClient(authorization: "production_t2wns2y2_dfy45jdj3dxkmz5m")

let mockAPIClient = MockAPIClient(authorization: "development_tokenization_key")!
mockAPIClient.cannedConfigurationResponseBody = BTJSON(value: [
"applePay" : [
"status" : "production"
]
])
let applePayClient = BTApplePayClient(apiClient: mockAPIClient)

applePayClient.apiClient = mockAPIClient

let payment = MockPKPayment()

let expectation = self.expectation(description: "Tokenized card")
Expand Down
9 changes: 7 additions & 2 deletions V7_MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _Documentation for v7 will be published to https://developer.paypal.com/braintre
1. [PayPal](#paypal)
1. [PayPal Native Checkout](#paypal-native-checkout)
1. [American Express](#american-express)
1. [Apple Pay](#american-express)

## Supported Versions

Expand Down Expand Up @@ -72,6 +73,10 @@ use the [PayPal (web)](https://developer.paypal.com/braintree/docs/guides/paypal

## American Express
Update initializer for `BTAmericanExpressClient`:
```diff
- var amexClient = BTAmericanExpressClient(apiClient: apiClient)
+ var amexClient = BTAmericanExpressClient(authorization: "<CLIENT_AUTHORIZATION>")
+ var amexClient = BTAmericanExpressClient(authorization: "<CLIENT_AUTHORIZATION>")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up this diff code. Wondering if we are intentionally keeping the + and -?


## Apple Pay
Update initializer for `BTApplePayClient`:
- var applePayClient = BTApplePayClient(apiClient: apiClient)
+ var applePayClient = BTApplePayClient(authorization: "<CLIENT_AUTHORIZATION>")
Loading