1
votes

So i have a customer which already has a card created.
On the frontend, i give the option to use the existing card or a different one.
Following the API docs, for the new card, i create the token, send it to my backend...

In the backend:

const paymentInfo = {
  customer: customerId,
  amount: Number(total) * 100,
  currency: 'usd',
  source: existingCardId || token
}

const charge = await stripe.charges.create(paymentInfo)

If i pay with the existing card, the charge goes through, but if i send a new token, I get an error back:

Customer cus_G4V0KvxKMmln01 does not have a linked source with ID tok_1FYMLTAOg97eusNI2drudzlJ.

From the API Docs: https://stripe.com/docs/api/charges/create

source optional A payment source to be charged. This can be the ID of a card (i.e., credit or debit card), a bank account, a source, a token, or a connected account. For certain sources—namely, cards, bank accounts, and attached sources—you must also pass the ID of the associated customer.

1
I'm not sure i understand, how would i do that?André Alçada Padez

1 Answers

0
votes

I found the solution:

if (token) {
  const card = await stripe.customers.createSource(customerId, {
    source: token
  })
  paymentInfo.source = card.id
}