2017-08-31 33 views
1

我实现了一个条纹付款,但问题是,我不断收到此错误:条纹API错误 - 缺少必要的PARAM:量 - PHP库

{status: 500, error: "Missing required param: amount."}

如可以在代码中可以看出,我有所有的参数,但我仍然不知道为什么会出现这个错误。这里是我使用的代码:

public function process(){ 
    try { 
     Stripe::setApiKey('sk_text_key'); 
     $charge = Stripe_Charge::create(array(
        "amount" => 2000, 
        "currency" => "USD", 
        "card" => $this->input->post('access_token'), 
        "description" => "Stripe Payment" 
     )); 
    } catch (Stripe_InvalidRequestError $e) { 
     // Invalid parameters were supplied to Stripe's API 
     echo json_encode(array('status' => 500, 'error' => $e->getMessage())); 
     exit(); 
    } 
} 

这是我使用提交表单的JavaScript:

$(function() { 
    var $form = $('#payment-form'); 
    $form.submit(function(event) { 
     $form.find('.submit').prop('disabled', true); 
     $form.find('.submit').val('Please wait...'); 

     Stripe.card.createToken($form, stripeResponseHandler); 
     return false; 
    }); 
}); 

function stripeResponseHandler(status, response) 
{ 
    if (response.error) { 
     alert(response.error.message); 
    } 
    else { 
     $.ajax({ 
      url: '<?php echo base_url('payment/process');?>', 
      data: {access_token: response.id}, 
      type: 'POST', 
      dataType: 'JSON', 
      success: function(response){ 
       console.log(response); 
       if(response.success) 
       window.location.href="<?php echo base_url('booking/thankyou'); ?>"; 
      }, 
      error: function(error){ 
       console.log(error); 
      } 
     }); 
    } 
} 
+0

您使用的是什么版本的Stripe? – Hatef

+0

我可以看到 - 你试图将标记设置为CARD ....根据我的想法,它是“源”。还有......尝试检查使用的条纹版本。像例如从我的侧:$电荷= \条纹\充电::创建(阵列( \t \t \t \t \t “量”=> 20000,//量美分 \t \t \t \t \t “货币”=>“USD ”, \t \t \t \t \t “源”=> $令牌, \t \t \t \t \t “说明”> '附加付款' \t \t \t \t \t)); – Vyacheslav

回答

4

我看你使用的是传统的代码,在条纹的新版本你可以使Charge这样的:

// Token is created using Stripe.js or Checkout! 
// Get the payment token ID submitted by the form: 
$token = $_POST['stripeToken']; 

$charge = \Stripe\Charge::create(array(
    "amount" => 2000, 
    "currency" => "usd", 
    "description" => "Stripe Payment", 
    "source" => $token, 
)); 

查看更多例子here

+0

Class'Stripe \ Charge'not found –

+0

这就是我在我的控制器付款require_once(APPPATH。'libraries/Stripe/lib/Stripe.php')上调用它的方法。 –

+1

为什么不使用命名空间?无论如何,尝试包括这一个:'require_once(APPPATH。'库/ Stripe/lib/Charge.php');' – Hatef