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 ++++++++------------- 3 files changed, 46 insertions(+), 88 deletions(-) delete mode 100644 engines/billing/app/controllers/subscription_controller.rb (limited to 'engines/billing/app/controllers') 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 -- cgit v1.2.3