0

I managed to get Stripe working and processing Jobs, where a user must pay a one-time charge in order to create a Job record.

After putting the call to Stripe::Charge.create in a background job I can't manage to figure out how to pass the charge.id from Stripe::Charge.create to an Order object.

I planned to move the Order.create call into the sidekiq worker and access the charge.id directly, but I can't access the @job within the worker because a stripeToken can't be used more than once. Any idea on how I can still save the charge.id to an Order? (separate from the main Job model)

JobsController

def create
    ...

    if @job.create_with_stripe(params[:stripeToken])
      if @job.save
        Order.create(
          # Can't figure out how to pass the charge.id from StripePaymentJob
          :charge_id    => @charge.id,
          :job_id       => @job.id
        )
      end
      ...
  end

Job Model

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

    if valid?
      StripePaymentJob.perform_later(token, SecureRandom.uuid)
    else
    ...
  end

Stripe Worker

class StripePaymentJob < ApplicationJob
  queue_as :default

  def perform(token, idempotent_key)
    @charge = Stripe::Charge.create({
      ...
    }, { idempotency_key: idempotent_key })
  end
end
3
  • Removed some of the code for security reasons May 27, 2018 at 21:40
  • Can you save the job before you call create_with_stripe? Sidekiq pro I think supports callbacks, but without paying for that I think you will need to re-order how some of these things are being saved. If you can save the job first you can pass the job id into the worker and then create the order in the worker.
    – Jon
    May 30, 2018 at 6:22
  • @Jon Ah right. My problem is that I want to save the job only after Stripe has been called. Actually, I guess that a user expects to be charged at some point. No reason why I can't save the job and then process the charge. Do you have any other ideas on how I can store the charge_id? I feel it's pertinent to have to retrieve past charges. May 30, 2018 at 15:30

1 Answer 1

3
+50

I would consider this approach:

  • Before requesting the job be performed, create the order record with a nil charge_id
  • After the Stripe transaction has been completed in the job, update the order with the returned charge_id

To do this, you can rely on globalid, which allows your job to access the order directly. See http://guides.rubyonrails.org/active_job_basics.html#globalid for info.

Update your job implementation to now update the object after the job has completed successfully:

class StripePaymentJob < ApplicationJob  
  queue_as :default

  def perform(token, idempotent_key, order)
    @charge = Stripe::Charge.create({
      ...
    }, { idempotency_key: idempotent_key })
    order.update(charge_id: @charge.id, job_id: self.jid)
  end
end

Obviously you will need more logging and error handling, since this is happening in the background and you'll not know when and what fails.

Your job model will now call the perform_later as

order = Order.create
StripePaymentJob.perform_later(order, token, SecureRandom.uuid)

Let me know if this works, or if I need to update this answer.

2
  • Thanks for the response @Phil....I'll check this out in the evening and write back. May 31, 2018 at 17:06
  • I prefer passing the ID rather than the entire object where ActiveJob Globalid library does the work behind the scenes to serialize/deserialize the ActiveRecord instance. ActiveJob had an Error while trying to deserialize arguments because I have a 'UrlValidator' on the object that was being passed. Jun 2, 2018 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.