Stripe 支付:由于客户 cus_***** 没有带 ID tok_***** 的链接卡而出错

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

在测试模式下,当我创建新客户并尝试付款时,出现此错误。

Customer cus_7Zz2BCnybIZLGw does not have a linked card with ID tok_17Kp8GAwLkQPB7OqrrM73VVI

我使用的卡号:4242424242424242 exp_month:12 exp_year 2016

return 响应是,

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

输入费用数据是,

 $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' => test@test.com,
                'description'   => 'my payment',
                'customer'      => $customer_id
              );

有三种不同的方式create a charge:

  • 仅使用 source 参数。在这种情况下,source 需要是 token or source ID (created by Checkout or Stripe.js),即以 tok_src_.

  • 开头的字符串
  • 仅使用 customer 参数。在这种情况下,customer 需要是一个 customer ID,即以 cus_ 开头的字符串。将通过客户的默认付款方式收费。

  • 同时使用 customersource 参数。在这种情况下,customer 需要像前面的情况一样是客户 ID,但 source 应该是已经附加到客户的付款来源的 ID。付款来源可以是 cards (ID starts with card_), bank accounts (ID starts with ba_) or sources(ID 以 src_ 开头)。

在您的例子中,您在 source 参数中传递令牌 ID,在 customer 参数中传递客户 ID。

如果这是一张新卡,您应该首先使用令牌 create a card 给客户,然后使用卡 ID 创建费用。如果已经为该客户保存了卡,则无需再次收集卡信息(因此根本不需要创建令牌)。

这段代码帮助了我。 说不定对你也有帮助。

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." , 
]);

这是我的解决流程,帮我防止重复支付并完成流程,如有需要,请随时改进

  1. 以条纹形式添加

     <input type="hidden" name="idempotency" id="idempotency" value="{{ genRandAlphaNumber(8)}}">
    
  2. genRandAlphaNumber 是创建字符串以避免在 stripe 中重复支付的函数

    function genRandAlphaNumber($length) {
         $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         $randomString = '';
         for ($i = 0; $i < $length; $i++) {
             $randomString .= $characters[rand(0, strlen($characters) - 1)];
         }
         return $randomString;
    }
    
  1. 条带化Post进程

     $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();
     }