【问题标题】:Handling Variable Amount (Donation) With Stripe & PHP使用 Stripe 和 PHP 处理可变金额(捐赠)
【发布时间】:2017-07-30 20:51:59
【问题描述】:

我需要允许用户使用 Stripe 输入自定义金额。他们在输入中输入他们想要支付的费用。然后我有一个脚本来收集默认条纹结帐按钮下方的金额。但是我应该如何处理charge.php 上的收费服务器端?

donation.php

    //user enter custom amount
    <input type="number" class='form-control' 
    id="custom-donation-amount" 
    placeholder="$50.00" min="0" step="10.00 " />

    //default stripe checkout button
      <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_XXXXXXXXXXXXXXX"
        data-amount="0"
        data-name="ORGANIZATION"
        data-description="Saving Penguins"
        data-image="logo.png"
        data-locale="auto"
        data-zip-code="true"
        data-billing-address="true"
        data-label="DONATE"
        data-currency="usd">
      </script>

//script to collect variable amount
  <script type="text/javascript">
    $(function() {
        $('.donate-button').click(function(event) {
            var amount = $('#custom-donation-amount').val();        
            $('.stripe-button').attr('data-amount', amount)
        });
    });
</script>

charge.php

<?php require_once('./config.php');

  $token  = $_POST['stripeToken'];
  $email  = $_POST['stripeEmail'];

  $customer = \Stripe\Customer::create(array(
      'source'  => $token,
      'email' => $email)
  );

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'usd'
  ));

   header("Location: confirmation.php");
die();


?>

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    在您放置 Checkout 的 &lt;form&gt; 中,您还希望添加一个输入以从用户那里收集金额。请注意我输入的名称为amount

    <form method="POST" action="/charge.php">
    <input type="number" class="form-control" name="amount" id="custom-donation-amount" placeholder="50.00" min="0" step="10.00" />
    <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_XXXXXXXXXXXXXXX"
    data-name="ORGANIZATION"
    data-description="Saving Penguins"
    data-image="logo.png"
    data-locale="auto"
    data-billing-address="true"
    data-label="DONATE"
    data-currency="usd">
    </script>
    </form>
    

    在您的后端,您可以在 Checkout 提交时获取此 amount 字段,就像您获取 Stripe 创建的令牌一样

    $token  = $_POST['stripeToken'];
    $email  = $_POST['stripeEmail'];
    $amount = $_POST['amount'];
    
    // your code to create a charge
    // ...
    

    如果您希望动态更改结帐窗口中显示的金额,则应使用自定义结帐,而不是基本结帐块,否则可能无法正确更新。

    https://stripe.com/docs/checkout#integration-custom

    或者,对于小提琴: https://jsfiddle.net/ywain/g2ufa8xr/

    【讨论】:

    • 是否可以设置一个条形表单,只收集用户输入的自定义金额而不尝试在后端收集任何内容?我只想要在 jsfiddle 中看到的 jquery 功能,但我只想将它传递给 stripe,就是这样(现在直到我更精通后端的东西)......
    • @DeltaFlyer 你总是需要一个后端!但这不必太多。您可以在任何网络主机上使用 Heroku、AWS Lambda 之类的非常小的实例。
    猜你喜欢
    • 2021-05-29
    • 2021-10-25
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2013-07-20
    • 2021-07-19
    相关资源
    最近更新 更多