Skip to content

Commit

Permalink
fix: Add some checkoutData in the case of error (#987)
Browse files Browse the repository at this point in the history
* Add some checkoutData in the case of error

* Added tests for checkoutData in error

* increase scope of test
  • Loading branch information
NQuinn27 authored Aug 28, 2024
1 parent 5a0e71c commit e952dd2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ extension PaymentMethodTokenizationViewModel {
userInfo: .errorUserInfoDictionary(),
diagnosticsId: UUID().uuidString)
}

self.setCheckoutDataFromError(primerErr)
return PrimerDelegateProxy.raisePrimerDidFailWithError(primerErr, data: self.paymentCheckoutData)
}
.done { merchantErrorMessage in
Expand Down Expand Up @@ -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 {}
Expand Down
1 change: 0 additions & 1 deletion Tests/Primer/Extensions/ErrorExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e952dd2

Please sign in to comment.