Class: BillingLogic::Strategies::IndependentPaymentStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/billing_logic/strategies/independent_payment_strategy.rb

Overview

The BaseStrategy defines generic functions used by various BillingLogic::Strategies.

Defined Under Namespace

Classes: ProductComparator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ IndependentPaymentStrategy

Returns a new instance of IndependentPaymentStrategy.



15
16
17
18
19
20
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 15

def initialize(opts = {})
  @current_state = opts.delete(:current_state) || []
  @desired_state = opts.delete(:desired_state) || []
  @command_list = ToStringArray.new
  @payment_command_builder_class = opts.delete(:payment_command_builder_class) || default_command_builder
end

Instance Attribute Details

#current_stateObject

Returns the value of attribute current_state.



13
14
15
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 13

def current_state
  @current_state
end

#default_command_builder=(value) ⇒ Object

Sets the attribute default_command_builder

Parameters:

  • value

    the value to set the attribute default_command_builder to.



13
14
15
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 13

def default_command_builder=(value)
  @default_command_builder = value
end

#desired_stateObject

Returns the value of attribute desired_state.



13
14
15
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 13

def desired_state
  @desired_state
end

#payment_command_builder_classObject

Returns the value of attribute payment_command_builder_class.



13
14
15
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 13

def payment_command_builder_class
  @payment_command_builder_class
end

Instance Method Details

#active_or_pending_profilesArray<PaymentProfile>

CurrentState PaymentProfiles with payment_profile of ‘ActiveProfile’ or ‘PendingProfile’

Returns:

  • (Array<PaymentProfile>)

    array of all ‘ActiveProfile’ or ‘PendingProfile’ PaymentProfiles for the CurrentState



79
80
81
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 79

def active_or_pending_profiles
  current_state.reject { |profile| !profile.active_or_pending? }
end

#active_productsArray<BillingEngine::Client::Product>

Returns an array of active BillingEngine::Client::Products from the CurrentState

Returns:

  • (Array<BillingEngine::Client::Product>)

    array of active BillingEngine::Client::Products in the CurrentState



71
72
73
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 71

def active_products
  current_state.active_products
end

#active_profilesArray<PaymentProfile>

Returns an array of PaymentProfiles with profile_status ‘ActiveProfile’ or ‘PendingProfile’

Returns:

  • (Array<PaymentProfile>)

    array of PaymentProfiles in the CurrentState with profile_status ‘ActiveProfile’ or ‘PendingProfile’



64
65
66
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 64

def active_profiles
  active_or_pending_profiles
end

#add_commands_for_products_to_be_removed!Object

this should be part of a separate strategy object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 191

def add_commands_for_products_to_be_removed!
  active_profiles.each do |profile|

    # We need to issue refunds before cancelling profiles
    refund_options = issue_refunds_if_necessary(profile)
    remaining_products = remaining_products_after_product_removal_from_profile(profile)

    if remaining_products.empty? # all products in payment profile needs to be removed

      @command_list << cancel_recurring_payment_command(profile, refund_options)

    elsif remaining_products.size == profile.products.size # nothing has changed
    #
    # do nothing
    #
    else  # only some products are being removed and the profile needs to be updated

      if remaining_products.size >= 1

        @command_list << remove_product_from_payment_profile(profile.identifier,
                                                             removed_products_from_profile(profile),
                                                             refund_options)
      else

        @command_list << cancel_recurring_payment_command(profile, refund_options)
        @command_list << create_recurring_payment_command(remaining_products,
                                                          :paid_until_date => profile.paid_until_date,
                                                          :period => extract_period_from_product_list(remaining_products))
      end
    end
  end
end

#cancel_recurring_payment_command(profile, opts = {}) ⇒ Object

these messages seems like they should be pluggable



256
257
258
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 256

def cancel_recurring_payment_command(profile, opts = {})
  payment_command_builder_class.cancel_recurring_payment_commands(profile, opts)
end

#command_listString

Returns a string representing the commands the Strategy generates

Returns:

  • (String)

    the string representation of the commands the Strategy generates



25
26
27
28
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 25

def command_list
  calculate_list
  @command_list.flatten
end

#create_recurring_payment_command(products, opts = {:paid_until_date => Date.current}) ⇒ Object



264
265
266
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 264

def create_recurring_payment_command(products, opts = {:paid_until_date => Date.current})
  payment_command_builder_class.create_recurring_payment_commands(products, opts)
end

#disable_subscription(profile_id) ⇒ Object



251
252
253
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 251

def disable_subscription(profile_id)
  { :disable => true }
end

