简体   繁体   中英

Stripe in Swift

I'm have a custom form for the card payment system. The form contain three textfields with the name cardNumber, cvcNumber and expiry date. i'm using stripe for the payment system. The stripe test key is successfully configured. But in my view controller when i write a code for STPCard() its shows me some error. the cocoapods are installed fine but still its showing an error, i'm trying to get the token from stripe but it isn't happeneing. My code is this,

 // Initiate the card
    let stripCard = STPCard()

    // Split the expiration date to extract Month & Year
    if self.expieryTxt.text?.isEmpty == false {
        let expirationDate = self.expieryTxt.text?.components( separatedBy: "/")
        let expMonth = UInt(expirationDate![0].toInt()!)
        let expYear = UInt(expirationDate![1].toInt()!)

        // Send the card info to Strip to get the token
        stripCard.nuber = self.cardNumberTxt.text
        stripCard.cvc = self.cvcNumberTxt.text
        stripCard.expMonth = expMonth
        stripCard.expYear = expYear
    }


    var underlyingError: NSError?
    stripCard.validateCardReturningError(&underlyingError)
    if underlyingError != nil {
        self.handleError(error: underlyingError!)
        return
    }

    STPAPIClient.sharedClient().createTokenWithCard(stripCard, completion: { (token, error) -> Void in

        if error != nil {
            self.handleError(error!)
            return
        }

        self.postStripeToken(token!)
    })

Errors are 'init()' is unavailable: You cannot directly instantiate an STPCard. You should only use one that has been returned from an STPAPIClient callback. 'init()' is unavailable: You cannot directly instantiate an STPCard. You should only use one that has been returned from an STPAPIClient callback.

and other one is this,

Value of type 'String' has no member 'toInt'

Value of type error, try this:

let expMonth = UInt(Int(expirationDate![0])!)
let expYear = UInt(Int(expirationDate![1])!)

Another error says, that you cant create "empty" STPCard, you have get one from STPAPIClient, then you can use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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