Skip to content

Commit f1725ed

Browse files
committed
Add retry to sync_checkout_session to handle subscriptions that have a slight delay after completing the session
1 parent d5133f6 commit f1725ed

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
### Unreleased
44

5+
* Add `retry` to `Pay::Stripe.sync_checkout_session`
6+
Subscriptions aren't instantly attached to a Checkout Session, so this retry gives a chance for completion.
7+
This helps build a more seamless experience when redirecting after a successful checkout so the next page knows the user has subscribed successfully.
8+
59
### 11.1.0
610

711
* Add text parts to emails

app/models/pay/stripe/charge.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ def self.sync(charge_id, object: nil, stripe_account: nil, try: 0, retries: 1)
6565
create!(attrs.merge(customer: pay_customer, processor_id: object.id))
6666
end
6767
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
68-
try += 1
69-
raise unless try <= retries
70-
71-
sleep 0.1
72-
retry
68+
if try > retries
69+
raise
70+
else
71+
try += 1
72+
sleep 0.15**try
73+
retry
74+
end
7375
end
7476

7577
def api_record

app/models/pay/stripe/payment_method.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def self.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1)
4141
pay_payment_method.update!(attributes)
4242
pay_payment_method
4343
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
44-
try += 1
45-
if try <= retries
46-
sleep 0.1
47-
retry
48-
else
44+
if try > retries
4945
raise
46+
else
47+
try += 1
48+
sleep 0.15**try
49+
retry
5050
end
5151
end
5252

lib/pay/stripe.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,25 @@ def self.find_by_client_reference_id(client_reference_id)
142142
nil
143143
end
144144

145-
def self.sync_checkout_session(session_id, stripe_account: nil)
145+
# Subscriptions aren't always immediately associated, so we want to retry by default
146+
def self.sync_checkout_session(session_id, stripe_account: nil, try: 0, retries: 5)
146147
checkout_session = ::Stripe::Checkout::Session.retrieve({id: session_id, expand: ["payment_intent.latest_charge"]}, {stripe_account: stripe_account}.compact)
147148
case checkout_session.mode
148149
when "payment"
149150
if (id = checkout_session.payment_intent.try(:latest_charge)&.id)
150-
Pay::Stripe::Charge.sync(id, stripe_account: stripe_account)
151+
Pay::Stripe::Charge.sync(id, stripe_account: stripe_account, retries: 5)
151152
end
152153
when "subscription"
153154
Pay::Stripe::Subscription.sync(checkout_session.subscription, stripe_account: stripe_account)
154155
end
156+
rescue ::Stripe::InvalidRequestError
157+
if try > retries
158+
raise
159+
else
160+
try += 1
161+
sleep 0.15**try
162+
retry
163+
end
155164
end
156165
end
157166
end

0 commit comments

Comments
 (0)