-
Notifications
You must be signed in to change notification settings - Fork 103
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
fix: Persist inbound stripe webhooks #2972
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ancorcruz
force-pushed
the
fix/persisted-webhooks
branch
2 times, most recently
from
December 17, 2024 16:49
c083548
to
d3e6956
Compare
nudded
reviewed
Dec 18, 2024
nudded
reviewed
Dec 18, 2024
ancorcruz
force-pushed
the
fix/persisted-webhooks
branch
from
December 19, 2024 16:09
8fb4b83
to
eb955bc
Compare
vincent-pochet
approved these changes
Dec 20, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌
nudded
reviewed
Dec 20, 2024
vincent-pochet
approved these changes
Dec 23, 2024
skip failed and processed.
in pending or processing status...
retriable is defined as inbound webhooks in: - processing status, and with processing_at earlier than 2 hours ago - pending status, and created_at earlier than 2 hours ago
only contruct event as paylaoad is already validated.
Co-authored-by: Vincent Pochet <[email protected]>
ancorcruz
force-pushed
the
fix/persisted-webhooks
branch
from
January 2, 2025 14:45
e2becd3
to
e9c1268
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Currently, when our application receives webhooks from third-party services (e.g., Stripe, GoCardless, Adyen), their payloads are immediately queued in Sidekiq for background processing. While this approach works in most cases, it introduces a critical reliability issue, especially in self-hosted environments where Sidekiq may lose jobs if workers are abruptly terminated or overloaded.
This unreliability has led to issues like pending transactions that are never resolved despite receiving valid webhooks. For example, a reported issue involved a wallet credit purchase where the payment settled on Stripe, the webhook was received, but the transaction remained pending indefinitely until manually resolved.
To address this, we aim to ensure that no webhook payload is lost and processing remains consistent, even under adverse conditions.
Description
This PR introduces a robust mechanism for handling inbound webhooks by persisting them in the database before processing. Key changes include:
Database Persistence:
inbound_webhooks
table immediately upon receipt. This ensures all webhook payloads are safely persisted in a reliable system before processing.Background Processing:
InboundWebhooks::CreateService
is introduced to persist the webhook and enqueue it for processing.InboundWebhooks::ProcessJob
Sidekiq worker processes the persisted webhooks, invoking theInboundWebhooks::ProcessService
InboundWebhooks::ProcessService
processes the persisted webhooks invoking the appropriate payment provider service.Retry Mechanisms:
Controller Update:
WebhooksController
actions (stripe, adyen, and gocardless) now use the InboundWebhooks::CreateService and respond with 200 OK once the webhook is persisted successfully.Backward Compatibility:
Key Changes
InboundWebhook
ActiveRecord model to persist webhook payloads with fields such assource
,event_type
,payload
,status
.InboundWebhooks::CreateService
to handle webhook persistence and Sidekiq job enqueueing.InboundWebhooks::ProcessJob
to process persisted webhooks from Sidekiq queue.InboundWebhooks::ProcessService
to call the respective provider's handle incoming webhook service.PaymentProviders::Stripe::HandleIncomingWebhookService
to process webhooks from the database instead of relying solely on direct Sidekiq queues.WebhooksController#stripe
action to integrate with the new service and workflow.Benefits
Reliability: Ensures no webhook is lost, even if Sidekiq workers fail.
Resilience: Provides a retry mechanism for failed webhook processing.
Transparency: Makes webhook processing traceable with statuses (pending, processing, processed, failed).