首页 > 解决方案 > SwiftUI - Stripe STPPaymentContext 视图未使用 STPPaymentConfiguration 更新

问题描述

所以我已经在我的项目中实现了 Stripe。一切都按预期工作。但是,我确实希望使用一些可用的配置选项。例如,将默认付款国家/地区设置为“英国”。但是,这会导致我的应用程序在找到 nil 值时崩溃。我也在尝试使用其他一些设置,如下所示:

self.config.requiredBillingAddressFields = .full
self.config.appleMerchantIdentifier = "dummy-merchant-id"
self.config.cardScanningEnabled = true
self.config.applePayEnabled = true

我似乎可以改变的观点中唯一的事情是.requiredBillingAddressFields. 其他一切似乎都没有注册。我的代码如下,为了清楚起见,我删除了与 Stripe 无关的内容:

struct CheckOutView: View {
    @StateObject var paymentContextDelegate: PaymentContextDelegate
    let config = STPPaymentConfiguration()
    @State private var paymentContext: STPPaymentContext!
    
    var body: some View {

        HStack {
            BorderedButton(text: "Pay Now") {
                self.paymentContext.requestPayment()
            }
            
            Spacer()
        }
        HStack {
            BorderedButton(text: paymentContextDelegate.paymentMethodButtonTitle) {
                self.paymentContext.presentPaymentOptionsViewController()
            }
            Spacer()
        }
        .onAppear() {
            DispatchQueue.main.async {
                self.paymentContextConfiguration()
                
            }
        }
    }
    
    //MARK: - Configuration
    func paymentContextConfiguration() {


        self.config.requiredBillingAddressFields = .full
        self.config.appleMerchantIdentifier = "dummy-merchant-id"
        self.config.cardScanningEnabled = true
        self.config.applePayEnabled = true
        self.config.verifyPrefilledShippingAddress = true
        self.config.canDeletePaymentOptions = true

        
        
        
        self.paymentContext = STPPaymentContext(customerContext: customerContext, configuration: self.config, theme: .defaultTheme)
        
        self.paymentContext.delegate = self.paymentContextDelegate
        
        let keyWindow = UIApplication.shared.connectedScenes
            .filter({$0.activationState == .foregroundActive})
            .map({$0 as? UIWindowScene})
            .compactMap({$0})
            .first?.windows
            .filter({$0.isKeyWindow}).last
        
        
        self.paymentContext.hostViewController = keyWindow?.rootViewController
        
    }
}

任何帮助,将不胜感激!谢谢你。

标签: swiftswiftuistripe-payments

解决方案


这更像是一个猜测,但也许配置选项正在应用,但只是没有满足它们启用的功能的其他要求?您如何确定除帐单地址以外的所有其他内容均未注册?

例如applePayEnabled,仅当设备本身支持 Apple Pay 时才会显示 Apple Pay 选项(在钱包中有真实卡的物理设备上它可以工作(但您必须有一个真实的 Apple Merchant ID),在模拟器上可以斑驳的)。

因为cardScanningEnabled也许您没有在您的应用程序中启用权利?https://github.com/stripe/stripe-ios#card-scanning-beta

检查配置是否适用的一种简单方法可能是设置requiredShippingAddressFields,因为它是独立的并且非常直观。


推荐阅读