2015-11-02 40 views
0

有史以來第一篇文章!Stripe on iOS:NSInvalidArgumentException for createTokenWithPayment

我一直在試圖解決這個問題好幾天 - 我在Swift中創建了一個超級基本的Stripe Payment示例來學習移動支付。該代碼應該與我在同一臺機器/局域網上設置的基本python服務器通信。

的問題是,當我按下支付按鈕,蘋果付費觀看大作,但按工資從那裏(在模擬器上),然後導致崩潰與下面的日誌輸出:

-[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

你如何整合條紋SDK?這裏的一個理論是,如果您將其作爲靜態庫導入,則需要確保使用-ObjC標誌構建它 - 有關更多說明,請參見https://stripe.com/docs/mobile/ios#manual-installation

+0

Jflinter,恰巧我今天做到了!這確實擺脫了我得到的錯誤信息 - 謝謝。現在代碼停留在Apple Pay的付款步驟,我更新了我的帖子以反映它。 – AluminumFoot