Rails - Stripe :: InvalidRequestError (should provide a source or client).

I am creating an application (based on an online resource). You can register or log in using the program. Then you can buy the product. Or make your own list and sell your products.

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

Here is the order_controller.rb code 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 look at the Stripe API documentation here: Example download documentation and here: Download the full API description

I am not sure how to handle :resource or customer . I saw in other materials on the Internet that some people create a client. Other sites say :card out of date, so I'm a little confused.

I will leave the github repository of my project and feel free to take a look. I am also trying to deal with translations and recipients. Project repository

Thanks.

+5
source share
1 answer

As mentioned in the docs , stripe expects to mention the client or source when creating the charge. So you need either

  • Create a customer in the strip (if you want to also pay for this customer in the future) from the token you received and indicate that the customer when creating the charge,

     customer = Stripe::Customer.create(source: params[:stripeToken]) charge = Stripe::Charge.create({ :amount => 400, :currency => "usd", :customer => customer.id, :description => "Charge for test@example.com " }) 
  • Or, if you do not want to create a client, then directly mention the received token as a source,

     Stripe::Charge.create({ :amount => 400, :currency => "usd", :source => params[:stripeToken], :description => "Charge for test@example.com " }) 
+8
source

Source: https://habr.com/ru/post/1233095/


All Articles