diff options
author | claucece <soficeli0@gmail.com> | 2015-09-21 23:27:05 -0500 |
---|---|---|
committer | claucece <soficeli0@gmail.com> | 2015-10-05 22:39:30 -0500 |
commit | 2e1d21f53f0f96fba544b592fde84af2f4879a24 (patch) | |
tree | fc7d512c67002774ac8135dee16cc7f7ccd1919e /engines/billing | |
parent | f55fd33542c68fc8fc4519d622a41ef9517ebee3 (diff) |
subscriptions, haml and translations
Diffstat (limited to 'engines/billing')
-rw-r--r-- | engines/billing/:w | 99 | ||||
-rw-r--r-- | engines/billing/app/controllers/payments_controller.rb | 10 | ||||
-rw-r--r-- | engines/billing/app/controllers/subscriptions_controller.rb | 105 | ||||
-rw-r--r-- | engines/billing/app/views/payments/_customer_form.html.haml | 10 | ||||
-rw-r--r-- | engines/billing/app/views/payments/_form.html.erb | 16 | ||||
-rw-r--r-- | engines/billing/app/views/payments/default.html.erb | 27 | ||||
-rw-r--r-- | engines/billing/app/views/payments/new.html.erb | 31 | ||||
-rw-r--r-- | engines/billing/app/views/payments/new.html.haml | 21 | ||||
-rw-r--r-- | engines/billing/app/views/subscriptions/_customer_form.html.erb (renamed from engines/billing/app/views/payments/_customer_form.html.erb) | 0 | ||||
-rw-r--r-- | engines/billing/app/views/subscriptions/new.html.erb | 35 | ||||
-rw-r--r-- | engines/billing/app/views/subscriptions/new.html.haml | 15 | ||||
-rw-r--r-- | engines/billing/config/locales/en.yml | 6 |
12 files changed, 286 insertions, 89 deletions
diff --git a/engines/billing/:w b/engines/billing/:w new file mode 100644 index 0000000..8629136 --- /dev/null +++ b/engines/billing/:w @@ -0,0 +1,99 @@ +class SubscriptionsController < BillingBaseController + before_filter :require_login + 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] + # for now, admins cannot create or destroy subscriptions for others: + before_filter :confirm_self, :only => [:new, :create] + + def new + if current_user.has_payment_info? + @client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id) + else + @client_token = Braintree::ClientToken.generate + end + @subscriptions = Braintree::Plan.all + end + + def create + @result = Braintree::Subscription.create( + payment_method_token: params[:payment_method_nonce], + plan_id: Braintree::Plan.all + ) + 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 + @result = Braintree::Subscription.cancel params[:id] + end + + def index + customer = Customer.find_by_user_id(@user.id) + @subscriptions = customer.subscriptions(nil, false) + 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 + end + + def confirm_self + @user == current_user + end + + def confirm_self_or_admin + access_denied unless confirm_self or admin? + end + +end 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 diff --git a/engines/billing/app/views/payments/_customer_form.html.haml b/engines/billing/app/views/payments/_customer_form.html.haml new file mode 100644 index 0000000..82828cd --- /dev/null +++ b/engines/billing/app/views/payments/_customer_form.html.haml @@ -0,0 +1,10 @@ +%p + = t(:personal_info) +%div + = text_field_tag :first_name, "",placeholder: "First Name", class: "radius" +%div + = text_field_tag :last_name, "",placeholder: "Last Name", class: "radius" +%div + = text_field_tag :company, "",placeholder: "Company", class: "radius" +%div + = text_field_tag :phone, "",placeholder: "Phone", class: "radius" diff --git a/engines/billing/app/views/payments/_form.html.erb b/engines/billing/app/views/payments/_form.html.erb deleted file mode 100644 index 9713981..0000000 --- a/engines/billing/app/views/payments/_form.html.erb +++ /dev/null @@ -1,16 +0,0 @@ --# slim template -= form_tag organization_payment_path(organization) do - - #dropin - - = submit_tag 'Confirm', class: 'btn push--ends' - -- content_for :javascript do - = javascript_include_tag "https://js.braintreegateway.com/v2/braintree.js" - javascript: - var client_token = "#{@organization.payment['client_token']}"; - braintree.setup( - client_token, - 'dropin', { - container: 'dropin' - }); diff --git a/engines/billing/app/views/payments/default.html.erb b/engines/billing/app/views/payments/default.html.erb new file mode 100644 index 0000000..bbecf46 --- /dev/null +++ b/engines/billing/app/views/payments/default.html.erb @@ -0,0 +1,27 @@ +<h2 class="mbs">New Donation</h2> +<br> +<%= form_tag confirm_payment_path, id: "checkout-form" do %> + <% if current_user and !current_user.has_payment_info? %> + <%= render 'customer_form' unless @anonymous_user%> + <% end %> +<br> +<p>Please enter your donation details (this is a donation and will not be applied towards your account):</p> +<div id="payment-form"></div> +<div id='coinbase-container-id'></div> +<input type="text" name="amount" placeholder="Enter amount"> +<input type="submit" class="btn btn-primary" value="Donate"> +<% end %> + +<script src="https://js.braintreegateway.com/v2/braintree.js"></script> +<script> + // We generated a client token for you so you can test out this code + // immediately. In a production-ready integration, you will need to + // generate a client token on your server (see section below). +var clientToken = "<%= @client_token %>"; +braintree.setup(clientToken, "dropin", { + container: "payment-form", + form: "checkout-form", + coinbase: { container: "coinbase-container-id" } +} + ); +</script> diff --git a/engines/billing/app/views/payments/new.html.erb b/engines/billing/app/views/payments/new.html.erb deleted file mode 100644 index 03fc5b9..0000000 --- a/engines/billing/app/views/payments/new.html.erb +++ /dev/null @@ -1,31 +0,0 @@ -<h2 class="mbs">New Donation</h2> -<br> -<%= form_tag confirm_payment_path, id: "checkout-form" do %> - <%# add migration to user first. Not sure about the anonymous part. %> - <%# render 'customer_form' unless current_user.has_payment_info? && current_user.where.not.is_anonymous? %> - <% if current_user and !current_user.has_payment_info? %> - <%= render 'customer_form' unless @anonymous_user%> - <% end %> - <br> - <p>Please enter your donation details (this is a donation and will not be applied towards your account):</p> - <div id="payment-form"></div> - <div id='coinbase-container-id'></div> - <input type="text" name="amount" placeholder="Enter amount"> - <input type="submit" class="btn btn-primary" value="Donate"> -<% end %> - -<script src="https://js.braintreegateway.com/v2/braintree.js"></script> -<script> -// We generated a client token for you so you can test out this code -// immediately. In a production-ready integration, you will need to -// generate a client token on your server (see section below). -var clientToken = "<%= @client_token %>"; - -braintree.setup(clientToken, "dropin", { - container: "payment-form", - form: "checkout-form", - coinbase: { container: "coinbase-container-id" } -} - ); -</script> - diff --git a/engines/billing/app/views/payments/new.html.haml b/engines/billing/app/views/payments/new.html.haml new file mode 100644 index 0000000..8d5bd5a --- /dev/null +++ b/engines/billing/app/views/payments/new.html.haml @@ -0,0 +1,21 @@ +%h2.mbs + = t(:new_donation) +%br/ += 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"} +%script{:src => "https://js.braintreegateway.com/v2/braintree.js"} +:javascript + var clientToken = "#{@client_token}"; + braintree.setup(clientToken, "dropin", { + container: "payment-form", + form: "checkout-form", + coinbase: { container: "coinbase-container-id" } + }); diff --git a/engines/billing/app/views/payments/_customer_form.html.erb b/engines/billing/app/views/subscriptions/_customer_form.html.erb index 2e8a3b1..2e8a3b1 100644 --- a/engines/billing/app/views/payments/_customer_form.html.erb +++ b/engines/billing/app/views/subscriptions/_customer_form.html.erb diff --git a/engines/billing/app/views/subscriptions/new.html.erb b/engines/billing/app/views/subscriptions/new.html.erb new file mode 100644 index 0000000..f3e143a --- /dev/null +++ b/engines/billing/app/views/subscriptions/new.html.erb @@ -0,0 +1,35 @@ +<script src="https://js.braintreegateway.com/v2/braintree.js"></script> +<h2 class="mbs">Subscriptions</h2> +<br> +<%= form_tag subscriptions_path, id: "checkout-form" do %> + <% if current_user and !current_user.has_payment_info? %> + <%= render 'customer_form' unless @anonymous_user%> + <% end %> +<br> +<p>Choose subcription:</p> +<ul> + <% @subscriptions.each do |subscription| %> + <li> + <%= subscription.name %> + - + <%= subscription.price %> + <%= simple_form_for :subscription, :url => :subscriptions, :id => "checkout-form-#{subscription.id}" do |f| %> + <input type="hidden" name="plan_id" id="" value="<%= subscription.id%>" /> + <div id="payment-form-<%= subscription.id%>"></div> + <div class="form-actions"> + <%= f.submit t(:subscribe), :class => 'btn btn-primary' %> + </div> + <script type="text/javascript" charset="utf-8"> + var clientToken = "<%= @client_token %>"; + braintree.setup(clientToken, "dropin", { + container: "payment-form-<%= subscription.id%>", + coinbase: { container: "coinbase-container-id" } + }); + </script> + <% end %> + </li> + <% end %> +</ul> +<div id="payment-form"></div> +<div id='coinbase-container-id'></div> +<% end %> diff --git a/engines/billing/app/views/subscriptions/new.html.haml b/engines/billing/app/views/subscriptions/new.html.haml deleted file mode 100644 index 4183458..0000000 --- a/engines/billing/app/views/subscriptions/new.html.haml +++ /dev/null @@ -1,15 +0,0 @@ -- if @payment_method_token - %h1 - Subscribe to plan - = #currently just one plan - = @plans[0].name - = number_to_currency(@plans[0].price) - = simple_form_for :subscription, :url => :subscriptions do |f| - = hidden_field_tag :payment_method_token, @payment_method_token - = hidden_field_tag :plan_id, @plans[0].id - .form-actions - = f.submit t(:subscribe), :class => 'btn btn-primary' -- else - = t(:must_create_customer) - %p - = link_to t(:create_new_customer), new_customer_path diff --git a/engines/billing/config/locales/en.yml b/engines/billing/config/locales/en.yml index 1300958..20ba03c 100644 --- a/engines/billing/config/locales/en.yml +++ b/engines/billing/config/locales/en.yml @@ -8,4 +8,8 @@ en: plan: "Plan" description: "Description" cost: "Cost" - free: "Free"
\ No newline at end of file + free: "Free" + 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" + personal_info: "Please enter your personal info:" |