2017-08-31 32 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