在Stripe PaymentOptionViewController中未调用添加新卡



我正在尝试在SwiftUI中实现Stripe Add Payment Method。因此,用户可以添加付款方式或从列表中进行选择。经过许多天的搜索,我能够实现一个有效的PaymentOptionsView。但是,当点击添加新卡时,它不会显示STPAddCardViewController以输入新的支付方式

这是显示支付选项的代码

import Foundation
import SwiftUI
import Stripe
struct PaymentOptionsView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, STPPaymentOptionsViewControllerDelegate {
var control: PaymentOptionsView
init(_ control: PaymentOptionsView) {
self.control = control
}
// Implement required delegate methods here:
func paymentOptionsViewControllerDidCancel(_ paymentOptionsViewController: STPPaymentOptionsViewController) {
}
func paymentOptionsViewControllerDidFinish(_ paymentOptionsViewController: STPPaymentOptionsViewController) {
}
func paymentOptionsViewController(_ paymentOptionsViewController: STPPaymentOptionsViewController, didFailToLoadWithError error: Error) {
}
}
func makeUIViewController(context: UIViewControllerRepresentableContext<PaymentOptionsView>) -> STPPaymentOptionsViewController {
let config = STPPaymentConfiguration()
//          config.requiredBillingAddressFields = .none
config.appleMerchantIdentifier = "dummy-merchant-id"
return STPPaymentOptionsViewController(configuration: config, theme: STPTheme(), apiAdapter: STPCustomerContext(keyProvider: MyAPIClient()), delegate: context.coordinator)
}

func updateUIViewController(_ uiViewController: STPPaymentOptionsViewController, context: UIViewControllerRepresentableContext<PaymentOptionsView>) { }
}

这是APIClient

class MyAPIClient: NSObject, STPCustomerEphemeralKeyProvider {

func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let customerId = "cus_LoK4MNElrbzeVg"
let url = "https://us-central1-test-mmmm.cloudfunctions.net/app/createEphemeralKey"


AF.request(url, method: .post, parameters: [
"api_version": "2020-08-27",
"customerId": customerId,
])
.validate(statusCode: 200..<300)
.responseJSON { responseJSON in
switch responseJSON.result {
case .success(let json):
completion(json as? [String: AnyObject], nil)
case .failure(let error):
print("Error creating customer Key (retrieving ephemeral key) with Alamofire. See: MyAPIClient - func createCustomerKey")
completion(nil, error)
}
}
}
}

我做错了什么?我如何实现添加新卡的方法?

这段代码实际上运行得很好。我最初是通过.sheet显示PaymentOptionsView(isPresented:所以当按下添加卡按钮时,它不允许转换到新视图。当PaymentOptions view显示为时

.background( // add a hidden `NavigationLink` in the background
NavigationLink(destination:  PaymentOptionsView(), isActive: $showPaymentSheet) {EmptyView()}
.hidden()
)

一切都好。当按下添加新卡按钮时,视图将完美地转换为STPAddCardViewController。

最新更新