Javascript 条带错误 400 - 不能多次使用条带令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30746307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:41:29  来源:igfitidea点击:

Stripe Error 400 - Cannot use stripe token more than once

javascriptphpstripe-payments

提问by alaboudi

I keep receiving error code 400 on my stripe dashboard. It seems like im using the same stripe token more than once and this produces an error. Below is my code.

我的条纹仪表板上不断收到错误代码 400。似乎我不止一次使用相同的条带令牌,这会产生错误。下面是我的代码。

Js:

JS:

    <script src="https://checkout.stripe.com/checkout.js"></script>
    <script>

    var handler = StripeCheckout.configure({
        key: 'pk_test_******************',
        image: '/img/documentation/checkout/marketplace.png',
        token: function(token) {
          /*$.post("php/charge.php",{stripeToken:token.id},function(data,status){
              console.log("Data: "+ data+"\nStatus: "+status);
          });*/
          alert(token.used);//alerts false
          $.post("php/charge.php",{stripeToken:token.id});
          alert(token.used);// still alerts false
        }
      });

      $('#myButton').on('click', function(e) {
        // Open Checkout with further options
        handler.open({
          name: 'Demo Site',
          description: '2 widgets',
          currency: "cad",
          amount: 2000
        });
        e.preventDefault();
      });

      // Close Checkout on page navigation
      $(window).on('popstate', function() {
        handler.close();
      });
    </script>

Php:

博士:

<?php
  require_once('config.php');

  $token  = $_POST['stripeToken'];

  $customer = \Stripe\Customer::create(array(
      'email' => '[email protected]',
      'card'  => $token
  ));

  //try {
    $charge = \Stripe\Charge::create(array(
      "amount" => 1000, // amount in cents, again
      "currency" => "cad",
      "source" => $token,
      "description" => "Example charge")
    );
    //}catch(\Stripe\Error\Card $e) {
      // The card has been declined
    //}
?>

Can anyone tell my why I cant charge a customer? How am I using a key multiple times?

谁能告诉我为什么我不能向客户收费?我如何多次使用一个密钥?

回答by Tushar

You do use the token twice.

您确实使用了两次令牌。

First, when creating the customer. Second, when trying to charge the card.

首先,在创建客户时。第二,在尝试为卡充电时。

Instead, you can create a customer and and then pass $customer->idto Stripe when you create the charge:

相反,您可以创建一个客户,然后$customer->id在创建费用时传递给 Stripe:

$charge = \Stripe\Charge::create(array(
  "amount" => 1000, // amount in cents, again
  "currency" => "cad",
  "customer" => $customer->id,
  "description" => "Example charge")
);

回答by Ali Zain

You have to create the customer to charge him multiple times.

您必须创建客户以多次向他收费。

1) Add the Credit card token to customer and create customer

1)将信用卡令牌添加到客户并创建客户

2) Use the customer Id to charge users

2)使用客户ID向用户收费

if (isset($_POST['stripeToken'])){

        $token = $_POST['stripeToken'];

// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "description" => "Example customer")
);


// Charge the Customer instead of the card
\Stripe\Charge::create(array(
  "amount" => 1000, # amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id)
);
    }

for more help visit: https://stripe.com/docs/tutorials/charges

如需更多帮助,请访问:https: //stripe.com/docs/tutorials/charges