断言失败:“secret”格式与 iOS Swift 中预期的客户端机密格式不匹配

问题描述 投票:0回答:1

第一个API的实现 Stripe 有三个 API,我已经正确实现了这三个 API,并正确显示了它们的结果,但我在打开付款表时遇到问题,打开了一半的付款表,然后应用程序崩溃了。

func postRequestForCustomer() {
        let headers: HTTPHeaders = [
            Constants.authorization: Constants.bearer
        ]
        AF.request(Constants.stripeBaseURL + Constants.customers, method: .post, encoding: JSONEncoding.default, headers: headers)
            .validate(statusCode: 200..<300)
            .response { [self] response in
                switch response.result {
                case .success(let value):
                    if let jsonData = value {
                        do {
                            let json = try JSON(data: jsonData)
                            if let customerId = json["id"].string {
                                postRequestForEphemeralKey(customerID: customerId)
                            }
                        } catch {
                            print("Error parsing JSON for customer creation: \(error.localizedDescription)")
                        }
                    } else {
                        print("Unexpected response format for customer creation")
                    }
                case .failure(let error):
                    print("Customer Creation Error: \(error.localizedDescription)")
                }
            }
    }

第二个API的实现

func postRequestForEphemeralKey(customerID: String) {
        let headers: HTTPHeaders = [
            Constants.authorization: Constants.bearer,
            Constants.stripe_Version: Constants.stripe_Version_date
        ]
        let params: [String: Any] = [
            Constants.Params.customer: customerID
        ]
        AF.request(Constants.stripeBaseURL + Constants.ephemeral_keys, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers)
            .validate(statusCode: 200..<300)
            .response { [self] response in
                switch response.result {
                case .success(let value):
                    if let jsonData = value {
                        print("Ephemeral Key Response: \(String(data: jsonData, encoding: .utf8) ?? "")")
                        postRequestForPayment(customerID: customerID, amount: 10000)
                    } else {
                        print("Unexpected response format for ephemeral key")
                    }
                case .failure(let error):
                    if let data = response.data, let errorMessage = String(data: data, encoding: .utf8) {
                        print("Ephemeral Key Error: \(errorMessage)")
                    } else {
                        print("Ephemeral Key Error: \(error.localizedDescription)")
                    }
                }
            }
    }

第三个API的实现

func postRequestForPayment(customerID: String, amount: Int) {
        let headers: HTTPHeaders = [
            Constants.authorization: Constants.bearer,
        ]

        let params: [String: Any] = [
            Constants.Params.customer: customerID,
            Constants.Params.amount: amount,
            Constants.Params.currency: Constants.Params.usd
        ]

        AF.request(Constants.stripeBaseURL + Constants.payment_intents, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers)
            .validate(statusCode: 200..<300)
            .response { [self] response in
                switch response.result {
                case .success(let value):
                    if let jsonData = value {
                        do {
                            let json = try JSON(data: jsonData)
                            if let ephKeyID = json["id"].string {
                                openPaymentSheet()
                                print("Ephemeral Key ID: \(ephKeyID)")
                            } else {
                                print("Ephemeral Key ID not found in response")
                            }
                        } catch {
                            print("Error parsing JSON for ephemeral key: \(error.localizedDescription)")
                        }
                    } else {
                        print("Unexpected response format for ephemeral key")
                    }

                case .failure(let error):
                    if let data = response.data, let errorMessage = String(data: data, encoding: .utf8) {
                        print("Ephemeral Key Error: \(errorMessage)")
                    } else {
                        print("Ephemeral Key Error: \(error.localizedDescription)")
                    }
                }
            }
    }

打开付款单

func openPaymentSheet() {
        if let paymentSheet = paymentSheet {
            paymentSheet.present(from: self) { paymentResult in
                // MARK: Handle the payment result
                switch paymentResult {
                case .completed:
                    print("Your order is confirmed")
                case .canceled:
                    print("Canceled!")
                case .failed(let error):
                    print("Payment failed: \(error)")
                }
            }
        } else {
            print("Payment sheet is nil. Check the configuration.")
        }
    }

这里是viewDidLoad方法

override func viewDidLoad() {
        super.viewDidLoad()
        xibRegister()
        setUI()
        let configuration = PaymentSheet.Configuration()
        paymentSheet = PaymentSheet(paymentIntentClientSecret: Constants.secret_Key, configuration: configuration)
    }

我要打开付款单。

ios swift stripe-payments payment
1个回答
0
投票

初始化 PaymentSheet 时,您是否传递了 Stripe 密钥(格式为

sk_test_123
)?如果是这样,那就可以解释您遇到的错误。

paymentIntentClientSecret
参数需要支付意图
client_secret
(格式为
pi_xxx_secret_yyy
),这与您的帐户范围的密钥不同。您需要通过 Stripe 的 API 创建付款意图,并使用创建后返回的
client_secret
来正确设置付款表。 他们在“接受付款”文档中讨论了所有这些内容这里

© www.soinside.com 2019 - 2024. All rights reserved.