Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#8565] Update payment intent descriptions #8582

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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