2014-11-07 76 views
1

我试图在以下问题stripe checkout custom button not charging中提供的条纹中使用自定义付款的方法。用户输入提交按钮上显示的金额值。条纹定制付款不充电

的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' => '[email protected]', 
     '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的有点沙基,考虑到如果我插入固定号码到我的$amount变量在charge.php中,它确实收取该付款。

任何援助将不胜感激。

回答

0

是的,你的分配量形式的方法是incorrect.you要做到这一点使用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> 
+0

我已对指定点进行了指定的更改。脚本必须包含在[docs](https://stripe.com/docs/checkout/v2)所述的表单元素中。 – 2014-11-07 02:49:33

+0

@QuintonJasonJr对不起,我不熟悉Strip API。但是,我的指针仍然有效,只需在相关行中进行修改即可。 – Himal 2014-11-07 03:23:04