Skip to content

Commit

Permalink
Merge pull request #229 from solidusio-contrib/add-order-creation-ser…
Browse files Browse the repository at this point in the history
…viceish-object

Introduce Installment OrderCreator configuration
  • Loading branch information
aldesantis authored May 6, 2021
2 parents dafa723 + d6e3654 commit 5e222ed
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,17 @@
# with new subscription cycles by clearing any past failed installment when a new one is created

# config.clear_past_installments = true

# ==================================== Custom Order Creation =====================================
#
# This settings allows the customization of the creation of each Installment Order by means of
# providing a class that can be switched / inherited
#
#
# the order_creator_class is initialized and called on the creation of the Order for each Subscription
# Installment.
# If you want to add simple extra attributes to the Order (such as a channel), that can be done by
# overriding the `extra_attributes` method on a subclass
#
# config.order_creator_class = 'SolidusSubscriptions::OrderCreator'
end
1 change: 1 addition & 0 deletions lib/solidus_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'solidus_subscriptions/dispatcher/out_of_stock_dispatcher'
require 'solidus_subscriptions/dispatcher/payment_failed_dispatcher'
require 'solidus_subscriptions/dispatcher/success_dispatcher'
require 'solidus_subscriptions/order_creator'

module SolidusSubscriptions
class << self
Expand Down
11 changes: 2 additions & 9 deletions lib/solidus_subscriptions/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ def process
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,
currency: installment.subscription.currency
)
SolidusSubscriptions.configuration.order_creator_class.new(installment).call
end

def populate_order(order)
Expand All @@ -61,7 +54,7 @@ def finalize_order(order)
order.payments.create(
payment_method: installment.subscription.payment_method_to_use,
source: installment.subscription.payment_source_to_use,
amount: order.total,
amount: order.total
)
end

Expand Down
13 changes: 9 additions & 4 deletions lib/solidus_subscriptions/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ module SolidusSubscriptions
class Configuration
attr_accessor(
:maximum_total_skips, :maximum_reprocessing_time, :churn_buster_account_id,
:churn_buster_api_key, :clear_past_installments, :processing_error_handler,
:churn_buster_api_key, :clear_past_installments, :processing_error_handler
)

attr_writer(
:success_dispatcher_class, :failure_dispatcher_class, :payment_failed_dispatcher_class,
:out_of_stock_dispatcher, :maximum_successive_skips, :reprocessing_interval,
:minimum_cancellation_notice, :processing_queue, :subscription_line_item_attributes,
:subscription_attributes, :subscribable_class,
:subscription_attributes, :subscribable_class, :order_creator_class
)

def success_dispatcher_class
Expand Down Expand Up @@ -57,7 +57,7 @@ def subscription_line_item_attributes
:subscribable_id,
:interval_length,
:interval_units,
:end_date,
:end_date
]
end

Expand All @@ -69,7 +69,7 @@ def subscription_attributes
{
shipping_address_attributes: Spree::PermittedAttributes.address_attributes,
billing_address_attributes: Spree::PermittedAttributes.address_attributes
},
}
]
end

Expand All @@ -81,5 +81,10 @@ def subscribable_class
def churn_buster?
churn_buster_account_id.present? && churn_buster_api_key.present?
end

def order_creator_class
@order_creator_class ||= 'SolidusSubscriptions::OrderCreator'
@order_creator_class.constantize
end
end
end
29 changes: 29 additions & 0 deletions lib/solidus_subscriptions/order_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module SolidusSubscriptions
class OrderCreator
def initialize(installment)
@installment = installment
end

def call
::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,
currency: installment.subscription.currency,
**extra_attributes
)
end

private

def extra_attributes
{}
end

attr_reader :installment
end
end

0 comments on commit 5e222ed

Please sign in to comment.