ios - Apple Pay 和 Stripe : Token not being sent to Stripe

标签 ios stripe-payments applepay

在我的应用程序中设置 Apple Pay,在设备上运行时似乎工作正常。使用 Stripe 作为支付处理器,但它没有将 token 发送到 Stripe,但似乎正在向我的 iPhone 数字钱包中列出的信用卡收费。

我遇到的问题是,一旦我按下“使用 Touch ID 付款”,我的 Apple Pay 表中就会出现一个复选标记,但 Xcode 中会出现以下页面:

error

Apple Pay 和 Stripe 代码

    var paymentSucceeded: Bool = false


func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController,
                                        didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {

    STPAPIClient.shared().createToken(with: payment) { (token, error) in
        print("I am here")

            if error != nil {
                completion(.failure)
                print("failed")

            } else {
                self.paymentSucceeded = true
                completion(.success)
                print("woohoo")
            }

            self.createBackendCharge(with: token!, completion: completion)
            print("created Backend Charge")
            self.postStripeToken(token: token!)
            print("posted stripe token")

    }

} // paymentAuthorizationViewController( didAuthorizePayment )


func createBackendCharge(with token: STPToken, completion: @escaping (_: PKPaymentAuthorizationStatus) -> Void) {
    //We are printing Stripe token here, you can charge the Credit Card using this token from your backend.
    print("Stripe Token is \(token)")
    completion(.success)

} // createBackendCharge func



func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {

    controller.dismiss(animated: true, completion: {
        if (self.paymentSucceeded) {
            // show a receipt page
        }
    })

} // paymentAuthorizationViewControllerDidFinish()


    @IBAction func applePayPressed(_ sender: UIButton) {

    // we have already accepted the request from viewDriverBids
    // all that remains is to complete payment

    print("enable apple pay")

    // send user to Apple Pay to make payment

    let paymentNetworks = [PKPaymentNetwork.visa, .masterCard, .interac, .discover, .amex]

    if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {
        paymentRequest = PKPaymentRequest()
        paymentRequest.currencyCode = "CAD"
        paymentRequest.countryCode = "CA"
        paymentRequest.merchantIdentifier = "merchant.com.xxx"
        paymentRequest.supportedNetworks = paymentNetworks
        paymentRequest.merchantCapabilities = .capability3DS
        paymentRequest.requiredShippingAddressFields = [.all]
        paymentRequest.paymentSummaryItems = self.rydes()

        let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
        applePayVC.delegate = self
        self.present(applePayVC, animated: true, completion: {

            rRydeHandler.Instance.completeApplePay()

            self.paymentComplete = true
            self.updateDriverInfoView()
        })

    }  else {

        print("Tell the user they need to set up Apple Pay!")
    }

} // applePayPressed func ACTION

后端服务器功能

func postStripeToken(token: STPToken) {

    let URL = "http://localhost/donate/payment.php"
    let params = ["stripeToken": token.tokenId,
                  "amount": Int(self.driverInfoView.rydeFare.text!)!,
                  "currency": "cad",
                  "description": self.riderName] as [String : Any]

    let manager = AFHTTPSessionManager()
    manager.post(URL, parameters: params, success: { (operation, responseObject) -> Void in

        if let response = responseObject as? [String: String] {

            let alertController = UIAlertController(title: response["status"], message: response["message"], preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        }

    }) { (operation, error) -> Void in
        self.handleError(error as NSError)
        print(error)
    }
}

如何解决这个问题?

最佳答案

启用异常断点,然后 Xcode 应该在导致问题的行上崩溃。

这几乎肯定是由代码中的 ! 之一引起的。

强制解开值是非常危险的。将其打开在 guard letif let 中总是更好、更安全。

关于ios - Apple Pay 和 Stripe : Token not being sent to Stripe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320517/

相关文章:

IOS AVPlayer 获取 fps

javascript - 在 node.js 中使用 stripe 实现 ach

ios - PKPaymentAuthorizationViewController 在 iOS 8.4 上意外崩溃

iphone - 如何启动/打开苹果支付或钱包应用程序

ios - 非 -'@objc' 方法 'paymentAuthorizationViewControllerDidFinish' 不满足 '@objc' 协议(protocol)的要求

ios - 将 UIVibrancyEffect 添加到 UITableView 单元格

ios - 如何在初始化器中使用 Swift 扩展函数?

ios - 如何使用 Swift 从 STPPaymentMethodsViewController 获取支付 token ?

ruby-on-rails - Multi-Tenancy 应用程序的 Stripe Connect 重定向 uri 问题

xcode - 我们可以在模拟器中测试Apple pay吗