Checking successful charge with Stripe for rails

Context:

I use Stripe checkout to accept a one-time payment in rails. I have a charge controller as shown below. At first I used striped webhook to listen to charge.succeeded, but ran into some problems due to the asynchronous nature of webhooks. I moved the business logic to the controller. If the customer order is successful, I save the customer and some other details in db.

My question is:

Is this verification sufficient to ensure a successful payment?

if charge["paid"] == true 

Stripe documentation for Stripe :: Charge.create states, "Returns a charged object if the charge succeeds. Causes an error if something goes wrong. A common source of error is an invalid or expired card or a valid card with insufficient balance available.

My ChargesController:

 class ChargesController < ApplicationController def new end def create # Amount in cents @amount = 100 temp_job_id = cookies[:temp_job_id] customer_email = TempJobPost.find_by(id: temp_job_id).company[:email] customer = Stripe::Customer.create( :email => customer_email, :card => params[:stripeToken] ) charge = Stripe::Charge.create( :customer => customer.id, :amount => @amount, :description => 'Rails Stripe customer', :currency => 'usd', :metadata => {"job_id"=> temp_job_id} ) # TODO: charge.paid or charge["paid"] if charge["paid"] == true #Save customer to the db end # need to test this and refactor this using begin-->rescue--->end rescue Stripe::CardError => e flash[:error] = e.message redirect_to charges_path end end 
+8
ruby ruby-on-rails stripe-payments webhooks payment-processing
source share
1 answer

Yes, that’s all you need to do. If the charge succeeds, Stripe will return a Charge object, and you can check its paid parameter. If the failure failed, we will send an exception.

Cheers, Larry

PS I am working on support on Stripe.

+11
source share

All Articles