From b26d10fe7d87b570bd888fa2a2543f3675278f8b Mon Sep 17 00:00:00 2001 From: claucece Date: Fri, 25 Sep 2015 00:04:19 -0500 Subject: add subscriptions --- .../billing/app/controllers/payments_controller.rb | 4 +- .../app/controllers/subscription_controller.rb | 7 -- .../app/controllers/subscriptions_controller.rb | 123 ++++++++------------- engines/billing/app/helpers/billing_helper.rb | 2 +- engines/billing/app/views/payments/new.html.haml | 5 +- .../billing/app/views/subscriptions/index.html.erb | 38 ++++--- .../billing/app/views/subscriptions/show.html.erb | 16 +-- engines/billing/config/locales/en.yml | 7 ++ engines/billing/config/routes.rb | 11 +- 9 files changed, 88 insertions(+), 125 deletions(-) delete mode 100644 engines/billing/app/controllers/subscription_controller.rb (limited to 'engines') diff --git a/engines/billing/app/controllers/payments_controller.rb b/engines/billing/app/controllers/payments_controller.rb index 4a047ad..871f1b4 100644 --- a/engines/billing/app/controllers/payments_controller.rb +++ b/engines/billing/app/controllers/payments_controller.rb @@ -21,9 +21,9 @@ class PaymentsController < BillingBaseController def confirm make_transaction if @result.success? - flash[:success] = "Congratulations! Your transaction has been successfully!" + flash[:success] = I18n.t(:donation_sucess) else - flash[:error] = "Something went wrong while processing your donation. Please try again!" + flash[:error] = I18n.t(:donation_not_sucess) end redirect_to action: :new, locale: params[:locale] end diff --git a/engines/billing/app/controllers/subscription_controller.rb b/engines/billing/app/controllers/subscription_controller.rb deleted file mode 100644 index 5328c48..0000000 --- a/engines/billing/app/controllers/subscription_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class SubscriptionsController < BillingBaseController - -before_filter :require_admin -before_filter :require_login -before_filter :confirm_cancel_subscription, :only => [:destroy] -before_filter :confirm_no_pending_active_pastdue_subscription, :only => [:new, :create] -before_filter :confirm_self, :only => [:new, :create] diff --git a/engines/billing/app/controllers/subscriptions_controller.rb b/engines/billing/app/controllers/subscriptions_controller.rb index 65db21b..1d29cac 100644 --- a/engines/billing/app/controllers/subscriptions_controller.rb +++ b/engines/billing/app/controllers/subscriptions_controller.rb @@ -1,107 +1,72 @@ class SubscriptionsController < BillingBaseController before_filter :require_login - before_filter :confirm_cancel_subscription, :only => [:destroy] + before_filter :assign_user + before_filter :confirm_cancel_subscription, only: [:destroy] + before_filter :generate_client_token, only: [:show] + before_filter :get_braintree_customer, only: [:subscribe] def index - @subscriptions = Braintree::Plan.all + if @user.subscription_id + @subscription = Braintree::Subscription.find @user.subscription_id + @plan = Braintree::Plan.all.select{ |plan| plan.id == @subscription.plan_id }.first + else + @subscriptions = Braintree::Plan.all + end end def show - @subscriptions = Braintree::Plan.all - @subscriptions = Braintree::Plan.all.find params[:subscription_id] - end - - def search - @subscription = Braintree::Subscription.search params[:status] - end - - def new - if current_user.braintree_customer_id - @client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id) - else - @client_token = Braintree::ClientToken.generate - end - @subscriptions = Braintree::Plan.all + @plan = Braintree::Plan.all.select{ |plan| plan.id == params[:id] }.first end - def create - @result = Braintree::Subscription.create( - payment_method_token: braintree_customer.payment_methods.first.token, - plan_id: params[:plan_id], - ) + def subscribe + @result = Braintree::Subscription.create(payment_method_token: @customer.payment_methods.first.token, + plan_id: params[:id]) if @result.success? - flash[:success] = "Congratulations! Your transaction has been successfully!" + @user.update_attributes subscription_id: @result.subscription.id + flash[:success] = I18n.t(:subscription_sucess) else - flash[:error] = "Something went wrong while processing your donation. Please try again!" + flash[:error] = I18n.t(:subscription_not_sucess) end - redirect_to action: :new, locale: params[:locale] + redirect_to action: :index, locale: params[:locale] end - def braintree_customer - if current_user.braintree_customer_id - Braintree::Customer.find current_user.braintree_customer_id + def unsubscribe + @result = Braintree::Subscription.cancel(@user.subscription_id) + if @result.success? + @user.update_attributes subscription_id: nil + flash[:success] = I18n.t(:unsubscription_sucess) else - customer = Braintree::Customer.create(payment_method_nonce: params[:payment_method_nonce]).customer - current_user.update_attributes braintree_customer_id: customer.id - customer + flash[:error] = I18n.t(:unsubscription_not_sucess) end + redirect_to action: :index, locale: params[:locale] end - def confirm - @result = Braintree::Subscription.sale( - payment_method_token: params[:payment_method_nonce], - plan_id: params[:plan_id], - ) + private + def assign_user + @user = current_user end - def _confirm - make_subscription - if @result.success? - flash[:success] = "Congratulations! Your transaction has been successfully!" + def generate_client_token + if current_user.braintree_customer_id + @client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id) else - flash[:error] = "Something went wrong while processing your donation. Please try again!" + @client_token = Braintree::ClientToken.generate end - redirect_to action: :new, locale: params[:locale] end -private - - def make_subscription - unless current_user.has_payment_info? - subs_with_user_info + def get_braintree_customer + if current_user.braintree_customer_id + @customer = Braintree::Customer.find(current_user.braintree_customer_id) else - subs_without_user_info + @customer = Braintree::Customer.create( + payment_method_nonce: params[:payment_method_nonce], + first_name: params[:first_name], + last_name: params[:last_name], + company: params[:company], + email: current_user.email, + phone: params[:phone] + ).customer + current_user.update_attributes braintree_customer_id: @customer.id end end - - def subs_with_user_info - # don't show link to subscribe if they are already subscribed? - @result = Braintree::Subscription.sale( - payment_method_token: params[:payment_method_nonce], - plans_id: Braintree::Plan.all, - customer: { - first_name: params[:first_name], - last_name: params[:last_name], - company: params[:company], - email: current_user.email, - phone: params[:phone] - }, - options: { - store_in_vault: true - }) - current_user.update_attributes(braintree_customer_id: @result.transaction.customer_details.id) if @result.success? - end - - def subs_without_user_info - @result = Braintree::Subscription.sale( - payment_method_token: params[:payment_method_nonce], - plans_id: Braintree::Plan.all - ) - end - - def destroy - @subscriptions = Braintree::Subscription.cancel params[:id] - end - - end diff --git a/engines/billing/app/helpers/billing_helper.rb b/engines/billing/app/helpers/billing_helper.rb index 650c15d..7b9f8bb 100644 --- a/engines/billing/app/helpers/billing_helper.rb +++ b/engines/billing/app/helpers/billing_helper.rb @@ -14,7 +14,7 @@ module BillingHelper if (admin? and user == current_user) billing_admin_path else - :index_subscription + subscriptions_path end end diff --git a/engines/billing/app/views/payments/new.html.haml b/engines/billing/app/views/payments/new.html.haml index 8d5bd5a..67018b2 100644 --- a/engines/billing/app/views/payments/new.html.haml +++ b/engines/billing/app/views/payments/new.html.haml @@ -4,13 +4,12 @@ = form_tag confirm_payment_path, id: "checkout-form" do - if current_user and !current_user.has_payment_info? = render 'customer_form' unless current_user.is_anonymous? - %br/ %p = t(:donation_info) %div{:id => "payment-form" } %div{:id => "coinbase-container-id" } - %input{:name => "amount", :placeholder => "Enter amount", :type => "text"} - %input.btn.btn-primary{:type => "submit", :value => "Donate"} + %input{:name => "amount", :placeholder => "#{t(:donation_amount)}", :type => "text"} + %input.btn.btn-primary{:type => "submit", :value => "#{t(:donate)}"} %script{:src => "https://js.braintreegateway.com/v2/braintree.js"} :javascript var clientToken = "#{@client_token}"; diff --git a/engines/billing/app/views/subscriptions/index.html.erb b/engines/billing/app/views/subscriptions/index.html.erb index 2037de3..688e371 100644 --- a/engines/billing/app/views/subscriptions/index.html.erb +++ b/engines/billing/app/views/subscriptions/index.html.erb @@ -1,33 +1,35 @@

