[PR #378] [MERGED] Persist Stripe webhook events and enforce idempotent processing #682

Closed
opened 2026-03-13 21:03:43 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/378
Author: @AJaySi
Created: 3/4/2026
Status: Merged
Merged: 3/5/2026
Merged by: @AJaySi

Base: mainHead: codex/add-webhook-event-persistence-to-stripeservice


📝 Commits (1)

  • c604dc8 Add Stripe webhook event persistence and idempotency

📊 Changes

2 files changed (+90 additions, -14 deletions)

View changed files

📝 backend/models/subscription_models.py (+16 -0)
📝 backend/services/subscription/stripe_service.py (+74 -14)

📄 Description

Motivation

  • Ensure Stripe webhook handlers are idempotent by recording processed event.id before applying subscription mutations to avoid double-processing on retries or concurrent deliveries.
  • Improve operational visibility and troubleshooting for webhook processing by storing lifecycle timestamps, attempt counts, and error details.

Description

  • Added a new ProcessedStripeEvent model mapped to the processed_stripe_events table with fields event_id, event_type, status, attempt_count, received_at, processing_started_at, processed_at, last_error, and updated_at.
  • Updated StripeService.handle_webhook to require event.id, persist or update a ProcessedStripeEvent record into processing state before dispatching handlers, and short-circuit with success when the event is already processed.
  • On successful handling, mark the record processed and set processed_at; on exceptions, set failed and capture a truncated last_error for observability and replay troubleshooting.
  • Added basic race protection around event creation using IntegrityError handling to avoid duplicate processing under concurrent deliveries.

Testing

  • Verified syntax/compilation with python -m py_compile backend/models/subscription_models.py backend/services/subscription/stripe_service.py, which completed successfully.

Codex Task


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/378 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/4/2026 **Status:** ✅ Merged **Merged:** 3/5/2026 **Merged by:** [@AJaySi](https://github.com/AJaySi) **Base:** `main` ← **Head:** `codex/add-webhook-event-persistence-to-stripeservice` --- ### 📝 Commits (1) - [`c604dc8`](https://github.com/AJaySi/ALwrity/commit/c604dc87ec36b89b3a983ccf46f7dc068894dcec) Add Stripe webhook event persistence and idempotency ### 📊 Changes **2 files changed** (+90 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `backend/models/subscription_models.py` (+16 -0) 📝 `backend/services/subscription/stripe_service.py` (+74 -14) </details> ### 📄 Description ### Motivation - Ensure Stripe webhook handlers are idempotent by recording processed `event.id` before applying subscription mutations to avoid double-processing on retries or concurrent deliveries. - Improve operational visibility and troubleshooting for webhook processing by storing lifecycle timestamps, attempt counts, and error details. ### Description - Added a new `ProcessedStripeEvent` model mapped to the `processed_stripe_events` table with fields `event_id`, `event_type`, `status`, `attempt_count`, `received_at`, `processing_started_at`, `processed_at`, `last_error`, and `updated_at`. - Updated `StripeService.handle_webhook` to require `event.id`, persist or update a `ProcessedStripeEvent` record into `processing` state before dispatching handlers, and short-circuit with success when the event is already `processed`. - On successful handling, mark the record `processed` and set `processed_at`; on exceptions, set `failed` and capture a truncated `last_error` for observability and replay troubleshooting. - Added basic race protection around event creation using `IntegrityError` handling to avoid duplicate processing under concurrent deliveries. ### Testing - Verified syntax/compilation with `python -m py_compile backend/models/subscription_models.py backend/services/subscription/stripe_service.py`, which completed successfully. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69a8469bea1083289791796db86e4683) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-13 21:03:43 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#682
No description provided.