Stripe Payment: Getting Error as Customer cus_***** does not have a linked card with ID tok_*****
Asked Answered
J

3

55

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 tok_17Kp8GAwLkQPB7OqrrM73VVI

Im using card number : 4242424242424242 exp_month :12 exp_year 2016

The return response is,

Array
(
    [charge_status] => 
    [error_info] => Array
        (
            [type] => invalid_request_error
            [message] => Customer cus_7Zz2BCnybIZLGw does not have a linked card with ID tok_17Kp8GAwLkQPB7OqrrM73VVI.
            [param] => card
            [code] => missing
        )

    [message] => Customer cus_7Zz2BCnybIZLGw does not have a linked card with ID tok_17Kp8GAwLkQPB7OqrrM73VVI.
)

Input Charge Data is,

 $customer = Stripe_Customer::create(array(
      'account_balance' => 100,
      'source' => $token,
      'email' => strip_tags(trim($email))
    )
  );

$customer_id = $customer->id;

$charge   = array(
                'card'          => 4242424242424242, 
                'amount'        => 100, 
                'currency'      => 'cad', 
                'receipt_email' => [email protected],
                'description'   => 'my payment',
                'customer'      => $customer_id
              );
Jobey answered 22/12, 2015 at 12:30 Comment(0)
B
112

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).

Bashaw answered 22/12, 2015 at 12:55 Comment(4)
@Bashaw what about when using an Apple Pay token? It seems like it's not possible to associate Apple Pay transactions with a customer account and have them use the Price Sheet and TouchID UI each time. Either I turn the token to a card and then it behaves just like a stored card and not like Apple Pay, or I get a new token each time and there is no charge history attached to the customer account.Melosa
@emkman: I suppose you could use Apple Pay to create a new token each time, attach the token to the existing customer object, create the charge using the customer object then delete the card from the Apple Pay token. That way you can use Apple Pay to authenticate each transaction and still keep a charge history on the customer object.Bashaw
@Ywain. Just reading through this, and while it is helpful, it unfortunately does not explain why the error happened based on the original question (maybe I missed something obvious).Enow
@IonicBurger The OP provided a customer ID in the customer parameter and a token ID in the source parameter. When providing both the customer and source parameters, source must be a card ID. If source is a token ID, then no customer parameter should be passed.Bashaw
B
8

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." , 
]);
Bandy answered 27/5, 2020 at 15:0 Comment(2)
The part I missed was \Stripe\Customer::createSource this. Now it is working. Thanks!Anglonorman
That createSource saved me. Couldn't find that anywhere elseOffspring
R
0

This is my solution process, and works for me to prevent double payment and finish the proccess, feel free to improve if necesary

  1. in stripe form add

     <input type="hidden" name="idempotency" id="idempotency" value="{{ genRandAlphaNumber(8)}}">
    
  2. 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;
    }
    
  1. 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();
     }
    
Refractor answered 11/7, 2020 at 3:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.