Skip to content

Commit

Permalink
Remove CardDelegate, functions and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KunJeongPark committed Oct 15, 2024
1 parent 942f5e1 commit ef45510
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 402 deletions.
48 changes: 8 additions & 40 deletions Demo/Demo/ViewModels/CardPaymentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CardPayments
import CorePayments
import FraudProtection

class CardPaymentViewModel: ObservableObject, CardDelegate {
class CardPaymentViewModel: ObservableObject {

@Published var state = CardPaymentState()
private var payPalDataCollector: PayPalDataCollector?
Expand Down Expand Up @@ -117,7 +117,6 @@ class CardPaymentViewModel: ObservableObject, CardDelegate {
let config = try await configManager.getCoreConfig()
cardClient = CardClient(config: config)
payPalDataCollector = PayPalDataCollector(config: config)
cardClient?.delegate = self
let cardRequest = CardRequest(orderID: orderID, card: card, sca: sca)
let result = try await cardClient?.approveOrderAsync(request: cardRequest)
if let result {
Expand All @@ -128,11 +127,15 @@ class CardPaymentViewModel: ObservableObject, CardDelegate {
)
approveResultSuccessResult(approveResult: finalResult)
} else {
self.state.approveResultResponse = .error(message: "cardResult was nil")
DispatchQueue.main.async {
self.state.approveResultResponse = .error(message: "cardResult was nil")
}
}
} catch {
self.state.approveResultResponse = .error(message: error.localizedDescription)
print("failed in checkout with card. \(error.localizedDescription)")
DispatchQueue.main.async {
self.state.approveResultResponse = .error(message: error.localizedDescription)
print("failed in checkout with card. \(error.localizedDescription)")
}
}
}

Expand All @@ -149,39 +152,4 @@ class CardPaymentViewModel: ObservableObject, CardDelegate {
self.state.approveResultResponse = .error(message: vaultError.localizedDescription)
}
}

// MARK: - Card Delegate

func card(_ cardClient: CardPayments.CardClient, didFinishWithResult result: CardPayments.CardResult) {
approveResultSuccessResult(
approveResult: CardPaymentState.CardResult(
id: result.orderID,
status: result.status,
didAttemptThreeDSecureAuthentication: result.didAttemptThreeDSecureAuthentication
)
)
}

func card(_ cardClient: CardPayments.CardClient, didFinishWithError error: CorePayments.CoreSDKError) {
print("Error here")
DispatchQueue.main.async {
self.state.approveResultResponse = .error(message: error.localizedDescription)
}
}

func cardDidCancel(_ cardClient: CardPayments.CardClient) {
print("Card Payment Canceled")
DispatchQueue.main.async {
self.state.approveResultResponse = .idle
self.state.approveResult = nil
}
}

func cardThreeDSecureWillLaunch(_ cardClient: CardPayments.CardClient) {
print("About to launch 3DS")
}

