collab: Record cancellation reason on billing subscriptions (#22853)
This PR updates the `billing_subscriptions` in the database to record the cancellation reason from Stripe. We're primarily interested in this so we can check for subscriptions that were canceled for being `past_due`. Release Notes: - N/A
This commit is contained in:
parent
69dde8e31d
commit
b78396505f
5 changed files with 43 additions and 8 deletions
|
@ -12,8 +12,8 @@ use serde::{Deserialize, Serialize};
|
|||
use serde_json::json;
|
||||
use std::{str::FromStr, sync::Arc, time::Duration};
|
||||
use stripe::{
|
||||
BillingPortalSession, CreateBillingPortalSession, CreateBillingPortalSessionFlowData,
|
||||
CreateBillingPortalSessionFlowDataAfterCompletion,
|
||||
BillingPortalSession, CancellationDetailsReason, CreateBillingPortalSession,
|
||||
CreateBillingPortalSessionFlowData, CreateBillingPortalSessionFlowDataAfterCompletion,
|
||||
CreateBillingPortalSessionFlowDataAfterCompletionRedirect,
|
||||
CreateBillingPortalSessionFlowDataType, CreateCustomer, Customer, CustomerId, EventObject,
|
||||
EventType, Expandable, ListEvents, Subscription, SubscriptionId, SubscriptionStatus,
|
||||
|
@ -21,8 +21,10 @@ use stripe::{
|
|||
use util::ResultExt;
|
||||
|
||||
use crate::api::events::SnowflakeRow;
|
||||
use crate::db::billing_subscription::{StripeCancellationReason, StripeSubscriptionStatus};
|
||||
use crate::llm::{DEFAULT_MAX_MONTHLY_SPEND, FREE_TIER_MONTHLY_SPENDING_LIMIT};
|
||||
use crate::rpc::{ResultExt as _, Server};
|
||||
use crate::{db::UserId, llm::db::LlmDatabase};
|
||||
use crate::{
|
||||
db::{
|
||||
billing_customer, BillingSubscriptionId, CreateBillingCustomerParams,
|
||||
|
@ -32,10 +34,6 @@ use crate::{
|
|||
},
|
||||
stripe_billing::StripeBilling,
|
||||
};
|
||||
use crate::{
|
||||
db::{billing_subscription::StripeSubscriptionStatus, UserId},
|
||||
llm::db::LlmDatabase,
|
||||
};
|
||||
use crate::{AppState, Cents, Error, Result};
|
||||
|
||||
pub fn router() -> Router {
|
||||
|
@ -679,6 +677,12 @@ async fn handle_customer_subscription_event(
|
|||
.and_then(|cancel_at| DateTime::from_timestamp(cancel_at, 0))
|
||||
.map(|time| time.naive_utc()),
|
||||
),
|
||||
stripe_cancellation_reason: ActiveValue::set(
|
||||
subscription
|
||||
.cancellation_details
|
||||
.and_then(|details| details.reason)
|
||||
.map(|reason| reason.into()),
|
||||
),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
@ -791,6 +795,16 @@ impl From<SubscriptionStatus> for StripeSubscriptionStatus {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<CancellationDetailsReason> for StripeCancellationReason {
|
||||
fn from(value: CancellationDetailsReason) -> Self {
|
||||
match value {
|
||||
CancellationDetailsReason::CancellationRequested => Self::CancellationRequested,
|
||||
CancellationDetailsReason::PaymentDisputed => Self::PaymentDisputed,
|
||||
CancellationDetailsReason::PaymentFailed => Self::PaymentFailed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds or creates a billing customer using the provided customer.
|
||||
async fn find_or_create_billing_customer(
|
||||
app: &Arc<AppState>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue