条纹定制付款不收费

时间:2014-11-07 00:36:36

标签: php stripe-payments

我尝试在以下问题stripe checkout custom button not charging中提供的Stripe中实施使用自定义付款的方法。用户输入提交按钮上显示的金额值。

的index.php

<form action="charge.php" method="post">
  <input class="form-control" type="number" id="donation-amount" placeholder="20.00" min="0" step="5.00"/>
  <script src="https://checkout.stripe.com/v2/checkout.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
  <button id="customButton">Donate</button>
    <script>
      $('#customButton').click(function(){
        var token = function(res){
          var $input = $('<input type=hidden name=stripeToken />').val(res.id);
          $('form').append($input).submit();
        };
        var amount = $("#donation-amount").val() * 100;
        StripeCheckout.open({
          key:         'pk_test_*************************',
          address:     false,
          amount:      amount,
          currency:    'usd',
          name:        'Test Customer',
          description: 'Demo Description',
          panelLabel:  'Checkout',
          token:       token
        });
        return false;
      });
    </script>
    <input type="hidden" name="chargeamount" val=<?php amount ?>/>
</form>

提交按钮显示正确的金额,但点击提交按钮时会出现白屏并且不收费。

Charge.php

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

  $token  = $_POST['stripeToken'];
  $amount = $_POST['chargeamount'];

  $customer = Stripe_Customer::create(array(
      'email' => 'test@example.com',
      'card'  => $token
  ));

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

  echo '<h1>Successfully charged '. $amount. '!</h1>';
?>

的config.php

<?php
require_once('vendor/stripe/lib/Stripe.php');

$stripe = array(
  "secret_key"      => "sk_test_************************",
  "publishable_key" => "pk_test_************************"
);

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

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

  echo '<h1>Successfully charged '. $amount .'!</h1>';
?>

我认为我在表单底部input的实现有点不确定,考虑到我在charge.php中的$amount变量中插入固定数字,它实际上收取了这笔款项。

非常感谢任何协助。

1 个答案:

答案 0 :(得分:0)

是的,您为表单分配金额的方法不正确。您必须使用JavaScript执行此操作

请注意,任何人都可以更改金额。所以你可能想在处理付款之前检查实际金额。

<form action="charge.php" method="post">

  <!-- .... -->

  <input class="form-control" type="number" id="donation-amount" placeholder="20.00" min="0" step="5.00"/>

  <button id="customButton">Donate</button>

  <!-- give an id to charge amount field  -->
  <input type="hidden" id="amount" name="chargeamount" val=""/>

  <!-- .... -->

<script>

    //....

    var amount = $("#donation-amount").val() * 100;

    //Assign the amount to the amount field.
    $('input#amount').val(amount);

    //....

</script>

</form>