Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(billing): atomically credit paid reload webhooks (#7743)
Peter Chinman5d5bc657d5a0
diff --git a/.sqlx/query-59cfeed91e1becaca22994e1a481dbdf9ff112bc2150f1d33590aa10d3649704.json b/.sqlx/query-59cfeed91e1becaca22994e1a481dbdf9ff112bc2150f1d33590aa10d3649704.jsonnew file mode 100644index 0000000000..55e93bdb9a--- /dev/null+++ b/.sqlx/query-59cfeed91e1becaca22994e1a481dbdf9ff112bc2150f1d33590aa10d3649704.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "ALTER TABLE ai_credit_ledger DROP CONSTRAINT reject_test_reload",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "59cfeed91e1becaca22994e1a481dbdf9ff112bc2150f1d33590aa10d3649704"+}diff --git a/.sqlx/query-6892f4c0b364dc0e872a5b2f7d25f810f250f0c6538ba2ba5039028cff7cb0c9.json b/.sqlx/query-6892f4c0b364dc0e872a5b2f7d25f810f250f0c6538ba2ba5039028cff7cb0c9.jsonnew file mode 100644index 0000000000..3f401b9c86--- /dev/null+++ b/.sqlx/query-6892f4c0b364dc0e872a5b2f7d25f810f250f0c6538ba2ba5039028cff7cb0c9.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT status::text AS \"status!\" FROM ai_credit_reload WHERE stripe_invoice_id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "status!",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "6892f4c0b364dc0e872a5b2f7d25f810f250f0c6538ba2ba5039028cff7cb0c9"+}diff --git a/.sqlx/query-6d7fde916e4f434289c7d0d58238c59fe90acc05d923b75fcf7c7946509ec22b.json b/.sqlx/query-6d7fde916e4f434289c7d0d58238c59fe90acc05d923b75fcf7c7946509ec22b.jsonnew file mode 100644index 0000000000..71f7040ac9--- /dev/null+++ b/.sqlx/query-6d7fde916e4f434289c7d0d58238c59fe90acc05d923b75fcf7c7946509ec22b.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO ai_credit_ledger (id, user_id, kind, delta_cents, stripe_reference, note)\n VALUES ($1, $2, 'purchase', $3, $4, 'Automatic reload')\n ON CONFLICT (stripe_reference) WHERE stripe_reference IS NOT NULL DO NOTHING\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Int8",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "6d7fde916e4f434289c7d0d58238c59fe90acc05d923b75fcf7c7946509ec22b"+}diff --git a/.sqlx/query-9125e357358995b562dccd6ebf9e5a4bd85f21079aef642ebe3c1beb88e5cb9b.json b/.sqlx/query-9125e357358995b562dccd6ebf9e5a4bd85f21079aef642ebe3c1beb88e5cb9b.jsonnew file mode 100644index 0000000000..f8c6747dfb--- /dev/null+++ b/.sqlx/query-9125e357358995b562dccd6ebf9e5a4bd85f21079aef642ebe3c1beb88e5cb9b.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT user_id FROM ai_credit_reload WHERE stripe_invoice_id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "user_id",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "9125e357358995b562dccd6ebf9e5a4bd85f21079aef642ebe3c1beb88e5cb9b"+}diff --git a/.sqlx/query-e14a1cdd414be53099d0765b82256730865aaa0ba9a1f9e272851a0ca778e6c8.json b/.sqlx/query-e14a1cdd414be53099d0765b82256730865aaa0ba9a1f9e272851a0ca778e6c8.jsonnew file mode 100644index 0000000000..4792defcb2--- /dev/null+++ b/.sqlx/query-e14a1cdd414be53099d0765b82256730865aaa0ba9a1f9e272851a0ca778e6c8.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "ALTER TABLE ai_credit_ledger ADD CONSTRAINT reject_test_reload\n CHECK (stripe_reference <> 'in_retry')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "e14a1cdd414be53099d0765b82256730865aaa0ba9a1f9e272851a0ca778e6c8"+}diff --git a/crates/ai_billing/src/domain/ports.rs b/crates/ai_billing/src/domain/ports.rsindex 21cbce8515..fcf2a3fada 100644--- a/crates/ai_billing/src/domain/ports.rs+++ b/crates/ai_billing/src/domain/ports.rs@@ -344,6 +344,8 @@ pub trait BillingRepo: Send + Sync + 'static {/// amount when the invoice was one of ours *and* the status changed./// `Paid` is terminal: a late or duplicate failure event never un-pays a/// reload, and re-reporting the current status is a no-op.+ /// A transition to `Paid` atomically books the purchased credits, deduplicated+ /// by invoice, so a failed credit write leaves the status retryable.fn resolve_credit_reload_invoice(&self,stripe_invoice_id: &str,diff --git a/crates/ai_billing/src/domain/service.rs b/crates/ai_billing/src/domain/service.rsindex 85da05aaf7..1e5bd05113 100644--- a/crates/ai_billing/src/domain/service.rs+++ b/crates/ai_billing/src/domain/service.rs@@ -1040,11 +1040,7 @@ whereif !paid {return self.repo.suspend_auto_reload(&resolved.payer).await;}- // Idempotent on the invoice: the collector may have booked these- // credits before the webhook arrived.- self.repo- .record_credit_reload(&resolved.payer, resolved.amount_cents, stripe_invoice_id)- .await?;+ // Resolving a paid reload atomically books its credits with the status.// The reloaded credits cover any usage that was waiting on them.if let Err(e) = self.settle(&resolved.payer).await {tracing::warn!(error = ?e, "settlement after credit reload failed");diff --git a/crates/ai_billing/src/domain/service/test.rs b/crates/ai_billing/src/domain/service/test.rsindex 32955ef756..7c4c19848e 100644--- a/crates/ai_billing/src/domain/service/test.rs+++ b/crates/ai_billing/src/domain/service/test.rs@@ -618,13 +618,21 @@ impl BillingRepo for FakeRepo {&& r.status != CreditReloadStatus::Paid&& r.status != status});- Ok(found.map(|r| {+ let resolved = found.map(|r| {r.status = status;ResolvedReload {payer: user("[email protected]"),amount_cents: r.amount_cents,}- }))+ });+ if let Some(resolved) = &resolved+ && status == CreditReloadStatus::Paid+ && !s.purchases.iter().any(|r| r == stripe_invoice_id)+ {+ s.purchases.push(stripe_invoice_id.to_string());+ s.balance += resolved.amount_cents;+ }+ Ok(resolved)}async fn suspend_auto_reload(&self, _payer: &MacroUserIdStr<'_>) -> Result<()> {self.state.lock().unwrap().auto_reload_suspended = true;diff --git a/crates/ai_billing/src/outbound/pg_billing_repo.rs b/crates/ai_billing/src/outbound/pg_billing_repo.rsindex 0d3970f8c6..8f5d625385 100644--- a/crates/ai_billing/src/outbound/pg_billing_repo.rs+++ b/crates/ai_billing/src/outbound/pg_billing_repo.rs@@ -54,6 +54,30 @@ fn invalid_payer(e: impl std::fmt::Display) -> BillingError {BillingError::Storage(anyhow::anyhow!("invalid payer id: {e}"))}+async fn book_credit_reload(+ conn: &mut sqlx::PgConnection,+ payer: &MacroUserIdStr<'_>,+ amount_cents: i64,+ stripe_invoice_id: &str,+) -> Result<bool> {+ let id = macro_uuid::generate_uuid_v7();+ let result = sqlx::query!(+ r#"+ INSERT INTO ai_credit_ledger (id, user_id, kind, delta_cents, stripe_reference, note)+ VALUES ($1, $2, 'purchase', $3, $4, 'Automatic reload')+ ON CONFLICT (stripe_reference) WHERE stripe_reference IS NOT NULL DO NOTHING+ "#,+ id,+ payer.as_ref(),+ amount_cents,+ stripe_invoice_id,+ )+ .execute(conn)+ .await+ .map_err(storage)?;+ Ok(result.rows_affected() == 1)+}+impl BillingRepo for PgBillingRepo {async fn legacy_seats(&self,@@ -958,27 +982,13 @@ impl BillingRepo for PgBillingRepo {amount_cents: i64,stripe_invoice_id: &str,) -> Result<bool> {- let id = macro_uuid::generate_uuid_v7();let mut tx = self.pool.begin().await.map_err(storage)?;lock_payer(&mut tx, payer.as_ref()).await.map_err(funding_storage)?;- let result = sqlx::query!(- r#"- INSERT INTO ai_credit_ledger (id, user_id, kind, delta_cents, stripe_reference, note)- VALUES ($1, $2, 'purchase', $3, $4, 'Automatic reload')- ON CONFLICT (stripe_reference) WHERE stripe_reference IS NOT NULL DO NOTHING- "#,- id,- payer.as_ref(),- amount_cents,- stripe_invoice_id,- )- .execute(&mut *tx)- .await- .map_err(storage)?;+ let booked = book_credit_reload(&mut tx, payer, amount_cents, stripe_invoice_id).await?;tx.commit().await.map_err(storage)?;- Ok(result.rows_affected() == 1)+ Ok(booked)}async fn resolve_credit_reload_invoice(@@ -986,10 +996,26 @@ impl BillingRepo for PgBillingRepo {stripe_invoice_id: &str,status: CreditReloadStatus,) -> Result<Option<ResolvedReload>> {+ let mut tx = self.pool.begin().await.map_err(storage)?;+ let Some(payer) = sqlx::query_scalar!(+ "SELECT user_id FROM ai_credit_reload WHERE stripe_invoice_id = $1",+ stripe_invoice_id,+ )+ .fetch_optional(&mut *tx)+ .await+ .map_err(storage)?+ else {+ return Ok(None);+ };+ let payer = MacroUserIdStr::try_from(payer).map_err(invalid_payer)?;+ // Match the account-before-reload lock order used by reload reservation.+ lock_payer(&mut tx, payer.as_ref())+ .await+ .map_err(funding_storage)?;// `paid` is terminal and re-reporting the current status changes// nothing, so a late or duplicate webhook cannot un-pay a reload or// book its credits twice.- let row = sqlx::query!(+ let Some(row) = sqlx::query!(r#"UPDATE ai_credit_reloadSET status = ($2::text)::ai_credit_reload_status, updated_at = NOW()@@ -1001,16 +1027,27 @@ impl BillingRepo for PgBillingRepo {stripe_invoice_id,status.to_string(),)- .fetch_optional(&self.pool)+ .fetch_optional(&mut *tx).await- .map_err(storage)?;- row.map(|r| {- Ok(ResolvedReload {- payer: MacroUserIdStr::try_from(r.user_id).map_err(invalid_payer)?,- amount_cents: r.amount_cents,- })- })- .transpose()+ .map_err(storage)?+ else {+ return Ok(None);+ };+ let resolved = ResolvedReload {+ payer: MacroUserIdStr::try_from(row.user_id).map_err(invalid_payer)?,+ amount_cents: row.amount_cents,+ };+ if status == CreditReloadStatus::Paid {+ book_credit_reload(+ &mut tx,+ &resolved.payer,+ resolved.amount_cents,+ stripe_invoice_id,+ )+ .await?;+ }+ tx.commit().await.map_err(storage)?;+ Ok(Some(resolved))}async fn suspend_auto_reload(&self, payer: &MacroUserIdStr<'_>) -> Result<()> {diff --git a/crates/ai_billing/src/outbound/pg_billing_repo/test.rs b/crates/ai_billing/src/outbound/pg_billing_repo/test.rsindex 00a2b958d9..92795be85d 100644--- a/crates/ai_billing/src/outbound/pg_billing_repo/test.rs+++ b/crates/ai_billing/src/outbound/pg_billing_repo/test.rs@@ -1013,6 +1013,7 @@ async fn reload_invoice_webhooks_resolve_once_and_never_unpay(pool: PgPool) {.expect("status changed");assert_eq!(paid.payer.as_ref(), payer().as_ref());assert_eq!(paid.amount_cents, 10_000);+ assert_eq!(repo.credit_balance_cents(&payer()).await.unwrap(), 10_000);assert!(repo.resolve_credit_reload_invoice("in_1", CreditReloadStatus::Paid).await@@ -1026,6 +1027,98 @@ async fn reload_invoice_webhooks_resolve_once_and_never_unpay(pool: PgPool) {.unwrap().is_none());+ assert_eq!(repo.credit_balance_cents(&payer()).await.unwrap(), 10_000);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn reload_invoice_credit_write_failure_leaves_paid_webhook_retryable(pool: PgPool) {+ let repo = PgBillingRepo::new(pool.clone(), AiPricing::testing());+ let now = Utc::now();+ enable_auto_reload(&repo, &thresholds(None)).await;+ let reserved = repo+ .reserve_credit_reload(&payer(), now, 0, now)+ .await+ .unwrap()+ .expect("reload reserved");+ repo.finish_credit_reload(reserved.id, Some("in_retry"), CreditReloadStatus::Pending)+ .await+ .unwrap();++ // Force the ledger INSERT to fail after the paid-status UPDATE executes.+ sqlx::query!(+ "ALTER TABLE ai_credit_ledger ADD CONSTRAINT reject_test_reload+ CHECK (stripe_reference <> 'in_retry')"+ )+ .execute(&pool)+ .await+ .unwrap();+ assert!(+ repo.resolve_credit_reload_invoice("in_retry", CreditReloadStatus::Paid)+ .await+ .is_err()+ );+ assert_eq!(repo.credit_balance_cents(&payer()).await.unwrap(), 0);+ let status = sqlx::query_scalar!(+ r#"SELECT status::text AS "status!" FROM ai_credit_reload WHERE stripe_invoice_id = $1"#,+ "in_retry",+ )+ .fetch_one(&pool)+ .await+ .unwrap();+ assert_eq!(status, "pending");++ sqlx::query!("ALTER TABLE ai_credit_ledger DROP CONSTRAINT reject_test_reload")+ .execute(&pool)+ .await+ .unwrap();+ assert!(+ repo.resolve_credit_reload_invoice("in_retry", CreditReloadStatus::Paid)+ .await+ .unwrap()+ .is_some()+ );+ assert_eq!(repo.credit_balance_cents(&payer()).await.unwrap(), 10_000);+ // Re-delivery cannot issue the purchased credits twice.+ assert!(+ repo.resolve_credit_reload_invoice("in_retry", CreditReloadStatus::Paid)+ .await+ .unwrap()+ .is_none()+ );+ assert_eq!(repo.credit_balance_cents(&payer()).await.unwrap(), 10_000);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn reload_invoice_webhook_deduplicates_credits_booked_by_collector(pool: PgPool) {+ let repo = PgBillingRepo::new(pool, AiPricing::testing());+ let now = Utc::now();+ enable_auto_reload(&repo, &thresholds(None)).await;+ let reserved = repo+ .reserve_credit_reload(&payer(), now, 0, now)+ .await+ .unwrap()+ .expect("reload reserved");+ repo.finish_credit_reload(+ reserved.id,+ Some("in_collector"),+ CreditReloadStatus::Pending,+ )+ .await+ .unwrap();+ repo.record_credit_reload(&payer(), reserved.amount_cents, "in_collector")+ .await+ .unwrap();++ assert!(+ repo.resolve_credit_reload_invoice("in_collector", CreditReloadStatus::Paid)+ .await+ .unwrap()+ .is_some()+ );+ assert_eq!(+ repo.credit_balance_cents(&payer()).await.unwrap(),+ reserved.amount_cents+ );}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]