Skip to content

Commit

Permalink
Update payment intent descriptions
Browse files Browse the repository at this point in the history
After successful payments ensure the payment intent description is
correct. This helps accounting procedures.

Fixes #8565
  • Loading branch information
gbp committed Feb 13, 2025
1 parent dface8e commit 16f5184
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
17 changes: 8 additions & 9 deletions app/controllers/alaveteli_pro/stripe_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ def customer_subscription_deleted
end

def invoice_payment_succeeded
charge_id = @stripe_event.data.object.charge

return unless charge_id

charge = Stripe::Charge.retrieve(charge_id)

subscription_id = @stripe_event.data.object.subscription
subscription = Stripe::Subscription.retrieve(
id: subscription_id, expand: ['plan.product']
)
plan_name = subscription.plan.product.name
description = "#{pro_site_name}: #{plan_name}"

Stripe::Charge.update(
charge.id, description: "#{pro_site_name}: #{plan_name}"
)
charge_id = @stripe_event.data.object.charge
Stripe::Charge.update(charge_id, description: description) if charge_id

payment_intent_id = @stripe_event.data.object.payment_intent
Stripe::PaymentIntent.update(
payment_intent_id, description: description
) if payment_intent_id
end

def invoice_payment_failed
Expand Down
3 changes: 2 additions & 1 deletion doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Highlighted Features

Add additional InfoRequest embargo scopes (Graeme Porteous)
* Update Stripe payment description after Pro payments (Graeme Porteous)
* Add additional InfoRequest embargo scopes (Graeme Porteous)

# 0.45.3.1

Expand Down
34 changes: 32 additions & 2 deletions spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@
plan: { id: stripe_plan.id }
}
],
subscription: stripe_subscription.id
subscription: stripe_subscription.id,
payment_intent: payment_intent.id
)
end

let(:paid_invoice) { invoice.pay }

let(:charge) { Stripe::Charge.retrieve(paid_invoice.charge) }

let(:payment_intent) do
Stripe::PaymentIntent.create(
amount: stripe_plan.amount,
currency: stripe_plan.currency
)
end

let(:stripe_event) do
StripeMock.mock_webhook_event(
'customer.subscription.deleted', items: stripe_subscription.items
Expand Down Expand Up @@ -388,6 +396,7 @@ def send_request
'invoice.payment_succeeded',
lines: paid_invoice.lines,
charge: paid_invoice.charge,
payment_intent: nil,
subscription: stripe_subscription.id
)
end
Expand All @@ -398,10 +407,31 @@ def send_request
end
end

context 'when there is a payment intent for an invoice' do
let(:stripe_event) do
StripeMock.mock_webhook_event(
'invoice.payment_succeeded',
lines: paid_invoice.lines,
charge: nil,
payment_intent: paid_invoice.payment_intent,
subscription: stripe_subscription.id
)
end

it 'updates the payment intent description with the site and plan name' do
expect(Stripe::PaymentIntent.retrieve(payment_intent.id).description).
to eq('Alaveteli Professional: Test')
end
end

context 'when there is no charge for an invoice' do
let(:stripe_event) do
StripeMock.mock_webhook_event(
'invoice.payment_succeeded', lines: paid_invoice.lines, charge: nil
'invoice.payment_succeeded',
lines: paid_invoice.lines,
charge: nil,
payment_intent: nil,
subscription: stripe_subscription.id
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"livemode": false,
"metadata": {},
"next_payment_attempt": null,
"payment_intent": null,
"paid": true,
"period_end": 1464084258,
"period_start": 1464084258,
Expand Down

0 comments on commit 16f5184

Please sign in to comment.