fix supporter plan management

This commit is contained in:
Kyle Drake 2014-10-12 09:11:20 -07:00
parent f44ce014d3
commit 52fde7a0d5
4 changed files with 31 additions and 24 deletions

31
app.rb
View file

@ -271,68 +271,74 @@ post '/plan/create' do
DB.transaction do DB.transaction do
customer = Stripe::Customer.create( customer = Stripe::Customer.create(
card: params[:stripe_token], card: params[:stripe_token],
description: current_site.username, description: "#{parent_site.username} - #{parent_site.id}",
email: current_site.email, email: current_site.email,
plan: params[:selected_plan] plan: params[:selected_plan]
) )
current_site.update stripe_customer_id: customer.id, plan_ended: false parent_site.update stripe_customer_id: customer.id, plan_ended: false
plan_name = customer.subscriptions.first['plan']['name'] plan_name = customer.subscriptions.first['plan']['name']
if current_site.email || parent_site.email
EmailWorker.perform_async({ EmailWorker.perform_async({
from: 'web@neocities.org', from: 'web@neocities.org',
reply_to: 'contact@neocities.org', reply_to: 'contact@neocities.org',
to: current_site.email, to: current_site.email || parent_site.email,
subject: "[Neocities] You've become a supporter!", subject: "[Neocities] You've become a supporter!",
body: Tilt.new('./views/templates/email_subscription.erb', pretty: true).render(self, plan_name: plan_name) body: Tilt.new('./views/templates/email_subscription.erb', pretty: true).render(self, plan_name: plan_name)
}) })
end end
end
redirect '/plan' redirect '/plan'
end end
def get_plan_name(customer_id) def get_plan_name(customer_id)
subscriptions = Stripe::Customer.retrieve(current_site.stripe_customer_id).subscriptions.all subscriptions = Stripe::Customer.retrieve(parent_site.stripe_customer_id).subscriptions.all
@plan_name = subscriptions.first.plan.name @plan_name = subscriptions.first.plan.name
end end
def require_active_subscription
redirect '/plan' unless parent_site.supporter? && !parent_site.plan_ended
end
get '/plan/manage' do get '/plan/manage' do
require_login require_login
redirect '/plan' unless current_site.supporter? && !current_site.plan_ended require_active_subscription
@title = 'Manage Plan' @title = 'Manage Plan'
@plan_name = get_plan_name current_site.stripe_customer_id @plan_name = get_plan_name parent_site.stripe_customer_id
erb :'plan/manage' erb :'plan/manage'
end end
get '/plan/end' do get '/plan/end' do
require_login require_login
redirect '/plan' unless current_site.supporter? && !current_site.plan_ended require_active_subscription
@title = 'End Plan' @title = 'End Plan'
@plan_name = get_plan_name current_site.stripe_customer_id @plan_name = get_plan_name parent_site.stripe_customer_id
erb :'plan/end' erb :'plan/end'
end end
post '/plan/end' do post '/plan/end' do
require_login require_login
redirect '/plan' unless current_site.supporter? && !current_site.plan_ended require_active_subscription
recaptcha_is_valid = ENV['RACK_ENV'] == 'test' || recaptcha_valid? recaptcha_is_valid = ENV['RACK_ENV'] == 'test' || recaptcha_valid?
if !recaptcha_is_valid if !recaptcha_is_valid
@error = 'Recaptcha was filled out incorrectly, please try re-entering.' @error = 'Recaptcha was filled out incorrectly, please try re-entering.'
@plan_name = get_plan_name current_site.stripe_customer_id @plan_name = get_plan_name parent_site.stripe_customer_id
halt erb :'plan/end' halt erb :'plan/end'
end end
customer = Stripe::Customer.retrieve current_site.stripe_customer_id customer = Stripe::Customer.retrieve parent_site.stripe_customer_id
subscriptions = customer.subscriptions.all subscriptions = customer.subscriptions.all
DB.transaction do DB.transaction do
subscriptions.each do |subscription| subscriptions.each do |subscription|
customer.subscriptions.retrieve(subscription.id).delete customer.subscriptions.retrieve(subscription.id).delete
end end
current_site.update plan_ended: true parent_site.update plan_ended: true
end end
redirect '/plan' redirect '/plan'
@ -1454,6 +1460,7 @@ def current_site
end end
def parent_site def parent_site
return nil if current_site.nil?
current_site.parent? ? current_site : current_site.parent current_site.parent? ? current_site : current_site.parent
end end

