From e952dd2de35252569eb79a83097cc534428b77b8 Mon Sep 17 00:00:00 2001 From: Niall Quinn Date: Wed, 28 Aug 2024 11:47:22 +0200 Subject: [PATCH] fix: Add some checkoutData in the case of error (#987) * Add some checkoutData in the case of error * Added tests for checkoutData in error * increase scope of test --- ...entMethodTokenizationViewModel+Logic.swift | 22 ++++++++++++++++++- .../Extensions/ErrorExtensionTests.swift | 1 - ...mentMethodTokenizationViewModelTests.swift | 21 ++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel+Logic.swift b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel+Logic.swift index b7b82c3ee0..7b42a06d06 100644 --- a/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel+Logic.swift +++ b/Sources/PrimerSDK/Classes/User Interface/TokenizationViewModels/PaymentMethodTokenizationViewModel+Logic.swift @@ -150,7 +150,7 @@ extension PaymentMethodTokenizationViewModel { userInfo: .errorUserInfoDictionary(), diagnosticsId: UUID().uuidString) } - + self.setCheckoutDataFromError(primerErr) return PrimerDelegateProxy.raisePrimerDidFailWithError(primerErr, data: self.paymentCheckoutData) } .done { merchantErrorMessage in @@ -516,6 +516,26 @@ Make sure you call the decision handler otherwise the SDK will hang. self.didStartPayment = nil self.didFinishPayment = nil } + + func setCheckoutDataFromError(_ error: PrimerError) { + if let checkoutData = error.checkoutData { + self.paymentCheckoutData = checkoutData + } + } +} + +extension PrimerError { + var checkoutData: PrimerCheckoutData? { + switch self { + case .paymentFailed(_, let paymentId, _, _, _): + return PrimerCheckoutData( + payment: PrimerCheckoutDataPayment(id: paymentId, + orderId: nil, + paymentFailureReason: PrimerPaymentErrorCode.failed)) + default: + return nil + } + } } extension PaymentMethodTokenizationViewModel: PaymentMethodTypeViaPaymentMethodTokenDataProviding {} diff --git a/Tests/Primer/Extensions/ErrorExtensionTests.swift b/Tests/Primer/Extensions/ErrorExtensionTests.swift index 4f56be109d..343592f6a8 100644 --- a/Tests/Primer/Extensions/ErrorExtensionTests.swift +++ b/Tests/Primer/Extensions/ErrorExtensionTests.swift @@ -177,5 +177,4 @@ final class ErrorExtensionTests: XCTestCase { XCTAssertFalse(differentDomainError.isNetworkError, "Expected error from a different domain to not be identified as a network error") } - } diff --git a/Tests/Primer/Tokenization/View Models/CardFormPaymentMethodTokenizationViewModelTests.swift b/Tests/Primer/Tokenization/View Models/CardFormPaymentMethodTokenizationViewModelTests.swift index f06e434061..3505e590e8 100644 --- a/Tests/Primer/Tokenization/View Models/CardFormPaymentMethodTokenizationViewModelTests.swift +++ b/Tests/Primer/Tokenization/View Models/CardFormPaymentMethodTokenizationViewModelTests.swift @@ -174,6 +174,27 @@ final class CardFormPaymentMethodTokenizationViewModelTests: XCTestCase, Tokeniz } } + func test_checkoutDataFromError() throws { + + let sut = PaymentMethodTokenizationViewModel(config: PrimerPaymentMethod(id: "id", + implementationType: .nativeSdk, + type: "PMT", + name: "", + processorConfigId: nil, + surcharge: nil, + options: nil, + displayMetadata: nil)) + + let error = PrimerError.paymentFailed(paymentMethodType: "PMT", paymentId: "123", status: "FAILED", userInfo: nil, diagnosticsId: "id") + sut.setCheckoutDataFromError(error) + + XCTAssertEqual(sut.paymentCheckoutData?.payment?.id, "123") + XCTAssertEqual(sut.paymentCheckoutData?.payment?.paymentFailureReason, PrimerPaymentErrorCode.failed) + + let error2 = PrimerError.cancelled(paymentMethodType: "PMT", userInfo: nil, diagnosticsId: "id") + XCTAssertNil(error2.checkoutData) + } + // MARK: Helpers private var checkoutModule: PrimerAPIConfiguration.CheckoutModule {