Skip to content

Commit

Permalink
Pass one installment at a time to dispatcher classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Nov 27, 2020
1 parent b002ab1 commit 90ee35e
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 41 deletions.
8 changes: 4 additions & 4 deletions app/services/solidus_subscriptions/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def process
populate_order(order)
finalize_order(order)

SolidusSubscriptions.configuration.success_dispatcher_class.new([installment], order).dispatch
SolidusSubscriptions.configuration.success_dispatcher_class.new(installment, order).dispatch
rescue StateMachines::InvalidTransition
if order.payments.any?(&:failed?)
SolidusSubscriptions.configuration.payment_failed_dispatcher_class.new([installment], order).dispatch
SolidusSubscriptions.configuration.payment_failed_dispatcher_class.new(installment, order).dispatch
else
SolidusSubscriptions.configuration.failure_dispatcher_class.new([installment], order).dispatch
SolidusSubscriptions.configuration.failure_dispatcher_class.new(installment, order).dispatch
end
rescue ::Spree::Order::InsufficientStock
SolidusSubscriptions.configuration.out_of_stock_dispatcher_class.new([installment], order).dispatch
SolidusSubscriptions.configuration.out_of_stock_dispatcher_class.new(installment, order).dispatch
end

order
Expand Down
13 changes: 3 additions & 10 deletions app/services/solidus_subscriptions/dispatcher/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@
module SolidusSubscriptions
module Dispatcher
class Base
attr_reader :installments, :order
attr_reader :installment, :order

# Returns a new instance of this dispatcher.
#
# @param installments [Array<SolidusSubscriptions::Installment>] The installments to process
# with this dispatcher
# @param order [Spree::Order] The order that was generated as a result of these installments
#
# @return [SolidusSubscriptions::Dispatcher]
def initialize(installments, order = nil)
@installments = installments
def initialize(installment, order)
@installment = installment
@order = order
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# frozen_string_literal: true

# Handles failed installments.
module SolidusSubscriptions
module Dispatcher
class FailureDispatcher < Base
def dispatch
order.touch(:completed_at)
order.cancel
installments.each do |installment|
installment.failed!(order)
end
installment.failed!(order)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# frozen_string_literal: true

# Handles installments that cannot be processed for lack of stock.
module SolidusSubscriptions
module Dispatcher
class OutOfStockDispatcher < Base
def dispatch
installments.each(&:out_of_stock)
installment.out_of_stock
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# frozen_string_literal: true

# Handles payment failures for subscription installments.
module SolidusSubscriptions
module Dispatcher
class PaymentFailedDispatcher < Base
def dispatch
order.touch(:completed_at)
order.cancel
installments.each do |installment|
installment.payment_failed!(order)
end
installment.payment_failed!(order)

::Spree::Event.fire(
'solidus_subscriptions.installments_failed_payment',
installments: installments,
'solidus_subscriptions.installment_failed_payment',
installment: installment,
order: order,
)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# frozen_string_literal: true

# Handles installments that are processed successfully.
module SolidusSubscriptions
module Dispatcher
class SuccessDispatcher < Base
def dispatch
installments.each do |installment|
installment.success!(order)
end
installment.success!(order)

::Spree::Event.fire(
'solidus_subscriptions.installments_succeeded',
installments: installments,
'solidus_subscriptions.installment_processed',
installment: installment,
order: order,
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module ChurnBusterSubscriber

event_action :report_subscription_cancellation, event_name: 'solidus_subscriptions.subscription_canceled'
event_action :report_subscription_ending, event_name: 'solidus_subscriptions.subscription_ended'
event_action :report_payment_success, event_name: 'solidus_subscriptions.installments_succeeded'
event_action :report_payment_failure, event_name: 'solidus_subscriptions.installments_failed_payment'
event_action :report_payment_success, event_name: 'solidus_subscriptions.installment_succeeded'
event_action :report_payment_failure, event_name: 'solidus_subscriptions.installment_failed_payment'
event_action :report_payment_method_change, event_name: 'solidus_subscriptions.subscription_payment_method_changed'

def report_subscription_cancellation(event)
Expand Down
2 changes: 1 addition & 1 deletion spec/services/solidus_subscriptions/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
def stub_dispatcher(klass, installment)
instance_spy(klass).tap do |dispatcher|
allow(klass).to receive(:new).with(
[installment],
installment,
an_instance_of(Spree::Order)
).and_return(dispatcher)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

order = build_stubbed(:order)
installments = build_list(:installment, 2)
installment = build_stubbed(:installment)
Spree::Event.fire(
'solidus_subscriptions.installments_succeeded',
installments: installments,
'solidus_subscriptions.installment_succeeded',
installment: installment,
order: order,
)

Expand All @@ -46,10 +46,10 @@
allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

order = build_stubbed(:order)
installments = build_list(:installment, 2)
installment = build_stubbed(:installment)
Spree::Event.fire(
'solidus_subscriptions.installments_failed_payment',
installments: installments,
'solidus_subscriptions.installment_failed_payment',
installment: installment,
order: order,
)

Expand Down

0 comments on commit 90ee35e

Please sign in to comment.