iOS上的条带:createTokenWithPayment的NSInvalidArgumentException

时间:2015-11-02 19:30:22

标签: ios swift stripe-payments

首先在这里发帖!。

我一直试图解决这个问题好几天 - 我在Swift中创建了一个超级基本Stripe Payment示例来学习移动支付。此代码应与我在同一台机器/ LAN上设置的基本python服务器通信。

问题是,只要按下付款按钮,Apple Pay视图就会出现,但是从那里按下付款(在模拟器上)然后会导致以下日志输出崩溃:

-[STPAPIClient createTokenWithPayment:completion:]: unrecognized selector sent to instance 0x7fe54a4f2d00  
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[STPAPIClient createTokenWithPayment:completion:]: unrecognized selector sent to instance 0x7fe54a4f2d00'

似乎无法在Stripe或Swift的文档中找到解决方案。 FWIW,这是基于Ray Wenderlich Apple Pay的例子。 [link]代码如下,非常感谢帮助!

更新:添加-ObjC链接器标志会删除上述错误。然而,我无法弄清楚的另一个错误:Apple Pay屏幕现在提供的付款未完成'当我按下付款时签字。添加了screenshot以供参考。

import Foundation
import UIKit
import PassKit
import Stripe

class Payment: UIViewController, PKPaymentAuthorizationViewControllerDelegate {

    // MISC. PROPERTIES
    var userHasAgreed = true  // made true for this test ONLY
    let SupportedPaymentNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkMasterCard, PKPaymentNetworkAmex]
    let ApplePaySwagMerchantID = "-my merchant id-"

    // IBOUTLETS
    @IBAction func pay(sender: AnyObject) {

        if userHasAgreed == false {
            let alertController = UIAlertController(title: "Missed Step", message: "Please Agree to the User Terms and Agreements to continue", preferredStyle: .Alert)
            let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in }
            alertController.addAction(OKAction)
            self.presentViewController(alertController, animated: true) { }
        } else {
            // pay fn
            let request = PKPaymentRequest()
            request.merchantIdentifier = ApplePaySwagMerchantID
            request.supportedNetworks = SupportedPaymentNetworks
            request.merchantCapabilities = PKMerchantCapability.Capability3DS
            request.countryCode = "US"
            request.currencyCode = "USD"

            var summaryItems = [PKPaymentSummaryItem]()
            summaryItems.append(PKPaymentSummaryItem(label: "Food Total", amount: 12.99 as NSDecimalNumber))

            request.paymentSummaryItems = summaryItems
            request.requiredShippingAddressFields = PKAddressField.Email

            // Display the view controller.
            let viewController = PKPaymentAuthorizationViewController(paymentRequest: request)
            viewController.delegate = self
            presentViewController(viewController, animated: true, completion: nil)
        }
    }

    // MARK -- APPLE PAY WITH STRIPE
    func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: ((PKPaymentAuthorizationStatus) -> Void)) {


        let apiClient = STPAPIClient(publishableKey: "-my test publishable keys")
        apiClient.createTokenWithPayment(payment, completion: { (token, error) -> Void in
            if error == nil {
                if let token = token {
                    self.createBackendChargeWithToken(token, completion: { (result, error) -> Void in
                        if result == STPBackendChargeResult.Success {
                            completion(PKPaymentAuthorizationStatus.Success)
                        }
                        else {
                            completion(PKPaymentAuthorizationStatus.Failure)
                        }
                    })
                }
            }
            else {
                completion(PKPaymentAuthorizationStatus.Failure)
            }
        })
    }

    func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController) {
        // We always need to dismiss our payment view controller when done.
        dismissViewControllerAnimated(true, completion: nil)
    }

    func createBackendChargeWithToken(token: STPToken, completion: STPTokenSubmissionHandler) {
        let url = NSURL(string: "-my ip address-:5000/pay")
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")

        let body = ["stripeToken": token.tokenId,
            "amount": NSDecimalNumber(string: "12.99"),
            "description": "Food Total",
            "shipping": [
                "zip": "20148"]
        ]

        var error: NSError?
        do {
            request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions())
        } catch let error1 as NSError {
            error = error1
            request.HTTPBody = nil
        } catch {
            fatalError()
        }

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in
            if (error != nil) {
                completion(.Failure, nil)
            } else {
                completion(.Success, nil)
                return
            }
        }

        completion(STPBackendChargeResult.Failure, NSError(domain: StripeDomain, code: 50, userInfo: [NSLocalizedDescriptionKey: "Token value is \(token.tokenId)."]))
    }

}

1 个答案:

答案 0 :(得分:1)

您如何整合Stripe SDK?这里的一个理论是,如果您将其作为静态库导入,则需要确保使用-ObjC标记构建它 - 有关此处的更多说明,请参阅https://stripe.com/docs/mobile/ios#manual-installation