Class: SolidusSubscriptions::Checkout

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_subscriptions/checkout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(installments) ⇒ Checkout

Get a new instance of a Checkout

to be used when generating a new order

Parameters:

  • installments (Array<Installment>)

    The collection of installments

Raises:



17
18
19
20
# File 'app/models/solidus_subscriptions/checkout.rb', line 17

def initialize(installments)
  @installments = installments
  raise UserMismatchError.new(installments) if different_owners?
end

Instance Attribute Details

#installmentsArray<Installment> (readonly)

Returns The collection of installments to be used when generating a new order.

Returns:

  • (Array<Installment>)

    The collection of installments to be used when generating a new order



9
10
11
# File 'app/models/solidus_subscriptions/checkout.rb', line 9

def installments
  @installments
end

Instance Method Details

#orderSpree::Order

The order fulfilling the consolidated installment

Returns:

  • (Spree::Order)


56
57
58
59
60
61
62
63
# File 'app/models/solidus_subscriptions/checkout.rb', line 56

def order
  @order ||= Spree::Order.create(
    user: user,
    email: user.email,
    store: subscription.store || Spree::Store.default,
    subscription_order: true
  )
end

#processSpree::Order

Generate a new Spree::Order based on the information associated to the installments

Returns:

  • (Spree::Order)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/solidus_subscriptions/checkout.rb', line 26

def process
  populate

  # Installments are removed and set for future processing if they are
  # out of stock. If there are no line items left there is nothing to do
  return if installments.empty?

  if checkout
    Config.success_dispatcher_class.new(installments, order).dispatch
    return order
  end

  # A new order will only have 1 payment that we created
  if order.payments.any?(&:failed?)
    Config.payment_failed_dispatcher_class.new(installments, order).dispatch
    installments.clear
    nil
  end
ensure
  # Any installments that failed to be processed will be reprocessed
  unfulfilled_installments = installments.select(&:unfulfilled?)
  if unfulfilled_installments.any?
    Config.failure_dispatcher_class.
      new(unfulfilled_installments, order).dispatch
  end
end