From 78b11c75dd38ad50272f21455387e2a8b634d9d6 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Thu, 13 Nov 2014 23:53:44 -0800 Subject: [PATCH] code for legacy supporters --- app.rb | 97 +++++------------------------------------ models/site.rb | 18 ++++++++ views/plan/_pricing.erb | 25 ++++++++--- 3 files changed, 48 insertions(+), 92 deletions(-) diff --git a/app.rb b/app.rb index 999ac4cc..51838592 100644 --- a/app.rb +++ b/app.rb @@ -268,7 +268,16 @@ def generate_question end get '/plan/?' do - @title = 'Supporter' + @title = 'Support Us' + + if parent_site && parent_site.unconverted_legacy_supporter? + customer = Stripe::Customer.retrieve(parent_site.stripe_customer_id) + subscription = customer.subscriptions.first + parent_site.stripe_subscription_id = subscription.id + parent_site.plan_type = subscription.plan.id + parent_site.save_changes + end + erb :'plan/index' end @@ -321,92 +330,6 @@ get '/plan/thanks' do erb :'plan/thanks' end -=begin -get '/plan/?' do - @title = 'Supporter' - erb :'plan/index' -end - -post '/plan/create' do - require_login - - DB.transaction do - customer = Stripe::Customer.create( - card: params[:stripe_token], - description: "#{parent_site.username} - #{parent_site.id}", - email: current_site.email, - plan: params[:selected_plan] - ) - - parent_site.update stripe_customer_id: customer.id, plan_ended: false - - plan_name = customer.subscriptions.first['plan']['name'] - - if current_site.email || parent_site.email - EmailWorker.perform_async({ - from: 'web@neocities.org', - reply_to: 'contact@neocities.org', - to: current_site.email || parent_site.email, - subject: "[Neocities] You've become a supporter!", - body: Tilt.new('./views/templates/email_subscription.erb', pretty: true).render(self, plan_name: plan_name) - }) - end - end - - redirect '/plan' -end - -def get_plan_name(customer_id) - subscriptions = Stripe::Customer.retrieve(parent_site.stripe_customer_id).subscriptions.all - @plan_name = subscriptions.first.plan.name -end - -def require_active_subscription - redirect '/plan' unless parent_site.supporter? && !parent_site.plan_ended -end - -get '/plan/manage' do - require_login - require_active_subscription - @title = 'Manage Plan' - @plan_name = get_plan_name parent_site.stripe_customer_id - erb :'plan/manage' -end - -get '/plan/end' do - require_login - require_active_subscription - @title = 'End Plan' - @plan_name = get_plan_name parent_site.stripe_customer_id - erb :'plan/end' -end - -post '/plan/end' do - require_login - require_active_subscription - - recaptcha_is_valid = ENV['RACK_ENV'] == 'test' || recaptcha_valid? - - if !recaptcha_is_valid - @error = 'Recaptcha was filled out incorrectly, please try re-entering.' - @plan_name = get_plan_name parent_site.stripe_customer_id - halt erb :'plan/end' - end - - customer = Stripe::Customer.retrieve parent_site.stripe_customer_id - subscriptions = customer.subscriptions.all - - DB.transaction do - subscriptions.each do |subscription| - customer.subscriptions.retrieve(subscription.id).delete - end - parent_site.update plan_ended: true - end - - redirect '/plan' -end -=end - get '/site/:username/tip' do |username| @site = Site[username: username] @title = "Tip #{@site.title}" diff --git a/models/site.rb b/models/site.rb index 3c9e1dd9..6e5e7258 100644 --- a/models/site.rb +++ b/models/site.rb @@ -115,6 +115,15 @@ class Site < Sequel::Model unlimited_site_creation: false ) + LEGACY_SUPPORTER_PRICES = { + plan_one: 1, + plan_two: 2, + plan_three: 3, + plan_four: 4, + plan_five: 5 + } + + many_to_many :tags one_to_many :profile_comments @@ -879,8 +888,17 @@ class Site < Sequel::Model PLAN_FEATURES[plan_type.to_sym][:name] end + def unconverted_legacy_supporter? + stripe_customer_id && !plan_ended && values[:plan_type].nil? && stripe_subscription_id.nil? + end + + def legacy_supporter? + !values[:plan_type].match(/plan_/).nil? + end + def plan_type return 'free' if values[:plan_type].nil? + return 'supporter' if values[:plan_type].match /^plan_/ values[:plan_type] end diff --git a/views/plan/_pricing.erb b/views/plan/_pricing.erb index f5d1656f..75e7b175 100644 --- a/views/plan/_pricing.erb +++ b/views/plan/_pricing.erb @@ -67,9 +67,19 @@

Supporter

-
$<%= Site::PLAN_FEATURES[:supporter][:price] %>
-
per month, billed annually
- <%== plan_pricing_button :supporter %> + <% if parent_site && parent_site.legacy_supporter? %> +
$<%= Site::LEGACY_SUPPORTER_PRICES[parent_site[:plan_type].to_sym] %>
+
per month, billed annually
+ <% else %> +
$<%= Site::PLAN_FEATURES[:supporter][:price] %>
+
per month, billed annually
+ <% end %> + + <% if parent_site && parent_site.legacy_supporter? %> +
Current Plan
+ <% else %> + <%== plan_pricing_button :supporter %> + <% end %>