Stripe'无法向没有活动卡的客户收费'

时间:2016-10-29 07:33:10

标签: javascript php jquery stripe-payments

这是直接的网站。 所以我按照教程,但我显然有些不对劲。 我在服务器上使用SSL。

如果这是他们的代码那么我哪里出错了。 正确包含了jquery,v2.js和stripe。

<!DOCTYPE html>
<html lang="en">
    <head>
      <!-- Stripe.js used; fewer compliance requirements! -->
      <!-- Include Stripe.js, either in your <head> or as a direct descendant of <body>
      at the end of the page -->
      <script type="text/javascript" src="v2.js"></script>
      <script type="text/javascript" src="development-bundle"></script>
      <script type="text/javascript">
        Stripe.setPublishableKey('pk_test_thenthetestkeyidontwanttoshare');
      </script>
      <script>
        function stripeResponseHandler(status, response) {
        // Grab the form:
          var $form = $('#payment-form');

          if (response.error) { // Problem!

            // Show the errors on the form:
            $form.find('.payment-errors').text(response.error.message);
            $form.find('.submit').prop('disabled', false); // Re-enable submission

          } else { // Token was created!

            // Get the token ID:
            var token = response.id;

            // Insert the token ID into the form so it gets submitted to the server:
            $form.append($('<input type="hidden" name="stripeToken">').val(token));

            // Submit the form:
            $form.get(0).submit();
          }
        };
        $(function() {
          var $form = $('#payment-form');
          $form.submit(function(event) {
            // Disable the submit button to prevent repeated clicks:
            $form.find('.submit').prop('disabled', true);

            // Request a token from Stripe:
            Stripe.card.createToken($form, stripeResponseHandler);

            // Prevent the form from being submitted:
            return false;
          });
        });
      </script>
    </head>
    <body>
        <form action="charge.php" method="POST" id="payment-form">
          <span class="payment-errors"></span>

          <div class="form-row">
            <label>
              <span>Card Number</span>
              <input type="text" size="16" data-stripe="number">
            </label>
          </div>

          <div class="form-row">
            <label>
              <span>Expiration (MM/YY)</span>
              <input type="text" size="2" data-stripe="exp_month">
            </label>
            <span> / </span>
            <input type="text" size="4" data-stripe="exp_year">
          </div>

          <div class="form-row">
            <label>
              <span>CVC</span>
              <input type="text" size="3" data-stripe="cvc">
            </label>
          </div>

          <div class="form-row">
            <label>
              <span>Billing Zip</span>
              <input type="text" size="5" data-stripe="address_zip">
            </label>
          </div>

          <input type="submit" class="submit" value="Submit Payment">
        </form>
    </body>
</html>


<?php
    include('stripe/init.php');
    \Stripe\Stripe::setApiKey("sk_test_otherkeyidontwanttoshare");

    // Get the credit card details submitted by the form
    $token = $_POST['stripeToken'];

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

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

它给了我这个错误:

  

致命错误:来自/home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php:114的未捕获异常'Stripe \ Error \ Card',并显示消息'无法向没有活动卡的客户收费' API请求'req_9SlHeDVEeYFup4'
  堆栈跟踪:
   #0 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php(266):Stripe \ ApiRequestor-&gt; handleApiError('{\ ​​n“error”:{\ n ...',402,数组,数组)
   #1 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php(65):Stripe \ ApiRequestor-&gt; _interpretResponse('{\ ​​n“error”:{\ n ...',402,阵列)
   #2 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiResource.php(120):Stripe \ ApiRequestor-&gt; request('post','/ v1 / charges',Array,Array)
   #3 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiResource.php(160):Stripe \ ApiResource :: _ staticRequest('post','/ v1 / charges',Array,NULL)
   #4 /home3/blueman/public_html/assistforlife.com/stripe /lib/Charge.php(73):Stripe \ ApiResource :: _ create(Array,NULL)    #5 /home3/blueman/public_html/assistforlife.com/charge.ph位于第114行的/home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php

我写了自己的代码,它说我的卡的价值是“空” 我重新写了它借用别人的代码,它发布了客户,但没有任何收费信息。

1 个答案:

答案 0 :(得分:1)

在服务器端PHP代码中,$token必须具有null值,因此创建的客户没有付款来源。然后,当您尝试使用它来创建费用时,请求将失败,并且#34;无法向没有活动卡的客户收取费用#34;。

您的表单代码看起来是正确的,因此我不确定为什么$token为空。我建议您使用浏览器的开发工具,确保Stripe.js创建的令牌ID("tok_...")确实作为stripeToken POST参数发送。

根据这条线,无关的说明:

<script type="text/javascript" src="v2.js"></script>

看起来你在本地托管Stripe.js。请注意,为了符合PCI标准,您必须使用位于Stripe自己的服务器上的版本https://js.stripe.com/v2/