Class: Spree::ShippingLabels::Purchase

Inherits:
Workflow
  • Object
show all
Defined in:
app/workflows/spree/shipping_labels/purchase.rb

Overview

Buys a carrier label for a parcel through the owner's fulfillment provider and records it as a Spree::ShippingLabel with the delivery it mints (docs/plans/6.0-shipping-labels-and-deliveries.md).

Idempotency lives here, not in the provider: an owner already holding an active label is refused, so a provider is never asked twice and needs no bookkeeping of its own. The file is fetched into private storage right after purchase; a fetch that fails is retried in the background and the provider's URL is kept until then.

In the workflow tier for the carrier I/O and its hooks: a host app that wants to veto a purchase (a cut-off window, a carrier allowlist) or act on a bought label (print queue, cost ledger) hangs it here.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Workflow

#call, define_argument_readers, #errors, #halt!, hooks, inherited, model_name, #read_attribute_for_validation, #reject!, workflow_key

Methods included from Spree::ServiceModule::Base

#call, prepended

Methods included from Workflow::ControlFlow

#failure

Instance Attribute Details

#shipping_label ⇒ Object (readonly)

The label that was bought, readable by hook handlers.



20
21
22
# File 'app/workflows/spree/shipping_labels/purchase.rb', line 20

def shipping_label
  @shipping_label
end

Instance Method Details

#perform(owner:) ⇒ Spree::ServiceModule::Result

Returns the shipping label on success.

Parameters:

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/workflows/spree/shipping_labels/purchase.rb', line 24

def perform(owner:)
  super

  run_hooks :validate

  step :ensure_purchasable
  step :claim_purchase, on_flow_failure: :release_claim

  # Carrier I/O — never inside a transaction.
  external_step :purchase

  ApplicationRecord.transaction do
    step :record_label
    step :record_delivery
  end

  step :store_file

  shipping_label.publish_event('shipping_label.purchased')
  run_hooks :after_purchase
  success(shipping_label.reload)
end