Skip to content

Commit bac93c9

Browse files
committed
Fix #44 add replay-failed-events.py
1 parent ac799d0 commit bac93c9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

runbooks/replay-failed-events.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import stripe
2+
import os
3+
import subprocess
4+
5+
"""
6+
Purpose: Replay completly failed webhook events
7+
8+
The above should not happen (due to retries)
9+
But if all retries fail, then eventually no more retries
10+
are performed. (Aka do better monitoring/alerting)
11+
12+
See
13+
https://docs.stripe.com/webhooks/process-undelivered-events?locale=en-GB
14+
"""
15+
16+
# Settings
17+
STRIPE_API_KEY = os.getenv("STRIPE_API_KEY")
18+
19+
# All subscribie shops sign-up through the 'master' subscribie shop
20+
# because dogfooding https://en.wikipedia.org/wiki/Eating_your_own_dog_food
21+
SUBSCRIBIE_MASTER_CONNECT_ACCOUNT_ID = os.getenv(
22+
"SUBSCRIBIE_MASTER_CONNECT_ACCOUNT_ID"
23+
) # noqa: E501
24+
WEBHOOK_ID = os.getenv("WEBHOOK_ID") # the endpoint prod webhook router
25+
26+
EVENTS_ENDING_BEFORE_EVENT_ID = os.getenv("EVENTS_ENDING_BEFORE_EVENT_ID")
27+
28+
stripe.api_key = STRIPE_API_KEY
29+
30+
events = stripe.Event.list(
31+
ending_before=EVENTS_ENDING_BEFORE_EVENT_ID, # Specify an event ID that was sent just before the webhook endpoint became unavailable. # noqa:E501
32+
# types=["payment_intent.succeeded", "payment_intent.payment_failed"], # all by default # noqa: E501
33+
delivery_success=False,
34+
stripe_account=SUBSCRIBIE_MASTER_CONNECT_ACCOUNT_ID,
35+
)
36+
37+
for event in events.auto_paging_iter():
38+
subprocess.run(
39+
f"stripe events resend --live --api-key {STRIPE_API_KEY} --account={SUBSCRIBIE_MASTER_CONNECT_ACCOUNT_ID} {event.id} --webhook-endpoint={WEBHOOK_ID}", # noqa: E501
40+
shell=True,
41+
)

0 commit comments

Comments
 (0)