diff --git a/app/stripe_webhook.rb b/app/stripe_webhook.rb index 04bae724..b0c7834b 100644 --- a/app/stripe_webhook.rb +++ b/app/stripe_webhook.rb @@ -1,3 +1,23 @@ +def stripe_get_site_from_event(event) + customer_id = event['data']['object']['customer'] + customer = Stripe::Customer.retrieve customer_id + + # Some old accounts only have a username for the desc + desc_split = customer.description.split(' - ') + + if desc_split.length == 1 + site_where = {username: desc_split.first} + end + + if desc_split.last.to_i == 0 + site_where = {username: desc_split.first} + else + site_where = {id: desc_split.last} + end + + site.where(site_where).first +end + post '/stripe_webhook' do event = JSON.parse request.body.read if event['type'] == 'customer.created' @@ -14,16 +34,7 @@ post '/stripe_webhook' do end if event['type'] == 'charge.failed' - - EmailWorker.perform_async({ - from: 'web@neocities.org', - to: 'kyle@neocities.org', - subject: "[Neocities] charge.failed debug", - body: event.inspect - }) - - site_id = event['data']['object']['description'].split(' - ').last - site = Site[site_id] + site = stripe_get_site_from_event event EmailWorker.perform_async({ from: 'web@neocities.org', @@ -34,8 +45,7 @@ post '/stripe_webhook' do end if event['type'] == 'customer.subscription.deleted' - site_id = event['data']['object']['description'].split(' - ').last - site = Site[site_id] + site = stripe_get_site_from_event event site.stripe_subscription_id = nil site.plan_type = nil site.save_changes validate: false diff --git a/views/templates/email/charge_failure.erb b/views/templates/email/charge_failure.erb index 26145982..1cfd91fd 100644 --- a/views/templates/email/charge_failure.erb +++ b/views/templates/email/charge_failure.erb @@ -2,9 +2,9 @@ Hello, We're writing to let you know that Neocities has had an issue charging your card. We will try again automatically in a few days, but we just wanted to give you a heads up in case something is wrong with your card. -We will retry over the next few weeks. If we can't make a successful charge, your account will be downgraded from supporter to the free plan. +We will retry over the next few weeks. If we can't make a successful charge, we will automatically switch your account over to the free plan. -If you need to change the card on file, you can do so on the settings page. If you run into any issues, please contact us at https://neocities.org/contact. +If you need to change the card on file, you can do so on your account settings page. If you run into any issues, please contact us at https://neocities.org/contact. Regards, - The Neocities Team