dtpoius74857 2014-11-07 00:36
浏览 43
已采纳

条纹定制付款不收费

I'm trying to implement the method for using a custom payment in Stripe provided in the following question, stripe checkout custom button not charging. The user inputs a value for the amount which is displayed on the submit button.

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>

The submit button displays the correct amount, but when clicking the submit button a white screen with no charge occurring.

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>';
?>

I'm thinking my implementation of the input at the bottom of my form is a bit shakey, considering that if I insert a fixed number into my $amount variable in the charge.php, it DOES actually charge that payment.

Any assistance would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongxingdu9594 2014-11-07 01:18
    关注

    Yes, your method of assigning the amount to the form is incorrect.you have to do that using JavaScript

    Note that anyone can change the amount. so you might wanna check the actual amount before processing the payment.

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。