Apple Pay and Stripe:令牌未被发送到Stripe

时间:2017-04-10 10:14:45

标签: ios stripe-payments applepay

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

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

error

Apple Pay&的代码条纹

    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)
    }
}

我将非常感谢您如何解决此问题。

1 个答案:

答案 0 :(得分:1)

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

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

强制展开值非常危险。在guard letif let中展开它总是更好,更安全。