条纹付款:获取错误,因为客户cus _ *****没有带ID ID的链接卡_ *****

时间:2015-12-22 12:30:53

标签: javascript php jquery stripe-payments payment

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

  

客户cus_7Zz2BCnybIZLGw没有带ID的链接卡   tok_17Kp8GAwLkQPB7OqrrM73VVI

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

回复回复是,

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

4 个答案:

答案 0 :(得分:77)

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

  • 仅限source参数。在这种情况下,source必须是tokensource ID(由CheckoutStripe.js创建),即以{{1}开头的字符串}或tok_

  • 仅限src_参数。在这种情况下,customer必须是customer ID,即以customer开头的字符串。将收取客户的默认付款来源。

  • 同时包含cus_customer个参数。在这种情况下,source必须是上一种情况下的客户ID,但customer应该是已附加到客户的付款来源的ID。付款来源可以是cards(ID以source开头),bank accounts(ID以card_开头)或sources(ID以ba_开头)。

在您的情况下,您在src_参数中传递了一个令牌ID以及source参数中的客户ID。

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

答案 1 :(得分:1)

这段代码帮助了我。 它也可能对您有帮助。

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

答案 2 :(得分:0)

我已设置" SourceToken"在StripeCustomerCreateOptions AND" SourceTokenOrExistingSourceId"在StripeChargeCreateOptions(SourceToken)中为我的StripeCustomer。

删除SourceTokenOrExistingSourceId的分配解决了我的StripeCharge问题,因为令牌创建了冲突的ID。

答案 3 :(得分:0)

这是我的解决流程,它可以防止重复付款并完成流程,如有必要,请随时进行改进

  1. 以条纹形式添加

     <input type="hidden" name="idempotency" id="idempotency" value="{{ genRandAlphaNumber(8)}}">
    
  2. genRandAlphaNumber是用于创建字符串以避免在条带中重复付款的功能

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

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