Not able to get stripe payment token in iOS Swift

591 views Asked by At

I am implementing stripe paymentSDK and after implementing my code I am getting error in my console, here my code:

View Controller Class

 func stripPayment(CVC:String){
    // Initiate the card
    let stripCard = STPCardParams()
    if cardMM == "" && cardYY == "" {
    // Send the card info to Strip to get the token
        stripCard.number=card_number
        stripCard.name=card_name
        stripCard.cvc=CVC
        stripCard.expYear=UInt(cardYY)!
        stripCard.expMonth=UInt(cardMM)!
    }
    print("card_number:",card_number)
    print("card_name:",card_name)
    print("CVC:",CVC)
    print("cardYY:",cardYY)
    print("cardMM:",cardMM)
    STPAPIClient.shared.createToken(withCard: stripCard) { (token: STPToken?, error: Error?) in
        print("Printing Strip response:\(String(describing: token?.allResponseFields))\n\n")
        print("Printing Strip Token:\(String(describing: token?.tokenId))")
        if error != nil {
            print(error)
            print(error?.localizedDescription ?? "")
        }
        if token != nil{
            print("Token:",token!.tokenId)
            self.placeOrderApi(tokenStripe: token!.tokenId, completion: {
                self.pushVC(id: "PaymentConfirmationVCID")
            })
        }
    }
}

App Delegate DidFinishLaunch

STPAPIClient.shared.publishableKey = "pk_test_51KuLPhJ4jB1YaTRoUayoteEXbtl5QvbayNeDDhjv4Nto9UvdM4LcTsP2EXAbU0IuOoKsIsOAk565HzvjLRPVP9wY00Ro5T3PYq"

Error I am getting in console

Printing Strip Token:nil Optional(Error Domain=com.stripe.lib Code=50 "There was an unexpected error -- try again in a few seconds" UserInfo={com.stripe.lib:ErrorParameterKey=card[number], NSLocalizedDescription=There was an unexpected error -- try again in a few seconds, com.stripe.lib:ErrorMessageKey=Missing required param: card[number]., com.stripe.lib:StripeErrorTypeKey=invalid_request_error, com.stripe.lib:StripeErrorCodeKey=parameter_missing}) There was an unexpected error -- try again in a few seconds

1

There are 1 answers

1
karllekko On

It's a bit hard to say what the problem is without showing where card_number is defined and what type it is. Your code overall does work given appropriate values like

let card_number = "4242424242424242"
let card_name = "Jane Doe"
let CVC = "123"
let cardYY = "27"
let cardMM = "12"

But putting that aside, it's unusual to collect the raw card details and create Tokens like this and post them to a backend, it's a legacy integration. I'd suggest using Stripe's current integrations and plug-in payment components directly instead https://stripe.com/docs/payments/accept-a-payment?platform=ios