Class: BillingLogic::PaymentCommandBuilder
- Inherits:
-
Object
- Object
- BillingLogic::PaymentCommandBuilder
- Defined in:
- lib/billing_logic/payment_command_builder.rb
Overview
Converts an array of BillingEngine::Client::Products and a next_payment_date into (string) commands for creating recurring_payments. Also converts a list of PaymentProfile#ids into commands for canceling their recurring_payments.
Class Method Summary collapse
-
.cancel_recurring_payment_commands(*profile_ids) ⇒ String
The (string) command for canceling recurring payments for the passed-in array of PaymentProfile#ids.
-
.create_recurring_payment_commands(products, next_payment_date = Date.current) ⇒ String
The (string) command for creating recurring payments for the passed-in array of BillingEngine::Client::Products with the passed-in next_payment_date.
Instance Method Summary collapse
-
#group_products_by_billing_cycle ⇒ Object
Groups BillingEngine::Client::Products into a hash with keys of BillingEngine::Client::Product#billing_cycle and values of the products.
-
#initialize(products) ⇒ PaymentCommandBuilder
constructor
Creates a PaymentCommandBuilder from an array of BillingEngine::Client::Products.
Constructor Details
#initialize(products) ⇒ PaymentCommandBuilder
Creates a PaymentCommandBuilder from an array of BillingEngine::Client::Products
11 12 13 |
# File 'lib/billing_logic/payment_command_builder.rb', line 11 def initialize(products) @products = products end |
Class Method Details
.cancel_recurring_payment_commands(*profile_ids) ⇒ String
Returns the (string) command for canceling recurring payments for the passed-in array of PaymentProfile#ids.
43 44 45 46 47 48 49 50 |
# File 'lib/billing_logic/payment_command_builder.rb', line 43 def cancel_recurring_payment_commands(*profile_ids) profile_ids.map do |profile_id| { :action => :cancel_recurring_payment, :payment_profile_id => profile_id } end end |
.create_recurring_payment_commands(products, next_payment_date = Date.current) ⇒ String
Returns the (string) command for creating recurring payments for the passed-in array of BillingEngine::Client::Products with the passed-in next_payment_date.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/billing_logic/payment_command_builder.rb', line 29 def create_recurring_payment_commands(products, next_payment_date = Date.current) self.new(products).group_products_by_billing_cycle.map do |k, prods| { :action => 'create_recurring_payment', :products => prods, :price => prods.inject(0) { |a, e| a + e.price; a }, :next_payment_date => next_payment_date, :billing_cycle => k } end end |
Instance Method Details
#group_products_by_billing_cycle ⇒ Object
Groups BillingEngine::Client::Products into a hash with keys of BillingEngine::Client::Product#billing_cycle and values of the products
16 17 18 19 20 21 22 |
# File 'lib/billing_logic/payment_command_builder.rb', line 16 def group_products_by_billing_cycle @products.inject({}) do |a, e| a[e.billing_cycle] ||= [] a[e.billing_cycle] << e a end end |