javascript - strip 错误 400 - 不能多次使用 strip token

标签 javascript php stripe-payments

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

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' => 'test@test.com',
      '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
    //}
?>

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

最佳答案

您确实使用了 token 两次。

首先,创建客户时。 其次,尝试对卡进行充值时。

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

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

关于javascript - strip 错误 400 - 不能多次使用 strip token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746307/

相关文章:

javascript - 错误 javascript 代码显示标签值未定义

php - 为动态输入数组插入空值

php - 为什么我不能更改我的 laravel 项目的生产模式?

ssl - 处理付款和搭载 ssl

python - 如何创建基于事件(每 x 事件)的 Stripe 订阅计划,而不是基于时间间隔(每 x 个月)的订阅计划?

javascript - 如何从 JScript 或 Testcomplete 调用 Windows API?

javascript - 带数组的循环不适用于 innerHTML

ios - 通过 IOS 应用程序中的 webview Controller 付款

javascript - 使用 WebView 的 Rtmp 视频流

php - 如何在表单中显示从数据库中取出的数据