Skip to content

Commit

Permalink
feat(ad-hoc): make card saving public (#356)
Browse files Browse the repository at this point in the history
* Make card saving functionality public
* Disable Checkout3DS integration in example (due to its incompatibility with Swift 6)
  • Loading branch information
andrii-vysotskyi-cko authored Sep 25, 2024
1 parent bf9338e commit 774a135
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Combine
import SwiftUI
@_spi(PO) import ProcessOut
import ProcessOutUI
import ProcessOutCheckout3DS

@MainActor
final class CardPaymentViewModel: ObservableObject {
Expand All @@ -36,16 +35,19 @@ final class CardPaymentViewModel: ObservableObject {
// MARK: - Private Methods

private func commonInit() {
// todo(andrii-vysotskyi): allow using Checkout3DS when compatibility with Swift 6 is restored
state = .init(
authenticationService: .init(
sources: [.test, .checkout], id: \.self, selection: .test
sources: [.test], id: \.self, selection: .test
),
cardTokenization: nil
)
}

private func setCardTokenizationItem() {
let configuration = POCardTokenizationConfiguration(isCardholderNameInputVisible: false)
let configuration = POCardTokenizationConfiguration(
isCardholderNameInputVisible: false, isSavingAllowed: true
)
let cardTokenizationItem = CardPaymentViewModelState.CardTokenization(
id: UUID().uuidString,
configuration: configuration,
Expand All @@ -72,7 +74,8 @@ final class CardPaymentViewModel: ObservableObject {
name: UUID().uuidString,
amount: state.invoice.amount,
currency: state.invoice.currencyCode,
returnUrl: Constants.returnUrl
returnUrl: Constants.returnUrl,
customerId: Constants.customerId
)
return try await invoicesService.createInvoice(request: request)
} else {
Expand All @@ -92,36 +95,11 @@ extension CardPaymentViewModel: POCardTokenizationDelegate {
saveSource: save,
clientSecret: invoice.clientSecret
)
let threeDSService: PO3DSService
switch state.authenticationService.selection {
case .test:
threeDSService = POTest3DSService(returnUrl: Constants.returnUrl)
case .checkout:
threeDSService = POCheckout3DSServiceBuilder()
.with(delegate: self)
.with(environment: .sandbox)
.build()
}
let threeDSService = POTest3DSService(returnUrl: Constants.returnUrl)
try await invoicesService.authorizeInvoice(request: invoiceAuthorizationRequest, threeDSService: threeDSService)
}
}

extension CardPaymentViewModel: POCheckout3DSServiceDelegate {

func handle(redirect: PO3DSRedirect, completion: @escaping (Result<String, POFailure>) -> Void) {
Task { @MainActor in
let session = POWebAuthenticationSession(
redirect: redirect, returnUrl: Constants.returnUrl, completion: completion
)
if await session.start() {
return
}
let failure = POFailure(message: "Unable to process redirect", code: .generic(.mobile))
completion(.failure(failure))
}
}
}

extension CardPaymentViewModel {

/// Convenience initializer that resolves its dependencies automatically.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ final class DynamicCheckoutViewModel: ObservableObject {
name: UUID().uuidString,
amount: state.invoice.amount,
currency: state.invoice.currencyCode,
returnUrl: Constants.returnUrl
returnUrl: Constants.returnUrl,
customerId: Constants.customerId
)
return try await invoicesService.createInvoice(request: request)
} else {
Expand Down
3 changes: 0 additions & 3 deletions Example/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ settings:
packages:
ProcessOutUI:
path: ../
ProcessOutCheckout3DS:
path: ../

targets:
Example:
Expand All @@ -34,7 +32,6 @@ targets:
PRODUCT_BUNDLE_IDENTIFIER: com.processout.processout-example
dependencies:
- package: ProcessOutUI
- package: ProcessOutCheckout3DS
ExampleUiTests:
type: bundle.ui-testing
sources: ExampleUiTests
Expand Down
24 changes: 24 additions & 0 deletions Sources/ProcessOutUI/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1261,11 +1261,35 @@
},
"card-tokenization.save-card-message" : {
"localizations" : {
"ar" : {
"stringUnit" : {
"state" : "translated",
"value" : "احفظ تفاصيل هذه البطاقة لاستخدامها في المستقبل"
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Save this card for future payments"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Enregistrer cette carte pour les paiements futurs"
}
},
"pl" : {
"stringUnit" : {
"state" : "translated",
"value" : "Zapisz tę kartę na przyszłe płatności"
}
},
"pt-PT" : {
"stringUnit" : {
"state" : "translated",
"value" : "Guardar este cartão para futuros pagamentos"
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public struct POCardTokenizationConfiguration {

/// Indicates whether the UI should display a control that allows the user
/// to choose whether to save their card details for future payments.
@_spi(PO)
public let isSavingAllowed: Bool

/// Metadata related to the card.
Expand All @@ -49,28 +48,7 @@ public struct POCardTokenizationConfiguration {
primaryActionTitle: String? = nil,
cancelActionTitle: String? = nil,
billingAddress: POBillingAddressConfiguration = .init(),
metadata: [String: String]? = nil
) {
self.title = title
self.isCardholderNameInputVisible = isCardholderNameInputVisible
self.shouldCollectCvc = shouldCollectCvc
self.primaryActionTitle = primaryActionTitle
self.cancelActionTitle = cancelActionTitle
self.billingAddress = billingAddress
self.isSavingAllowed = false
self.metadata = metadata
isSchemeSelectionAllowed = false
}

@_spi(PO)
public init(
title: String? = nil,
isCardholderNameInputVisible: Bool = true,
shouldCollectCvc: Bool = true,
primaryActionTitle: String? = nil,
cancelActionTitle: String? = nil,
billingAddress: POBillingAddressConfiguration = .init(),
isSavingAllowed: Bool,
isSavingAllowed: Bool = false,
metadata: [String: String]? = nil
) {
self.title = title
Expand Down

0 comments on commit 774a135

Please sign in to comment.