diff options
author | claucece <soficeli0@gmail.com> | 2015-09-25 00:04:19 -0500 |
---|---|---|
committer | claucece <soficeli0@gmail.com> | 2015-10-05 22:41:20 -0500 |
commit | b26d10fe7d87b570bd888fa2a2543f3675278f8b (patch) | |
tree | 2b73a88026b78ef5f78a881ac7c0eac91d6632ac /engines/billing | |
parent | 577c1f3e92040ab79fcd67b818c7dc8531aaa211 (diff) |
add subscriptions
Diffstat (limited to 'engines/billing')
-rw-r--r-- | engines/billing/app/controllers/payments_controller.rb | 4 | ||||
-rw-r--r-- | engines/billing/app/controllers/subscription_controller.rb | 7 | ||||
-rw-r--r-- | engines/billing/app/controllers/subscriptions_controller.rb | 123 | ||||
-rw-r--r-- | engines/billing/app/helpers/billing_helper.rb | 2 | ||||
-rw-r--r-- | engines/billing/app/views/payments/new.html.haml | 5 | ||||
-rw-r--r-- | engines/billing/app/views/subscriptions/index.html.erb | 38 | ||||
-rw-r--r-- | engines/billing/app/views/subscriptions/show.html.erb | 16 | ||||
-rw-r--r-- | engines/billing/config/locales/en.yml | 7 | ||||
-rw-r--r-- | engines/billing/config/routes.rb | 11 |
9 files changed, 88 insertions, 125 deletions
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 @@ <h2 class="mbs">Subscriptions</h2> <br> -<br> -<div id="last"> Lastest Subscriptions: - <% if params[:search] == "active" %> - <%= render :partial => "subscriptions/subscription_details" %> - <%# Add destroy, route %> - <%=link_to "Unsubscribe", :show_subscription, class: "button" %> - </div> +<h4><div id="last"> Lastest Subscriptions:</h4> + <% if @user.subscription_id %> + <ul> + <% @subscription.transactions.each do |transaction| %> + <li> + Date: <%= transaction.created_at %> + </li> + <% end %> + </ul> + <ul> + <%= link_to "Unsubscribe from #{@plan.name}", unsubscribe_subscription_path(@subscription.plan_id), method: :delete, class: "btn btn-danger" %> + </ul> +</div> <br> <% else %> - <p> No subscriptions </p> - <p>Choose subcription:</p> + <p> No subscriptions. </p> + <br> + <h4>Choose subcription:</h4> <br> <ul class="nav nav-tabs nav-stacked"> <% @subscriptions.each do |subscription| %> <li> - <div class="btn-group"> - <button class="btn"> <%= subscription.name %> - <%= subscription.price %> </button> + <div class="well"> + <%= subscription.name %> + <%= subscription.price %> </div> <div> - <%=link_to "Subscribe", :show_subscription, class: "button" %> + <%= link_to "Choose", subscription_path(subscription.id), class: "btn btn-info" %> </div> <br> </li> <% end %> <% end %> </ul> - - - - - 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 @@ <script src="https://js.braintreegateway.com/v2/braintree.js"></script> <h2 class="mbs">New Subscription </h2> -<br/> -<%= simple_form_for :subscription, :url => :subscriptions, :id => "checkout-form-#{subscription.id}" do |f| %> - <input name="plan_id" type="hidden" value="<%= subscription.id %>"></input> +<%= 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 %> <br/> <p> Please enter payment details: </p> - <div id="payment-form-<%= subscription.id %>"></div> + <div id="payment-form"></div> + <div id="coinbase-container-id"></div> <div class="form-actions"> - <%= f.submit t(:choose), :class => 'btn btn-primary' %> + <%= f.submit t(:subscribe), :class => 'btn btn-primary' %> </div> <script charset="utf-8" type="text/javascript"> var clientToken = "<%= @client_token %>"; braintree.setup(clientToken, "dropin", { - container: "payment-form-#{subscription.id}", + container: "payment-form", coinbase: { container: "coinbase-container-id" } }); </script> - <% end %> -<div id="payment-form"></div> -<div id="coinbase-container-id"></div> - +<% 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 |