【问题标题】:Stripe custom payment not charging条纹自定义付款不收费
【发布时间】:2015-01-03 16:53:26
【问题描述】:

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

索引.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>';
?>

配置.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 变量中插入一个固定数字,它确实会收取费用付款。

任何帮助将不胜感激。

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    是的,您将金额分配给表单的方法不正确。您必须使用 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>
    

    【讨论】:

    • 我已经对某个点进行了指定的更改。脚本必须包含在docs 所述的表单元素中。
    • @QuintonJasonJr 抱歉,我不熟悉 Strip API。但是,我的指针仍然有效。只需在相关行中进行修改即可。
    猜你喜欢
    • 2016-07-18
    • 2018-11-17
    • 2023-02-03
    • 1970-01-01
    • 2018-10-20
    • 2018-05-06
    • 2018-04-04
    • 2015-02-26
    • 2016-08-03
    相关资源
    最近更新 更多