Skip to content

Commit

Permalink
Update failed payment notification
Browse files Browse the repository at this point in the history
Don't send the notification when the user has no subscriptions which are
past due.
  • Loading branch information
gbp committed Nov 27, 2024
1 parent dbdda04 commit 8fbf59f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def invoice_payment_succeeded

def invoice_payment_failed
account = pro_account_from_stripe_event(@stripe_event)
return unless account
return unless account&.subscription?

AlaveteliPro::SubscriptionMailer.payment_failed(account.user).deliver_now
end
Expand Down
39 changes: 29 additions & 10 deletions spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,44 @@ def send_request
StripeMock.mock_webhook_event('invoice.payment_failed')
end

let!(:user) do
user = FactoryBot.create(:pro_user)
user.pro_account.stripe_customer_id = stripe_event.data.object.customer
user.pro_account.save!
user
let(:customer_id) { stripe_event.data.object.customer }

let(:pro_account) do
FactoryBot.create(:pro_account, stripe_customer_id: customer_id)
end

before do
send_request
allow(ProAccount).to receive(:find_by).
with(stripe_customer_id: customer_id).and_return(pro_account)
end

it 'handles the event' do
expect(response.status).to eq(200)
end

it 'notifies the user that their payment failed' do
mail = ActionMailer::Base.deliveries.first
expect(mail.subject).to match(/Payment failed/)
expect(mail.to).to include(user.email)
context 'the user has a subscription' do
before do
allow(pro_account).to receive(:subscription?).and_return(true)
send_request
end

it 'notifies the user that their payment failed' do
mail = ActionMailer::Base.deliveries.first
expect(mail.subject).to match(/Payment failed/)
expect(mail.to).to include(pro_account.user.email)
end
end

context 'the user does not have a subscription' do
before do
allow(pro_account).to receive(:subscription?).and_return(false)
send_request
end

it 'does not notify the user that their payment failed' do
mail = ActionMailer::Base.deliveries.first
expect(mail).to be_nil
end
end
end

Expand Down

0 comments on commit 8fbf59f

Please sign in to comment.