diff --git a/app/services/solidus_subscriptions/checkout.rb b/app/services/solidus_subscriptions/checkout.rb index e348547b..83fc35c2 100644 --- a/app/services/solidus_subscriptions/checkout.rb +++ b/app/services/solidus_subscriptions/checkout.rb @@ -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 diff --git a/app/services/solidus_subscriptions/dispatcher/base.rb b/app/services/solidus_subscriptions/dispatcher/base.rb index 84b87101..30c50346 100644 --- a/app/services/solidus_subscriptions/dispatcher/base.rb +++ b/app/services/solidus_subscriptions/dispatcher/base.rb @@ -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] 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 diff --git a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb index 286a4c94..54f8f2b7 100644 --- a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb +++ b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb index ef79f843..fe3e7014 100644 --- a/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb +++ b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb index 74bee883..a56332ed 100644 --- a/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb +++ b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb index 78563a45..bacb8e32 100644 --- a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb +++ b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb @@ -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 diff --git a/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb b/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb index aa9dfa48..34540038 100644 --- a/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb +++ b/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb @@ -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) diff --git a/spec/services/solidus_subscriptions/checkout_spec.rb b/spec/services/solidus_subscriptions/checkout_spec.rb index 0b9d7f69..b06b41ae 100644 --- a/spec/services/solidus_subscriptions/checkout_spec.rb +++ b/spec/services/solidus_subscriptions/checkout_spec.rb @@ -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 diff --git a/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb b/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb index 572913bd..11ada2c4 100644 --- a/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb +++ b/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb @@ -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, ) @@ -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, )