Permalink
Browse files

Implemented shortcut for updating price of existing subscription

  • Loading branch information...
1 parent e8b7a74 commit d73c0a1dfe7eb9abe0b707f8741b4f09dbadfe1e Alex Lebedev committed Mar 30, 2009
@@ -6,4 +6,19 @@ class Subscription < ActiveRecord::Base
def billing_amount
self.net_amount + self.taxes_amount
end
+
+ # Recalculate price and taxes
+ #
+ # subscription_config should be initialized SubscriptionManagement instance
+ def recalc_price(subscription_config)
+ total_tax = subscription_config.all_taxes[self.taxes_id]["taxes"].inject(0){|sum,item| sum + item["rate"]} # sum all applicable taxes
+ self.net_amount = self.quantity * subscription_config.all_tariff_plans[self.tariff_plan_id]["price"]
+ self.taxes_amount = self.net_amount * total_tax
+ end
+
+ def update_quantity(qty, subscription_config)
+ self.quantity = qty
+ self.recalc_price(subscription_config)
+ self.save
+ end
end
@@ -84,21 +84,16 @@ def subscribe(options)
subscription.tariff_plan_id = options[:tariff_plan]
subscription.quantity = options[:quantity]
-
# Set tariff-related fields
tariff = @all_tariff_plans[subscription.tariff_plan_id]
raise ArgumentError, 'Invalid tariff given: %s' % subscription.tariff_plan_id if tariff.nil?
subscription.currency = tariff["currency"]
subscription.periodicity = tariff["payment_term"]["periodicity"]
-
+ subscription.taxes_id = get_applicable_taxes_id(options[:account_country], options[:account_state])
subscription.starts_on = options[:start_date] + tariff["payment_term"]["trial_days"]
subscription.ends_on = options[:end_date]
- # Set tax- and payment-related fields
- subscription.taxes_id = get_applicable_taxes_id(options[:account_country], options[:account_state])
- total_tax = @all_taxes[subscription.taxes_id]["taxes"].inject(0){|sum,item| sum + item["rate"]} # sum all taxes
- subscription.net_amount = subscription.quantity * tariff["price"]
- subscription.taxes_amount = subscription.net_amount * total_tax
+ subscription.recalc_price(self)
subscription.status = 'pending'
subscription.save
@@ -140,7 +135,10 @@ def pay_for_subscription(subscription_id, card, payment_options)
# Update subscription through specified payment gateway
def update_subscription(subscription_id, options)
+ subscription = Subscription.find_by_id(subscription_id)
profile = get_active_profile(subscription_id)
+ options[:amount] = Money.new(subscription.billing_amount, subscription.currency)
+ options[:taxes_amount_included] = Money.new(subscription.taxes_amount,subscription.currency)
gw = RecurringBilling::RecurringBillingGateway.get_instance(@gateway)
unless new_gateway_reference = gw.update_or_recreate(profile.gateway_reference, options)
raise StandardError, 'Cannot update subscription through gateway: ' + gw.last_response.message
@@ -11,29 +11,30 @@ def setup
:gateways_config => File.dirname(__FILE__) + '/../../../recurring_billing/test/fixtures.yml',
:gateway => :paypal
)
+ @subscription_options = {
+ :account_id => 'test_acc_id',
+ :account_country => 'US',
+ :account_state => 'CA',
+ :tariff_plan => 'solo_monthly',
+ :start_date => (Date.today + 1),
+ :quantity => 1,
+ :end_date => DateTime.new(2010, 12, 11)
+ }
+ @credit_card = credit_card()
end
+ # TODO: Split into several smaller tests
# Checks if there are no exceptions while manipulating class with legit data; based on demo.rb
def test_crud_is_working
- options = {
- :account_id => 'test_acc_id',
- :account_country => 'US',
- :account_state => 'CA',
- :tariff_plan => 'solo_monthly',
- :start_date => (Date.today + 1),
- :quantity => 1,
- :end_date => DateTime.new(2010, 12, 11)
- }
- credit_card = credit_card()
credit_card_2 = credit_card(4929838635250031, {:year => Time.now.year + 5})
credit_card_3 = credit_card(4929273971564532, {:year => Time.now.year + 3})
assert_raise StandardError do; @sm.get_active_profile(-1); end
- subscription_id = @sm.subscribe(options)
+ subscription_id = @sm.subscribe(@subscription_options)
assert_equal 'pending', Subscription.find_by_id(subscription_id).status
assert_raise StandardError do; @sm.get_active_profile(subscription_id); end
- @sm.pay_for_subscription(subscription_id, credit_card, {})
+ @sm.pay_for_subscription(subscription_id, @credit_card, {})
subscription = Subscription.find_by_id(subscription_id)
assert_equal 'ok', subscription.status
assert_equal 700, subscription.net_amount
@@ -109,4 +110,19 @@ def test_crud_is_working
assert_equal 'canceled', Subscription.find_by_id(subscription_id).status
end
+ def test_update_subscription_amount
+ subscription_id = @sm.subscribe(@subscription_options)
+ @sm.pay_for_subscription(subscription_id, @credit_card, {})
+ subscription = Subscription.find_by_id(subscription_id)
+ assert_equal 'ok', subscription.status
+ initial_net_price, initial_tax = subscription.net_amount, subscription.taxes_amount
+
+ subscription.update_quantity(subscription.quantity*2, @sm)
+ @sm.update_subscription(subscription.id, {})
+ #puts subscription.inspect
+ profile = @sm.get_active_profile(subscription)
+ #puts profile.inspect
+ assert_equal(2*initial_net_price, profile.net_amount)
+ assert_equal(2*initial_tax, profile.taxes_amount)
+ end
end

0 comments on commit d73c0a1

Please sign in to comment.