Skip to content

Commit

Permalink
completion and analytics into notify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KunJeongPark committed Oct 22, 2024
1 parent 24ff08c commit 19cb340
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class PayPalWebViewModel: ObservableObject, PayPalWebCheckoutDelegate {
Task {
do {
payPalWebCheckoutClient = try await getPayPalClient()
payPalWebCheckoutClient?.delegate = self
guard let payPalWebCheckoutClient else {
print("Error initializing PayPalWebCheckoutClient")
return
Expand Down
24 changes: 15 additions & 9 deletions Sources/PayPalWebPayments/PayPalWebCheckoutClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class PayPalWebCheckoutClient: NSObject {
guard let payPalCheckoutURL = URL(string: payPalCheckoutURLString),
let payPalCheckoutURLComponents = payPalCheckoutReturnURL(payPalCheckoutURL: payPalCheckoutURL)
else {
completion(nil, PayPalWebCheckoutClientError.payPalURLError)
self.notifyCheckoutFailure(with: PayPalWebCheckoutClientError.payPalURLError, completion: completion)
return
}

Expand All @@ -64,27 +64,23 @@ public class PayPalWebCheckoutClient: NSObject {
if let error = error {
switch error {
case ASWebAuthenticationSessionError.canceledLogin:
self.analyticsService?.sendEvent("paypal-web-payments:failed")
completion(nil, PayPalWebCheckoutClientError.paypalCancellationError)
self.notifyCheckoutFailure(with: PayPalWebCheckoutClientError.paypalCancellationError, completion: completion)
return
default:
self.analyticsService?.sendEvent("paypal-web-payments:failed")
completion(nil, PayPalWebCheckoutClientError.webSessionError(error))
self.notifyCheckoutFailure(with: PayPalWebCheckoutClientError.webSessionError(error), completion: completion)
return
}
}

if let url = url {
guard let orderID = self.getQueryStringParameter(url: url.absoluteString, param: "token"),
let payerID = self.getQueryStringParameter(url: url.absoluteString, param: "PayerID") else {
self.analyticsService?.sendEvent("paypal-web-payments:failed")
completion(nil, PayPalWebCheckoutClientError.malformedResultError)
self.notifyCheckoutFailure(with: PayPalWebCheckoutClientError.malformedResultError, completion: completion)
return
}

let result = PayPalWebCheckoutResult(orderID: orderID, payerID: payerID)
self.analyticsService?.sendEvent("paypal-web-payments:vault-wo-purchase:succeeded")
completion(result, nil)
self.notifyCheckoutSuccess(for: result, completion: completion)
}
}
)
Expand Down Expand Up @@ -163,6 +159,16 @@ public class PayPalWebCheckoutClient: NSObject {
return url.queryItems?.first { $0.name == param }?.value
}

private func notifyCheckoutSuccess(for result: PayPalWebCheckoutResult, completion: (PayPalWebCheckoutResult?, Error?) -> Void) {
self.analyticsService?.sendEvent("paypal-web-payments:succeeded")
completion(result, nil)
}

private func notifyCheckoutFailure(with error: CoreSDKError, completion: (PayPalWebCheckoutResult?, Error?) -> Void) {
self.analyticsService?.sendEvent("paypal-web-payments:succeeded")
completion(nil, error)
}

private func notifyVaultSuccess(for result: PayPalVaultResult) {
analyticsService?.sendEvent("paypal-web-payments:vault-wo-purchase:succeeded")
vaultDelegate?.paypal(self, didFinishWithVaultResult: result)
Expand Down

0 comments on commit 19cb340

Please sign in to comment.