Class: Spree::Checkout::Advance

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/checkout/advance.rb

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(order:, state: nil, shipping_method_id: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/spree/checkout/advance.rb', line 6

def call(order:, state: nil, shipping_method_id: nil)
  return failure(order) if state.present? && !order.has_checkout_step?(state)
  return success(order) if state.present? && order.passed_checkout_step?(state)

  old_state = order.state
  order_updater_ran = false

  # We need to check how many times we transitioned between checkout steps and return the error if no transition has been made
  # We'll always return an error when passing the `state` arg and not reaching the targeted state
  transitions_count = 0

  until cannot_make_transition?(order, state)
    next_result = Spree::Dependencies.checkout_next_service.constantize.call(order: order)
    return failure(order, order.errors) if next_result.failure? && (transitions_count.zero? || state.present?)

    transitions_count +=1

    # Quick Checkout with Google Pay not always sends events for shipping method selection
    # we have to check this after payment

    if order.delivery? &&
        shipping_method_id.present? &&
        order.shipments.count == 1 &&
        order.shipping_method.id != shipping_method_id

      result = Spree::Checkout::SelectShippingMethod.call(order: order, params: { shipping_method_id: shipping_method_id })

      # We're running the order update inside Spree::Checkout::SelectShippingMethod
      order_updater_ran = result.success?
    end
  end

  if order.state != old_state && !order_updater_ran
    order.updater.update_shipment_state
    order.updater.update_payment_state
    order.save! if order.changed?
  end

  success(order)
end