func cardThreeDSecureDidFinish(_ cardClient: CardPayments.CardClient) {
print("Finished 3DS")
}
}
8 changes: 0 additions & 8 deletions PayPal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
BEA100F026EFA7C20036A6A5 /* NetworkingClientError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEA100EF26EFA7C20036A6A5 /* NetworkingClientError.swift */; };
BEA100F226EFA7DE0036A6A5 /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEA100F126EFA7DE0036A6A5 /* Environment.swift */; };
CB16E6D8285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB16E6D7285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift */; };
CB16E6DA285B7B7300FD6F52 /* MockCardDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB16E6D9285B7B7300FD6F52 /* MockCardDelegate.swift */; };
CB1A47F22820AFED00BD8184 /* PayPalPayLaterButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1A47F12820AFED00BD8184 /* PayPalPayLaterButton.swift */; };
CB1A47F42820BA5D00BD8184 /* PaymentButtonEdges.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1A47F32820BA5D00BD8184 /* PaymentButtonEdges.swift */; };
CB1A47F62820BAA600BD8184 /* PaymentButtonLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1A47F52820BAA600BD8184 /* PaymentButtonLabel.swift */; };
Expand All @@ -130,7 +129,6 @@
CB22C018291049500097E592 /* PayPalPayLaterButton_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB22C017291049500097E592 /* PayPalPayLaterButton_Tests.swift */; };
CB4BE27D2847AF6F00EA2DD1 /* SCA.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4BE27C2847AF6F00EA2DD1 /* SCA.swift */; };
CB4BE27E2847EA7D00EA2DD1 /* WebAuthenticationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE4F784827EB629100FF4C0E /* WebAuthenticationSession.swift */; };
CB4BE2802847F01000EA2DD1 /* CardDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4BE27F2847F01000EA2DD1 /* CardDelegate.swift */; };
CB4BE28A28512A9800EA2DD1 /* MockQuededURLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4BE28928512A9800EA2DD1 /* MockQuededURLSession.swift */; };
CB4BE28B2851423900EA2DD1 /* MockWebAuthenticationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE8A7F627EA54A000AC301B /* MockWebAuthenticationSession.swift */; };
CB4BE28C2851427600EA2DD1 /* MockViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE8A7F427EA544000AC301B /* MockViewController.swift */; };
Expand Down Expand Up @@ -294,7 +292,6 @@
BEDB7FE32788AB8E00CEA554 /* Coordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator.swift; sourceTree = "<group>"; };
BEF3FF1627AC5DF3006B4B69 /* Coordinator_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator_Tests.swift; sourceTree = "<group>"; };
CB16E6D7285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeConfirmPaymentResponse.swift; sourceTree = "<group>"; };
CB16E6D9285B7B7300FD6F52 /* MockCardDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCardDelegate.swift; sourceTree = "<group>"; };
CB1A47F12820AFED00BD8184 /* PayPalPayLaterButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PayPalPayLaterButton.swift; sourceTree = "<group>"; };
CB1A47F32820BA5D00BD8184 /* PaymentButtonEdges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentButtonEdges.swift; sourceTree = "<group>"; };
CB1A47F52820BAA600BD8184 /* PaymentButtonLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentButtonLabel.swift; sourceTree = "<group>"; };
Expand All @@ -304,7 +301,6 @@
CB1A48042822BCED00BD8184 /* PaymentButton+ImageAsset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PaymentButton+ImageAsset.swift"; sourceTree = "<group>"; };
CB22C017291049500097E592 /* PayPalPayLaterButton_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PayPalPayLaterButton_Tests.swift; sourceTree = "<group>"; };
CB4BE27C2847AF6F00EA2DD1 /* SCA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SCA.swift; sourceTree = "<group>"; };
CB4BE27F2847F01000EA2DD1 /* CardDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardDelegate.swift; sourceTree = "<group>"; };
CB4BE285284802C200EA2DD1 /* PaymentSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentSource.swift; sourceTree = "<group>"; };
CB4BE28928512A9800EA2DD1 /* MockQuededURLSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockQuededURLSession.swift; sourceTree = "<group>"; };
CB51FF7127FCB947001A97F5 /* PayPalWebCheckoutFundingSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PayPalWebCheckoutFundingSource.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -488,7 +484,6 @@
isa = PBXGroup;
children = (
CB16E6D7285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift */,
CB16E6D9285B7B7300FD6F52 /* MockCardDelegate.swift */,
3B109B3A2A85B54800D8135F /* MockCardVaultDelegate.swift */,
808E3EDC2A981F240017FE46 /* MockCheckoutOrdersAPI.swift */,
808E3EDE2A981F390017FE46 /* MockVaultPaymentTokensAPI.swift */,
Expand Down Expand Up @@ -554,7 +549,6 @@
children = (
06CE009A26F3D5A40000CC46 /* CardClient.swift */,
80DCC59D2719DB6F00EC7C5A /* CardClientError.swift */,
CB4BE27F2847F01000EA2DD1 /* CardDelegate.swift */,
3B22E8B52A840ECF00962E34 /* CardVaultDelegate.swift */,
80DBC9D829C336D500462539 /* APIRequests */,
065A4DBD26FCDA270007014A /* Models */,
Expand Down Expand Up @@ -1229,7 +1223,6 @@
8048D28C270B9DE00072214A /* ConfirmPaymentSourceResponse.swift in Sources */,
3D1763A22720722A00652E1C /* CardResult.swift in Sources */,
BC0A82A5270B9533006E9A21 /* ConfirmPaymentSourceRequest.swift in Sources */,
CB4BE2802847F01000EA2DD1 /* CardDelegate.swift in Sources */,
3BD82DBB2A835AF900CBE764 /* UpdateSetupTokenResponse.swift in Sources */,
80E8DAE126B8784600FAFC3F /* Card.swift in Sources */,
3B22E8B82A841AEA00962E34 /* CardVaultResult.swift in Sources */,
Expand All @@ -1253,7 +1246,6 @@
3B109B3C2A85CC6200D8135F /* MockCardVaultDelegate.swift in Sources */,
80B27AF12A9E9EE60008EA45 /* VaultPaymentTokensAPI_Tests.swift in Sources */,
CB16E6D8285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift in Sources */,
CB16E6DA285B7B7300FD6F52 /* MockCardDelegate.swift in Sources */,
3B783DC32B7A69C2004623DB /* FakeUpdateSetupTokenResponse.swift in Sources */,
BC0A82A6270B9954006E9A21 /* ConfirmPaymentSourceRequest_Tests.swift in Sources */,
808E3EDD2A981F240017FE46 /* MockCheckoutOrdersAPI.swift in Sources */,
Expand Down
85 changes: 0 additions & 85 deletions Sources/CardPayments/CardClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import CorePayments

public class CardClient: NSObject {

public weak var delegate: CardDelegate?
public weak var vaultDelegate: CardVaultDelegate?

private let checkoutOrdersAPI: CheckoutOrdersAPI
Expand Down Expand Up @@ -75,40 +74,6 @@ public class CardClient: NSObject {
/// - Parameters:
/// - orderId: Order id for approval
/// - request: The request containing the card
public func approveOrder(request: CardRequest) {
analyticsService = AnalyticsService(coreConfig: config, orderID: request.orderID)
analyticsService?.sendEvent("card-payments:3ds:started")
Task {
do {
let result = try await checkoutOrdersAPI.confirmPaymentSource(cardRequest: request)

if result.status == "PAYER_ACTION_REQUIRED",
let url = result.links?.first(where: { $0.rel == "payer-action" })?.href {
guard getQueryStringParameter(url: url, param: "flow") == "3ds",
url.contains("helios"),
let url = URL(string: url) else {
self.notifyFailure(with: CardClientError.threeDSecureURLError)
return
}

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

let cardResult = CardResult(orderID: result.id, status: result.status, didAttemptThreeDSecureAuthentication: false)
notifySuccess(for: cardResult)
}
} catch let error as CoreSDKError {
analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:failed")
notifyFailure(with: error)
} catch {
analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:failed")
notifyFailure(with: CardClientError.unknownError)
}
}
}

public func approveOrderAsync(request: CardRequest) async throws -> CardResult {
analyticsService = AnalyticsService(coreConfig: config, orderID: request.orderID)
analyticsService?.sendEvent("card-payments:3ds:started")
Expand Down Expand Up @@ -141,41 +106,6 @@ public class CardClient: NSObject {
}
}

private func startThreeDSecureChallenge(
url: URL,
orderId: String
) {
delegate?.cardThreeDSecureWillLaunch(self)

webAuthenticationSession.start(
url: url,
context: self,
sessionDidDisplay: { [weak self] didDisplay in
if didDisplay {
self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:succeeded")
} else {
self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:failed")
}
},
sessionDidComplete: { _, error in
self.delegate?.cardThreeDSecureDidFinish(self)
if let error = error {
switch error {
case ASWebAuthenticationSessionError.canceledLogin:
self.notifyCancellation()
return
default:
self.notifyFailure(with: CardClientError.threeDSecureError(error))
return
}
}

let cardResult = CardResult(orderID: orderId, status: nil, didAttemptThreeDSecureAuthentication: true)
self.notifySuccess(for: cardResult)
}
)
}

private func startAsyncThreeDSecureChallenge(
url: URL,
orderId: String
Expand Down Expand Up @@ -250,16 +180,6 @@ public class CardClient: NSObject {
}
)
}

private func notifySuccess(for result: CardResult) {
analyticsService?.sendEvent("card-payments:3ds:succeeded")
delegate?.card(self, didFinishWithResult: result)
}

private func notifyFailure(with error: CoreSDKError) {
analyticsService?.sendEvent("card-payments:3ds:failed")
delegate?.card(self, didFinishWithError: error)
}

private func notifyVaultSuccess(for vaultResult: CardVaultResult) {
analyticsService?.sendEvent("card-payments:vault-wo-purchase:succeeded")
Expand All @@ -271,11 +191,6 @@ public class CardClient: NSObject {
vaultDelegate?.card(self, didFinishWithVaultError: vaultError)
}

private func notifyCancellation() {
analyticsService?.sendEvent("card-payments:3ds:challenge:user-canceled")
delegate?.cardDidCancel(self)
}

private func notifyVaultCancellation() {
analyticsService?.sendEvent("card-payments:vault-wo-purchase:challenge:canceled")
vaultDelegate?.cardThreeDSecureDidCancel(self)
Expand Down
2 changes: 1 addition & 1 deletion Sources/CardPayments/CardClientError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ enum CardClientError {
)

static let threeDSecureCancellation = CoreSDKError(
code: Code.threeDSCancellation.rawValue,
code: Code.threeDSCancellation.rawValue,
domain: domain,
errorDescription: "3DS verification has been cancelled by the user"
)
Expand Down
35 changes: 0 additions & 35 deletions Sources/CardPayments/CardDelegate.swift

This file was deleted.

Loading

0 comments on commit ef45510

Please sign in to comment.