Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customer does not have linked source with id

I keep getting the following error when viewing stripe test trasactions:

{
  "error": {
    "type": "invalid_request_error",
    "message": "Customer cus_9tW8Cf0Xvm9lRv does not have a linked source with ID tok_19Zj5rAANnhOmz4ex3ri2jtW.",
    "param": "source",
    "code": "missing"
  }
}

my swift code to call the api is as follows:

    @IBAction func payButtonWasPressed() {
        stripeCard = STPCardParams()

        let expirationDate = self.cardExpiryTextField.text!.components(separatedBy: "/")
        let expMonth = UInt(expirationDate[0])
        let expYear = UInt(expirationDate[1])

        stripeCard.number = self.cardNumberTextField.text
        stripeCard.cvc = self.cardCVVTextField.text
        stripeCard.expMonth = expMonth!
        stripeCard.expYear = expYear!

        STPAPIClient.shared().createToken(withCard: stripeCard, completion: { (token, error) -> Void in

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

            self.chargeUsingToken(token!)
        })
    }

    func handleError(_ error: NSError) {
        UIAlertView(title: "Please Try Again",
                    message: error.localizedDescription,
                    delegate: nil,
                    cancelButtonTitle: "OK").show()

    }

    func chargeUsingToken(_ token: STPToken) {

        let URL = "https://splitterstripeserver.herokuapp.com/charge"
        let params = ["source": token.tokenId,
                      "stripe_token": token.tokenId,
                      "amount": total] as [String : Any]

        let manager = AFHTTPSessionManager()
        manager.post(URL, parameters: params, success: { (operation, responseObject) -> Void in

            if let response = responseObject as? [String: String] {
                UIAlertView(title: response["status"],
                    message: response["message"],
                    delegate: nil,
                    cancelButtonTitle: "OK").show()
            }

        }) { (operation, error) -> Void in
            self.handleError(error as NSError)
        }
    }

and I am using the example backend found on the stripe github

On my app I get a pop-up saying Request failed: payment required (402). I have tried a lot of different things but can't seem to get successful response. I cant see what im doing wrong and need a fresh pair of eyes. Any help would be great. Thanks

like image 619
Wazza Avatar asked Sep 17 '25 03:09

Wazza


1 Answers

Once you create the card token with Stripe Checkout or Stripe.js, you send it to your server where you use it to create a customer via the API. That card becomes "saved" with that customer and you can then charge it in the future. If you want to add another card to the same customer you would use the Create Card API to pass the new token and add that card.

When you create a charge, you pass the customer id (cus_XXX) in the customer parameter to charge the default card. If you want to charge a specific card, you would pass the card id (card_YYY) in the source parameter along with the customer.

At the moment, you are trying to pass a customer id and a card token which is not supported. You'll need to save that card on the customer first and then pass the new card id in the source parameter as mentioned above.

like image 56
koopajah Avatar answered Sep 19 '25 18:09

koopajah