记录了条带呼叫,但没有进行测试收费


Stripe Calls Logged but No Test Charges Being Made

我正在MAMP堆栈上运行,试图创建一个带有自定义按钮测试费用的Stripe Checkout。信用卡验证发生了,甚至还记得我第二次。此外,我的Stripe仪表板上出现了200个POST日志,但没有收费记录。所以,也许表单代码与服务器代码不一致。。。

具有自定义"注册"按钮的index.html代码为:

<script src="bower_components/jquery/dist/jquery.min.js"></script>  <!-- In the head -->
...
...
<?php require_once('/php/config.php'); ?>
<form action="/php/charge.php" method="post">
<a href="#" id="stripeButton" class="button">Sign Up</a>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
  var handler = StripeCheckout.configure({
  key: 'pk_test_*************************',
  image: 'img/logo.png',
  token: function(token) {
    // Use the token to create the charge with a server-side script.
    // You can access the token ID with `token.id`
  }
});
document.getElementById('stripeButton').addEventListener('click', function(e) {
    // Open Checkout with further options
    handler.open({
    name: 'Awesome Subscription',
    description: 'Unlimited Awesomeness',
    amount: 2000,
    panelLabel: '{{amount}} Per Month'
});
e.preventDefault();
});
</script>
</form> 

config.php文件中的以下代码几乎取自stripe的php教程:

<?php
  require_once('../stripe/lib/Stripe.php');
  $stripe = array(
    'secret_key'      => 'sk_test_************************',
    'publishable_key' => 'pk_test_************************'
    );
  Stripe::setApiKey($stripe['secret_key']);
?>

在同一/php/文件夹中,charge.php文件包含:

<?php
  require_once(dirname(__FILE__) . '/config.php');
  $token  = $_POST['stripeToken'];
  $customer = Stripe_Customer::create(array(
      'email' => 'customer@example.com',
      'card'  => $token
  ));
  $charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => 2000,
      'currency' => 'usd'
  ));
  echo '<h1>Successfully charged $20.00!</h1>';
?>

关于我可能做错了什么,有什么想法吗?MAMP是否足以允许index.html中的代码访问.php文件并创建费用?我还仔细检查了我的秘密和可发布的测试密钥是否正确。为什么在我的设置中,签出代码不与服务器端通信?

----更新----我收到Stripe支持的电子邮件回复,建议我添加

echo('Hello world!');

到我的PHP文件,看看代码是否正在运行。我确实把它添加到了config.php和charge.php文件中,但我没有看到任何显示"Hello World!"的地方。所以看起来PHP文件没有运行。我还看了一下JavaScript控制台。。。并且没有错误。

那么,我该如何让我的PHP文件"运行"呢?

已解决。

是的,MAMP足以使HTML与PHP文件通信。如果MAMP成功"运行",PHP应该可以正常工作。PHP毕竟是MAMP的一部分。

问题是Stripe的自定义结账页面上提供的令牌函数没有任何代码来告诉它该做什么并将表单提交到PHP文件。您应该使用自己的代码来填充函数。这是一个愚蠢的错误,但我并没有以这种方式解读,并提供了评论:

token: function(token) {
    // Use the token to create the charge with a server-side script.
    // You can access the token ID with `token.id`
  }

我遇到了这个StackOverflow线程,尽管代码似乎有点旧,因为它与Stripe现在在其自定义结账页面上的内容不完全匹配,但我使用了以下代码中的令牌函数内容来填充我的令牌函数,并添加了它的提交功能。

var token = function(res){
 var $input = $('<input type=hidden name=stripeToken />').val(res.id);
 $('form').append($input).submit();
 };

所以现在我的index.html(我的注册按钮所在的位置)看起来是这样的:

<form action="/php/charge.php" method="post">
<a href="#" id="stripeButton" class="special-button">Sign Up</a>
</form>
...
...
<!-- Just before body tag close -->
<script src="bower_components/jquery/dist/jquery.min.js"></script> <!-- jQuery coming from Zurb Foundation install -->
...
<script src="https://checkout.stripe.com/checkout.js"></script>
        <script>
          var handler = StripeCheckout.configure({
            key: 'pk_test_************************',
            image: 'img/logo.png',
            token: function(token) {
              var $input = $('<input type=hidden name=stripeToken />').val(token.id);
              $('form').append($input).submit(); <!-- Code pulled from other StackOverflow thread -->
            }
          });
          document.getElementById('stripeButton').addEventListener('click', function(e) {
            // Open Checkout with further options
            handler.open({
              name: 'Product Name',
              description: 'Product Description',
              amount: 2000,
              panelLabel: '{{amount}} Per Month'
            });
            e.preventDefault();
          });
        </script>

注意*从旧代码中,res.id被传递到.val(),所以我只是用token.id替换了res.id,以在他们的评论中反映Stripe的建议。因此,实现上述代码既可以进行卡验证检查,也可以在Stripe仪表板上显示客户和费用。以及表单taking my to charge.php以显示收费确认页面。