【问题标题】:Stripe Error 400 - Cannot use stripe token more than once条带错误 400 - 不能多次使用条带令牌
【发布时间】:2015-08-25 02:42:54
【问题描述】:

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

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
    //}
?>

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

【问题讨论】:

    标签: javascript php stripe-payments


    【解决方案1】:

    您确实使用了两次令牌。

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

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

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

    【讨论】:

    • 也许从那时起 stripe 改变了 API,但现在如果你想使用客户 ID,你必须提供“客户”而不是“来源”。所以数组是:array('amout' => 1000, 'currency' => 'cad', 'customer' => $customer->id, 'description' => 'example charge')
    • 如果我们想多次使用同一张卡,是否必须创建客户?
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 如果使用一次性付费,然后立即订阅一天锚,则需要客户 ID。
    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 2014-03-19
    • 2014-06-02
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2018-07-05
    • 2017-09-04
    相关资源
    最近更新 更多