在SwiftUI中确认条纹支付(iOS 16)

yrdbyhpb  于 2023-01-12  发布在  Swift
关注(0)|答案(1)|浏览(212)

我正在SwiftUI中使用Stripe API接受卡支付。
我设法使用UIViewRepresentable Package 的STPPaymentCardTextField成功付款,并使用提供的工作表修改器.paymentConfirmationSheet(....)确认。
当我在iOS 16中采用新的NavigationStack时,问题出现了。如果.paymentConfirmationSheet(....)出现在新的NavigationStack中,它似乎无法正常工作。
是否有其他方法可以在SwiftUI中确认卡支付?如何解决此问题?
如果我切换回NavigationView,它会按预期工作,但我想使用NavigationStack的新功能。
我的演示 checkout 视图及其视图模型

struct TestCheckout: View {
    
    @ObservedObject var model = TestCheckoutViewModel()
    
    var body: some View {
        
        VStack {
            CardField(paymentMethodParams: $model.paymentMethodParams)
            
            Button("Pay") {
                model.pay()
            }
            .buttonStyle(LargeButtonStyle())
            .paymentConfirmationSheet(isConfirmingPayment: $model.confirmPayment,
                                      paymentIntentParams: model.paymentIntentParams,
                                      onCompletion: model.onPaymentComplete)
        }
    }
}

class TestCheckoutViewModel: ObservableObject {
    @Published var paymentMethodParams: STPPaymentMethodParams?
    @Published var confirmPayment = false
    @Published var paymentIntentParams = STPPaymentIntentParams(clientSecret: "")
    
    func pay() {
        
        Task {
            
            do {
                // Create dummy payment intent
                let paymentIntent = try await StripeManager.shared.getPaymentIntent(orderId: "", amount: 1000)
                
                // Collect card details
                let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntent.secret)
                paymentIntentParams.paymentMethodParams = paymentMethodParams
                
                // Submit the payment
                DispatchQueue.main.async {
                    self.paymentIntentParams = paymentIntentParams
                    self.confirmPayment = true
                }
            } catch {
                print(error)
            }
        }
    }

    func onPaymentComplete(status: STPPaymentHandlerActionStatus, paymentIntent: STPPaymentIntent?, error: Error?) {
        print("Payment completed: \(error)")
    }
}

现在这个行不通了

struct TestView: View {
    var body: some View {
        NavigationStack {
            NavigationLink("Checkout", value: "checkout")
                .navigationDestination(for: String.self) { string in
                    if string == "checkout" {
                        TestCheckout()
                    }
                }
        }
    }
}

但这个

struct TestView: View {
    var body: some View {
        TestCheckout()
    }
}

我得到的错误是:

Error Domain=com.stripe.lib Code=50 "There was an unexpected error -- try again in a few seconds" UserInfo={NSLocalizedDescription=There was an unexpected error -- try again in a few seconds, com.stripe.lib:StripeErrorTypeKey=invalid_request_error, com.stripe.lib:StripeErrorCodeKey=payment_intent_unexpected_state, com.stripe.lib:ErrorMessageKey=Nemůžete potvrdit tento Platební záměr, protože v něm chybí platební metoda. Můžete buď aktualizovat Platební záměr s platební metodou a pak jej znovu potvrdit, nebo jej znovu potvrďte přímo s platební metodou.}
0s0u357o

0s0u357o1#

最后我发现了问题所在。
问题出在@ObservedObject var model = TestCheckoutViewModel()上,由于某种原因,它不再与@ObservedObject一起工作,它必须是@StateObject

相关问题