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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP