Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Stripe::InvalidRequestError (Must provide source or customer.)

I'm building an application (based in online resource). You can sign up or login with devise. Then, you can buy a product. Or make your own list and sell your products.

I'm integrating Stripe. When I create the Charge, I get this error in the console: Stripe::InvalidRequestError (Must provide source or customer.).

Here is the code of the orders_controller.rb for the charge action:

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

Of course I'm taking a look in the Stripe API Documentation here: Charge documentation example and here: Charge full API Reference

I'm not sure how to handle the :resource or customer. I saw in other materials in the web that some people create a customer. In other sites it says that the :card is deprecated, so I'm a little confused.

I will leave the github repository of my project, and feel free to take a look. I'm trying to deal with Transfers and Recipients too. Project Repository

Thanks.

like image 656
Carlos Orellana Avatar asked Oct 06 '15 21:10

Carlos Orellana


1 Answers

As mentioned in the docs, stripe expects either customer or source to be mentioned while creating a charge. So, you either need to

  • Create a customer on stripe(if you want to charge that customer in future too) from the token you received, and mention that customer while creating a charge,

    customer = Stripe::Customer.create(source: params[:stripeToken])
    
    charge = Stripe::Charge.create({
      :amount => 400,
      :currency => "usd",
      :customer => customer.id,
      :description => "Charge for [email protected]"
    })
    
  • Or, if you don't want to create a customer, then directly mention received token as a source,

    Stripe::Charge.create({
      :amount => 400,
      :currency => "usd",
      :source => params[:stripeToken],
      :description => "Charge for [email protected]"
    })
    
like image 85
Yogesh Khater Avatar answered Nov 03 '22 17:11

Yogesh Khater