【问题标题】:SwiftUI - Stripe STPPaymentContext view not updating with STPPaymentConfigurationSwiftUI - Stripe STPPaymentContext 视图未使用 STPPaymentConfiguration 更新
【发布时间】:2020-12-21 21:36:00
【问题描述】:

所以我已经在我的项目中实现了 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
        
    }
}

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

【问题讨论】:

    标签: swift swiftui stripe-payments


    【解决方案1】:

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

    例如,applePayEnabled 只会在设备本身支持 Apple Pay 的情况下显示 Apple Pay 选项(在钱包中有真实卡的物理设备上它可以工作(但您必须拥有真实的 Apple 商家 ID) ,在模拟器上可能是不完整的)。

    对于cardScanningEnabled,您的应用可能没有启用授权? https://github.com/stripe/stripe-ios#card-scanning-beta

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

    【讨论】:

    • 我感觉 Apple 需要支付功能,这很好,但即使尝试将默认国家/地区设置为英国(使用 Stripe API 状态的 ISO 代码)也会导致我的应用程序崩溃,因为它发现了nil 值,这没有意义并且让我感到困惑。是的,我可以篡改requiredShippingAddressFields。我选择退出此选项,但是,我可以肯定地篡改它。编辑 - 关于卡片扫描,您是对的。我没有权利。
    • 嗯,也许 ISO 代码是 GB 而您正在尝试 UK?请注意为什么这会导致关于 nil 值的崩溃,如果没有看到您尝试的确切代码 + 错误的堆栈跟踪,很难说。
    • 我试过 GB/UK。我什至尝试过更改paymentContext.paymentCountry,这会导致同样的问题,这很奇怪。
    猜你喜欢
    • 2020-12-21
    • 2021-10-14
    • 2020-03-06
    • 2020-05-02
    • 1970-01-01
    • 2021-07-11
    • 2020-07-12
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多