From 16dd27dab051bfd7fd8edd7721391e3bd80a62ea Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Thu, 19 Nov 2020 13:14:38 +0100 Subject: Streamline and simplify `SolidusSubscriptions::Checkout` This service object contained a lot of indirection and took on too many responsibilities. The new version is much more streamlined and the flow of operations should be much clearer. --- app/services/solidus_subscriptions/checkout.rb | 170 ++++++--------------- .../solidus_subscriptions/order_builder.rb | 42 ----- .../solidus_subscriptions/user_mismatch_error.rb | 17 --- 3 files changed, 44 insertions(+), 185 deletions(-) delete mode 100644 app/services/solidus_subscriptions/order_builder.rb delete mode 100644 app/services/solidus_subscriptions/user_mismatch_error.rb (limited to 'app/services/solidus_subscriptions') diff --git a/app/services/solidus_subscriptions/checkout.rb b/app/services/solidus_subscriptions/checkout.rb index c40849a..e348547 100644 --- a/app/services/solidus_subscriptions/checkout.rb +++ b/app/services/solidus_subscriptions/checkout.rb @@ -1,155 +1,73 @@ # frozen_string_literal: true -# This class takes a collection of installments and populates a new spree -# order with the correct contents based on the subscriptions associated to the -# intallments. This is to group together subscriptions being -# processed on the same day for a specific user module SolidusSubscriptions class Checkout - # @return [Array] The collection of installments to be used - # when generating a new order - attr_reader :installments + attr_reader :installment - delegate :user, to: :subscription - - # Get a new instance of a Checkout - # - # @param installments [Array] The collection of installments - # to be used when generating a new order - def initialize(installments) - @installments = installments - raise UserMismatchError.new(installments) if different_owners? + def initialize(installment) + @installment = installment end - # Generate a new Spree::Order based on the information associated to the - # installments - # - # @return [Spree::Order] 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 - SolidusSubscriptions.configuration.success_dispatcher_class.new(installments, order).dispatch - return order + order = create_order + + begin + populate_order(order) + finalize_order(order) + + 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 + else + 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 end - # A new order will only have 1 payment that we created - if order.payments.any?(&:failed?) - SolidusSubscriptions.configuration.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? - SolidusSubscriptions.configuration.failure_dispatcher_class. - new(unfulfilled_installments, order).dispatch - end + order end - # The order fulfilling the consolidated installment - # - # @return [Spree::Order] - def order - @order ||= ::Spree::Order.create( - user: user, - email: user.email, - store: subscription.store || ::Spree::Store.default, + private + + def create_order + ::Spree::Order.create( + user: installment.subscription.user, + email: installment.subscription.user.email, + store: installment.subscription.store || ::Spree::Store.default, subscription_order: true, - subscription: subscription + subscription: installment.subscription ) end - private + def populate_order(order) + installment.subscription.line_items.each do |line_item| + order.contents.add(line_item.subscribable, line_item.quantity) + end + end - def checkout + def finalize_order(order) + ::Spree::PromotionHandler::Cart.new(order).activate order.recalculate - apply_promotions order.checkout_steps[0...-1].each do case order.state - when "address" - order.ship_address = ship_address - order.bill_address = bill_address - when "payment" - create_payment + when 'address' + order.ship_address = installment.subscription.shipping_address_to_use + order.bill_address = installment.subscription.billing_address_to_use + when 'payment' + order.payments.create( + payment_method: installment.subscription.payment_method_to_use, + source: installment.subscription.payment_source_to_use, + amount: order.total, + ) end order.next! end - # Do this as a separate "quiet" transition so that it returns true or - # false rather than raising a failed transition error - order.complete - end - - def populate - unfulfilled_installments = [] - - order_line_items = installments.flat_map do |installment| - line_items = installment.line_item_builder.spree_line_items - - unfulfilled_installments.push(installment) if line_items.empty? - - line_items - end - - # Remove installments which had no stock from the active list - # They will be reprocessed later - @installments -= unfulfilled_installments - if unfulfilled_installments.any? - SolidusSubscriptions.configuration.out_of_stock_dispatcher_class.new(unfulfilled_installments).dispatch - end - - return if installments.empty? - - order_builder.add_line_items(order_line_items) - end - - def order_builder - @order_builder ||= OrderBuilder.new(order) - end - - def subscription - installments.first.subscription - end - - def ship_address - subscription.shipping_address_to_use - end - - def bill_address - subscription.billing_address_to_use - end - - def payment_source - subscription.payment_source_to_use - end - - def payment_method - subscription.payment_method_to_use - end - - def create_payment - order.payments.create( - source: payment_source, - amount: order.total, - payment_method: payment_method, - ) - end - - def apply_promotions - ::Spree::PromotionHandler::Cart.new(order).activate - order.updater.update # reload totals - end - - def different_owners? - installments.map { |i| i.subscription.user }.uniq.length > 1 + order.complete! end end end diff --git a/app/services/solidus_subscriptions/order_builder.rb b/app/services/solidus_subscriptions/order_builder.rb deleted file mode 100644 index a577e98..0000000 --- a/app/services/solidus_subscriptions/order_builder.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -# This class is responsible for adding line items to order without going -# through order contents. -module SolidusSubscriptions - class OrderBuilder - attr_reader :order - - # Get a new instance of an OrderBuilder - # - # @param order [Spree::Order] The order to be built - # - # @return [SolidusSubscriptions::OrderBuilder] - def initialize(order) - @order = order - end - - # Add line items to an order. If the order already - # has a line item for a given variant_id, update the quantity. Otherwise - # add the line item to the order. - # - # @param items [Array] The order to add the line item to - # @return [Array Date: Fri, 27 Nov 2020 16:52:05 +0100 Subject: Reorganize hierarchy of dispatcher classes --- app/services/solidus_subscriptions/dispatcher.rb | 23 -------------------- .../solidus_subscriptions/dispatcher/base.rb | 25 ++++++++++++++++++++++ .../dispatcher/failure_dispatcher.rb | 16 ++++++++++++++ .../dispatcher/out_of_stock_dispatcher.rb | 12 +++++++++++ .../dispatcher/payment_failed_dispatcher.rb | 22 +++++++++++++++++++ .../dispatcher/success_dispatcher.rb | 20 +++++++++++++++++ .../solidus_subscriptions/failure_dispatcher.rb | 14 ------------ .../out_of_stock_dispatcher.rb | 10 --------- .../payment_failed_dispatcher.rb | 20 ----------------- .../solidus_subscriptions/success_dispatcher.rb | 18 ---------------- 10 files changed, 95 insertions(+), 85 deletions(-) delete mode 100644 app/services/solidus_subscriptions/dispatcher.rb create mode 100644 app/services/solidus_subscriptions/dispatcher/base.rb create mode 100644 app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb create mode 100644 app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb create mode 100644 app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb create mode 100644 app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/failure_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/out_of_stock_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/payment_failed_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/success_dispatcher.rb (limited to 'app/services/solidus_subscriptions') diff --git a/app/services/solidus_subscriptions/dispatcher.rb b/app/services/solidus_subscriptions/dispatcher.rb deleted file mode 100644 index 0472f79..0000000 --- a/app/services/solidus_subscriptions/dispatcher.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - class Dispatcher - attr_reader :installments, :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 - @order = order - end - - def dispatch - raise NotImplementedError - end - end -end diff --git a/app/services/solidus_subscriptions/dispatcher/base.rb b/app/services/solidus_subscriptions/dispatcher/base.rb new file mode 100644 index 0000000..84b8710 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/base.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module SolidusSubscriptions + module Dispatcher + class Base + attr_reader :installments, :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 + @order = order + end + + def dispatch + raise NotImplementedError + end + end + end +end diff --git a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb new file mode 100644 index 0000000..286a4c9 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb @@ -0,0 +1,16 @@ +# 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 + end + 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 new file mode 100644 index 0000000..ef79f84 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb @@ -0,0 +1,12 @@ +# 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) + end + 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 new file mode 100644 index 0000000..74bee88 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb @@ -0,0 +1,22 @@ +# 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 + + ::Spree::Event.fire( + 'solidus_subscriptions.installments_failed_payment', + installments: installments, + order: order, + ) + end + end + end +end diff --git a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb new file mode 100644 index 0000000..78563a4 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb @@ -0,0 +1,20 @@ +# 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 + + ::Spree::Event.fire( + 'solidus_subscriptions.installments_succeeded', + installments: installments, + order: order, + ) + end + end + end +end diff --git a/app/services/solidus_subscriptions/failure_dispatcher.rb b/app/services/solidus_subscriptions/failure_dispatcher.rb deleted file mode 100644 index c77d4b0..0000000 --- a/app/services/solidus_subscriptions/failure_dispatcher.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -# Handles failed installments. -module SolidusSubscriptions - class FailureDispatcher < Dispatcher - def dispatch - order.touch(:completed_at) - order.cancel - installments.each do |installment| - installment.failed!(order) - end - end - end -end diff --git a/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb b/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb deleted file mode 100644 index 05484f4..0000000 --- a/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -# Handles installments that cannot be processed for lack of stock. -module SolidusSubscriptions - class OutOfStockDispatcher < Dispatcher - def dispatch - installments.each(&:out_of_stock) - end - end -end diff --git a/app/services/solidus_subscriptions/payment_failed_dispatcher.rb b/app/services/solidus_subscriptions/payment_failed_dispatcher.rb deleted file mode 100644 index 29eb291..0000000 --- a/app/services/solidus_subscriptions/payment_failed_dispatcher.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -# Handles payment failures for subscription installments. -module SolidusSubscriptions - class PaymentFailedDispatcher < Dispatcher - def dispatch - order.touch(:completed_at) - order.cancel - installments.each do |installment| - installment.payment_failed!(order) - end - - ::Spree::Event.fire( - 'solidus_subscriptions.installments_failed_payment', - installments: installments, - order: order, - ) - end - end -end diff --git a/app/services/solidus_subscriptions/success_dispatcher.rb b/app/services/solidus_subscriptions/success_dispatcher.rb deleted file mode 100644 index ce55266..0000000 --- a/app/services/solidus_subscriptions/success_dispatcher.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -# Handles installments that are processed successfully. -module SolidusSubscriptions - class SuccessDispatcher < Dispatcher - def dispatch - installments.each do |installment| - installment.success!(order) - end - - ::Spree::Event.fire( - 'solidus_subscriptions.installments_succeeded', - installments: installments, - order: order, - ) - end - end -end -- cgit v1.2.3 From 784c1f07c61964c1a05549bffb0a709763bcf2d6 Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Fri, 27 Nov 2020 17:55:00 +0100 Subject: Pass one installment at a time to dispatcher classes --- app/services/solidus_subscriptions/checkout.rb | 8 ++++---- app/services/solidus_subscriptions/dispatcher/base.rb | 13 +++---------- .../solidus_subscriptions/dispatcher/failure_dispatcher.rb | 5 +---- .../dispatcher/out_of_stock_dispatcher.rb | 3 +-- .../dispatcher/payment_failed_dispatcher.rb | 9 +++------ .../solidus_subscriptions/dispatcher/success_dispatcher.rb | 9 +++------ 6 files changed, 15 insertions(+), 32 deletions(-) (limited to 'app/services/solidus_subscriptions') diff --git a/app/services/solidus_subscriptions/checkout.rb b/app/services/solidus_subscriptions/checkout.rb index e348547..83fc35c 100644 --- a/app/services/solidus_subscriptions/checkout.rb +++ b/app/services/solidus_subscriptions/checkout.rb @@ -15,15 +15,15 @@ module SolidusSubscriptions 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 84b8710..30c5034 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 286a4c9..54f8f2b 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 ef79f84..fe3e701 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 74bee88..a56332e 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 78563a4..0ae71dc 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_succeeded', + installment: installment, order: order, ) end -- cgit v1.2.3 From ce4edc06e6079d8c098f1d0754e3c8e31b355e2d Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Wed, 20 Jan 2021 10:50:50 +0100 Subject: Move all business logic to `lib` It wasn't clear why certain business logic should live in `app/services` while other should live in `lib`. By unifying everything in one directory, we make it easier for developers to inspect the code and reduce the cognitive load when implementing new classes. --- app/services/solidus_subscriptions/checkout.rb | 73 ---------------------- .../solidus_subscriptions/dispatcher/base.rb | 18 ------ .../dispatcher/failure_dispatcher.rb | 13 ---- .../dispatcher/out_of_stock_dispatcher.rb | 11 ---- .../dispatcher/payment_failed_dispatcher.rb | 19 ------ .../dispatcher/success_dispatcher.rb | 17 ----- .../solidus_subscriptions/line_item_builder.rb | 36 ----------- .../subscription_generator.rb | 65 ------------------- .../subscription_line_item_builder.rb | 23 ------- 9 files changed, 275 deletions(-) delete mode 100644 app/services/solidus_subscriptions/checkout.rb delete mode 100644 app/services/solidus_subscriptions/dispatcher/base.rb delete mode 100644 app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb delete mode 100644 app/services/solidus_subscriptions/line_item_builder.rb delete mode 100644 app/services/solidus_subscriptions/subscription_generator.rb delete mode 100644 app/services/solidus_subscriptions/subscription_line_item_builder.rb (limited to 'app/services/solidus_subscriptions') diff --git a/app/services/solidus_subscriptions/checkout.rb b/app/services/solidus_subscriptions/checkout.rb deleted file mode 100644 index 83fc35c..0000000 --- a/app/services/solidus_subscriptions/checkout.rb +++ /dev/null @@ -1,73 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - class Checkout - attr_reader :installment - - def initialize(installment) - @installment = installment - end - - def process - order = create_order - - begin - populate_order(order) - finalize_order(order) - - 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 - else - 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 - end - - order - end - - private - - def create_order - ::Spree::Order.create( - user: installment.subscription.user, - email: installment.subscription.user.email, - store: installment.subscription.store || ::Spree::Store.default, - subscription_order: true, - subscription: installment.subscription - ) - end - - def populate_order(order) - installment.subscription.line_items.each do |line_item| - order.contents.add(line_item.subscribable, line_item.quantity) - end - end - - def finalize_order(order) - ::Spree::PromotionHandler::Cart.new(order).activate - order.recalculate - - order.checkout_steps[0...-1].each do - case order.state - when 'address' - order.ship_address = installment.subscription.shipping_address_to_use - order.bill_address = installment.subscription.billing_address_to_use - when 'payment' - order.payments.create( - payment_method: installment.subscription.payment_method_to_use, - source: installment.subscription.payment_source_to_use, - amount: order.total, - ) - end - - order.next! - end - - order.complete! - end - end -end diff --git a/app/services/solidus_subscriptions/dispatcher/base.rb b/app/services/solidus_subscriptions/dispatcher/base.rb deleted file mode 100644 index 30c5034..0000000 --- a/app/services/solidus_subscriptions/dispatcher/base.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - module Dispatcher - class Base - attr_reader :installment, :order - - def initialize(installment, order) - @installment = installment - @order = order - end - - def dispatch - raise NotImplementedError - end - end - end -end diff --git a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb deleted file mode 100644 index 54f8f2b..0000000 --- a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - module Dispatcher - class FailureDispatcher < Base - def dispatch - order.touch(:completed_at) - order.cancel - installment.failed!(order) - end - 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 deleted file mode 100644 index fe3e701..0000000 --- a/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - module Dispatcher - class OutOfStockDispatcher < Base - def dispatch - installment.out_of_stock - end - 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 deleted file mode 100644 index a56332e..0000000 --- a/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - module Dispatcher - class PaymentFailedDispatcher < Base - def dispatch - order.touch(:completed_at) - order.cancel - installment.payment_failed!(order) - - ::Spree::Event.fire( - 'solidus_subscriptions.installment_failed_payment', - installment: installment, - order: order, - ) - end - end - end -end diff --git a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb deleted file mode 100644 index 0ae71dc..0000000 --- a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - module Dispatcher - class SuccessDispatcher < Base - def dispatch - installment.success!(order) - - ::Spree::Event.fire( - 'solidus_subscriptions.installment_succeeded', - installment: installment, - order: order, - ) - end - end - end -end diff --git a/app/services/solidus_subscriptions/line_item_builder.rb b/app/services/solidus_subscriptions/line_item_builder.rb deleted file mode 100644 index c9aedb9..0000000 --- a/app/services/solidus_subscriptions/line_item_builder.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -# This class is responsible for taking SubscriptionLineItems and building -# them into Spree::LineItems which can be added to an order -module SolidusSubscriptions - class LineItemBuilder - attr_reader :subscription_line_items - - # Get a new instance of a LineItemBuilder - # - # @param subscription_line_items[Array] The - # subscription line item to be converted into a Spree::LineItem - # - # @return [SolidusSubscriptions::LineItemBuilder] - def initialize(subscription_line_items) - @subscription_line_items = subscription_line_items - end - - # Get a new (unpersisted) Spree::LineItem which matches the details of - # :subscription_line_item - # - # @return [Array] - def spree_line_items - line_items = subscription_line_items.map do |subscription_line_item| - variant = subscription_line_item.subscribable - - next unless variant.can_supply?(subscription_line_item.quantity) - - ::Spree::LineItem.new(variant: variant, quantity: subscription_line_item.quantity) - end - - # Either all line items for an installment are fulfilled or none are - line_items.all? ? line_items : [] - end - end -end diff --git a/app/services/solidus_subscriptions/subscription_generator.rb b/app/services/solidus_subscriptions/subscription_generator.rb deleted file mode 100644 index 8153912..0000000 --- a/app/services/solidus_subscriptions/subscription_generator.rb +++ /dev/null @@ -1,65 +0,0 @@ -# frozen_string_literal: true - -# This module is responsible for taking SolidusSubscriptions::LineItem -# objects and creating SolidusSubscriptions::Subscription Objects -module SolidusSubscriptions - module SubscriptionGenerator - extend self - - SubscriptionConfiguration = Struct.new(:interval_length, :interval_units, :end_date) - - # Create and persist a subscription for a collection of subscription - # line items - # - # @param subscription_line_items [Array] The - # subscription_line_items to be activated - # - # @return [SolidusSubscriptions::Subscription] - def activate(subscription_line_items) - return if subscription_line_items.empty? - - order = subscription_line_items.first.order - configuration = subscription_configuration(subscription_line_items.first) - - subscription_attributes = { - user: order.user, - line_items: subscription_line_items, - store: order.store, - shipping_address: order.ship_address, - billing_address: order.bill_address, - payment_source: order.payments.valid.last&.payment_source, - payment_method: order.payments.valid.last&.payment_method, - **configuration.to_h - } - - Subscription.create!(subscription_attributes) do |sub| - sub.actionable_date = sub.next_actionable_date - end - end - - # Group a collection of line items by common subscription configuration - # options. Grouped subscription_line_items can belong to a single - # subscription. - # - # @param subscription_line_items [Array] The - # subscription_line_items to be grouped. - # - # @return [Array>] - def group(subscription_line_items) - subscription_line_items.group_by do |li| - subscription_configuration(li) - end. - values - end - - private - - def subscription_configuration(subscription_line_item) - SubscriptionConfiguration.new( - subscription_line_item.interval_length, - subscription_line_item.interval_units, - subscription_line_item.end_date - ) - end - end -end diff --git a/app/services/solidus_subscriptions/subscription_line_item_builder.rb b/app/services/solidus_subscriptions/subscription_line_item_builder.rb deleted file mode 100644 index 7354102..0000000 --- a/app/services/solidus_subscriptions/subscription_line_item_builder.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - module SubscriptionLineItemBuilder - private - - def create_subscription_line_item(line_item) - SolidusSubscriptions::LineItem.create!( - subscription_params.merge(spree_line_item: line_item) - ) - - # Rerun the promotion handler to pickup subscription promotions - ::Spree::PromotionHandler::Cart.new(line_item.order).activate - line_item.order.recalculate - end - - def subscription_params - params.require(:subscription_line_item).permit( - SolidusSubscriptions.configuration.subscription_line_item_attributes - ) - end - end -end -- cgit v1.2.3