2014-12-13 70 views
3

我正在嘗試使用條紋在我的應用程序中使用Laravel製作小型付款表單。我甚至跟着Laracast的教程。我得到這個錯誤使用Laravel條形支付錯誤(您必須提供卡或客戶ID)

Stripe_InvalidRequestError 
You must supply either a card or a customer id 

//billing.js 
(function(){ 

    var StripeBilling = { 

     init: function(){ 
      this.form=$('#billing-form'); 
      this.submitButton = this.form.find('input[type=submit]'); 
      this.submitButtonValue = this.submitButton.val(); 

      var stripeKey=$('meta[name="publishable-key"]').attr('content'); 

      Stripe.setPublishableKey(stripeKey); 

      this.bindEvents(); 
     }, 

     bindEvents: function(){ 
      this.form.on('submit', $.proxy(this.sendToken, this)); 
     }, 

     sendToken: function(event){ 
      this.submitButton.val('One Moment').prop('disabled', true); 
      Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this)); 

      event.preventDefault(); 

     }, 

     stripeResponseHandler: function(status, response){ 
      if(response.error){ 
       this.form.find('.payment-errors').show().text(response.error.message); 
       return this.submitButton.prop('disabled', false).val(this.submitButtonValue); 
      } 


      $('<div>', { 
       type: 'hidden', 
       name: 'stripe-token', 
       value: response.id 
      }).appendTo(this.form); 

      this.form[0].submit(); 
     } 
    }; 

    StripeBilling.init(); 

})(); 

//StripeBilling.php 

<?php 
namespace Acme\Billing; 

use Stripe; 
use Stripe_Charge; 
use Config; 

class StripeBilling implements BillingInterface { 
    public function __construct() 
    { 
     Stripe::setApiKey(Config::get('stripe.secrete_key')); 
    } 
    public function charge(array $data) 
    { 
     try 
     { 
      return Stripe_Charge::create([ 
       'amount' => 1000, // $10 
       'currency' => 'usd', 
       'description' => $data['email'], 
       'card'=>$data['token'] 
      ]); 
     } 
     catch(Stripe_CardError $e) 
     { 
      dd('Card was declined'); 
     } 
    } 
} 

可能是什麼問題?我甚至從github採取了相同的代碼,但同樣的錯誤。一切都和Laracast相同。任何想法?

回答

1

編輯2:你已經使用在Stripe::setApiKey(Config::get('stripe.secrete_key'))secrete_key - 它應該是secret_key

編輯1:你billing.js和laracasts'之間的唯一區別是在Stripe.createToken結束兩個右括號之間的空間:

Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this)); 

假設這並不能解決問題,有您是否在處理費用之前嘗試創建了條紋客戶?我有一個類似的系統(來自同一Laracast),它首先創建一個客戶:

public function createStripeCustomer($email, $token) 
{ 
    $key = Config::get('stripe.secret'); 

    Stripe::setApiKey($key); 

    $customer = Stripe::customers()->create([ 
     'card' => $token, 
     'email' => $email, 
     'description' => 'desc' 
    ]); 
    // error checking 

    return $customer['id']; 

你想被返回客戶ID,您那麼Stripe_Charge數組中使用:

return Stripe_Charge::create(
     [ 
      'amount' => 1000, // $10 
      'currency' => 'usd', 
      'customer' => $customer['id'], 
      'description' => $data['email'], 
      'card'=>$data['token'] 
     ]); 
相關問題