diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 1f1b3c01..3afb8141 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -10,17 +10,15 @@ def invoice_payment_succeeded end def customer_subscription_deleted - Subscription.find_by(stripe_user_id: @customer.id) - .update(active: false, percent_off: 0) + subscription = Subscription.find_by(stripe_user_id: @customer.id) + subscription.update(active: false, percent_off: 0) + subscription.recipient.users.each do |user| + SubscriptionMailer.deactivated(user).deliver_now + end rescue NoMethodError nil end - def subscription_expired - # TODO: deactivate Beehive::Subscription at_period_end - # TODO: send reminder emails - end - private def load_customer diff --git a/app/mailers/subscription_mailer.rb b/app/mailers/subscription_mailer.rb new file mode 100644 index 00000000..c97f68a5 --- /dev/null +++ b/app/mailers/subscription_mailer.rb @@ -0,0 +1,6 @@ +class SubscriptionMailer < ApplicationMailer + def deactivated(user) + @user = user + mail(to: @user.email, subject: 'Subscription Expired - Beehive') + end +end diff --git a/app/views/subscription_mailer/deactivated.text.erb b/app/views/subscription_mailer/deactivated.text.erb new file mode 100644 index 00000000..039acf28 --- /dev/null +++ b/app/views/subscription_mailer/deactivated.text.erb @@ -0,0 +1,17 @@ +Hi <%= @user&.first_name %> + +Your 'Pro' subscription on www.beehivegiving.org (Beehive) has now come to an end. + +As Beehive is a project of a charity, we really appreciate that you upgraded your account a year ago. Your contribution has helped us sustain the project and provide our grant funding suitability checking service to many more fund seekers - thank you! + +We know there is plenty we could do to improve our service, and as a valued customer we'd love to ask you a few brief questions about this. + +To say thank you for your feedback and support, we'll happily extend your 'Pro' subscription for another year (free of charge). + +Just reply to this email and let us know you'd be happy to answer some questions. + +Big thanks, +The Beehive Team + +-- +This is an automated message from www.beehivegiving.org diff --git a/spec/features/subscriptions_spec.rb b/spec/features/subscriptions_spec.rb index 44d4d6de..541c3b09 100644 --- a/spec/features/subscriptions_spec.rb +++ b/spec/features/subscriptions_spec.rb @@ -157,11 +157,11 @@ scenario 'webhook customer-subscription-deleted', type: :request do Subscription.last.update(percent_off: 10) + status = helper.stripe_subscription(@db[:recipient]).status - expect(Subscription.last.active).to eq true - expect(Subscription.last.percent_off).to eq 10 - expect(helper.stripe_subscription(@db[:recipient]).status) - .to eq 'active' + expect(Subscription.last.active).to eq(true) + expect(Subscription.last.percent_off).to eq(10) + expect(status).to eq('active') event = StripeMock.mock_webhook_event( 'customer.subscription.deleted', @@ -172,16 +172,13 @@ params: event.to_json, headers: { 'Content-Type': 'application/json' } - expect(Subscription.last.active).to eq false - expect(Subscription.last.percent_off).to eq 0 - expect(event.data.object.status).to eq 'canceled' - end + expect(Subscription.last.active).to eq(false) + expect(Subscription.last.percent_off).to eq(0) + expect(event.data.object.status).to eq('canceled') - scenario 'remaining free checks hidden' do - @app.setup_funds - @db[:proposal].save! - visit fund_path(Fund.first, @db[:proposal]) - expect(page).not_to have_button 'Check eligibility (3 left)' + deliveries = ActionMailer::Base.deliveries + expect(deliveries.size).to eq(1) + expect(deliveries.last.subject).to eq('Subscription Expired - Beehive') end end end diff --git a/spec/mailers/previews/subscription_mailer_preview.rb b/spec/mailers/previews/subscription_mailer_preview.rb new file mode 100644 index 00000000..4da09d92 --- /dev/null +++ b/spec/mailers/previews/subscription_mailer_preview.rb @@ -0,0 +1,6 @@ +class SubscriptionMailerPreview < ActionMailer::Preview + def deactivated + user = OpenStruct.new(email: 'email@example.com', first_name: 'John') + SubscriptionMailer.deactivated(user) + end +end