log invoice, etc. tokens

This commit is contained in:
Brandon Thomas committed 2025-09-12 01:00:04 -04:00
1 parent f272dbf401
commit 07722766dd
8 files changed
+34 -6

No files matched your search

@@ -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<WalletUpdateSummary> {
@@ -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,
@@ -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<WalletUpdateSummary> {
@@ -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,
@@ -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<String>,
}
@@ -125,4 +128,7 @@ pub struct SubscriptionPaidEvent {
/// TODO: - maybe not - This controls whether it is active or not.
pub maybe_cancel_at: Option<NaiveDateTime>,
pub maybe_canceled_at: Option<NaiveDateTime>,
/// Token to track in the wallet_ledger_events
pub ledger_event_ref: Option<String>,
}
@@ -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(())
}
@@ -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.
@@ -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?;
}
@@ -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,
})
@@ -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,
})