#extract_period_from_product_list(products) ⇒ Object



224
225
226
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 224

def extract_period_from_product_list(products)
  products.first.billing_cycle.period
end

#inactive_productsArray<BillingEngine::Client::Product>

Returns an array of inactive BillingEngine::Client::Products in the CurrentState

Returns:

  • (Array<BillingEngine::Client::Product>)

    array of inactive BillingEngine::Client::Products in the CurrentState



57
58
59
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 57

def inactive_products
  neither_active_nor_pending_profiles.map { |profile| profile.products }.flatten
end

#issue_refunds_if_necessary(profile) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 236

def issue_refunds_if_necessary(profile)
  ret = {}
  if !profile.refundable_payment_amount(removed_products_from_profile(profile)).zero?
    ret.merge!(refund_recurring_payments_command(profile.identifier, profile.refundable_payment_amount(removed_products_from_profile(profile))))
    ret.merge!(disable_subscription(profile.identifier))
  elsif ((Date.current - 1) <= profile.billing_start_date) # && (Date.current >= profile.billing_start_date)
    ret.merge!(disable_subscription(profile.identifier))
  end
  ret
end

#neither_active_nor_pending_profilesArray<PaymentProfile>

CurrentState PaymentProfiles with payment_profile of neither ‘ActiveProfile’ nor ‘PendingProfile’ (i.e., either ‘CancelledProfile’ or ‘ComplimentaryProfile’)

‘ActiveProfile’ nor ‘PendingProfile’

Returns:

  • (Array<PaymentProfile>)

    array of all PaymentProfiles for the CurrentState with payment_profile of neither



88
89
90
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 88

def neither_active_nor_pending_profiles
  current_state.reject { |profile| profile.active_or_pending? }
end

#products_to_be_addedArray<BillingEngine::Client::Product>

Returns an array of BillingEngine::Client::Products to be added because they’re desired but not active

Returns:

  • (Array<BillingEngine::Client::Product>)

    array of desired but inactive BillingEngine::Client::Products scheduled to be added



39
40
41
42
43
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 39

def products_to_be_added
  desired_state.reject do |product|
    ProductComparator.new(product).in_like?(active_products)
  end
end

#products_to_be_added_grouped_by_dateObject

Returns BillingEngine::Client::Products to be added, grouped by date



32
33
34
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 32

def products_to_be_added_grouped_by_date
  group_by_date(products_to_be_added)
end

#products_to_be_removedArray<BillingEngine::Client::Product>

Returns an array of BillingEngine::Client::Products to be removed because they’re active but not desired

Returns:

  • (Array<BillingEngine::Client::Product>)

    array of active but no longer desired BillingEngine::Client::Products scheduled for removal



48
49
50
51
52
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 48

def products_to_be_removed
  active_products.reject do |product|
    ProductComparator.new(product).included?(desired_state)
  end
end

#profiles_by_status(active_or_pending = nil) ⇒ Object

Deprecated.

Too confusing. Please directly call #active_or_pending_profiles or #neither_active_nor_pending_profiles



93
94
95
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 93

def profiles_by_status(active_or_pending = nil)
  active_or_pending ? active_or_pending_profiles : neither_active_nor_pending_profiles
end

#refund_recurring_payments_command(profile_id, amount) ⇒ Object



247
248
249
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 247

def refund_recurring_payments_command(profile_id, amount)
  { :refund => amount, :profile_id => profile_id }
end

#remaining_products_after_product_removal_from_profile(profile) ⇒ Object



228
229
230
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 228

def remaining_products_after_product_removal_from_profile(profile)
  profile.active_products.reject { |product| products_to_be_removed.include?(product) }
end

#remove_product_from_payment_profile(profile_id, removed_products, opts = {}) ⇒ Object



260
261
262
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 260

def remove_product_from_payment_profile(profile_id, removed_products, opts = {})
  payment_command_builder_class.remove_product_from_payment_profile(profile_id, removed_products, opts)
end

#removed_products_from_profile(profile) ⇒ Object



232
233
234
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 232

def removed_products_from_profile(profile)
  profile.products.select { |product| products_to_be_removed.include?(product) }
end

#reset_command_list!Object



317
318
319
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 317

def reset_command_list!
  @command_list.clear
end

#with_products_to_be_added(&block) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/billing_logic/strategies/independent_payment_strategy.rb', line 268

def with_products_to_be_added(&block)
  unless (products_to_be_added = products_to_be_added_grouped_by_date).empty?
    products_to_be_added.each do |group_of_products, date|
      yield(group_of_products, date)
    end
  end
end