Skip to content

Commit

Permalink
feat(ad-hoc): expose payment method when authorizing dynamic checkout (
Browse files Browse the repository at this point in the history
…#369)

Include payment method information that is used to
authorize invoice during dynamic checkout.
  • Loading branch information
andrii-vysotskyi-cko authored Oct 15, 2024
1 parent e884978 commit 0baf6c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ final class DynamicCheckoutViewModel: ObservableObject {
extension DynamicCheckoutViewModel: PODynamicCheckoutDelegate {

func dynamicCheckout(
willAuthorizeInvoiceWith request: inout POInvoiceAuthorizationRequest
willAuthorizeInvoiceWith request: inout POInvoiceAuthorizationRequest,
using paymentMethod: PODynamicCheckoutPaymentMethod
) async -> any PO3DS2Service {
POTest3DSService()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import PassKit
import ProcessOut
@_spi(PO) import ProcessOut

/// Dynamic checkout module delegate.
@_spi(PO)
Expand All @@ -21,7 +21,8 @@ public protocol PODynamicCheckoutDelegate: AnyObject {
/// Your implementation may alter request parameters and return new request but make
/// sure that invoice id and source stay the same.
func dynamicCheckout(
willAuthorizeInvoiceWith request: inout POInvoiceAuthorizationRequest
willAuthorizeInvoiceWith request: inout POInvoiceAuthorizationRequest,
using paymentMethod: PODynamicCheckoutPaymentMethod
) async -> PO3DS2Service

/// Asks delegate whether user should be allowed to continue after failure or module should complete.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ final class DynamicCheckoutDefaultInteractor:
var authorizationRequest = POInvoiceAuthorizationRequest(
invoiceId: startedState.invoice.id, source: card.id
)
let threeDSService = await delegate.dynamicCheckout(willAuthorizeInvoiceWith: &authorizationRequest)
let threeDSService = await delegate.dynamicCheckout(
willAuthorizeInvoiceWith: &authorizationRequest, using: .applePay(method)
)
try await invoicesService.authorizeInvoice(
request: authorizationRequest, threeDSService: threeDSService
)
Expand Down Expand Up @@ -463,7 +465,12 @@ final class DynamicCheckoutDefaultInteractor:
let response = try await alternativePaymentsService.authenticate(
using: method.configuration.redirectUrl
)
try await authorizeInvoice(source: response.gatewayToken, saveSource: false, startedState: startedState)
try await authorizeInvoice(
using: .alternativePayment(method),
source: response.gatewayToken,
saveSource: false,
startedState: startedState
)
setSuccessState()
} catch {
restart(toRecoverPaymentProcessingError: error)
Expand Down Expand Up @@ -554,7 +561,9 @@ final class DynamicCheckoutDefaultInteractor:
if let redirectUrl = method.configuration.redirectUrl {
source = try await alternativePaymentsService.authenticate(using: redirectUrl).gatewayToken
}
try await authorizeInvoice(source: source, saveSource: false, startedState: startedState)
try await authorizeInvoice(
using: .customerToken(method), source: source, saveSource: false, startedState: startedState
)
setSuccessState()
} catch {
restart(toRecoverPaymentProcessingError: error)
Expand Down Expand Up @@ -635,7 +644,12 @@ final class DynamicCheckoutDefaultInteractor:
}
}

private func authorizeInvoice(source: String, saveSource: Bool, startedState: State.Started) async throws {
private func authorizeInvoice(
using paymentMethod: PODynamicCheckoutPaymentMethod,
source: String,
saveSource: Bool,
startedState: State.Started
) async throws {
guard let delegate else {
throw POFailure(message: "Delegate must be set to authorize invoice.", code: .generic(.mobile))
}
Expand All @@ -646,7 +660,7 @@ final class DynamicCheckoutDefaultInteractor:
allowFallbackToSale: true,
clientSecret: startedState.clientSecret
)
let threeDSService = await delegate.dynamicCheckout(willAuthorizeInvoiceWith: &request)
let threeDSService = await delegate.dynamicCheckout(willAuthorizeInvoiceWith: &request, using: paymentMethod)
try await invoicesService.authorizeInvoice(request: request, threeDSService: threeDSService)
}
}
Expand All @@ -664,7 +678,12 @@ extension DynamicCheckoutDefaultInteractor: POCardTokenizationDelegate {
logger.error("Unable to process card in unsupported state: \(state).")
throw POFailure(message: "Something went wrong.", code: .internal(.mobile))
}
try await authorizeInvoice(source: card.id, saveSource: save, startedState: currentState.snapshot)
try await authorizeInvoice(
using: currentPaymentMethod(state: currentState),
source: card.id,
saveSource: save,
startedState: currentState.snapshot
)
}

func preferredScheme(issuerInformation: POCardIssuerInformation) -> String? {
Expand Down

0 comments on commit 0baf6c5

Please sign in to comment.