I am creating a simple web page where the Stripe Checkout will prompt the user for their card info, generate a token, and send it to my backend Go webserver that processes the payment for that token. The issue I'm having is that when I get the token, I try and create a customer and set the source to the token created by Stripe, but I get this response everytime:
charge failed: {"type":"invalid_request_error","message":"Customer cus_######### does not have a linked source with ID tok_#########.","code":"missing","param":"source","request_id":"req_######","status":404}
Creating the customer and associating the card with that customer works just fine. But, charging the card based on the token Stripe generated fails. Even though in the logs for that customer in my Stripe Dashboard it shows the attempted charge. So, it's trying to associate the charge with the correct customer, but failing because the token was generated without a specific customer in mind? I'm not really sure what is going on.
The code I have on the backend that accepts the token and processes everything is below:
stripe.Key = "sk_test_################"
customerParams := &stripe.CustomerParams{
Desc: "Customer for xyz.com",
Email: "[email protected]",
}
err := customerParams.SetSource("tok_####################")
if err != nil {
return nil, err
}
cus, err := customer.New(customerParams)
if err != nil {
return nil, err
}
// Charge the user's card
cp := &stripe.ChargeParams{
Amount: 100,
Currency: "usd",
Desc: "some description",
Customer: cus.ID,
}
err = cp.SetSource("tok_####################")
if err != nil {
return nil, err
}
err = charge.New(cp)
Also, if I remove the customer portion of this example, the charge goes through just fine with the above code. It just doesn't associate the charge with a customer obviously. However, as soon as I try to set the customer on the charge it fails.