diff --git a/app/plan.rb b/app/plan.rb index ee225d81..b79febd6 100644 --- a/app/plan.rb +++ b/app/plan.rb @@ -35,6 +35,9 @@ post '/plan/update' do end end + # Do not create customer if there is already a customer. Is this necessary? + redirect request.referrer if current_site.stripe_subscription_id + site ||= parent_site DB.transaction do diff --git a/app/settings.rb b/app/settings.rb index 74249cd1..2d91c3e1 100644 --- a/app/settings.rb +++ b/app/settings.rb @@ -334,3 +334,29 @@ get '/settings/unsubscribe_email/?' do end erb :'settings/account/unsubscribe' end + +post '/settings/update_card' do + require_login + + customer = Stripe::Customer.retrieve current_site.stripe_customer_id + + old_card_ids = customer.sources.collect {|s| s.id} + + begin + customer.sources.create source: params[:stripe_token] + rescue Stripe::InvalidRequestError => e + if e.message.match /cannot use a.+token more than once/ + flash[:error] = 'Card is already being used.' + redirect '/settings#billing' + else + raise e + end + end + + old_card_ids.each do |card_id| + customer.sources.retrieve(card_id).delete + end + + flash[:success] = 'Card information updated.' + redirect '/settings#billing' +end diff --git a/models/site.rb b/models/site.rb index eeeb89d2..6b9fc9e7 100644 --- a/models/site.rb +++ b/models/site.rb @@ -980,6 +980,10 @@ class Site < Sequel::Model PLAN_FEATURES[plan_type.to_sym][:name] end + def stripe_paying_supporter? + stripe_customer_id && !plan_ended && values[:plan_type].match(/free|special/).nil? + end + def unconverted_legacy_supporter? stripe_customer_id && !plan_ended && values[:plan_type].nil? && stripe_subscription_id.nil? end diff --git a/views/plan/_signupcode.erb b/views/plan/_signupcode.erb index 168c8656..1928f470 100644 --- a/views/plan/_signupcode.erb +++ b/views/plan/_signupcode.erb @@ -13,10 +13,6 @@ var signupform = $(event.target) - <% if current_site && parent_site.stripe_subscription_id %> - return true - <% end %> - var planError = $('#plan_error') planError.css('display', 'none') signupform.find(':submit').prop('disabled', true) diff --git a/views/settings/account.erb b/views/settings/account.erb index a6c305c1..0558abe2 100644 --- a/views/settings/account.erb +++ b/views/settings/account.erb @@ -27,6 +27,9 @@
  • Sites
  • Password
  • Email
  • + <% if current_site.stripe_paying_supporter? %> +
  • Billing
  • + <% end %>
    @@ -41,6 +44,11 @@
    <%== erb :'settings/account/email' %>
    + <% if current_site.stripe_paying_supporter? %> +
    + <%== erb :'settings/account/billing' %> +
    + <% end %>
    diff --git a/views/settings/account/billing.erb b/views/settings/account/billing.erb new file mode 100644 index 00000000..a9378954 --- /dev/null +++ b/views/settings/account/billing.erb @@ -0,0 +1,45 @@ +

    Change Credit Card

    +

    This allows you to change your currently stored credit card. You will be charged at the regular monthly cycle.

    + +
    + +
    + + + + +
    +

    + + + + + + + + + + + + +
    +
    + + Update Card +
    + +<%== erb :'plan/_signupcode', layout: false %> + +