Skip to content

Commit

Permalink
CardClient analytics audit
Browse files Browse the repository at this point in the history
Co-authored-by: Suraj Dhopati<[email protected]>
  • Loading branch information
KunJeongPark committed Dec 6, 2024
1 parent 8194323 commit 9b1fb41
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions Sources/CardPayments/CardClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class CardClient: NSObject {
/// if it fails, `CardVaultResult will be `nil` and `error` will describe the failure
public func vault(_ vaultRequest: CardVaultRequest, completion: @escaping (CardVaultResult?, CoreSDKError?) -> Void) {
analyticsService = AnalyticsService(coreConfig: config, setupToken: vaultRequest.setupTokenID)
analyticsService?.sendEvent("card-payments:vault-wo-purchase:started")
analyticsService?.sendEvent("card-payments:vault:started")
Task {
do {
let result = try await vaultAPI.updateSetupToken(cardVaultRequest: vaultRequest).updateVaultSetupToken
Expand All @@ -56,7 +56,7 @@ public class CardClient: NSObject {
self.notifyVaultFailure(with: CardError.threeDSecureURLError, completion: completion)
return
}
analyticsService?.sendEvent("card-payments:vault-wo-purchase:challenge-required")
analyticsService?.sendEvent("card-payments:vault:challenge-required")
startVaultThreeDSecureChallenge(url: url, setupTokenID: vaultRequest.setupTokenID, completion: completion)
} else {
let vaultResult = CardVaultResult(setupTokenID: result.id, status: result.status, didAttemptThreeDSecureAuthentication: false)
Expand Down Expand Up @@ -99,7 +99,7 @@ public class CardClient: NSObject {
/// if it fails, `CardResult will be `nil` and `error` will describe the failure
public func approveOrder(request: CardRequest, completion: @escaping (CardResult?, CoreSDKError?) -> Void) {
analyticsService = AnalyticsService(coreConfig: config, orderID: request.orderID)
analyticsService?.sendEvent("card-payments:3ds:started")
analyticsService?.sendEvent("card-payments:approve-order:started")
Task {
do {
let result = try await checkoutOrdersAPI.confirmPaymentSource(cardRequest: request)
Expand All @@ -113,19 +113,17 @@ public class CardClient: NSObject {
return
}

analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:challenge-required")
analyticsService?.sendEvent("card-payments:approve-order:challenge-required")
startThreeDSecureChallenge(url: url, orderId: result.id, completion: completion)
} else {
analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:succeeded")

let cardResult = CardResult(orderID: result.id, status: result.status, didAttemptThreeDSecureAuthentication: false)
notifyCheckoutSuccess(for: cardResult, completion: completion)
}
} catch let error as CoreSDKError {
analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:failed")
analyticsService?.sendEvent("card-payments:confirm-payment-source:failed")
notifyCheckoutFailure(with: error, completion: completion)
} catch {
analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:failed")
analyticsService?.sendEvent("card-payments:confirm-payment-source:failed")
notifyCheckoutFailure(with: CardError.unknownError, completion: completion)
}
}
Expand Down Expand Up @@ -161,9 +159,9 @@ public class CardClient: NSObject {
context: self,
sessionDidDisplay: { [weak self] didDisplay in
if didDisplay {
self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:succeeded")
self?.analyticsService?.sendEvent("card-payments:approve-order:auth-challenge-presentation:succeeded")
} else {
self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:failed")
self?.analyticsService?.sendEvent("card-payments:approve-order:auth-challenge-presentation:failed")
}
},
sessionDidComplete: { _, error in
Expand Down Expand Up @@ -201,9 +199,9 @@ public class CardClient: NSObject {
sessionDidDisplay: { [weak self] didDisplay in
if didDisplay {
// TODO: analytics for card vault
self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:succeeded")
self?.analyticsService?.sendEvent("card-payments:vault:auth-challenge-presentation:succeeded")
} else {
self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:failed")
self?.analyticsService?.sendEvent("card-payments:vault:auth-challenge-presentation:failed")
}
},
sessionDidComplete: { _, error in
Expand All @@ -225,32 +223,32 @@ public class CardClient: NSObject {
}

private func notifyCheckoutSuccess(for result: CardResult, completion: (CardResult?, CoreSDKError?) -> Void) {
analyticsService?.sendEvent("card-payments:3ds:succeeded")
analyticsService?.sendEvent("card-payments:approve-order:succeeded")
completion(result, nil)
}

private func notifyCheckoutFailure(with error: CoreSDKError, completion: (CardResult?, CoreSDKError?) -> Void) {
analyticsService?.sendEvent("card-payments:3ds:failed")
analyticsService?.sendEvent("card-payments:approve-order:failed")
completion(nil, error)
}

private func notifyCheckoutCancelWithError(with error: CoreSDKError, completion: (CardResult?, CoreSDKError?) -> Void) {
analyticsService?.sendEvent("card-payments:3ds:challenge:user-canceled")
analyticsService?.sendEvent("card-payments:approve-order:auth-challenge:canceled")
completion(nil, error)
}

private func notifyVaultSuccess(for vaultResult: CardVaultResult, completion: (CardVaultResult?, CoreSDKError?) -> Void) {
analyticsService?.sendEvent("card-payments:vault-wo-purchase:succeeded")
analyticsService?.sendEvent("card-payments:vault:succeeded")
completion(vaultResult, nil)
}

private func notifyVaultFailure(with vaultError: CoreSDKError, completion: (CardVaultResult?, CoreSDKError?) -> Void) {
analyticsService?.sendEvent("card-payments:vault-wo-purchase:failed")
analyticsService?.sendEvent("card-payments:vault:failed")
completion(nil, vaultError)
}

private func notifyVaultCancelWithError(with vaultError: CoreSDKError, completion: (CardVaultResult?, CoreSDKError?) -> Void) {
analyticsService?.sendEvent("card-payments:vault-wo-purchase:challenge:canceled")
analyticsService?.sendEvent("card-payments:vault:auth-challenge:canceled")
completion(nil, vaultError)
}
}
Expand Down

0 comments on commit 9b1fb41

Please sign in to comment.