In testing mode when I create a new customer and tries for payment, i got this error.
Customer cus_7Zz2BCnybIZLGw does not have a linked card with ID
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 with tok_
or src_
.
with the customer
parameter only. In this case, customer
needs to be a customer ID, i.e. a string that starts with cus_
. The customer's default payment source will be charged.
with both the customer
and source
parameters. In this case, customer
needs to be a customer ID as in the previous case, but source
should be the ID of a payment source that is already attached to the customer. Payment sources can be cards (ID starts with card_
), bank accounts (ID starts with ba_
) or sources (ID starts with src_
).
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' => 'jenyy@hotmail.co.us',
'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." ,
]);
This is my solution process, and works for me to prevent double payment and finish the proccess, feel free to improve if necesary
in stripe form add
<input type="hidden" name="idempotency" id="idempotency" value="{{ genRandAlphaNumber(8)}}">
genRandAlphaNumber is function to create a string to avoid double payment in stripe
function genRandAlphaNumber($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
In Stripe Post Proccess
$user = auth()->user();
try {
$cart = new Cart();
$cart->user_id = $user->id;
$cart->save();
$description = 'description';
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$idempotency = preg_replace('/[^a-z\d]/im', '', $request->idempotency);
$stripe_token = $request->stripeToken;
//if user's stripe_token is null, create the user
if (!$user->stripe_token) {
$result = \Stripe\Customer::create(array(
'name' => $user->name .' '. $user->name,
"email" => $user->email,
"source" => $stripe_token
));
if($result && $result->id) {
$user->stripe_id = $result->id;
$user->stripe_token = $stripe_token;
$user->save();
}
}
//if user has token
if($user->stripe_token) {
// charge customer with your amount
$result = \Stripe\Charge::create(array(
"currency" => 'usd',
"customer" => $user->stripe_id,
"amount" => $cart->price * 100,
"description" => $description
),
["idempotency_key" => $idempotency,]
);
Session::flash('success', 'Payment successful!');
$cart->status = 4;
$cart->save();
return redirect()->route('cart.finish', $cart->id);
}
} catch (\Exception $ex) {
if ($ex->getStripeCode() == "idempotency_key_in_use") {
sleep(2);
//search last cart
$cart = Cart::whereUser_id($user->id)->whereStatus(4)->orderBy('id', 'DESC')->first();
if (!is_null($cart)) {
Session::flash('success', 'Payment successful!, double payment prevented');
return redirect()->route('cart.finish', $cart->id);
}
return back();
}
if ($ex->getJsonBody()['error']['type'] == "idempotency_error") {
$cart = Cart::whereUser_id($user->id)->whereStatus(4)->orderBy('id', 'DESC')->first();
if (!is_null($cart)) {
Session::flash('success', 'Payment successful!...');
return redirect()->route('cart.membership.update', $cart->id);
}
return back();
}
return $ex->getMessage();
}