From b75b525080a795ffd6bdacde2c64efd341acbbb6 Mon Sep 17 00:00:00 2001 From: borisprimer <137198782+borisprimer@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:06:28 +0100 Subject: [PATCH] fix: Update error type when canceling Paypal payment (#1060) Update error type when canceling Paypal payment Co-authored-by: Boris Nikolic Co-authored-by: Niall Quinn --- .../Services/Network/WebAuthenticationService.swift | 12 ++++++------ .../NolPayTokenizationViewModel.swift | 3 ++- .../PayPalTokenizationViewModel.swift | 2 +- .../PaymentMethodTokenizationViewModel.swift | 3 ++- ...bRedirectPaymentMethodTokenizationViewModel.swift | 3 ++- .../PayPalTokenizationViewModelTests.swift | 2 +- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Sources/PrimerSDK/Classes/Services/Network/WebAuthenticationService.swift b/Sources/PrimerSDK/Classes/Services/Network/WebAuthenticationService.swift index 129a7b9302..2feb69e99e 100644 --- a/Sources/PrimerSDK/Classes/Services/Network/WebAuthenticationService.swift +++ b/Sources/PrimerSDK/Classes/Services/Network/WebAuthenticationService.swift @@ -11,24 +11,24 @@ import SafariServices protocol WebAuthenticationService { var session: ASWebAuthenticationSession? { get } - func connect(url: URL, scheme: String, _ completion: @escaping (Result) -> Void) + func connect(paymentMethodType: String, url: URL, scheme: String, _ completion: @escaping (Result) -> Void) } class DefaultWebAuthenticationService: NSObject, WebAuthenticationService { var session: ASWebAuthenticationSession? - func connect(url: URL, scheme: String, _ completion: @escaping (Result) -> Void) { + func connect(paymentMethodType: String, url: URL, scheme: String, _ completion: @escaping (Result) -> Void) { let webAuthSession = ASWebAuthenticationSession( url: url, callbackURLScheme: scheme, completionHandler: { (url, error) in if let url = url { completion(.success(url)) - } else if let error = error { - completion(.failure(PrimerError.underlyingErrors(errors: [error], - userInfo: .errorUserInfoDictionary(), - diagnosticsId: UUID().uuidString))) + } else if error != nil { + completion(.failure(PrimerError.cancelled(paymentMethodType: paymentMethodType, + userInfo: .errorUserInfoDictionary(), + diagnosticsId: UUID().uuidString))) } else { let additionalInfo: [String: String] = [ "message": "Failed to create web authentication session" ] completion(.failure(PrimerError.unknown(userInfo: .errorUserInfoDictionary(additionalInfo: additionalInfo), diff --git a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/NolPayTokenizationViewModel.swift b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/NolPayTokenizationViewModel.swift index 253cbd2be5..c679b25b3b 100644 --- a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/NolPayTokenizationViewModel.swift +++ b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/NolPayTokenizationViewModel.swift @@ -219,7 +219,8 @@ class NolPayTokenizationViewModel: PaymentMethodTokenizationViewModel { firstly { () -> Promise in if self.isCancelled { - let err = PrimerError.cancelled(paymentMethodType: self.config.type, userInfo: .errorUserInfoDictionary(), + let err = PrimerError.cancelled(paymentMethodType: self.config.type, + userInfo: .errorUserInfoDictionary(), diagnosticsId: UUID().uuidString) throw err } diff --git a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PayPalTokenizationViewModel.swift b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PayPalTokenizationViewModel.swift index 53d3799fba..ac585812d4 100644 --- a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PayPalTokenizationViewModel.swift +++ b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PayPalTokenizationViewModel.swift @@ -247,7 +247,7 @@ class PayPalTokenizationViewModel: PaymentMethodTokenizationViewModel { return } - webAuthenticationService.connect(url: url, scheme: scheme) { [weak self] result in + webAuthenticationService.connect(paymentMethodType: self.config.type, url: url, scheme: scheme) { [weak self] result in switch result { case .success(let url): seal.fulfill(url) diff --git a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel.swift b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel.swift index f170ecd020..17e872c2d4 100644 --- a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel.swift +++ b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel.swift @@ -110,7 +110,8 @@ class PaymentMethodTokenizationViewModel: NSObject, PaymentMethodTokenizationVie var cancelledError: PrimerError? self.didCancel = { self.isCancelled = true - cancelledError = PrimerError.cancelled(paymentMethodType: self.config.type, userInfo: .errorUserInfoDictionary(), + cancelledError = PrimerError.cancelled(paymentMethodType: self.config.type, + userInfo: .errorUserInfoDictionary(), diagnosticsId: UUID().uuidString) ErrorHandler.handle(error: cancelledError!) seal.reject(cancelledError!) diff --git a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/WebRedirectPaymentMethodTokenizationViewModel.swift b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/WebRedirectPaymentMethodTokenizationViewModel.swift index 630bfee0f2..d1abcff332 100644 --- a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/WebRedirectPaymentMethodTokenizationViewModel.swift +++ b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/WebRedirectPaymentMethodTokenizationViewModel.swift @@ -288,7 +288,8 @@ class WebRedirectPaymentMethodTokenizationViewModel: PaymentMethodTokenizationVi firstly { () -> Promise in if self.isCancelled { - let err = PrimerError.cancelled(paymentMethodType: self.config.type, userInfo: .errorUserInfoDictionary(), + let err = PrimerError.cancelled(paymentMethodType: self.config.type, + userInfo: .errorUserInfoDictionary(), diagnosticsId: UUID().uuidString) throw err } diff --git a/Tests/Primer/Tokenization/View Models/PayPalTokenizationViewModelTests.swift b/Tests/Primer/Tokenization/View Models/PayPalTokenizationViewModelTests.swift index de06ff31f9..c8a3273011 100644 --- a/Tests/Primer/Tokenization/View Models/PayPalTokenizationViewModelTests.swift +++ b/Tests/Primer/Tokenization/View Models/PayPalTokenizationViewModelTests.swift @@ -274,7 +274,7 @@ class MockWebAuthenticationService: WebAuthenticationService { var onConnect: ((URL, String) -> URL)? - func connect(url: URL, scheme: String, _ completion: @escaping (Result) -> Void) { + func connect(paymentMethodType: String, url: URL, scheme: String, _ completion: @escaping (Result) -> Void) { if let onConnect = onConnect { completion(.success(onConnect(url, scheme))) } else {