首页 文章

记录条纹调用但没有测试费用

提问于
浏览
2

我正在运行MAMP堆栈试图创建一个Stripe Checkout with a custom button测试费用 . 信用卡验证发生,甚至第二次记住我 . 此外,我的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's php tutorial

<?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文件“运行”?

1 回答

  • 2

    解决了 .

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

    问题是在Stripe's custom checkout页面上提供的令牌功能并没有以这种方式读入它并提供了评论:

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

    我遇到了this StackOverflow thread虽然代码似乎有点旧,因为它没有't completely match up with what Stripe has on their custom checkout page now, I used the token function contents of the following code to fill in my token function and add it's提交功能 .

    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仪表板上发布客户和费用 . 并将表格带到我的charge.php显示充电确认页面 .

相关问题