code for legacy supporters

This commit is contained in:
Kyle Drake 2014-11-13 23:53:44 -08:00
parent 9d6e2566dd
commit 78b11c75dd
3 changed files with 48 additions and 92 deletions

97
app.rb
View file

@ -268,7 +268,16 @@ def generate_question
end end
get '/plan/?' do 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' erb :'plan/index'
end end
@ -321,92 +330,6 @@ get '/plan/thanks' do
erb :'plan/thanks' erb :'plan/thanks'
end 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| get '/site/:username/tip' do |username|
@site = Site[username: username] @site = Site[username: username]
@title = "Tip #{@site.title}" @title = "Tip #{@site.title}"

View file

@ -115,6 +115,15 @@ class Site < Sequel::Model
unlimited_site_creation: false 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 many_to_many :tags
one_to_many :profile_comments one_to_many :profile_comments
@ -879,8 +888,17 @@ class Site < Sequel::Model
PLAN_FEATURES[plan_type.to_sym][:name] PLAN_FEATURES[plan_type.to_sym][:name]
end 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 def plan_type
return 'free' if values[:plan_type].nil? return 'free' if values[:plan_type].nil?
return 'supporter' if values[:plan_type].match /^plan_/
values[:plan_type] values[:plan_type]
end end

View file

@ -67,9 +67,19 @@
<div class="col col-25 supporter"> <div class="col col-25 supporter">
<div class="plan-image" data-original-title="No no, pick me!"></div> <div class="plan-image" data-original-title="No no, pick me!"></div>
<h3>Supporter</h3> <h3>Supporter</h3>
<div class="price">$<%= Site::PLAN_FEATURES[:supporter][:price] %></div> <% if parent_site && parent_site.legacy_supporter? %>
<div class="interval">per month, billed annually</div> <div class="price">$<%= Site::LEGACY_SUPPORTER_PRICES[parent_site[:plan_type].to_sym] %></div>
<%== plan_pricing_button :supporter %> <div class="interval">per month, billed annually</div>
<% else %>
<div class="price">$<%= Site::PLAN_FEATURES[:supporter][:price] %></div>
<div class="interval">per month, billed annually</div>
<% end %>
<% if parent_site && parent_site.legacy_supporter? %>
<div class="current-plan">Current Plan</div>
<% else %>
<%== plan_pricing_button :supporter %>
<% end %>
<ul> <ul>
<li><strong><%= Site::PLAN_FEATURES[:supporter][:space].to_space_pretty %></strong> storage</li> <li><strong><%= Site::PLAN_FEATURES[:supporter][:space].to_space_pretty %></strong> storage</li>
<li><strong><%= Site::PLAN_FEATURES[:supporter][:bandwidth].to_space_pretty %></strong> bandwidth</li> <li><strong><%= Site::PLAN_FEATURES[:supporter][:bandwidth].to_space_pretty %></strong> bandwidth</li>
@ -131,8 +141,13 @@
</th> </th>
<th class="professional"> <th class="professional">
<h3>Supporter</h3> <h3>Supporter</h3>
<p>$<%= Site::PLAN_FEATURES[:supporter][:price] %>/mo</p> <% if parent_site && parent_site.legacy_supporter? %>
<%== plan_pricing_button :supporter %> <p>$<%= Site::LEGACY_SUPPORTER_PRICES[parent_site.values[:plan_type].to_sym] %>/mo</p>
<div class="current-plan">Current Plan</div>
<% else %>
<p>$<%= Site::PLAN_FEATURES[:supporter][:price] %>/mo</p>
<%== plan_pricing_button :supporter %>
<% end %>
</th> </th>
<th class="professional"> <th class="professional">
<h3>Cat Bus</h3> <h3>Cat Bus</h3>