diff options
Diffstat (limited to 'engines/billing/app/controllers')
-rw-r--r-- | engines/billing/app/controllers/payments_controller.rb | 10 | ||||
-rw-r--r-- | engines/billing/app/controllers/subscriptions_controller.rb | 105 |
2 files changed, 89 insertions, 26 deletions
diff --git a/engines/billing/app/controllers/payments_controller.rb b/engines/billing/app/controllers/payments_controller.rb index 6ea149f..4a047ad 100644 --- a/engines/billing/app/controllers/payments_controller.rb +++ b/engines/billing/app/controllers/payments_controller.rb @@ -9,6 +9,7 @@ class PaymentsController < BillingBaseController end end +# not sure if this should be kept def index access_denied unless admin? or (@user == current_user) customer = Customer.find_by_user_id(@user.id) @@ -27,12 +28,15 @@ class PaymentsController < BillingBaseController redirect_to action: :new, locale: params[:locale] end + private def make_transaction - unless current_user.has_payment_info? - transact_with_user_info - else + if current_user.has_payment_info? transact_without_user_info + elsif current_user.is_anonymous? + transact_without_user_info + else + transact_with_user_info end end diff --git a/engines/billing/app/controllers/subscriptions_controller.rb b/engines/billing/app/controllers/subscriptions_controller.rb index f066b3c..0a9b412 100644 --- a/engines/billing/app/controllers/subscriptions_controller.rb +++ b/engines/billing/app/controllers/subscriptions_controller.rb @@ -1,6 +1,5 @@ class SubscriptionsController < BillingBaseController before_filter :require_login - before_filter :fetch_subscription, :only => [:show, :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] @@ -8,17 +7,87 @@ class SubscriptionsController < BillingBaseController before_filter :confirm_self, :only => [:new, :create] def new - # don't show link to subscribe if they are already subscribed? - credit_card = @customer.default_credit_card #safe to assume default? - @payment_method_token = credit_card.token - @plans = Braintree::Plan.all + 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 end - # show has no content, so not needed at this point. - 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 + @result = Braintree::Subscription.create( + payment_method_token: braintree_customer.payment_methods.first.token, + plan_id: params[:plan_id], + ) + if @result.success? + flash[:success] = "Congratulations! Your transaction has been successfully!" + else + flash[:error] = "Something went wrong while processing your donation. Please try again!" + end + redirect_to action: :new, locale: params[:locale] + end + + def braintree_customer + if current_user.braintree_customer_id + Braintree::Customer.find current_user.braintree_customer_id + else + customer = Braintree::Customer.create(payment_method_nonce: params[:payment_method_nonce]).customer + current_user.update_attributes braintree_customer_id: customer.id + customer + end + end + + def confirm + @result = Braintree::Subscription.sale( + payment_method_token: params[:payment_method_nonce], + plans_id: params[:plan_id], + ) + end + + def _confirm + make_subscription + if @result.success? + flash[:success] = "Congratulations! Your transaction has been successfully!" + else + flash[:error] = "Something went wrong while processing your donation. Please try again!" + end + redirect_to action: :new, locale: params[:locale] + end + +private + + def make_subscription + unless current_user.has_payment_info? + subs_with_user_info + else + subs_without_user_info + 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 @@ -30,26 +99,16 @@ class SubscriptionsController < BillingBaseController @subscriptions = customer.subscriptions(nil, false) end - private - - def fetch_subscription - @subscription = Braintree::Subscription.find params[:id] - @credit_card = Braintree::CreditCard.find @subscription.payment_method_token - @subscription_customer_id = @credit_card.customer_id - current_user_customer = Customer.find_by_user_id(current_user.id) - access_denied unless admin? or (current_user_customer and current_user_customer.braintree_customer_id == @subscription_customer_id) - - end def confirm_cancel_subscription access_denied unless view_context.allow_cancel_subscription(@subscription) end def confirm_no_pending_active_pastdue_subscription - @customer = Customer.find_by_user_id(@user.id) - 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 + #@customer = Customer.find_by_user_id(@user.id) + #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 def confirm_self |