AluminumFoot
AluminumFoot

Reputation: 1

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. This code should communicate with the basic python server I set up on the same machine/LAN.

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:

-[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. FWIW, this is based on the Ray Wenderlich Apple Pay example. [link] Code is below, help would be very appreciated!

Update: adding the -ObjC linker flag removes the above errors. There is however another error I can't figure out: Apple Pay screen now gives a 'Payment not completed' sign when I press 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)."]))
    }

}

Upvotes: 0

Views: 557

Answers (1)

jflinter
jflinter

Reputation: 986

How are you integrating the 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.

Upvotes: 1

Related Questions