From 609cee1c1738707b3d92251920f5c2093f2477b9 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Sat, 25 Oct 2014 16:56:53 -0700 Subject: [PATCH 1/2] track subscription ids --- app.rb | 51 ++++++++----- .../048_add_subscription_id_to_sites.rb | 9 +++ views/_team.erb | 33 +++++++++ views/about.erb | 35 +-------- views/plan/_pricing.erb | 74 ++++++++++--------- views/plan/thanks.erb | 31 ++++++++ 6 files changed, 147 insertions(+), 86 deletions(-) create mode 100644 migrations/048_add_subscription_id_to_sites.rb create mode 100644 views/_team.erb create mode 100644 views/plan/thanks.erb diff --git a/app.rb b/app.rb index 75595d52..364d3212 100644 --- a/app.rb +++ b/app.rb @@ -271,30 +271,43 @@ post '/plan/update' 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 || parent_site.email), - plan: params[:plan_type] - ) + if parent_site.stripe_subscription_id + customer = Stripe::Customer.retrieve parent_site.stripe_customer_id + subscription = customer.subscriptions.retrieve parent_site.stripe_subscription_id + subscription.plan = params[:plan_type] + subscription.save - parent_site.update( - stripe_customer_id: customer.id, - plan_ended: false, - plan_type: params[:plan_type] - ) + parent_site.update( + plan_ended: false, + plan_type: params[:plan_type] + ) + else + customer = Stripe::Customer.create( + card: params[:stripe_token], + description: "#{parent_site.username} - #{parent_site.id}", + email: (current_site.email || parent_site.email), + plan: params[:plan_type] + ) - 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: Site::PLAN_FEATURES[params[:plan_type].to_sym][:name]) - }) + parent_site.update( + stripe_customer_id: customer.id, + stripe_subscription_id: customer.subscriptions.first.id, + plan_ended: false, + plan_type: params[:plan_type] + ) end end + 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: Site::PLAN_FEATURES[params[:plan_type].to_sym][:name]) + }) + end + redirect '/plan/thanks' end diff --git a/migrations/048_add_subscription_id_to_sites.rb b/migrations/048_add_subscription_id_to_sites.rb new file mode 100644 index 00000000..099f9eee --- /dev/null +++ b/migrations/048_add_subscription_id_to_sites.rb @@ -0,0 +1,9 @@ +Sequel.migration do + up { + DB.add_column :sites, :stripe_subscription_id, :text + } + + down { + DB.drop_column :sites, :stripe_subscription_id + } +end \ No newline at end of file diff --git a/views/_team.erb b/views/_team.erb new file mode 100644 index 00000000..fa6db8bd --- /dev/null +++ b/views/_team.erb @@ -0,0 +1,33 @@ +
+
+

The Neocities Team

+ + +
+ +
+
+

Follow us on Twitter

+
\ No newline at end of file diff --git a/views/about.erb b/views/about.erb index 86c53652..60bb09c1 100644 --- a/views/about.erb +++ b/views/about.erb @@ -48,38 +48,5 @@ If you share our values, we would love your support. Spread the word! We also have a supporter plan to help pay for the site, and we accept donations through multiple options (including Bitcoin... and Dogecoin).

-
-
-

The Neocities Team

- - -
- -
-
-

Follow us on Twitter

-
- + <%== erb :'_team', layout: false %> diff --git a/views/plan/_pricing.erb b/views/plan/_pricing.erb index 1e084b42..fb3b914c 100644 --- a/views/plan/_pricing.erb +++ b/views/plan/_pricing.erb @@ -332,7 +332,7 @@ It's affordable. As low as $<%= Site::PLAN_FEATURES[:supporter][:price] %>/month (billed once every year). Higher tiers are optional (and appreciated!)
  • - You can cancel anytime. + You can cancel or change plans anytime. If you do, we'll refund/prorate the amount you didn't use.
  • @@ -375,7 +375,10 @@