0

我正在尝试使用自定义付款金额进行 Stripe 结帐。弹出的 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
    ));

?>
4

1 回答 1

0

在您的代码中,您只是将一个变量设置stripeTokentoken.Id. 您仍然需要将该变量传递给您的后端进行处理。

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

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

于 2017-10-17T22:05:39.147 回答