collab: Rework Stripe event processing (#15510)
This PR reworks how we process Stripe events for reconciliation purposes. The previous approach in #15480 turns out to not be workable, on account of the Stripe event IDs not being strictly in order. This meant that we couldn't reliably compare two arbitrary event IDs and determine which one was more recent. This new approach leans on the guidance that Stripe provides for webhooks events: > Webhook endpoints might occasionally receive the same event more than once. You can guard against duplicated event receipts by logging the [event IDs](https://docs.stripe.com/api/events/object#event_object-id) you’ve processed, and then not processing already-logged events. > > https://docs.stripe.com/webhooks#handle-duplicate-events We now record processed Stripe events in the `processed_stripe_events` table and use this to filter out events that have already been processed, so we do not process them again. When retrieving events from the Stripe events API we now buffer the unprocessed events so that we can sort them by their `created` timestamp and process them in (roughly) the order they occurred. Release Notes: - N/A
This commit is contained in:
parent
dca9400edf
commit
7c5f4b72fb
16 changed files with 242 additions and 158 deletions
|
@ -9,7 +9,6 @@ pub struct Model {
|
|||
pub id: BillingCustomerId,
|
||||
pub user_id: UserId,
|
||||
pub stripe_customer_id: String,
|
||||
pub last_stripe_event_id: Option<String>,
|
||||
pub created_at: DateTime,
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ pub struct Model {
|
|||
pub billing_customer_id: BillingCustomerId,
|
||||
pub stripe_subscription_id: String,
|
||||
pub stripe_subscription_status: StripeSubscriptionStatus,
|
||||
pub last_stripe_event_id: Option<String>,
|
||||
pub created_at: DateTime,
|
||||
}
|
||||
|
||||
|
|
16
crates/collab/src/db/tables/processed_stripe_event.rs
Normal file
16
crates/collab/src/db/tables/processed_stripe_event.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "processed_stripe_events")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub stripe_event_id: String,
|
||||
pub stripe_event_type: String,
|
||||
pub stripe_event_created_timestamp: i64,
|
||||
pub processed_at: DateTime,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
Loading…
Add table
Add a link
Reference in a new issue