From 0fe1678cd37c8e917cb28eed9eb28777d3a92283 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 1 Oct 2013 13:56:59 -0700 Subject: Allow admins to view past-due subscriptions. --- app/views/layouts/_navigation.html.haml | 2 +- .../app/controllers/billing_admin_controller.rb | 14 +++++++++++ billing/app/helpers/billing_helper.rb | 27 ++++++++++++++++++++++ billing/app/views/billing_admin/show.html.haml | 17 ++++++++++++++ .../subscriptions/_subscription_details.html.haml | 11 ++++++++- billing/config/routes.rb | 1 + users/app/views/overviews/show.html.haml | 2 +- 7 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 billing/app/controllers/billing_admin_controller.rb create mode 100644 billing/app/views/billing_admin/show.html.haml diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index b89655f..992aa46 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -3,5 +3,5 @@ = link_to_navigation t(:account_settings), edit_user_path(@user), :active => controller?(:users) - # will want link for identity settings = link_to_navigation t(:support_tickets), auto_tickets_path, :active => controller?(:tickets) - = link_to_navigation t(:billing_settings), show_or_new_customer_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:payment].present? + = link_to_navigation t(:billing_settings), billing_top_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:payment].present? = link_to_navigation t(:logout), logout_path, :method => :delete diff --git a/billing/app/controllers/billing_admin_controller.rb b/billing/app/controllers/billing_admin_controller.rb new file mode 100644 index 0000000..2a5165c --- /dev/null +++ b/billing/app/controllers/billing_admin_controller.rb @@ -0,0 +1,14 @@ +class BillingAdminController < BillingBaseController + before_filter :authorize_admin + + def show + @past_due_atleast_90_days = Braintree::Subscription.search do |search| + search.days_past_due >= 90 + end + + @all_past_due = Braintree::Subscription.search do |search| + search.status.is Braintree::Subscription::Status::PastDue + end + end + +end diff --git a/billing/app/helpers/billing_helper.rb b/billing/app/helpers/billing_helper.rb index 3c0691f..1dd3f38 100644 --- a/billing/app/helpers/billing_helper.rb +++ b/billing/app/helpers/billing_helper.rb @@ -9,6 +9,15 @@ module BillingHelper form_for object, options, &block end + def billing_top_link(user) + # for admins, top link will show special admin information, which has link to show their own customer information + if (admin? and user == current_user) + billing_admin_path + else + show_or_new_customer_link(user) + end + end + def show_or_new_customer_link(user) # Link to show if user is admin viewing another user, or user is already a customer. # Otherwise link to create a new customer. @@ -19,4 +28,22 @@ module BillingHelper end end + # a bit strange to put here, but we don't have a subscription model + def user_for_subscription(subscription) + + if (transaction = subscription.transactions.first) + # much quicker, but will only work if there is already a transaction associated with subscription (should generally be) + braintree_customer = transaction.customer_details + else + search_results = Braintree::Customer.search do |search| + search.payment_method_token.is subscription.payment_method_token + end + braintree_customer = search_results.first + end + + customer = Customer.find_by_braintree_customer_id(braintree_customer.id) + user = User.find(customer.user_id) + + end + end diff --git a/billing/app/views/billing_admin/show.html.haml b/billing/app/views/billing_admin/show.html.haml new file mode 100644 index 0000000..78843e5 --- /dev/null +++ b/billing/app/views/billing_admin/show.html.haml @@ -0,0 +1,17 @@ +- # todo: move into helper, as following 2 are pretty much identical +%legend= t(:more_than_90_days_past_due) +- if @past_due_atleast_90_days.empty? + = t(:none) +- else + - @past_due_atleast_90_days.each do |past_due_subscription| + = render :partial => "subscriptions/subscription_details", :locals => {:subscription => past_due_subscription, :show_user => user_for_subscription(past_due_subscription)} + +%legend= t(:all_past_due) +- if @all_past_due.empty? + = t(:none) +- else + - @all_past_due.each do |past_due_subscription| + = render :partial => "subscriptions/subscription_details", :locals => {:subscription => past_due_subscription, :show_user => user_for_subscription(past_due_subscription)} + +%legend= t(:your_settings) += link_to 'view own billing settings', show_or_new_customer_link(current_user) \ 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 index 6eda7ca..27b00c7 100644 --- a/billing/app/views/subscriptions/_subscription_details.html.haml +++ b/billing/app/views/subscriptions/_subscription_details.html.haml @@ -1,7 +1,16 @@ %p + - if local_assigns[:show_user] + User: + = link_to show_user.login, user_overview_path(show_user) + ID: = link_to subscription.id, user_subscription_path(@user, subscription.id) Balance: - = number_to_currency(subscription.balance) + - subscription_balance_currency = number_to_currency(subscription.balance) + - if subscription.balance > 0 + %font{:color => "red"} + = subscription_balance_currency + - else + = subscription_balance_currency Bill on: = subscription.billing_day_of_month Start date: diff --git a/billing/config/routes.rb b/billing/config/routes.rb index e024f43..dbdc24b 100644 --- a/billing/config/routes.rb +++ b/billing/config/routes.rb @@ -15,6 +15,7 @@ Rails.application.routes.draw do match 'credit_card_info/confirm' => 'credit_card_info#confirm', :as => :confirm_credit_card_info resources :subscriptions, :only => [:new, :create, :update] # index, show & destroy are within users path + match 'billing_admin' => 'billing_admin#show', :as => :billing_admin #match 'transactions/:product_id/new' => 'transactions#new', :as => :new_transaction #match 'transactions/confirm/:product_id' => 'transactions#confirm', :as => :confirm_transaction diff --git a/users/app/views/overviews/show.html.haml b/users/app/views/overviews/show.html.haml index d3409df..7bea370 100644 --- a/users/app/views/overviews/show.html.haml +++ b/users/app/views/overviews/show.html.haml @@ -19,4 +19,4 @@ %li= icon('user') + link_to(t(:overview_account), edit_user_path(@user)) - # %li= icon('envelope') + link_to(t(:overview_email), {insert path for user identities, presuambly} %li= icon('question-sign') + link_to(t(:overview_tickets), user_tickets_path(@user)) - %li= icon('shopping-cart') + link_to(t(:overview_billing), show_or_new_customer_link(@user)) if APP_CONFIG[:payment].present? + %li= icon('shopping-cart') + link_to(t(:overview_billing), billing_top_link(@user)) if APP_CONFIG[:payment].present? -- cgit v1.2.3 From e4d422142fb2db2153916bed5826651e8418b7a0 Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 3 Oct 2013 12:06:57 -0700 Subject: Some refactoring of displayed of past-due subscriptions. --- billing/app/helpers/billing_helper.rb | 13 +++++++++++++ billing/app/views/billing_admin/show.html.haml | 14 +++----------- .../views/subscriptions/_subscription_details.html.haml | 9 +++------ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/billing/app/helpers/billing_helper.rb b/billing/app/helpers/billing_helper.rb index 1dd3f38..68ed5b8 100644 --- a/billing/app/helpers/billing_helper.rb +++ b/billing/app/helpers/billing_helper.rb @@ -46,4 +46,17 @@ module BillingHelper end + def show_set_user_subscriptions(set) + if set.empty? + return t(:none) + else + subscriptions_to_display = '' + set.each do |past_due_subscription| + subscriptions_to_display += render :partial => "subscriptions/subscription_details", :locals => {:subscription => past_due_subscription, :show_user => user_for_subscription(past_due_subscription)} + end + subscriptions_to_display.html_safe + end + end + + end diff --git a/billing/app/views/billing_admin/show.html.haml b/billing/app/views/billing_admin/show.html.haml index 78843e5..a275a5e 100644 --- a/billing/app/views/billing_admin/show.html.haml +++ b/billing/app/views/billing_admin/show.html.haml @@ -1,17 +1,9 @@ -- # todo: move into helper, as following 2 are pretty much identical +- # todo: move into helper, as following 2 are pretty much identical (show_set_user_subscriptions is started) %legend= t(:more_than_90_days_past_due) -- if @past_due_atleast_90_days.empty? - = t(:none) -- else - - @past_due_atleast_90_days.each do |past_due_subscription| - = render :partial => "subscriptions/subscription_details", :locals => {:subscription => past_due_subscription, :show_user => user_for_subscription(past_due_subscription)} += show_set_user_subscriptions(@past_due_atleast_90_days) %legend= t(:all_past_due) -- if @all_past_due.empty? - = t(:none) -- else - - @all_past_due.each do |past_due_subscription| - = render :partial => "subscriptions/subscription_details", :locals => {:subscription => past_due_subscription, :show_user => user_for_subscription(past_due_subscription)} += show_set_user_subscriptions(@all_past_due) %legend= t(:your_settings) = link_to 'view own billing settings', show_or_new_customer_link(current_user) \ 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 index 27b00c7..fcf4bc4 100644 --- a/billing/app/views/subscriptions/_subscription_details.html.haml +++ b/billing/app/views/subscriptions/_subscription_details.html.haml @@ -5,12 +5,9 @@ ID: = link_to subscription.id, user_subscription_path(@user, subscription.id) Balance: - - subscription_balance_currency = number_to_currency(subscription.balance) - - if subscription.balance > 0 - %font{:color => "red"} - = subscription_balance_currency - - else - = subscription_balance_currency + - color = (subscription.balance > 0) ? "red" : "" + %font{:color => color} + = number_to_currency(subscription.balance) Bill on: = subscription.billing_day_of_month Start date: -- cgit v1.2.3 From 72af3e2efd8e0c8c399f0765da8bc6d1597fcb6b Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 3 Oct 2013 12:08:50 -0700 Subject: And removing comment.. --- billing/app/views/billing_admin/show.html.haml | 1 - 1 file changed, 1 deletion(-) diff --git a/billing/app/views/billing_admin/show.html.haml b/billing/app/views/billing_admin/show.html.haml index a275a5e..3881dc7 100644 --- a/billing/app/views/billing_admin/show.html.haml +++ b/billing/app/views/billing_admin/show.html.haml @@ -1,4 +1,3 @@ -- # todo: move into helper, as following 2 are pretty much identical (show_set_user_subscriptions is started) %legend= t(:more_than_90_days_past_due) = show_set_user_subscriptions(@past_due_atleast_90_days) -- cgit v1.2.3 From fe79b34965145f1ece98644bf1537866880cc230 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 7 Oct 2013 15:39:56 -0700 Subject: Fix typo in test name. --- billing/test/functional/subsciptions_controller_test.rb | 16 ---------------- billing/test/functional/subscriptions_controller_test.rb | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) delete mode 100644 billing/test/functional/subsciptions_controller_test.rb create mode 100644 billing/test/functional/subscriptions_controller_test.rb diff --git a/billing/test/functional/subsciptions_controller_test.rb b/billing/test/functional/subsciptions_controller_test.rb deleted file mode 100644 index a6a1057..0000000 --- a/billing/test/functional/subsciptions_controller_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'test_helper' -require 'fake_braintree' - -class SubscriptionsControllerTest < ActionController::TestCase - include CustomerTestHelper - - test "destroy cancels subscription" do - customer = stub_customer - login customer.user - result = Braintree::Subscription.create plan_id: 'my_plan', - payment_method_token: customer.braintree_customer.credit_cards.first.token - subscription = result.subscription - delete :destroy, id: subscription.id, user_id: customer.user.id - assert_equal "Canceled", Braintree::Subscription.find(subscription.id).status - end -end diff --git a/billing/test/functional/subscriptions_controller_test.rb b/billing/test/functional/subscriptions_controller_test.rb new file mode 100644 index 0000000..a6a1057 --- /dev/null +++ b/billing/test/functional/subscriptions_controller_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' +require 'fake_braintree' + +class SubscriptionsControllerTest < ActionController::TestCase + include CustomerTestHelper + + test "destroy cancels subscription" do + customer = stub_customer + login customer.user + result = Braintree::Subscription.create plan_id: 'my_plan', + payment_method_token: customer.braintree_customer.credit_cards.first.token + subscription = result.subscription + delete :destroy, id: subscription.id, user_id: customer.user.id + assert_equal "Canceled", Braintree::Subscription.find(subscription.id).status + end +end -- cgit v1.2.3 From 8b378d916caeaf7fd4b1da2aea45eab4b0ccbb39 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 8 Oct 2013 10:44:50 -0700 Subject: Minor tweaks. --- billing/app/views/subscriptions/_subscription_details.html.haml | 2 +- billing/test/functional/customers_controller_test.rb | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/billing/app/views/subscriptions/_subscription_details.html.haml b/billing/app/views/subscriptions/_subscription_details.html.haml index fcf4bc4..3a06c20 100644 --- a/billing/app/views/subscriptions/_subscription_details.html.haml +++ b/billing/app/views/subscriptions/_subscription_details.html.haml @@ -17,7 +17,7 @@ Plan: = subscription.plan_id Price: - = subscription.price + = number_to_currency(subscription.price) - color = (subscription.status == 'Active') ? "green" : "red" Status: %font{:color => color} diff --git a/billing/test/functional/customers_controller_test.rb b/billing/test/functional/customers_controller_test.rb index d4881bf..46c33c9 100644 --- a/billing/test/functional/customers_controller_test.rb +++ b/billing/test/functional/customers_controller_test.rb @@ -7,10 +7,11 @@ class CustomersControllerTest < ActionController::TestCase setup do @user = FactoryGirl.create :user @other_user = FactoryGirl.create :user - FakeBraintree.clear! - FakeBraintree.verify_all_cards! + #FakeBraintree.clear! + #FakeBraintree.verify_all_cards! testid = 'testid' - FakeBraintree::Customer.new({:credit_cards => [{:number=>"5105105105105100", :expiration_date=>"05/2013"}]}, {:id => testid, :merchant_id => Braintree::Configuration.merchant_id}) + #this wasn't actually being used + #FakeBraintree::Customer.new({:credit_cards => [{:number=>"5105105105105100", :expiration_date=>"05/2013"}]}, {:id => testid, :merchant_id => Braintree::Configuration.merchant_id}) # any reason to call the create instance method on the FakeBraintree::Customer ? @customer = Customer.new(:user_id => @other_user.id) @customer.braintree_customer_id = testid @@ -50,6 +51,7 @@ class CustomersControllerTest < ActionController::TestCase test "show" do + skip "show customer" login @other_user # Below will fail, as when we go to fetch the customer data, Braintree::Customer.find(params[:id]) won't find the customer as it is a FakeBraintree customer. #get :show, :id => @customer.braintree_customer_id -- cgit v1.2.3 From 4e471f6b35c012d2825f6be19e24ecd5fef8d636 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 8 Oct 2013 14:33:02 -0700 Subject: Consider pending & past due subscriptions as 'active' in the sense that they should prevent one from adding a new subscription. --- billing/app/controllers/subscriptions_controller.rb | 6 +++--- billing/app/models/customer.rb | 6 +++--- billing/app/views/customer/show.html.haml | 2 +- billing/app/views/subscriptions/index.html.haml | 8 ++++---- billing/app/views/subscriptions/show.html.haml | 2 +- billing/config/locales/en.yml | 3 ++- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 7689f35..4758adb 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -1,7 +1,7 @@ class SubscriptionsController < BillingBaseController before_filter :authorize before_filter :fetch_subscription, :only => [:show, :destroy] - before_filter :confirm_no_active_subscription, :only => [:new, :create] + before_filter :confirm_no_pending_active_pastdue_subscription, :only => [:new, :create] # for now, admins cannot create or destroy subscriptions for others: before_filter :confirm_self, :only => [:new, :create] @@ -38,10 +38,10 @@ class SubscriptionsController < BillingBaseController end - def confirm_no_active_subscription + def confirm_no_pending_active_pastdue_subscription @customer = Customer.find_by_user_id(@user.id) if subscription = @customer.subscriptions # will return active subscription, if it exists - redirect_to subscription_path(subscription.id), :notice => 'You already have an active subscription' + redirect_to user_subscription_path(@user, subscription.id), :notice => 'You already have a subscription' end end diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index f01c300..1acc7a5 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -40,19 +40,19 @@ class Customer < CouchRest::Model::Base end # based on 2nd parameter, either returns the single active subscription (or nil if there isn't one), or an array of all subsciptions - def subscriptions(braintree_data=nil, only_active=true) + def subscriptions(braintree_data=nil, only_pending_active_pastdue=true) self.with_braintree_data! return unless has_payment_info? subscriptions = [] self.default_credit_card.subscriptions.each do |sub| - if only_active and sub.status == 'Active' + if only_pending_active_pastdue and ['Pending', 'Active','Past Due'].include? sub.status return sub else subscriptions << sub end end - only_active ? nil : subscriptions + only_pending_active_pastdue ? nil : subscriptions end end diff --git a/billing/app/views/customer/show.html.haml b/billing/app/views/customer/show.html.haml index 243bd3b..562dc4b 100644 --- a/billing/app/views/customer/show.html.haml +++ b/billing/app/views/customer/show.html.haml @@ -18,7 +18,7 @@ = render :partial => "subscriptions/subscription_details", :locals => {:subscription => @active_subscription} - else %p - = t(:no_active_subscription) + = t(:no_relevant_subscription) - if current_user == @user %p .form-actions diff --git a/billing/app/views/subscriptions/index.html.haml b/billing/app/views/subscriptions/index.html.haml index 87771e5..3d4e8fd 100644 --- a/billing/app/views/subscriptions/index.html.haml +++ b/billing/app/views/subscriptions/index.html.haml @@ -1,8 +1,8 @@ %h2=t :all_subscriptions -- active = false +- pending_active_pastdue = false - @subscriptions.each do |s| - - if s.status == 'Active' - - active = true + - if ['Pending', 'Active','Past Due'].include? s.status + - pending_active_pastdue = true = render :partial => "subscription_details", :locals => {:subscription => s} -- if !active and @user == current_user +- if !pending_active_pastdue and @user == current_user = link_to 'subscribe to plan', new_subscription_path, :class => :btn \ No newline at end of file diff --git a/billing/app/views/subscriptions/show.html.haml b/billing/app/views/subscriptions/show.html.haml index 39f4d1a..b258e47 100644 --- a/billing/app/views/subscriptions/show.html.haml +++ b/billing/app/views/subscriptions/show.html.haml @@ -3,4 +3,4 @@ Current Subscription = render :partial => "subscription_details", :locals => {:subscription => @subscription} -= link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if @subscription.status == 'Active' # permission check or should that just be on show? += link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if ['Active', 'Pending'].include? @subscription.status # permission check or should that just be on show? # should you be able to cancel pending subscription? diff --git a/billing/config/locales/en.yml b/billing/config/locales/en.yml index 5245b17..952cfd6 100644 --- a/billing/config/locales/en.yml +++ b/billing/config/locales/en.yml @@ -2,4 +2,5 @@ en: create_new_customer: "Create a new Braintree Customer" must_create_customer: "You must store a customer in braintree before subscribing to a plan" subscribe: "Subscribe" - save_customer_info: "Save Customer Information" \ No newline at end of file + save_customer_info: "Save Customer Information" + no_relevant_subscription: "No subscription which is Active, Pending, or Past Due" \ No newline at end of file -- cgit v1.2.3 From 51f93fc87c9cadbe52877ddc3e7c5fd07866b397 Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 10 Oct 2013 11:35:26 -0700 Subject: Admins can cancel pastdue subscriptions, but users cannot cancel their own pastdue subscription, as then admins won't be able to search for them. --- billing/app/controllers/billing_admin_controller.rb | 1 + billing/app/controllers/subscriptions_controller.rb | 7 ++++++- billing/app/views/subscriptions/show.html.haml | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/billing/app/controllers/billing_admin_controller.rb b/billing/app/controllers/billing_admin_controller.rb index 2a5165c..419a937 100644 --- a/billing/app/controllers/billing_admin_controller.rb +++ b/billing/app/controllers/billing_admin_controller.rb @@ -8,6 +8,7 @@ class BillingAdminController < BillingBaseController @all_past_due = Braintree::Subscription.search do |search| search.status.is Braintree::Subscription::Status::PastDue + #cannot search by balance. end end diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 4758adb..3fd5ae5 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -1,6 +1,7 @@ class SubscriptionsController < BillingBaseController before_filter :authorize before_filter :fetch_subscription, :only => [:show, :destroy] + before_filter :only_admin_active_pending, :only => [:destroy] before_filter :confirm_no_pending_active_pastdue_subscription, :only => [:new, :create] # for now, admins cannot create or destroy subscriptions for others: before_filter :confirm_self, :only => [:new, :create] @@ -38,9 +39,13 @@ class SubscriptionsController < BillingBaseController end + def only_admin_active_pending + access_denied unless admin? or ['Pending', 'Active'].include? @subscription.status + end + def confirm_no_pending_active_pastdue_subscription @customer = Customer.find_by_user_id(@user.id) - if subscription = @customer.subscriptions # will return active subscription, if it exists + if subscription = @customer.subscriptions # will return pending, active or pastdue subscription, if it exists redirect_to user_subscription_path(@user, subscription.id), :notice => 'You already have a subscription' end end diff --git a/billing/app/views/subscriptions/show.html.haml b/billing/app/views/subscriptions/show.html.haml index b258e47..f4d644a 100644 --- a/billing/app/views/subscriptions/show.html.haml +++ b/billing/app/views/subscriptions/show.html.haml @@ -3,4 +3,4 @@ Current Subscription = render :partial => "subscription_details", :locals => {:subscription => @subscription} -= link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if ['Active', 'Pending'].include? @subscription.status # permission check or should that just be on show? # should you be able to cancel pending subscription? += link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if ['Active', 'Pending'].include? @subscription.status or admin? # permission check or should that just be on show? # should you be able to cancel pending subscription? -- cgit v1.2.3 From a6f32017f5c7802798f10e2f4041037fb5684def Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 15 Oct 2013 15:08:35 -0700 Subject: Add permissions to subscriptions index, and fix test to stub the subscription's balance. --- billing/app/controllers/subscriptions_controller.rb | 6 ++++++ billing/test/integration/subscription_test.rb | 12 +++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 3fd5ae5..0a1c733 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -2,6 +2,7 @@ class SubscriptionsController < BillingBaseController before_filter :authorize before_filter :fetch_subscription, :only => [:show, :destroy] before_filter :only_admin_active_pending, :only => [:destroy] + before_filter :confirm_self_or_admin, :only => [:index] before_filter :confirm_no_pending_active_pastdue_subscription, :only => [:new, :create] # for now, admins cannot create or destroy subscriptions for others: before_filter :confirm_self, :only => [:new, :create] @@ -17,6 +18,7 @@ class SubscriptionsController < BillingBaseController def create @result = Braintree::Subscription.create( :payment_method_token => params[:payment_method_token], :plan_id => params[:plan_id] ) + #if you want to test pastdue, can add :price => '2001', :trial_period => true,:trial_duration => 1,:trial_duration_unit => "day" and then wait a day end def destroy @@ -54,4 +56,8 @@ class SubscriptionsController < BillingBaseController @user == current_user end + def confirm_self_or_admin + access_denied unless confirm_self or admin? + end + end diff --git a/billing/test/integration/subscription_test.rb b/billing/test/integration/subscription_test.rb index b893896..6356177 100644 --- a/billing/test/integration/subscription_test.rb +++ b/billing/test/integration/subscription_test.rb @@ -10,28 +10,34 @@ class SubscriptionTest < ActionDispatch::IntegrationTest setup do Warden.test_mode! - @admin = stub_record :user, :admin => true + @admin = User.find_by_login('admin') || FactoryGirl.create(:user, login: 'admin') @customer = stub_customer @braintree_customer = @customer.braintree_customer response = Braintree::Subscription.create plan_id: '5', - payment_method_token: @braintree_customer.credit_cards.first.token + payment_method_token: @braintree_customer.credit_cards.first.token, + price: '10' @subscription = response.subscription Capybara.current_driver = Capybara.javascript_driver end teardown do Warden.test_reset! + @admin.destroy end - test "admin can see subscription for another" do + test "admin can see all subscriptions for another" do login_as @admin @customer.stubs(:subscriptions).returns([@subscription]) + @subscription.stubs(:balance).returns 0 visit user_subscriptions_path(@customer.user_id) assert page.has_content?("Subscriptions") assert page.has_content?("Status: Active") page.save_screenshot('/tmp/subscriptions.png') end + # test "user cannot see all subscriptions for other user" do + #end + #test "admin cannot add subscription for another" do #end -- cgit v1.2.3 From 92cb054d53aaac6864a6a805d9cdd3919f4a38bc Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 17 Oct 2013 13:58:54 -0700 Subject: Some cleanup of code to deal with past due subscriptions. --- .../app/controllers/billing_admin_controller.rb | 18 +++++++++++++++-- .../app/controllers/subscriptions_controller.rb | 6 +++--- billing/app/helpers/billing_helper.rb | 23 ++++++---------------- billing/app/views/billing_admin/show.html.haml | 5 ++--- .../subscriptions/_subscription_details.html.haml | 3 ++- billing/app/views/subscriptions/show.html.haml | 2 +- 6 files changed, 30 insertions(+), 27 deletions(-) diff --git a/billing/app/controllers/billing_admin_controller.rb b/billing/app/controllers/billing_admin_controller.rb index 419a937..cd6149f 100644 --- a/billing/app/controllers/billing_admin_controller.rb +++ b/billing/app/controllers/billing_admin_controller.rb @@ -2,14 +2,28 @@ class BillingAdminController < BillingBaseController before_filter :authorize_admin def show - @past_due_atleast_90_days = Braintree::Subscription.search do |search| + + br_atleast_90_days = Braintree::Subscription.search do |search| search.days_past_due >= 90 end + @past_due_atleast_90_days = braintree_resource_collection_to_array(br_atleast_90_days) - @all_past_due = Braintree::Subscription.search do |search| + br_all_past_due = Braintree::Subscription.search do |search| search.status.is Braintree::Subscription::Status::PastDue #cannot search by balance. end + @all_past_due = braintree_resource_collection_to_array(br_all_past_due) + + end + + private + + def braintree_resource_collection_to_array(braintree_resource_collection) + array = [] + braintree_resource_collection.each do |object| + array << object + end + array end end diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 0a1c733..01aaab4 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -1,7 +1,7 @@ class SubscriptionsController < BillingBaseController before_filter :authorize before_filter :fetch_subscription, :only => [:show, :destroy] - before_filter :only_admin_active_pending, :only => [:destroy] + before_filter :confirm_cancel_subscription, :only => [:destroy] before_filter :confirm_self_or_admin, :only => [:index] before_filter :confirm_no_pending_active_pastdue_subscription, :only => [:new, :create] # for now, admins cannot create or destroy subscriptions for others: @@ -41,8 +41,8 @@ class SubscriptionsController < BillingBaseController end - def only_admin_active_pending - access_denied unless admin? or ['Pending', 'Active'].include? @subscription.status + def confirm_cancel_subscription + access_denied unless view_context.allow_cancel_subscription(@subscription) end def confirm_no_pending_active_pastdue_subscription diff --git a/billing/app/helpers/billing_helper.rb b/billing/app/helpers/billing_helper.rb index 68ed5b8..b9e5e2e 100644 --- a/billing/app/helpers/billing_helper.rb +++ b/billing/app/helpers/billing_helper.rb @@ -33,30 +33,19 @@ module BillingHelper if (transaction = subscription.transactions.first) # much quicker, but will only work if there is already a transaction associated with subscription (should generally be) - braintree_customer = transaction.customer_details + braintree_customer_id = transaction.customer_details.id else - search_results = Braintree::Customer.search do |search| - search.payment_method_token.is subscription.payment_method_token - end - braintree_customer = search_results.first + credit_card = Braintree::CreditCard.find(subscription.payment_method_token) + braintree_customer_id = credit_card.customer_id end - customer = Customer.find_by_braintree_customer_id(braintree_customer.id) + customer = Customer.find_by_braintree_customer_id(braintree_customer_id) user = User.find(customer.user_id) end - def show_set_user_subscriptions(set) - if set.empty? - return t(:none) - else - subscriptions_to_display = '' - set.each do |past_due_subscription| - subscriptions_to_display += render :partial => "subscriptions/subscription_details", :locals => {:subscription => past_due_subscription, :show_user => user_for_subscription(past_due_subscription)} - end - subscriptions_to_display.html_safe - end + def allow_cancel_subscription(subscription) + ['Active', 'Pending'].include? subscription.status or (admin? and subscription.status == 'Past Due') end - end diff --git a/billing/app/views/billing_admin/show.html.haml b/billing/app/views/billing_admin/show.html.haml index 3881dc7..0382cf0 100644 --- a/billing/app/views/billing_admin/show.html.haml +++ b/billing/app/views/billing_admin/show.html.haml @@ -1,8 +1,7 @@ %legend= t(:more_than_90_days_past_due) -= show_set_user_subscriptions(@past_due_atleast_90_days) - += render(:partial => "subscriptions/subscription_details", :collection => @past_due_atleast_90_days, :as => 'subscription', :locals => {:show_user => true}) || t(:none) %legend= t(:all_past_due) -= show_set_user_subscriptions(@all_past_due) += render(:partial => "subscriptions/subscription_details", :collection => @all_past_due, :as => 'subscription', :locals => {:show_user => true}) || t(:none) %legend= t(:your_settings) = link_to 'view own billing settings', show_or_new_customer_link(current_user) \ 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 index 3a06c20..6145c95 100644 --- a/billing/app/views/subscriptions/_subscription_details.html.haml +++ b/billing/app/views/subscriptions/_subscription_details.html.haml @@ -1,7 +1,8 @@ %p - if local_assigns[:show_user] User: - = link_to show_user.login, user_overview_path(show_user) + - user_to_show = user_for_subscription(subscription) + = link_to user_to_show.login, user_overview_path(user_to_show) ID: = link_to subscription.id, user_subscription_path(@user, subscription.id) Balance: diff --git a/billing/app/views/subscriptions/show.html.haml b/billing/app/views/subscriptions/show.html.haml index f4d644a..2699db9 100644 --- a/billing/app/views/subscriptions/show.html.haml +++ b/billing/app/views/subscriptions/show.html.haml @@ -3,4 +3,4 @@ Current Subscription = render :partial => "subscription_details", :locals => {:subscription => @subscription} -= link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if ['Active', 'Pending'].include? @subscription.status or admin? # permission check or should that just be on show? # should you be able to cancel pending subscription? += link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if allow_cancel_subscription(@subscription) -- cgit v1.2.3