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

Add URL to PayPal App Switch Events #1477

Merged
merged 4 commits into from
Dec 3, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## unreleased
* BraintreePayPal
* Add `BTPayPalRequest.userPhoneNumber` optional property
* Send `url` in `event_params` for App Switch events to PayPal's analytics service (FPTI)
* BraintreeVenmo
* Send `url` in `event_params` for App Switch events to PayPal's analytics service (FPTI)

Expand Down
46 changes: 24 additions & 22 deletions Sources/BraintreePayPal/BTPayPalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ import BraintreeDataCollector
performSwitchRequest(appSwitchURL: url, paymentType: paymentType, completion: completion)
}

func invokedOpenURLSuccessfully(_ success: Bool, url: URL, completion: @escaping (BTPayPalAccountNonce?, Error?) -> Void) {
if success {
apiClient.sendAnalyticsEvent(
BTPayPalAnalytics.appSwitchSucceeded,
isVaultRequest: isVaultRequest,
linkType: linkType,
payPalContextID: payPalContextID,
appSwitchURL: url
)
BTPayPalClient.payPalClient = self
appSwitchCompletion = completion
} else {
apiClient.sendAnalyticsEvent(
BTPayPalAnalytics.appSwitchFailed,
isVaultRequest: isVaultRequest,
linkType: linkType,
payPalContextID: payPalContextID,
appSwitchURL: url
)
notifyFailure(with: BTPayPalError.appSwitchFailed, completion: completion)
}
}

// MARK: - App Switch Methods

func handleReturnURL(_ url: URL) {
Expand Down Expand Up @@ -404,28 +427,7 @@ import BraintreeDataCollector
}

application.open(redirectURL) { success in
self.invokedOpenURLSuccessfully(success, completion: completion)
}
}

private func invokedOpenURLSuccessfully(_ success: Bool, completion: @escaping (BTPayPalAccountNonce?, Error?) -> Void) {
if success {
apiClient.sendAnalyticsEvent(
BTPayPalAnalytics.appSwitchSucceeded,
isVaultRequest: isVaultRequest,
linkType: linkType,
payPalContextID: payPalContextID
)
BTPayPalClient.payPalClient = self
appSwitchCompletion = completion
} else {
apiClient.sendAnalyticsEvent(
BTPayPalAnalytics.appSwitchFailed,
isVaultRequest: isVaultRequest,
linkType: linkType,
payPalContextID: payPalContextID
)
notifyFailure(with: BTPayPalError.appSwitchFailed, completion: completion)
self.invokedOpenURLSuccessfully(success, url: redirectURL, completion: completion)
}
}

Expand Down
18 changes: 18 additions & 0 deletions UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,24 @@ class BTPayPalClient_Tests: XCTestCase {
XCTAssertNil(lastPostParameters["merchant_app_return_url"] as? String)
}

func testInvokedOpenURLSuccessfully_whenSuccess_sendsAppSwitchSucceededWithAppSwitchURL() {
let eventName = BTPayPalAnalytics.appSwitchSucceeded
let fakeURL = URL(string: "some-url")!
payPalClient.invokedOpenURLSuccessfully(true, url: fakeURL) { _, _ in }

XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.last!, eventName)
XCTAssertEqual(mockAPIClient.postedAppSwitchURL[eventName], fakeURL.absoluteString)
}

func testInvokedOpenURLSuccessfully_whenFailure_sendsAppSwitchFailedWithAppSwitchURL() {
let eventName = BTPayPalAnalytics.appSwitchFailed
let fakeURL = URL(string: "some-url")!
payPalClient.invokedOpenURLSuccessfully(false, url: fakeURL) { _, _ in }

XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first!, eventName)
XCTAssertEqual(mockAPIClient.postedAppSwitchURL[eventName], fakeURL.absoluteString)
}

// MARK: - Analytics

func testAPIClientMetadata_hasIntegrationSetToCustom() {
Expand Down