Subscriptions


-
-
Lastest Subscriptions: - <% if params[:search] == "active" %> - <%= render :partial => "subscriptions/subscription_details" %> - <%# Add destroy, route %> - <%=link_to "Unsubscribe", :show_subscription, class: "button" %> -
+

Lastest Subscriptions:

+ <% if @user.subscription_id %> + + +
<% else %> -

No subscriptions

-

Choose subcription:

+

No subscriptions.

+
+

Choose subcription:


- - - - - diff --git a/engines/billing/app/views/subscriptions/show.html.erb b/engines/billing/app/views/subscriptions/show.html.erb index f3c730d..92cf863 100644 --- a/engines/billing/app/views/subscriptions/show.html.erb +++ b/engines/billing/app/views/subscriptions/show.html.erb @@ -1,26 +1,22 @@

New Subscription

-
-<%= simple_form_for :subscription, :url => :subscriptions, :id => "checkout-form-#{subscription.id}" do |f| %> - +<%= simple_form_for :subscription, :url => subscribe_subscription_path(@plan.id), :id => "checkout-form" do |f| %> <% if current_user and !current_user.has_payment_info? %> <%= render 'customer_form'%> <% end %>

Please enter payment details:

-
+
+
- <%= f.submit t(:choose), :class => 'btn btn-primary' %> + <%= f.submit t(:subscribe), :class => 'btn btn-primary' %>
- <% end %> -
-
- +<% end %> diff --git a/engines/billing/config/locales/en.yml b/engines/billing/config/locales/en.yml index 20ba03c..9eeb03c 100644 --- a/engines/billing/config/locales/en.yml +++ b/engines/billing/config/locales/en.yml @@ -12,4 +12,11 @@ en: new_donation: "New Donation" donation_info: "Please enter your donation details (this is a donation and will not be applied towards your account, if you have one):" donation_amount: "Enter amount" + donate: "Donate" + donation_sucess: "Congratulations! Your transaction has been successfull!" + donation_not_sucess: "Something went wrong while processing your donation. Please try again!" + subscription_sucess: "Congratulations! Your are now subscribed" + subscription_not_sucess: "Something went wrong while processing your subscription. Please try again" personal_info: "Please enter your personal info:" + unsubscription_sucess: "You have been unsubscribed!" + unsubscription_not_sucess: "Something went wrong. Please try again!" diff --git a/engines/billing/config/routes.rb b/engines/billing/config/routes.rb index 444a837..229641f 100644 --- a/engines/billing/config/routes.rb +++ b/engines/billing/config/routes.rb @@ -7,17 +7,18 @@ Rails.application.routes.draw do # resources :payments, :only => [:index] # resources :subscriptions, :only => [:index, :destroy] #end + resources :subscriptions, :only => [:index, :show] do + member do + post 'subscribe' + delete 'unsubscribe' + end + end resources :customer, :only => [:new, :edit] - resources :credit_card_info, :only => [:edit] match 'customer/confirm/' => 'customer#confirm', :as => :confirm_customer match 'customer/show/:id' => 'customer#show', :as => :show_customer - 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 'subscriptions/index' => 'subscriptions#index', :as => :index_subscription - match 'subscriptions/show' => 'subscriptions#show', :as => :show_subscription end end -- cgit v1.2.3