简体   繁体   English

iOS上的条纹:createTokenWithPayment的NSInvalidArgumentException

[英]Stripe on iOS: NSInvalidArgumentException for createTokenWithPayment

First ever post here!. 第一次在这里发布!

I have been trying to resolve this issue for several days - I created a super basic Stripe Payment example in Swift to learn mobile Payments. 几天来,我一直在尝试解决此问题-我在Swift中创建了一个超级基本的Stripe Payment示例,以学习移动支付。 This code should communicate with the basic python server I set up on the same machine/LAN. 此代码应与我在同一台机器/ LAN上设置的基本python服务器通信。

The problem is, as soon as I press the pay button, the Apple Pay view comes up, but pressing pay from there (on the simulator) then results in a crash with the following log output: 问题是,一旦按下付款按钮,就会出现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'

Can't seem to find a solution in Stripe's or Swift's documentation for this. 在Stripe或Swift的文档中似乎找不到解决方案。 FWIW, this is based on the Ray Wenderlich Apple Pay example. FWIW,这是基于Ray Wenderlich Apple Pay的示例。 [link] Code is below, help would be very appreciated! [link]代码在下面,非常感谢您的帮助!

Update: adding the -ObjC linker flag removes the above errors. 更新:添加-ObjC链接器标志可消除上述错误。 There is however another error I can't figure out: Apple Pay screen now gives a 'Payment not completed' sign when I press Pay. 但是,还有一个我无法弄清的错误:当我按“付款”时,Apple Pay屏幕现在显示“付款未完成”标志。 Added a screenshot for reference. 添加了屏幕截图以供参考。

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)."]))
    }

}

How are you integrating the Stripe SDK? 您如何集成Stripe SDK? One theory here is that if you're importing it as a static library, you need to make sure that you're building it with the -ObjC flag - for more instructions here, see https://stripe.com/docs/mobile/ios#manual-installation . 这里的一种理论是,如果要将其作为静态库导入,则需要确保使用-ObjC标志进行构建-有关此处的更多说明,请参见https://stripe.com/docs/mobile / ios#manual-installation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM