2015-10-06 66 views
5

我正在構建一個應用程序(基於在線資源)。您可以註冊或使用設計登錄。然後,你可以買一個產品。或者製作自己的清單並出售你的產品。Rails - Stripe :: InvalidRequestError(必須提供源代碼或客戶。)

我正在整合Stripe。當我創建Charge時,在控制檯中出現此錯誤:Stripe::InvalidRequestError (Must provide source or customer.)

這裏是orders_controller.rb爲充電動作的代碼:

Stripe.api_key = ENV["STRIPE_API_KEY"] 
token = params[:stripeToken] 

begin 
    charge = Stripe::Charge.create(
    :amount => (@listing.price * 100).floor, 
    :currency => "usd", 
    :card => token 
    ) 
    flash[:notice] = "Thanks for ordering!" 
rescue Stripe::CardError => e 
    flash[:danger] = e.message 
end 

當然,我走在條紋API文檔這裏看看:Charge documentation example這裏:Charge full API Reference

我不知道如何處理:resourcecustomer。我在網絡上的其他材料中看到有些人創建了一個客戶。在其他網站上說:card已被棄用,所以我有點困惑。

我將離開我的項目的github存儲庫,並隨時查看。我正試圖處理轉賬和收件人。 Project Repository

謝謝。

回答

8

正如docs中提到的那樣,stripe希望在創建費用時提及客戶或消息來源。所以,你要麼需要

  • 上條紋創建一個客戶(如果你想在未來的客戶收取過)所收到的令牌,並註明客戶在創建一充,

    customer = Stripe::Customer.create(source: params[:stripeToken]) 
    
    charge = Stripe::Charge.create({ 
        :amount => 400, 
        :currency => "usd", 
        :customer => customer.id, 
        :description => "Charge for [email protected]" 
    }) 
    
  • 或者,如果你不希望創建一個客戶,然後直接提到收到標記爲一源,

    Stripe::Charge.create({ 
        :amount => 400, 
        :currency => "usd", 
        :source => params[:stripeToken], 
        :description => "Charge for [email protected]" 
    }) 
    
+0

謝謝。你很棒。但現在我只是有另一個錯誤:'stripe :: invalidrequesterror(不能轉移到沒有默認付款帳戶的收件人。) 我看看文檔。我的應用程序的過程是這樣的: 用戶唱歌。然後,你創建一個新產品,當你創建這個產品時,你添加你的銀行賬戶信息。 然後,我們添加您的帳戶,您可以購買。 –

+0

通過查看錯誤,似乎您正在將資金轉移給某個收件人。您正在使用什麼賬號進行轉賬? 您是否在Stripe文檔中檢查了[testing](https://stripe.com/docs/testing)部分,其中包含測試帳號? –

相關問題