From 07722766dd549e1f6216f38d99e60fe6c658ffb3 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 11 Sep 2025 00:35:01 -0400 Subject: [PATCH] log invoice, etc. tokens --- .../wallets/add_durable_banked_balance_to_wallet.rs | 5 ++--- .../wallets/refill_monthly_credits_balance_on_wallet.rs | 3 ++- .../artcraft_billing_action.rs | 6 ++++++ .../credits_pack/complete_credits_pack_purchase.rs | 8 +++++++- .../subscriptions/mark_subscription_as_paid.rs | 9 ++++++++- .../transactionally_fulfill_artcraft_billing_action.rs | 1 + .../invoice/invoice_paid_handler.rs | 7 +++++++ .../payment_intent/payment_intent_succeeded_extractor.rs | 1 + 8 files changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/schema/database/mysql_queries/src/queries/wallets/add_durable_banked_balance_to_wallet.rs b/crates/schema/database/mysql_queries/src/queries/wallets/add_durable_banked_balance_to_wallet.rs index 7dd7a076cd..a2db77d717 100644 --- a/crates/schema/database/mysql_queries/src/queries/wallets/add_durable_banked_balance_to_wallet.rs +++ b/crates/schema/database/mysql_queries/src/queries/wallets/add_durable_banked_balance_to_wallet.rs @@ -8,6 +8,7 @@ use tokens::tokens::wallets::WalletToken; pub async fn add_durable_banked_balance_to_wallet( wallet_token: &WalletToken, amount_to_add: u64, + maybe_ledger_ref: Option<&str>, transaction: &mut sqlx::Transaction<'_, MySql>, ) -> anyhow::Result { @@ -20,8 +21,6 @@ pub async fn add_durable_banked_balance_to_wallet( let existing_banked_balance = wallet.banked_credits; let new_banked_balance = existing_banked_balance.saturating_add(amount_to_add); - // TODO: Insert ledger event. - let result = sqlx::query!( r#" UPDATE wallets @@ -39,7 +38,7 @@ pub async fn add_durable_banked_balance_to_wallet( let record = InsertWalletLedgerEntry { wallet_token, entry_type: WalletLedgerEntryType::CreditBanked, - maybe_entity_ref: None, // TODO + maybe_entity_ref: maybe_ledger_ref.map(|t| t.to_string()), // Updated banked credits banked_credits_before: existing_banked_balance, diff --git a/crates/schema/database/mysql_queries/src/queries/wallets/refill_monthly_credits_balance_on_wallet.rs b/crates/schema/database/mysql_queries/src/queries/wallets/refill_monthly_credits_balance_on_wallet.rs index bcf6241350..2561a33a6f 100644 --- a/crates/schema/database/mysql_queries/src/queries/wallets/refill_monthly_credits_balance_on_wallet.rs +++ b/crates/schema/database/mysql_queries/src/queries/wallets/refill_monthly_credits_balance_on_wallet.rs @@ -8,6 +8,7 @@ use tokens::tokens::wallets::WalletToken; pub async fn refill_monthly_credits_balance_on_wallet( wallet_token: &WalletToken, monthly_amount: u64, + maybe_ledger_ref: Option<&str>, transaction: &mut sqlx::Transaction<'_, MySql>, ) -> anyhow::Result { @@ -37,7 +38,7 @@ pub async fn refill_monthly_credits_balance_on_wallet( let record = InsertWalletLedgerEntry { wallet_token, entry_type: WalletLedgerEntryType::CreditMonthly, - maybe_entity_ref: None, // TODO + maybe_entity_ref: maybe_ledger_ref.map(|s| s.to_string()), monthly_credits_before: existing_monthly_balance, monthly_credits_after: new_monthly_balance, diff --git a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/artcraft_billing_action.rs b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/artcraft_billing_action.rs index 74052db0ce..6e7cfd37e0 100644 --- a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/artcraft_billing_action.rs +++ b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/artcraft_billing_action.rs @@ -36,6 +36,9 @@ pub struct WalletCreditsPurchaseEvent { // NB: This is a multiplier on the pack's base value. pub quantity: u64, + + /// Token to track in the wallet_ledger_events + pub ledger_event_ref: Option, } @@ -125,4 +128,7 @@ pub struct SubscriptionPaidEvent { /// TODO: - maybe not - This controls whether it is active or not. pub maybe_cancel_at: Option, pub maybe_canceled_at: Option, + + /// Token to track in the wallet_ledger_events + pub ledger_event_ref: Option, } diff --git a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/credits_pack/complete_credits_pack_purchase.rs b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/credits_pack/complete_credits_pack_purchase.rs index fe87c056d9..b3bfeb2433 100644 --- a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/credits_pack/complete_credits_pack_purchase.rs +++ b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/credits_pack/complete_credits_pack_purchase.rs @@ -10,6 +10,7 @@ pub async fn complete_credits_pack_purchase( owner_user_token: &UserToken, pack: &StripeArtcraftCreditsPackInfo, quantity: u64, + maybe_ledger_ref: Option<&str>, transaction: &mut sqlx::Transaction<'_, sqlx::MySql>, ) -> anyhow::Result<()> { @@ -28,7 +29,12 @@ pub async fn complete_credits_pack_purchase( info!("Adding {} credits to wallet: {}", credits_purchased, wallet_token.as_str()); - let _result = add_durable_banked_balance_to_wallet(&wallet_token, credits_purchased, transaction).await?; + let _result = add_durable_banked_balance_to_wallet( + &wallet_token, + credits_purchased, + maybe_ledger_ref, + transaction, + ).await?; Ok(()) } diff --git a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/subscriptions/mark_subscription_as_paid.rs b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/subscriptions/mark_subscription_as_paid.rs index 0930b7956f..7e648c1bec 100644 --- a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/subscriptions/mark_subscription_as_paid.rs +++ b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/subscriptions/mark_subscription_as_paid.rs @@ -82,10 +82,17 @@ pub async fn mark_subscription_as_paid( }; let monthly_credits = details.artcraft_subscription.monthly_credits_amount; + + let maybe_ledger_ref = details.ledger_event_ref.as_deref(); info!("Adding {} monthly credits to wallet: {}", monthly_credits , wallet_token.as_str()); - let _result = refill_monthly_credits_balance_on_wallet(&wallet_token, monthly_credits, transaction).await?; + let _result = refill_monthly_credits_balance_on_wallet( + &wallet_token, + monthly_credits, + maybe_ledger_ref, + transaction + ).await?; // TODO: Insert wallet ledger entry. diff --git a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/transactionally_fulfill_artcraft_billing_action.rs b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/transactionally_fulfill_artcraft_billing_action.rs index d89383e2f6..ba51e18f46 100644 --- a/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/transactionally_fulfill_artcraft_billing_action.rs +++ b/crates/service/plugins/billing_artcraft/src/billing_action_fulfillment/transactionally_fulfill_artcraft_billing_action.rs @@ -23,6 +23,7 @@ pub async fn transactionally_fulfill_artcraft_billing_action( &purchase.owner_user_token, &purchase.pack, purchase.quantity, + purchase.ledger_event_ref.as_deref(), transaction, ).await?; } diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/invoice/invoice_paid_handler.rs b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/invoice/invoice_paid_handler.rs index 0d271c40b8..06c8e0d23b 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/invoice/invoice_paid_handler.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/invoice/invoice_paid_handler.rs @@ -117,7 +117,13 @@ pub async fn invoice_paid_handler( } }; + let ledger_event_ref = invoice.id + .as_ref() + .map(|id| id.to_string()) + .unwrap_or_else(|| stripe_event_descriptor.stripe_event_id.clone()); + info!("{} : invoice paid is for subscription.", stripe_event_descriptor); + Ok(EnrichedWebhookEvent { maybe_billing_action: Some(ArtcraftBillingAction::SubscriptionPaid(SubscriptionPaidEvent { @@ -137,6 +143,7 @@ pub async fn invoice_paid_handler( calculated_subscription_expires_at: subscription.current_billing_period_end, // TODO: This is incorrect. Use `calculate_subscription_end_date`. maybe_cancel_at: subscription.maybe_cancel_at, maybe_canceled_at: subscription.maybe_canceled_at, + ledger_event_ref: Some(ledger_event_ref), })), webhook_event_log_summary: event_log_summary, }) diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/payment_intent/payment_intent_succeeded_extractor.rs b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/payment_intent/payment_intent_succeeded_extractor.rs index 1db154e1ad..41cc34b368 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/payment_intent/payment_intent_succeeded_extractor.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_enrichment/payment_intent/payment_intent_succeeded_extractor.rs @@ -110,6 +110,7 @@ pub async fn payment_intent_succeeded_extractor( maybe_wallet_token, pack: credits_pack.clone(), quantity: purchase.quantity, + ledger_event_ref: Some(payment_intent_id), })), webhook_event_log_summary: event_log_summary, })