diff options
-rw-r--r-- | Gemfile.lock | 2 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-rw-r--r-- | app/views/layouts/_navigation.html.haml | 2 | ||||
-rw-r--r-- | config/locales/en/generic.en.yml | 6 | ||||
-rw-r--r-- | config/locales/es.yml | 2 | ||||
-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 |
14 files changed, 98 insertions, 129 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 3a0c535..9cb6229 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -316,4 +316,4 @@ DEPENDENCIES valid_email BUNDLED WITH - 1.10.5 + 1.10.6 diff --git a/app/models/user.rb b/app/models/user.rb index 391b2a5..4bb1e79 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,6 +10,8 @@ class User < CouchRest::Model::Base property :contact_email_key, String, :accessible => true property :invite_code, String, :accessible => true property :braintree_customer_id, Integer, :accessible => true + property :subscription_id, String, :accessible => true + property :enabled, TrueClass, :default => true # these will be null by default but we shouldn't ever pull them directly, but only via the methods that will return the full ServiceLevel diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 6dbd837..63a361a 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -8,7 +8,7 @@ active: controller?(:tickets) - if APP_CONFIG[:billing] = link_to_navigation :donations, new_payment_path, - active: (controller?(:payments) and action?(:new)) + active: (controller?(:donations) and action?(:new)) = link_to_navigation :subscriptions, billing_top_link(@braintree_customer_id), active: controller?(:subscriptions) = link_to_navigation :logout, logout_path, method: :delete diff --git a/config/locales/en/generic.en.yml b/config/locales/en/generic.en.yml index f268bff..be62a40 100644 --- a/config/locales/en/generic.en.yml +++ b/config/locales/en/generic.en.yml @@ -2,9 +2,9 @@ en: signup: "Sign Up" login: "Log In" logout: "Log Out" - + cancel: "Cancel" - + created: "Created" created_by_on: "Created by %{user} on %{time}" updated: "Updated" @@ -25,3 +25,5 @@ en: create_thing: "Create %{thing}" overview: "Overview" + choose: "Choose" + enter_amount: "Enter amount" diff --git a/config/locales/es.yml b/config/locales/es.yml index 99b8601..3c694fd 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -124,6 +124,8 @@ es: free: Gratuito new_donation: "Nueva Donación" donation_info: "Por favor, llena los detalles de tu donación (esta no será cargada a tu cuenta, si tienes una):" + donation_amount: "Ingrese el monto" + donate: "Donar" personal_info: "Por favor, ingresa tu información personal" support_tickets: Soporte email_notice_text: Ha sido añadido un nuevo comentario a esta instancia de ayuda. 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 |