Taylor Cooney

Joined

2,160 Experience
3 Lessons Completed
1 Question Solved

Activity

One thing I don't want to have to deal with are refunds because a customer happened to get charged twice. I want to ensure that I've designed my Rails app to be robust in the face of failure before I release it to production.

What is the best way to process Charge requests in the background? Currently I'm using sidekiq to invoke similar jobs (see Mailer example below). Moreover, should I be making idempotent requests? What's the best way to approach this? This way I don't have to worry about creating multiple charges, and also don't start blocking up the server with Charge requests.

Sidekiq Example
def send_job_post_email
JobMailer.send_job_post_email(user).deliver_later
end

JobsController < ApplicationController
def create
@job = current_user.jobs.build jobs_params.merge(stripe_token: stripe_params["stripeToken"])

if @job.create_with_stripe(params[:stripeToken])
  if @job.save
    @order = Order.create(
      :job_id =&gt; @job.id,
      :stripe_token =&gt; @job.stripe_token
    )
  end
  redirect_to job_order_path(@job, @order), notice: 'Registration was successfully created.'
else
  render_form
end

end


Job < ApplicationRecord
def create_with_stripe(token)
Stripe.api_key = Rails.application.secrets.stripe_secret_key

if valid?
  Stripe::Charge.create(
    :amount =&gt; 999,
    :currency =&gt; "cad",
    :source =&gt; token
  )
else
  false
end

rescue Stripe::CardError, Stripe::InvalidRequestError => e
errors.add :base, "Whoops! We were unable to process your card. #{e.message}"
false
end

Hey Chris...how do you differentiate your form, so that when a user edits a post they've already paid for, the Stripe pay button is replaced with a typical 'Submit' button. Thanks in advance
Great video Chris
Nice one, Chrisđź‘ŚMoving the payment logic to be inline with the Job makes a lot more sense. Can't wait to sign up for the video series, thanks!
It's been surprisingly difficult to find more information on creating one-off, or single charges for a user.

Currently I am making a job board where anyone can browse listings, and a user (Devise) has CRUD abilities for a job posting. I want to embed a Checkout in the Job#new form and charge a user $x.xx before the job is created. This Checkout would work with Stripe to capture credit card details, and not store the user as a repeating customer; a user should not have their credit card details stored on file and should have to input the credit card information each time they want to create a job.

Rough outline of how I'd approach this. Let me know if this seems logical:
1. adding the Stripe gem to the application’s Gemfile
2. create the actual charges in the existing Jobs Controller

 |   before_action :authenticate_user!, only: [:new]
 | 
 |   def new
 |     @job = current_user.jobs.build
 |   end
 | 

 |   def create
 |     @job = current_user.jobs.build(jobs_params)
 | 

 |     if @job.save
 |       # Pass Stripe::Charge.create
 |       redirect_to @job
 |     else
 |       # Handle rescue Stripe::CardError
 |       render 'new'
 |     end
 |   end

I'm not sure if this will have an impact on routes how this will change how I add the credit card form in Job#new in terms of getting the [:stripeToken].

= simple_form_for(@job) do |f|
 |   = f.input :title
 |   = f.input :company
 |   = f.input :location
 |   = f.input :url
 |   = f.button :submit

Hoping to get a clearer idea of how to implement one-off charges before diving into the implementation,

Cheers🍺