【问题标题】:Integrating Stripe Payments in iOS在 iOS 中集成 Stripe 支付
【发布时间】:2015-06-03 05:22:29
【问题描述】:

我正在尝试将 Stripe 付款集成到我的 iOS 应用中,但我似乎无法解决这些错误。我不确定他们是否与我的桥接头有关,但是否有人可以查看代码。 提前谢谢了。 这是我的 ViewControllerClass 中的代码:

class PaymentInfoViewController: UIViewController, UITextFieldDelegate {

    **var stripeView: STPView = STPView()**

    @IBOutlet var cardNumber: UITextField!
    @IBOutlet var expiryDate: UITextField!
    @IBOutlet var cvc: UITextField!

    @IBOutlet var sendPaymentInfo: UIButton!
    @IBAction func getStripeToken(sender: AnyObject) {
        let creditCard = STPCard() //creating a stripe card object
        creditCard.number = cardNumber.text
        creditCard.cvc = cvc.text

        //extracting month and year values from expiry date
        if (!expiryDate.text.isEmpty){
            let expArr = expiryDate.text.componentsSeparatedByString("/")
            if (expArr.count > 1)
            {
                var expMonth: NSNumber = expArr[0].toInt()!
                var expYear: NSNumber = expArr[1].toInt()!

                creditCard.expMonth = expMonth.unsignedLongValue
                creditCard.expYear = expYear.unsignedLongValue
            }
        }

        var error: NSError?
        if (creditCard.validateCardReturningError(&error)){
            var stripeError: NSError!
            **Stripe.createTokenWithCard(creditCard, completion: { (token, stripeError) -> Void in**
                if (stripeError != nil){
                    println("there is error");
                }
                else{
                    self.cardNumber.text = ""
                    self.expiryDate.text = ""
                    self.cvc.text = ""


                    //shows your stripe token
                    var alert = UIAlertController(title: "Your stripe token is: " + token.tokenId, message: "", preferredStyle: UIAlertControllerStyle.Alert)
                    var defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                    alert.addAction(defaultAction)
                    self.presentViewController(alert, animated: true, completion: nil)
                }
            })
        }else{
            //shows alert if information is not correct
            var alert = UIAlertController(title: "Please enter valid credit card details", message: "", preferredStyle: UIAlertControllerStyle.Alert)
            var defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
            alert.addAction(defaultAction)
            self.presentViewController(alert, animated: true, completion: nil)

        }


    }

用双星号括起来的部分是编译错误所在。

桥接头代码:

#import "Stripe.h"
@import Foundation;

【问题讨论】:

  • 有什么错误?
  • 第一个错误:使用未声明的类型“STPView”
  • 第二个错误:'createTokenWithCard(:completion:)' 不可用

标签: ios stripe-payments


【解决方案1】:

如果您使用 3.0 以上的 SDK 版本,您应该查看 github 上的迁移指南:https://github.com/stripe/stripe-ios#migration-guides

具体来说-“在 3.0 版之前,大多数令牌创建方法都是​​ Stripe 类的类方法。这些现在都是 STPAPIClient 类的实例方法。”

您可能希望通过以下方式获得更好的运气(从您的 getStripeToken 函数开始): ```@IBAction func getStripeToken(sender: AnyObject){ 让信用卡 = STPCard() 让 StripePublicKey = "your_key_here" 让 stpClient = STPAPIClient(publishableKey: StripePublicKey)

    creditCard.number = cardNum.text
    creditCard.cvc = cvc.text

    if (!exp.text.isEmpty) {
        let expArr = exp.text.componentsSeparatedByString("/")
        if (expArr.count > 1) {
            var expMonth: NSNumber = expArr[0].toInt()!
            var expYear: NSNumber = expArr[1].toInt()!

            creditCard.expMonth = expMonth.unsignedLongValue
            creditCard.expYear = expYear.unsignedLongValue
        }
    }

    var error: NSError?
    if (creditCard.validateCardReturningError(&error)){
        var stripeError: NSError!
        stpClient.createTokenWithCard(creditCard, completion: { (token, stripeError) -> Void in
            if (stripeError != nil){
                println("there is error");
            }
            else{
                self.cardNum.text = ""
                self.exp.text = ""
                self.cvc.text = ""
                self.email.text = ""

                var alert = UIAlertController(title: "Your stripe token is: " + token!.tokenId, message: "", preferredStyle: UIAlertControllerStyle.Alert)
                var defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                alert.addAction(defaultAction)
                self.presentViewController(alert, animated: true, completion: nil)
            }
        })
    }```

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2019-01-20
    • 2012-10-12
    • 2014-11-28
    • 1970-01-01
    • 2013-11-01
    • 2021-06-13
    • 2013-04-29
    • 2016-01-04
    相关资源
    最近更新 更多