首页 文章

使用PHP / JS提交的Stripe Checkout但不会显示在Stripe帐户中

提问于
浏览
0

我正在尝试使用自定义付款金额进行Stripe结帐 . 弹出的条纹表单将提交,但我的Stripe帐户中没有任何内容 .

在我的实际代码中,我确实输入了api密钥 . 我也没有使用作曲家 .

<form action="charge.php" method="POST" id="payment-form">
  <input style="width: 100px; float: right;" class="form-control" type="number" id="custom-donation-amount" placeholder="$50.00" min="0" step="5.00"/>
  <input class="donate-desc" style="width: 100%; float: right;" class="form-control" type="text" placeholder="What is this donation for?"/>
  <button style="float: left; margin-right: 10px;" id="customButton" class="simpay-payment-btn stripe-button-el"><span>Make a Donation</span></button>
  <script src="https://checkout.stripe.com/checkout.js"></script>
  <script>
    var handler = StripeCheckout.configure({
      key: 'test publishable key',
      image: 'image.png',
      token: function(token) {
        var stripeToken = token.id;
      }
    });

    document.getElementById('customButton').addEventListener('click', function(e) {
      // This line is the only real modification...
      var amount = jQuery("#custom-donation-amount").val() * 100;
      var desc = jQuery('.donate-desc').val();
      handler.open({
        name: 'name',
        description: desc,
        amount: Math.round(amount)
      });
      e.preventDefault();
    });
  </script>

这是charge.php

<?php
    require '/stripe/Stripe.php';

    $stripe = array(
        "secret_key"      => "test secret key",
        "publishable_key" => "test publishable key"
    );

    \Stripe\Stripe::setApiKey($stripe['secret_key']);

    $token  = $_POST['stripeToken'];

    $charge = \Stripe\Charge::create(array(
        'amount'   => $_POST['amount'],
        'descrition' => $_POST['description'],
        'currency' => 'usd',
        'source' => $token
    ));

?>

1 回答

  • 0

    在您的代码中,您只需将变量 stripeToken 设置为 token.Id . 您仍然需要将该变量传递给后端进行处理 .

    换句话说,在获得 token.id 值之后,您应该向后端的某个 endpoints 发出新请求(AJAX或其他)以进行处理 . 围绕脚本构建表单的方式不起作用 .

    你需要更像这样的东西:https://jsfiddle.net/osrLsc8m/

相关问题