From d3e4489cc4833c196eeef77f4fe0680c3f7e7a09 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 11 Jun 2013 12:16:13 -0700 Subject: More cleanup of billing code. --- billing/README.rdoc | 4 ++-- .../app/controllers/subscriptions_controller.rb | 24 ++++++++++++---------- billing/app/views/customer/edit.html.haml | 2 +- billing/app/views/customer/show.html.haml | 10 +++++++-- .../views/payments/_transaction_details.html.haml | 2 +- billing/app/views/payments/new.html.haml | 5 ++++- .../subscriptions/_subscription_details.html.haml | 12 +++++++---- billing/app/views/subscriptions/create.html.haml | 1 + billing/app/views/subscriptions/destroy.html.haml | 4 +++- billing/app/views/subscriptions/index.html.haml | 7 ++++++- billing/app/views/subscriptions/show.html.haml | 5 ++++- 11 files changed, 51 insertions(+), 25 deletions(-) (limited to 'billing') diff --git a/billing/README.rdoc b/billing/README.rdoc index fbf5b51..357c02e 100644 --- a/billing/README.rdoc +++ b/billing/README.rdoc @@ -10,14 +10,14 @@ In the top right, navigate to your username, and then 'My User' -> 'API Keys' Click the button to generate a new API key, and then click the 'View' link to the right of the key. -There is a section to select a snippet of code. Select 'Ruby' in the dropdown, and then the button to the right to copy this code to your clipboard. +There is a section to copy a snippet of code. Select 'Ruby' in the dropdown, and then the button to the right to copy this code to your clipboard. Then, paste the contents of the clipboard into config/initializers/braintree.rb You should not check the private key into version control. Now, you should be able to add charges to your own Sandbox when you run the webapp locally. -You also will want to add a Plan to your Sandbox. Within the Braintree Sandbox, navigate to 'Recurring Billing' -> 'Plans'. From here, you can add a new Plan. The values of the test plan are not important. +You also will want to add a Plan to your Sandbox. Within the Braintree Sandbox, navigate to 'Recurring Billing' -> 'Plans'. From here, you can add a new Plan. The values of the test plan are not important, but the ID will be displayed, so should pick something descriptive. Here are credit cared numbers to try in the Sandbox: diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 9217979..366a457 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -1,18 +1,13 @@ class SubscriptionsController < ApplicationController before_filter :authorize before_filter :fetch_subscription, :only => [:show, :destroy] + before_filter :confirm_no_active_subscription, :only => [:new, :create] def new # don't show link to subscribe if they are already subscribed? - customer = Customer.find_by_user_id(current_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' - else - credit_card = customer.default_credit_card #safe to assume default? - @payment_method_token = credit_card.token - @plans = Braintree::Plan.all - end + credit_card = @customer.default_credit_card #safe to assume default? + @payment_method_token = credit_card.token + @plans = Braintree::Plan.all end # show has no content, so not needed at this point. @@ -35,9 +30,16 @@ class SubscriptionsController < ApplicationController def fetch_subscription @subscription = Braintree::Subscription.find params[:id] @subscription_customer_id = @subscription.transactions.first.customer_details.id #all of subscriptions transactions should have same customer - customer = Customer.find_by_user_id(current_user.id) - access_denied unless customer and customer.braintree_customer_id == @subscription_customer_id + @customer = Customer.find_by_user_id(current_user.id) + access_denied unless @customer and @customer.braintree_customer_id == @subscription_customer_id # TODO: will presumably want to allow admins to view/cancel subscriptions for all users end + def confirm_no_active_subscription + @customer = Customer.find_by_user_id(current_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' + end + end + end diff --git a/billing/app/views/customer/edit.html.haml b/billing/app/views/customer/edit.html.haml index c2e5cb3..f9c1383 100644 --- a/billing/app/views/customer/edit.html.haml +++ b/billing/app/views/customer/edit.html.haml @@ -19,4 +19,4 @@ = link_to 'Change credit card', edit_credit_card_info_path(:id => @default_cc.token), :class => :btn = hidden_field_tag :tr_data, @tr_data = f.submit 'Save Customer Info', :class => :btn -= link_to 'Cancel', show_customer_path(@braintree_data.id), :class=> :btn \ No newline at end of file += link_to 'Show Customer Information', show_customer_path(@braintree_data.id), :class=> :btn \ No newline at end of file diff --git a/billing/app/views/customer/show.html.haml b/billing/app/views/customer/show.html.haml index d385d98..8ef5517 100644 --- a/billing/app/views/customer/show.html.haml +++ b/billing/app/views/customer/show.html.haml @@ -8,7 +8,13 @@ = render :partial => "payments/transaction_details", :locals => {:transaction => t} - counter += 1 = link_to 'Transaction History', payments_path +%h3 Subscriptions - if @active_subscription - %h3 Active Subscription = render :partial => "subscriptions/subscription_details", :locals => {:subscription => @active_subscription} - = link_to 'All subscriptions', subscriptions_path \ No newline at end of file +- else + %p + No active subscription + %p + = link_to 'subscribe to plan', new_subscription_path, :class => :btn +%p + = link_to 'All subscriptions', subscriptions_path diff --git a/billing/app/views/payments/_transaction_details.html.haml b/billing/app/views/payments/_transaction_details.html.haml index 53483d9..030639e 100644 --- a/billing/app/views/payments/_transaction_details.html.haml +++ b/billing/app/views/payments/_transaction_details.html.haml @@ -7,7 +7,7 @@ Status: = transaction.status Date - = transaction.created_at + = transaction.created_at.strftime("%Y-%m-%d") - if sub_start = transaction.subscription_details.billing_period_start_date From subscription which started = sub_start diff --git a/billing/app/views/payments/new.html.haml b/billing/app/views/payments/new.html.haml index d1d0aa9..14f6697 100644 --- a/billing/app/views/payments/new.html.haml +++ b/billing/app/views/payments/new.html.haml @@ -1,9 +1,12 @@ %h1 Payment -- if @result +- if @result and @result.errors.size > 0 %div{:style => "color: red;"} = h @result.errors.size error(s) +- if @result and @result.transaction.status == 'processor_declined' + %div{:style => "color: red;"} + Processor Declined = form_for :transaction, :params => @result && @result.params[:transaction], :errors => @result && @result.errors.for(:transaction), :builder => BraintreeFormHelper::BraintreeFormBuilder, :url => Braintree::TransparentRedirect.url, :html => {:autocomplete => "off"} do |f| #TODO: add helper = f.label :amount, "Amount" = f.text_field :amount diff --git a/billing/app/views/subscriptions/_subscription_details.html.haml b/billing/app/views/subscriptions/_subscription_details.html.haml index db9d75b..fb18210 100644 --- a/billing/app/views/subscriptions/_subscription_details.html.haml +++ b/billing/app/views/subscriptions/_subscription_details.html.haml @@ -6,10 +6,14 @@ = subscription.billing_day_of_month Start date: = subscription.first_billing_date - Paid through + Paid through: = subscription.paid_through_date - Price + Plan: + = subscription.plan_id + Price: = subscription.price - Status - = subscription.status + - color = (subscription.status == 'Active') ? "green" : "red" + Status: + %font{:color => color} + = subscription.status - # 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/create.html.haml b/billing/app/views/subscriptions/create.html.haml index e0585ab..2b6c5e9 100644 --- a/billing/app/views/subscriptions/create.html.haml +++ b/billing/app/views/subscriptions/create.html.haml @@ -2,6 +2,7 @@ %h1 Subscription Status = @result.subscription.status + = render :partial => "subscription_details", :locals => {:subscription => @result.subscription} - else %h1 Error: diff --git a/billing/app/views/subscriptions/destroy.html.haml b/billing/app/views/subscriptions/destroy.html.haml index f888e6d..e7ed6e8 100644 --- a/billing/app/views/subscriptions/destroy.html.haml +++ b/billing/app/views/subscriptions/destroy.html.haml @@ -2,4 +2,6 @@ Subscription destroyed - else Error: - = @result.message \ No newline at end of file + = @result.message +%p + = link_to 'Customer Information', show_customer_path(@customer.braintree_customer_id), :class=> :btn \ No newline at end of file diff --git a/billing/app/views/subscriptions/index.html.haml b/billing/app/views/subscriptions/index.html.haml index c885f90..0e84619 100644 --- a/billing/app/views/subscriptions/index.html.haml +++ b/billing/app/views/subscriptions/index.html.haml @@ -1,2 +1,7 @@ +- active = false - @subscriptions.each do |s| - = render :partial => "subscription_details", :locals => {:subscription => s} \ No newline at end of file + - if s.status == 'Active' + - active = true + = render :partial => "subscription_details", :locals => {:subscription => s} +- if !active + = 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 a3d57f9..6f108be 100644 --- a/billing/app/views/subscriptions/show.html.haml +++ b/billing/app/views/subscriptions/show.html.haml @@ -1,4 +1,7 @@ -%h1 Current Subscription +%h1 + - if @subscription.status == 'Active' + Current + Subscription = render :partial => "subscription_details", :locals => {:subscription => @subscription} = link_to 'Cancel Subscription', subscription_path, :confirm => 'Are you sure you want to cancel this subscription?', :method => :delete, :class => 'btn btn-danger' if @subscription.status == 'Active' # permission check or should that just be on show? = link_to 'Show Customer Data', show_customer_path(@subscription_customer_id), :class => :btn \ No newline at end of file -- cgit v1.2.3