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

Launch PayPal App For Checkout Flow #1504

Open
wants to merge 7 commits into
base: app-switch-checkout-feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
BlueprintName = "BraintreePayPalTests"
ReferencedContainer = "container:Braintree.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "BTPayPalClient_Tests/testTokenizeVaultAccount_whenPayPalAppApprovalURLMissingBAToken_returnsError()">
agedd marked this conversation as resolved.
Show resolved Hide resolved
</Test>
</SkippedTests>
</TestableReference>
<TestableReference
skipped = "NO"
Expand Down
13 changes: 4 additions & 9 deletions Sources/BraintreePayPal/BTPayPalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,7 @@ import BraintreeDataCollector

switch approvalURL.redirectType {
case .payPalApp(let url):
guard let baToken = approvalURL.baToken else {
self.notifyFailure(with: BTPayPalError.missingBAToken, completion: completion)
return
}

self.launchPayPalApp(with: url, baToken: baToken, completion: completion)
self.launchPayPalApp(with: url, completion: completion)
case .webBrowser(let url):
self.handlePayPalRequest(with: url, paymentType: request.paymentType, completion: completion)
}
Expand All @@ -381,7 +376,6 @@ import BraintreeDataCollector

private func launchPayPalApp(
with payPalAppRedirectURL: URL,
baToken: String,
completion: @escaping (BTPayPalAccountNonce?, Error?) -> Void
) {
apiClient.sendAnalyticsEvent(
Expand All @@ -392,12 +386,13 @@ import BraintreeDataCollector
)

var urlComponents = URLComponents(url: payPalAppRedirectURL, resolvingAgainstBaseURL: true)
urlComponents?.queryItems = [
URLQueryItem(name: "ba_token", value: baToken),
let additionalQueryItems: [URLQueryItem] = [
URLQueryItem(name: "source", value: "braintree_sdk"),
URLQueryItem(name: "switch_initiated_time", value: String(Int(round(Date().timeIntervalSince1970 * 1000))))
]

urlComponents?.queryItems?.append(contentsOf: additionalQueryItems)
agedd marked this conversation as resolved.
Show resolved Hide resolved

guard let redirectURL = urlComponents?.url else {
self.notifyFailure(with: BTPayPalError.invalidURL("Unable to construct PayPal app redirect URL."), completion: completion)
return
Expand Down
11 changes: 2 additions & 9 deletions Sources/BraintreePayPal/BTPayPalError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ 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 Expand Up @@ -75,10 +72,8 @@ public enum BTPayPalError: Error, CustomNSError, LocalizedError, Equatable {
return 10
case .appSwitchFailed:
return 11
case .missingBAToken:
return 12
case .missingPayPalRequest:
return 13
return 12
}
}

Expand Down Expand Up @@ -110,8 +105,6 @@ public enum BTPayPalError: Error, CustomNSError, LocalizedError, Equatable {
return "The App Switch return URL did not contain the cancel or success path."
case .appSwitchFailed:
return "UIApplication failed to perform app switch to PayPal."
case .missingBAToken:
return "Missing BA Token for PayPal App Switch."
case .missingPayPalRequest:
return "The PayPal Request was missing or invalid."
}
Expand Down
29 changes: 0 additions & 29 deletions UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -782,35 +782,6 @@ 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)
}

func testTokenizeVaultAccount_whenOpenURLReturnsFalse_returnsError() {
let fakeApplication = FakeApplication()
fakeApplication.cannedOpenURLSuccess = false
Expand Down