diff options
author | jessib <jessib@leap.se> | 2013-04-15 12:13:33 -0700 |
---|---|---|
committer | Azul <azul@leap.se> | 2013-07-17 10:47:12 +0200 |
commit | 2829e7020abcf6d2f708fbc7dfb3db98ad174255 (patch) | |
tree | 4d389dbb892211bdb3f0305403a45c38e9d5a1c7 /billing/app | |
parent | 29f1de49083d57895bdf8f999448678934fbeffc (diff) |
Displaying of user's subscription. Still not committed to whether user can have multiple subscriptions at a time or not.
Diffstat (limited to 'billing/app')
-rw-r--r-- | billing/app/controllers/customer_controller.rb | 2 | ||||
-rw-r--r-- | billing/app/controllers/subscriptions_controller.rb | 12 | ||||
-rw-r--r-- | billing/app/models/customer.rb | 19 | ||||
-rw-r--r-- | billing/app/views/customer/_subscription.html.haml | 14 | ||||
-rw-r--r-- | billing/app/views/customer/edit.html.haml | 3 | ||||
-rw-r--r-- | billing/app/views/subscriptions/_subscription_details.html.haml | 13 | ||||
-rw-r--r-- | billing/app/views/subscriptions/show.html.haml | 1 |
7 files changed, 45 insertions, 19 deletions
diff --git a/billing/app/controllers/customer_controller.rb b/billing/app/controllers/customer_controller.rb index a16e3fe..4192f4c 100644 --- a/billing/app/controllers/customer_controller.rb +++ b/billing/app/controllers/customer_controller.rb @@ -19,7 +19,7 @@ class CustomerController < BillingBaseController @tr_data = Braintree::TransparentRedirect. update_customer_data(:redirect_url => confirm_customer_url, :customer_id => params[:id]) - @subscriptions = @braintree_data.credit_cards.map(&:subscriptions).flatten + @subscriptions = customer.active_subscriptions(@braintree_data) else # TODO: will want to have case for admins, presumably access_denied diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 38b17b4..af20faf 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -5,9 +5,17 @@ class SubscriptionsController < ApplicationController # don't show link to subscribe if they are already subscribed? customer = Customer.find_by_user_id(current_user.id) - @payment_method_token = customer.default_credit_card.token - @plans = Braintree::Plan.all + if subscription = customer.single_subscription + redirect_to subscription_path(subscription.id) + else + credit_card = customer.default_credit_card #safe to assume default? + @payment_method_token = credit_card.token + @plans = Braintree::Plan.all + end + end + def show + @subscription = Braintree::Subscription.find params[:id] end diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index 8085d32..c7f216c 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -5,7 +5,7 @@ class Customer < CouchRest::Model::Base use_database "customers" belongs_to :user property :braintree_customer_id - + design do view :by_user_id view :by_braintree_customer_id @@ -35,5 +35,22 @@ class Customer < CouchRest::Model::Base braintree_data.credit_cards.find { |cc| cc.default? } end + #todo will this be plural? + def active_subscriptions(braintree_data=nil) + subscriptions = Array.new + braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id) + braintree_data.credit_cards.each do |cc| + cc.subscriptions.each do |sub| + subscriptions << sub if sub.status == 'Active' + end + end + subscriptions + end + + def single_subscription(braintree_data=nil) + self.active_subscriptions(braintree_data).first + end + + end diff --git a/billing/app/views/customer/_subscription.html.haml b/billing/app/views/customer/_subscription.html.haml index f2a0410..a57f6e9 100644 --- a/billing/app/views/customer/_subscription.html.haml +++ b/billing/app/views/customer/_subscription.html.haml @@ -1,13 +1 @@ -%p - = subscription.id - Balance: - = number_to_currency(subscription.balance) - Bill on: - = subscription.billing_day_of_month - Start date: - = subscription.first_billing_date - Paid through - = subscription.paid_through_date - Price - = subscription.price - - # would be good to get plan name but not sure if that is possible?
\ No newline at end of file += render :partial => "subscriptions/subscription_details", :locals => {:subscription => subscription} diff --git a/billing/app/views/customer/edit.html.haml b/billing/app/views/customer/edit.html.haml index 0f37e61..400c5e2 100644 --- a/billing/app/views/customer/edit.html.haml +++ b/billing/app/views/customer/edit.html.haml @@ -19,8 +19,7 @@ = # (#{link_to @default_cc.token, edit_credit_card_info_path(:id => @default_cc.token)}) - if @subscriptions.any? %li - Active Subscriptions + Active Subscriptions # todo: won't really have multiple subscriptions = render(:partial => "subscription", :collection => @subscriptions) - = hidden_field_tag :tr_data, @tr_data = f.submit 'Save Payment Info'
\ No newline at end of file diff --git a/billing/app/views/subscriptions/_subscription_details.html.haml b/billing/app/views/subscriptions/_subscription_details.html.haml new file mode 100644 index 0000000..f2a0410 --- /dev/null +++ b/billing/app/views/subscriptions/_subscription_details.html.haml @@ -0,0 +1,13 @@ +%p + = subscription.id + Balance: + = number_to_currency(subscription.balance) + Bill on: + = subscription.billing_day_of_month + Start date: + = subscription.first_billing_date + Paid through + = subscription.paid_through_date + Price + = subscription.price + - # would be good to get plan name but not sure if that is possible?
\ No newline at end of file diff --git a/billing/app/views/subscriptions/show.html.haml b/billing/app/views/subscriptions/show.html.haml new file mode 100644 index 0000000..4ee015c --- /dev/null +++ b/billing/app/views/subscriptions/show.html.haml @@ -0,0 +1 @@ += render :partial => "subscription_details", :locals => {:subscription => @subscription} |