ruby 55 lines · 9 steps

How a Rails checkout service object works

A plain Ruby service object wraps checkout in a transaction and returns a structured result instead of raising.

Explained by highlit
1class CheckoutService
2 Result = Struct.new(:success?, :order, :error, keyword_init: true)
3 
4 def self.call(...)
5 new(...).call
6 end
7 
8 def initialize(cart:, payment_token:, current_user:)
9 @cart = cart
10 @payment_token = payment_token
11 @current_user = current_user
12 end
13 
14 def call
15 return failure("Your cart is empty") if @cart.empty?
16 
17 order = nil
18 ActiveRecord::Base.transaction do
19 order = build_order
20 order.save!
21 reserve_inventory!(order)
22 charge = PaymentGateway.charge(token: @payment_token, amount_cents: order.total_cents)
23 order.update!(payment_reference: charge.id, status: :paid)
24 end
25 
26 OrderMailer.confirmation(order).deliver_later
27 @cart.clear!
28 Result.new(success?: true, order: order)
29 rescue PaymentGateway::Error => e
30 failure("Payment failed: #{e.message}")
31 rescue InventoryReservation::OutOfStock => e
32 failure("#{e.item_name} is out of stock")
33 end
34 
35 private
36 
37 def build_order
38 @current_user.orders.build(
39 total_cents: @cart.total_cents,
40 currency: @cart.currency,
41 status: :pending,
42 line_items_attributes: @cart.to_line_item_attributes
43 )
44 end
45 
46 def reserve_inventory!(order)
47 order.line_items.each do |item|
48 InventoryReservation.reserve!(item.variant, quantity: item.quantity)
49 end
50 end
51 
52 def failure(message)
53 Result.new(success?: false, error: message)
54 end
55end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Service objects keep multi-step business logic out of controllers and models in one testable place.
  2. 2Wrapping side effects in a transaction guarantees the order, inventory, and payment commit together or not at all.
  3. 3Returning a Result struct lets callers branch on success without rescuing exceptions themselves.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How a Rails checkout service object works — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code