View file

@ -12,7 +12,7 @@
<div class="content single-Col misc-page"> <div class="content single-Col misc-page">
<h3>Ending the Supporter Plan</h3> <h3>Ending the Supporter Plan</h3>
<p> <p>
You currently have the <strong><%= current_site.plan_name %> (<%= current_site.maximum_space.to_space_pretty %>)</strong> - <%= @plan_name %>. You currently have the <strong><%= parent_site.plan_name %> (<%= parent_site.maximum_space.to_space_pretty %>)</strong> - <%= @plan_name %>.
</p> </p>
<p> <p>
If you need to end the plan, you can do that here. We'll be sorry to see you go. If there's a reason you're ending that we can help with, please <a href="/contact">contact us</a> and we'll see if we can help you with your issue. Regardless, we'll let you keep your site and the extra space. We hope you'll decide to become a supporter again in the future! If you need to end the plan, you can do that here. We'll be sorry to see you go. If there's a reason you're ending that we can help with, please <a href="/contact">contact us</a> and we'll see if we can help you with your issue. Regardless, we'll let you keep your site and the extra space. We hope you'll decide to become a supporter again in the future!

View file

@ -17,9 +17,9 @@
<div class="content single-Col misc-page"> <div class="content single-Col misc-page">
<h3></h3> <h3></h3>
<% if current_site && current_site.supporter? && !current_site.plan_ended %> <% if parent_site && parent_site.supporter? && !parent_site.plan_ended %>
<p> <p>
You currently have the <strong><%= current_site.plan_name %> (<%= current_site.maximum_space.to_space_pretty %>)</strong>. You currently have the <strong><%= parent_site.plan_name %> (<%= parent_site.maximum_space.to_space_pretty %>)</strong>.
</p> </p>
<p> <p>
Your support means a lot to us. On behalf of Penelope the cat and everyone at Neocities, thank you. If there's anything we can do to make your experience even better, please don't hesitate to <a href="/contact">contact us</a>. Your support means a lot to us. On behalf of Penelope the cat and everyone at Neocities, thank you. If there's anything we can do to make your experience even better, please don't hesitate to <a href="/contact">contact us</a>.
@ -27,8 +27,8 @@
<a href="/plan/manage">Manage plan</a> <a href="/plan/manage">Manage plan</a>
<% else %> <% else %>
<% if current_site %> <% if parent_site %>
<p>You currently have the <strong>Free Plan (<%= current_site.maximum_space.to_space_pretty %>)</strong>. Need more space? Become a <strong>Neocities Supporter!</strong></p> <p>You currently have the <strong>Free Plan (<%= parent_site.maximum_space.to_space_pretty %>)</strong>. Need more space? Become a <strong>Neocities Supporter!</strong></p>
<h3>Why upgrade?</h3> <h3>Why upgrade?</h3>
<% else %> <% else %>

View file

@ -12,7 +12,7 @@
<div class="content single-Col misc-page"> <div class="content single-Col misc-page">
<h3>Change your Supporter Plan</h3> <h3>Change your Supporter Plan</h3>
<p> <p>
You currently have the <strong><%= current_site.plan_name %> (<%= current_site.maximum_space.to_space_pretty %>)</strong> - <%= @plan_name %>. You currently have the <strong><%= parent_site.plan_name %> (<%= parent_site.maximum_space.to_space_pretty %>)</strong> - <%= @plan_name %>.
</p> </p>
<a href="/plan/end">End plan</a> <a href="/plan/end">End plan</a>