javascript - Stripe 'Cannot charge a customer that has no active card'

标签 javascript php jquery stripe-payments

这是直接从网站上获取的。 所以我按照教程进行操作,但显然我出错了。 我把它放在带有 SSL 的服务器上。

如果这是他们的代码那么我哪里出错了。 jquery、v2.js 和 stripe 已正确包含。<​​/p>

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

它给了我这个错误:

Fatal error: Uncaught exception 'Stripe\Error\Card' with message 'Cannot charge a customer that has no active card' in /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php:114 from API request 'req_9SlHeDVEeYFup4'
Stack trace:
#0 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php(266): Stripe\ApiRequestor->handleApiError('{\n "error": {\n...', 402, Array, Array)
#1 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php(65): Stripe\ApiRequestor->_interpretResponse('{\n "error": {\n...', 402, Array)
#2 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiResource.php(120): Stripe\ApiRequestor->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 in /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php on line 114

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

最佳答案

在服务器端 PHP 代码中,$token 必须具有 null 值,以便在没有付款来源的情况下创建客户。然后,当您尝试使用它来创建费用时,请求会失败并显示“无法向没有有效卡的客户收费”。

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

在一个不相关的注释中,根据这一行:

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

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

关于javascript - Stripe 'Cannot charge a customer that has no active card',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40317034/

相关文章:

javascript - 从 Firefox 附加内容脚本中的 main.js 接收消息

php - 如何在我在查询中有查询的情况下返回结果

javascript - Vimeo 图像和 javascript

javascript - 无法正确获取 JSON/JSONP

javascript - 单击 'Show More' 按钮时 Div 滑过 Div

javascript - 如何使用 rollup.js 将所有依赖项嵌入到一个胖目标包中?

javascript - 单击时更改链接的 href 属性

PHP implode() 用 "array"替换所有数组值

javascript - 我想使用 jquery 单击链接后打开回复文本框

javascript - 如何将 jquery 代码转换为可重用的插件/函数