Stripe Payment: Getting Error as Customer cus_***** does not have a linked card with ID tok_*****
There are three different ways to create a charge:
with the
source
parameter only. In this case,source
needs to be a token or source ID (created by Checkout or Stripe.js), i.e. a string that starts withtok_
orsrc_
.with the
customer
parameter only. In this case,customer
needs to be a customer ID, i.e. a string that starts withcus_
. The customer's default payment source will be charged.with both the
customer
andsource
parameters. In this case,customer
needs to be a customer ID as in the previous case, butsource
should be the ID of a payment source that is already attached to the customer. Payment sources can be cards (ID starts withcard_
), bank accounts (ID starts withba_
) or sources (ID starts withsrc_
).
In your case, you're passing a token ID in the source
parameter along with a customer ID in the customer
parameter.
If this is a new card, you should first use the token to create a card on the customer, then create the charge with the card ID. If the card was already saved for this customer, then you don't need to collect the card information again (and thus don't need to create a token at all).
This code helped me. It might also help you.
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$customer = \Stripe\Customer::create([
'name' => 'Jenny Rosen',
'email' => '[email protected]',
'address' => [
'line1' => '510 Townsend St',
'postal_code' => '98140',
'city' => 'San Francisco',
'state' => 'CA',
'country' => 'US',
],
]);
\Stripe\Customer::createSource(
$customer->id,
['source' => $request->stripeToken]
);
Stripe\Charge::create ([
"customer" => $customer->id,
"amount" => 100 * 100,
"currency" => "usd",
"description" => "Test payment from stripe.test." ,
]);