Back to Practice

Practice · Payments · Card 1

What's the right idempotency key for this job?

A background job creates a payment intent. The job can retry. The wrong key turns one charge into two.

The code

A Sidekiq job that creates a Stripe PaymentIntent. The job retries on transient errors.

class ChargeOrderJob < ApplicationJob
  retry_on Stripe::APIConnectionError, wait: :exponentially_longer

  def perform(order_id)
    order = Order.find(order_id)

    intent = Stripe::PaymentIntent.create(
      { amount: order.total_cents, currency: "usd" },
      { idempotency_key: KEY_HERE }
    )

    order.create_payment!(intent_id: intent.id)
  end
end

The question

Which value of KEY_HERE prevents the charge from happening twice when the job retries?

Take a moment. Pick the best answer. Wrong picks reveal why they're wrong, which is half the point.