1use anyhow::{anyhow, bail, Context};
2use axum::{
3 extract::{self, Query},
4 routing::{get, post},
5 Extension, Json, Router,
6};
7use chrono::{DateTime, SecondsFormat, Utc};
8use collections::HashSet;
9use reqwest::StatusCode;
10use sea_orm::ActiveValue;
11use serde::{Deserialize, Serialize};
12use serde_json::json;
13use std::{str::FromStr, sync::Arc, time::Duration};
14use stripe::{
15 BillingPortalSession, CancellationDetailsReason, CreateBillingPortalSession,
16 CreateBillingPortalSessionFlowData, CreateBillingPortalSessionFlowDataAfterCompletion,
17 CreateBillingPortalSessionFlowDataAfterCompletionRedirect,
18 CreateBillingPortalSessionFlowDataType, CreateCustomer, Customer, CustomerId, EventObject,
19 EventType, Expandable, ListEvents, Subscription, SubscriptionId, SubscriptionStatus,
20};
21use util::ResultExt;
22
23use crate::api::events::SnowflakeRow;
24use crate::db::billing_subscription::{StripeCancellationReason, StripeSubscriptionStatus};
25use crate::llm::{DEFAULT_MAX_MONTHLY_SPEND, FREE_TIER_MONTHLY_SPENDING_LIMIT};
26use crate::rpc::{ResultExt as _, Server};
27use crate::{db::UserId, llm::db::LlmDatabase};
28use crate::{
29 db::{
30 billing_customer, BillingSubscriptionId, CreateBillingCustomerParams,
31 CreateBillingSubscriptionParams, CreateProcessedStripeEventParams,
32 UpdateBillingCustomerParams, UpdateBillingPreferencesParams,
33 UpdateBillingSubscriptionParams,
34 },
35 stripe_billing::StripeBilling,
36};
37use crate::{AppState, Cents, Error, Result};
38
39pub fn router() -> Router {
40 Router::new()
41 .route(
42 "/billing/preferences",
43 get(get_billing_preferences).put(update_billing_preferences),
44 )
45 .route(
46 "/billing/subscriptions",
47 get(list_billing_subscriptions).post(create_billing_subscription),
48 )
49 .route(
50 "/billing/subscriptions/manage",
51 post(manage_billing_subscription),
52 )
53 .route("/billing/monthly_spend", get(get_monthly_spend))
54}
55
56#[derive(Debug, Deserialize)]
57struct GetBillingPreferencesParams {
58 github_user_id: i32,
59}
60
61#[derive(Debug, Serialize)]
62struct BillingPreferencesResponse {
63 max_monthly_llm_usage_spending_in_cents: i32,
64}
65
66async fn get_billing_preferences(
67 Extension(app): Extension<Arc<AppState>>,
68 Query(params): Query<GetBillingPreferencesParams>,
69) -> Result<Json<BillingPreferencesResponse>> {
70 let user = app
71 .db
72 .get_user_by_github_user_id(params.github_user_id)
73 .await?
74 .ok_or_else(|| anyhow!("user not found"))?;
75
76 let preferences = app.db.get_billing_preferences(user.id).await?;
77
78 Ok(Json(BillingPreferencesResponse {
79 max_monthly_llm_usage_spending_in_cents: preferences
80 .map_or(DEFAULT_MAX_MONTHLY_SPEND.0 as i32, |preferences| {
81 preferences.max_monthly_llm_usage_spending_in_cents
82 }),
83 }))
84}
85
86#[derive(Debug, Deserialize)]
87struct UpdateBillingPreferencesBody {
88 github_user_id: i32,
89 max_monthly_llm_usage_spending_in_cents: i32,
90}
91
92async fn update_billing_preferences(
93 Extension(app): Extension<Arc<AppState>>,
94 Extension(rpc_server): Extension<Arc<crate::rpc::Server>>,
95 extract::Json(body): extract::Json<UpdateBillingPreferencesBody>,
96) -> Result<Json<BillingPreferencesResponse>> {
97 let user = app
98 .db
99 .get_user_by_github_user_id(body.github_user_id)
100 .await?
101 .ok_or_else(|| anyhow!("user not found"))?;
102
103 let max_monthly_llm_usage_spending_in_cents =
104 body.max_monthly_llm_usage_spending_in_cents.max(0);
105
106 let billing_preferences =
107 if let Some(_billing_preferences) = app.db.get_billing_preferences(user.id).await? {
108 app.db
109 .update_billing_preferences(
110 user.id,
111 &UpdateBillingPreferencesParams {
112 max_monthly_llm_usage_spending_in_cents: ActiveValue::set(
113 max_monthly_llm_usage_spending_in_cents,
114 ),
115 },
116 )
117 .await?
118 } else {
119 app.db
120 .create_billing_preferences(
121 user.id,
122 &crate::db::CreateBillingPreferencesParams {
123 max_monthly_llm_usage_spending_in_cents,
124 },
125 )
126 .await?
127 };
128
129 SnowflakeRow::new(
130 "Spend Limit Updated",
131 user.metrics_id,
132 user.admin,
133 None,
134 json!({
135 "user_id": user.id,
136 "max_monthly_llm_usage_spending_in_cents": billing_preferences.max_monthly_llm_usage_spending_in_cents,
137 }),
138 )
139 .write(&app.kinesis_client, &app.config.kinesis_stream)
140 .await
141 .log_err();
142
143 rpc_server.refresh_llm_tokens_for_user(user.id).await;
144
145 Ok(Json(BillingPreferencesResponse {
146 max_monthly_llm_usage_spending_in_cents: billing_preferences
147 .max_monthly_llm_usage_spending_in_cents,
148 }))
149}
150
151#[derive(Debug, Deserialize)]
152struct ListBillingSubscriptionsParams {
153 github_user_id: i32,
154}
155
156#[derive(Debug, Serialize)]
157struct BillingSubscriptionJson {
158 id: BillingSubscriptionId,
159 name: String,
160 status: StripeSubscriptionStatus,
161 cancel_at: Option<String>,
162 /// Whether this subscription can be canceled.
163 is_cancelable: bool,
164}
165
166#[derive(Debug, Serialize)]
167struct ListBillingSubscriptionsResponse {
168 subscriptions: Vec<BillingSubscriptionJson>,
169}
170
171async fn list_billing_subscriptions(
172 Extension(app): Extension<Arc<AppState>>,
173 Query(params): Query<ListBillingSubscriptionsParams>,
174) -> Result<Json<ListBillingSubscriptionsResponse>> {
175 let user = app
176 .db
177 .get_user_by_github_user_id(params.github_user_id)
178 .await?
179 .ok_or_else(|| anyhow!("user not found"))?;
180
181 let subscriptions = app.db.get_billing_subscriptions(user.id).await?;
182
183 Ok(Json(ListBillingSubscriptionsResponse {
184 subscriptions: subscriptions
185 .into_iter()
186 .map(|subscription| BillingSubscriptionJson {
187 id: subscription.id,
188 name: "Zed LLM Usage".to_string(),
189 status: subscription.stripe_subscription_status,
190 cancel_at: subscription.stripe_cancel_at.map(|cancel_at| {
191 cancel_at
192 .and_utc()
193 .to_rfc3339_opts(SecondsFormat::Millis, true)
194 }),
195 is_cancelable: subscription.stripe_subscription_status.is_cancelable()
196 && subscription.stripe_cancel_at.is_none(),
197 })
198 .collect(),
199 }))
200}
201
202#[derive(Debug, Deserialize)]
203struct CreateBillingSubscriptionBody {
204 github_user_id: i32,
205}
206
207#[derive(Debug, Serialize)]
208struct CreateBillingSubscriptionResponse {
209 checkout_session_url: String,
210}
211
212/// Initiates a Stripe Checkout session for creating a billing subscription.
213async fn create_billing_subscription(
214 Extension(app): Extension<Arc<AppState>>,
215 extract::Json(body): extract::Json<CreateBillingSubscriptionBody>,
216) -> Result<Json<CreateBillingSubscriptionResponse>> {
217 let user = app
218 .db
219 .get_user_by_github_user_id(body.github_user_id)
220 .await?
221 .ok_or_else(|| anyhow!("user not found"))?;
222
223 let Some(stripe_client) = app.stripe_client.clone() else {
224 log::error!("failed to retrieve Stripe client");
225 Err(Error::http(
226 StatusCode::NOT_IMPLEMENTED,
227 "not supported".into(),
228 ))?
229 };
230 let Some(stripe_billing) = app.stripe_billing.clone() else {
231 log::error!("failed to retrieve Stripe billing object");
232 Err(Error::http(
233 StatusCode::NOT_IMPLEMENTED,
234 "not supported".into(),
235 ))?
236 };
237 let Some(llm_db) = app.llm_db.clone() else {
238 log::error!("failed to retrieve LLM database");
239 Err(Error::http(
240 StatusCode::NOT_IMPLEMENTED,
241 "not supported".into(),
242 ))?
243 };
244
245 if app.db.has_active_billing_subscription(user.id).await? {
246 return Err(Error::http(
247 StatusCode::CONFLICT,
248 "user already has an active subscription".into(),
249 ));
250 }
251
252 let existing_billing_customer = app.db.get_billing_customer_by_user_id(user.id).await?;
253 if let Some(existing_billing_customer) = &existing_billing_customer {
254 if existing_billing_customer.has_overdue_invoices {
255 return Err(Error::http(
256 StatusCode::PAYMENT_REQUIRED,
257 "user has overdue invoices".into(),
258 ));
259 }
260 }
261
262 let customer_id = if let Some(existing_customer) = existing_billing_customer {
263 CustomerId::from_str(&existing_customer.stripe_customer_id)
264 .context("failed to parse customer ID")?
265 } else {
266 let customer = Customer::create(
267 &stripe_client,
268 CreateCustomer {
269 email: user.email_address.as_deref(),
270 ..Default::default()
271 },
272 )
273 .await?;
274
275 customer.id
276 };
277
278 let default_model = llm_db.model(rpc::LanguageModelProvider::Anthropic, "claude-3-5-sonnet")?;
279 let stripe_model = stripe_billing.register_model(default_model).await?;
280 let success_url = format!(
281 "{}/account?checkout_complete=1",
282 app.config.zed_dot_dev_url()
283 );
284 let checkout_session_url = stripe_billing
285 .checkout(customer_id, &user.github_login, &stripe_model, &success_url)
286 .await?;
287 Ok(Json(CreateBillingSubscriptionResponse {
288 checkout_session_url,
289 }))
290}
291
292#[derive(Debug, PartialEq, Deserialize)]
293#[serde(rename_all = "snake_case")]
294enum ManageSubscriptionIntent {
295 /// The user intends to cancel their subscription.
296 Cancel,
297 /// The user intends to stop the cancellation of their subscription.
298 StopCancellation,
299}
300
301#[derive(Debug, Deserialize)]
302struct ManageBillingSubscriptionBody {
303 github_user_id: i32,
304 intent: ManageSubscriptionIntent,
305 /// The ID of the subscription to manage.
306 subscription_id: BillingSubscriptionId,
307}
308
309#[derive(Debug, Serialize)]
310struct ManageBillingSubscriptionResponse {
311 billing_portal_session_url: Option<String>,
312}
313
314/// Initiates a Stripe customer portal session for managing a billing subscription.
315async fn manage_billing_subscription(
316 Extension(app): Extension<Arc<AppState>>,
317 extract::Json(body): extract::Json<ManageBillingSubscriptionBody>,
318) -> Result<Json<ManageBillingSubscriptionResponse>> {
319 let user = app
320 .db
321 .get_user_by_github_user_id(body.github_user_id)
322 .await?
323 .ok_or_else(|| anyhow!("user not found"))?;
324
325 let Some(stripe_client) = app.stripe_client.clone() else {
326 log::error!("failed to retrieve Stripe client");
327 Err(Error::http(
328 StatusCode::NOT_IMPLEMENTED,
329 "not supported".into(),
330 ))?
331 };
332
333 let customer = app
334 .db
335 .get_billing_customer_by_user_id(user.id)
336 .await?
337 .ok_or_else(|| anyhow!("billing customer not found"))?;
338 let customer_id = CustomerId::from_str(&customer.stripe_customer_id)
339 .context("failed to parse customer ID")?;
340
341 let subscription = app
342 .db
343 .get_billing_subscription_by_id(body.subscription_id)
344 .await?
345 .ok_or_else(|| anyhow!("subscription not found"))?;
346
347 if body.intent == ManageSubscriptionIntent::StopCancellation {
348 let subscription_id = SubscriptionId::from_str(&subscription.stripe_subscription_id)
349 .context("failed to parse subscription ID")?;
350
351 let updated_stripe_subscription = Subscription::update(
352 &stripe_client,
353 &subscription_id,
354 stripe::UpdateSubscription {
355 cancel_at_period_end: Some(false),
356 ..Default::default()
357 },
358 )
359 .await?;
360
361 app.db
362 .update_billing_subscription(
363 subscription.id,
364 &UpdateBillingSubscriptionParams {
365 stripe_cancel_at: ActiveValue::set(
366 updated_stripe_subscription
367 .cancel_at
368 .and_then(|cancel_at| DateTime::from_timestamp(cancel_at, 0))
369 .map(|time| time.naive_utc()),
370 ),
371 ..Default::default()
372 },
373 )
374 .await?;
375
376 return Ok(Json(ManageBillingSubscriptionResponse {
377 billing_portal_session_url: None,
378 }));
379 }
380
381 let flow = match body.intent {
382 ManageSubscriptionIntent::Cancel => CreateBillingPortalSessionFlowData {
383 type_: CreateBillingPortalSessionFlowDataType::SubscriptionCancel,
384 after_completion: Some(CreateBillingPortalSessionFlowDataAfterCompletion {
385 type_: stripe::CreateBillingPortalSessionFlowDataAfterCompletionType::Redirect,
386 redirect: Some(CreateBillingPortalSessionFlowDataAfterCompletionRedirect {
387 return_url: format!("{}/account", app.config.zed_dot_dev_url()),
388 }),
389 ..Default::default()
390 }),
391 subscription_cancel: Some(
392 stripe::CreateBillingPortalSessionFlowDataSubscriptionCancel {
393 subscription: subscription.stripe_subscription_id,
394 retention: None,
395 },
396 ),
397 ..Default::default()
398 },
399 ManageSubscriptionIntent::StopCancellation => unreachable!(),
400 };
401
402 let mut params = CreateBillingPortalSession::new(customer_id);
403 params.flow_data = Some(flow);
404 let return_url = format!("{}/account", app.config.zed_dot_dev_url());
405 params.return_url = Some(&return_url);
406
407 let session = BillingPortalSession::create(&stripe_client, params).await?;
408
409 Ok(Json(ManageBillingSubscriptionResponse {
410 billing_portal_session_url: Some(session.url),
411 }))
412}
413
414/// The amount of time we wait in between each poll of Stripe events.
415///
416/// This value should strike a balance between:
417/// 1. Being short enough that we update quickly when something in Stripe changes
418/// 2. Being long enough that we don't eat into our rate limits.
419///
420/// As a point of reference, the Sequin folks say they have this at **500ms**:
421///
422/// > We poll the Stripe /events endpoint every 500ms per account
423/// >
424/// > — https://blog.sequinstream.com/events-not-webhooks/
425const POLL_EVENTS_INTERVAL: Duration = Duration::from_secs(5);
426
427/// The maximum number of events to return per page.
428///
429/// We set this to 100 (the max) so we have to make fewer requests to Stripe.
430///
431/// > Limit can range between 1 and 100, and the default is 10.
432const EVENTS_LIMIT_PER_PAGE: u64 = 100;
433
434/// The number of pages consisting entirely of already-processed events that we
435/// will see before we stop retrieving events.
436///
437/// This is used to prevent over-fetching the Stripe events API for events we've
438/// already seen and processed.
439const NUMBER_OF_ALREADY_PROCESSED_PAGES_BEFORE_WE_STOP: usize = 4;
440
441/// Polls the Stripe events API periodically to reconcile the records in our
442/// database with the data in Stripe.
443pub fn poll_stripe_events_periodically(app: Arc<AppState>, rpc_server: Arc<Server>) {
444 let Some(stripe_client) = app.stripe_client.clone() else {
445 log::warn!("failed to retrieve Stripe client");
446 return;
447 };
448
449 let executor = app.executor.clone();
450 executor.spawn_detached({
451 let executor = executor.clone();
452 async move {
453 loop {
454 poll_stripe_events(&app, &rpc_server, &stripe_client)
455 .await
456 .log_err();
457
458 executor.sleep(POLL_EVENTS_INTERVAL).await;
459 }
460 }
461 });
462}
463
464async fn poll_stripe_events(
465 app: &Arc<AppState>,
466 rpc_server: &Arc<Server>,
467 stripe_client: &stripe::Client,
468) -> anyhow::Result<()> {
469 fn event_type_to_string(event_type: EventType) -> String {
470 // Calling `to_string` on `stripe::EventType` members gives us a quoted string,
471 // so we need to unquote it.
472 event_type.to_string().trim_matches('"').to_string()
473 }
474
475 let event_types = [
476 EventType::CustomerCreated,
477 EventType::CustomerUpdated,
478 EventType::CustomerSubscriptionCreated,
479 EventType::CustomerSubscriptionUpdated,
480 EventType::CustomerSubscriptionPaused,
481 EventType::CustomerSubscriptionResumed,
482 EventType::CustomerSubscriptionDeleted,
483 ]
484 .into_iter()
485 .map(event_type_to_string)
486 .collect::<Vec<_>>();
487
488 let mut pages_of_already_processed_events = 0;
489 let mut unprocessed_events = Vec::new();
490
491 log::info!(
492 "Stripe events: starting retrieval for {}",
493 event_types.join(", ")
494 );
495 let mut params = ListEvents::new();
496 params.types = Some(event_types.clone());
497 params.limit = Some(EVENTS_LIMIT_PER_PAGE);
498
499 let mut event_pages = stripe::Event::list(&stripe_client, ¶ms)
500 .await?
501 .paginate(params);
502
503 loop {
504 let processed_event_ids = {
505 let event_ids = event_pages
506 .page
507 .data
508 .iter()
509 .map(|event| event.id.as_str())
510 .collect::<Vec<_>>();
511 app.db
512 .get_processed_stripe_events_by_event_ids(&event_ids)
513 .await?
514 .into_iter()
515 .map(|event| event.stripe_event_id)
516 .collect::<Vec<_>>()
517 };
518
519 let mut processed_events_in_page = 0;
520 let events_in_page = event_pages.page.data.len();
521 for event in &event_pages.page.data {
522 if processed_event_ids.contains(&event.id.to_string()) {
523 processed_events_in_page += 1;
524 log::debug!("Stripe events: already processed '{}', skipping", event.id);
525 } else {
526 unprocessed_events.push(event.clone());
527 }
528 }
529
530 if processed_events_in_page == events_in_page {
531 pages_of_already_processed_events += 1;
532 }
533
534 if event_pages.page.has_more {
535 if pages_of_already_processed_events >= NUMBER_OF_ALREADY_PROCESSED_PAGES_BEFORE_WE_STOP
536 {
537 log::info!("Stripe events: stopping, saw {pages_of_already_processed_events} pages of already-processed events");
538 break;
539 } else {
540 log::info!("Stripe events: retrieving next page");
541 event_pages = event_pages.next(&stripe_client).await?;
542 }
543 } else {
544 break;
545 }
546 }
547
548 log::info!("Stripe events: unprocessed {}", unprocessed_events.len());
549
550 // Sort all of the unprocessed events in ascending order, so we can handle them in the order they occurred.
551 unprocessed_events.sort_by(|a, b| a.created.cmp(&b.created).then_with(|| a.id.cmp(&b.id)));
552
553 for event in unprocessed_events {
554 let event_id = event.id.clone();
555 let processed_event_params = CreateProcessedStripeEventParams {
556 stripe_event_id: event.id.to_string(),
557 stripe_event_type: event_type_to_string(event.type_),
558 stripe_event_created_timestamp: event.created,
559 };
560
561 // If the event has happened too far in the past, we don't want to
562 // process it and risk overwriting other more-recent updates.
563 //
564 // 1 day was chosen arbitrarily. This could be made longer or shorter.
565 let one_day = Duration::from_secs(24 * 60 * 60);
566 let a_day_ago = Utc::now() - one_day;
567 if a_day_ago.timestamp() > event.created {
568 log::info!(
569 "Stripe events: event '{}' is more than {one_day:?} old, marking as processed",
570 event_id
571 );
572 app.db
573 .create_processed_stripe_event(&processed_event_params)
574 .await?;
575
576 return Ok(());
577 }
578
579 let process_result = match event.type_ {
580 EventType::CustomerCreated | EventType::CustomerUpdated => {
581 handle_customer_event(app, stripe_client, event).await
582 }
583 EventType::CustomerSubscriptionCreated
584 | EventType::CustomerSubscriptionUpdated
585 | EventType::CustomerSubscriptionPaused
586 | EventType::CustomerSubscriptionResumed
587 | EventType::CustomerSubscriptionDeleted => {
588 handle_customer_subscription_event(app, rpc_server, stripe_client, event).await
589 }
590 _ => Ok(()),
591 };
592
593 if let Some(()) = process_result
594 .with_context(|| format!("failed to process event {event_id} successfully"))
595 .log_err()
596 {
597 app.db
598 .create_processed_stripe_event(&processed_event_params)
599 .await?;
600 }
601 }
602
603 Ok(())
604}
605
606async fn handle_customer_event(
607 app: &Arc<AppState>,
608 _stripe_client: &stripe::Client,
609 event: stripe::Event,
610) -> anyhow::Result<()> {
611 let EventObject::Customer(customer) = event.data.object else {
612 bail!("unexpected event payload for {}", event.id);
613 };
614
615 log::info!("handling Stripe {} event: {}", event.type_, event.id);
616
617 let Some(email) = customer.email else {
618 log::info!("Stripe customer has no email: skipping");
619 return Ok(());
620 };
621
622 let Some(user) = app.db.get_user_by_email(&email).await? else {
623 log::info!("no user found for email: skipping");
624 return Ok(());
625 };
626
627 if let Some(existing_customer) = app
628 .db
629 .get_billing_customer_by_stripe_customer_id(&customer.id)
630 .await?
631 {
632 app.db
633 .update_billing_customer(
634 existing_customer.id,
635 &UpdateBillingCustomerParams {
636 // For now we just leave the information as-is, as it is not
637 // likely to change.
638 ..Default::default()
639 },
640 )
641 .await?;
642 } else {
643 app.db
644 .create_billing_customer(&CreateBillingCustomerParams {
645 user_id: user.id,
646 stripe_customer_id: customer.id.to_string(),
647 })
648 .await?;
649 }
650
651 Ok(())
652}
653
654async fn handle_customer_subscription_event(
655 app: &Arc<AppState>,
656 rpc_server: &Arc<Server>,
657 stripe_client: &stripe::Client,
658 event: stripe::Event,
659) -> anyhow::Result<()> {
660 let EventObject::Subscription(subscription) = event.data.object else {
661 bail!("unexpected event payload for {}", event.id);
662 };
663
664 log::info!("handling Stripe {} event: {}", event.type_, event.id);
665
666 let billing_customer =
667 find_or_create_billing_customer(app, stripe_client, subscription.customer)
668 .await?
669 .ok_or_else(|| anyhow!("billing customer not found"))?;
670
671 let was_canceled_due_to_payment_failure = subscription.status == SubscriptionStatus::Canceled
672 && subscription
673 .cancellation_details
674 .as_ref()
675 .and_then(|details| details.reason)
676 .map_or(false, |reason| {
677 reason == CancellationDetailsReason::PaymentFailed
678 });
679
680 if was_canceled_due_to_payment_failure {
681 app.db
682 .update_billing_customer(
683 billing_customer.id,
684 &UpdateBillingCustomerParams {
685 has_overdue_invoices: ActiveValue::set(true),
686 ..Default::default()
687 },
688 )
689 .await?;
690 }
691
692 if let Some(existing_subscription) = app
693 .db
694 .get_billing_subscription_by_stripe_subscription_id(&subscription.id)
695 .await?
696 {
697 app.db
698 .update_billing_subscription(
699 existing_subscription.id,
700 &UpdateBillingSubscriptionParams {
701 billing_customer_id: ActiveValue::set(billing_customer.id),
702 stripe_subscription_id: ActiveValue::set(subscription.id.to_string()),
703 stripe_subscription_status: ActiveValue::set(subscription.status.into()),
704 stripe_cancel_at: ActiveValue::set(
705 subscription
706 .cancel_at
707 .and_then(|cancel_at| DateTime::from_timestamp(cancel_at, 0))
708 .map(|time| time.naive_utc()),
709 ),
710 stripe_cancellation_reason: ActiveValue::set(
711 subscription
712 .cancellation_details
713 .and_then(|details| details.reason)
714 .map(|reason| reason.into()),
715 ),
716 },
717 )
718 .await?;
719 } else {
720 // If the user already has an active billing subscription, ignore the
721 // event and return an `Ok` to signal that it was processed
722 // successfully.
723 //
724 // There is the possibility that this could cause us to not create a
725 // subscription in the following scenario:
726 //
727 // 1. User has an active subscription A
728 // 2. User cancels subscription A
729 // 3. User creates a new subscription B
730 // 4. We process the new subscription B before the cancellation of subscription A
731 // 5. User ends up with no subscriptions
732 //
733 // In theory this situation shouldn't arise as we try to process the events in the order they occur.
734 if app
735 .db
736 .has_active_billing_subscription(billing_customer.user_id)
737 .await?
738 {
739 log::info!(
740 "user {user_id} already has an active subscription, skipping creation of subscription {subscription_id}",
741 user_id = billing_customer.user_id,
742 subscription_id = subscription.id
743 );
744 return Ok(());
745 }
746
747 app.db
748 .create_billing_subscription(&CreateBillingSubscriptionParams {
749 billing_customer_id: billing_customer.id,
750 stripe_subscription_id: subscription.id.to_string(),
751 stripe_subscription_status: subscription.status.into(),
752 stripe_cancellation_reason: subscription
753 .cancellation_details
754 .and_then(|details| details.reason)
755 .map(|reason| reason.into()),
756 })
757 .await?;
758 }
759
760 // When the user's subscription changes, we want to refresh their LLM tokens
761 // to either grant/revoke access.
762 rpc_server
763 .refresh_llm_tokens_for_user(billing_customer.user_id)
764 .await;
765
766 Ok(())
767}
768
769#[derive(Debug, Deserialize)]
770struct GetMonthlySpendParams {
771 github_user_id: i32,
772}
773
774#[derive(Debug, Serialize)]
775struct GetMonthlySpendResponse {
776 monthly_free_tier_spend_in_cents: u32,
777 monthly_free_tier_allowance_in_cents: u32,
778 monthly_spend_in_cents: u32,
779}
780
781async fn get_monthly_spend(
782 Extension(app): Extension<Arc<AppState>>,
783 Query(params): Query<GetMonthlySpendParams>,
784) -> Result<Json<GetMonthlySpendResponse>> {
785 let user = app
786 .db
787 .get_user_by_github_user_id(params.github_user_id)
788 .await?
789 .ok_or_else(|| anyhow!("user not found"))?;
790
791 let Some(llm_db) = app.llm_db.clone() else {
792 return Err(Error::http(
793 StatusCode::NOT_IMPLEMENTED,
794 "LLM database not available".into(),
795 ));
796 };
797
798 let free_tier = user
799 .custom_llm_monthly_allowance_in_cents
800 .map(|allowance| Cents(allowance as u32))
801 .unwrap_or(FREE_TIER_MONTHLY_SPENDING_LIMIT);
802
803 let spending_for_month = llm_db
804 .get_user_spending_for_month(user.id, Utc::now())
805 .await?;
806
807 let free_tier_spend = Cents::min(spending_for_month, free_tier);
808 let monthly_spend = spending_for_month.saturating_sub(free_tier);
809
810 Ok(Json(GetMonthlySpendResponse {
811 monthly_free_tier_spend_in_cents: free_tier_spend.0,
812 monthly_free_tier_allowance_in_cents: free_tier.0,
813 monthly_spend_in_cents: monthly_spend.0,
814 }))
815}
816
817impl From<SubscriptionStatus> for StripeSubscriptionStatus {
818 fn from(value: SubscriptionStatus) -> Self {
819 match value {
820 SubscriptionStatus::Incomplete => Self::Incomplete,
821 SubscriptionStatus::IncompleteExpired => Self::IncompleteExpired,
822 SubscriptionStatus::Trialing => Self::Trialing,
823 SubscriptionStatus::Active => Self::Active,
824 SubscriptionStatus::PastDue => Self::PastDue,
825 SubscriptionStatus::Canceled => Self::Canceled,
826 SubscriptionStatus::Unpaid => Self::Unpaid,
827 SubscriptionStatus::Paused => Self::Paused,
828 }
829 }
830}
831
832impl From<CancellationDetailsReason> for StripeCancellationReason {
833 fn from(value: CancellationDetailsReason) -> Self {
834 match value {
835 CancellationDetailsReason::CancellationRequested => Self::CancellationRequested,
836 CancellationDetailsReason::PaymentDisputed => Self::PaymentDisputed,
837 CancellationDetailsReason::PaymentFailed => Self::PaymentFailed,
838 }
839 }
840}
841
842/// Finds or creates a billing customer using the provided customer.
843async fn find_or_create_billing_customer(
844 app: &Arc<AppState>,
845 stripe_client: &stripe::Client,
846 customer_or_id: Expandable<Customer>,
847) -> anyhow::Result<Option<billing_customer::Model>> {
848 let customer_id = match &customer_or_id {
849 Expandable::Id(id) => id,
850 Expandable::Object(customer) => customer.id.as_ref(),
851 };
852
853 // If we already have a billing customer record associated with the Stripe customer,
854 // there's nothing more we need to do.
855 if let Some(billing_customer) = app
856 .db
857 .get_billing_customer_by_stripe_customer_id(customer_id)
858 .await?
859 {
860 return Ok(Some(billing_customer));
861 }
862
863 // If all we have is a customer ID, resolve it to a full customer record by
864 // hitting the Stripe API.
865 let customer = match customer_or_id {
866 Expandable::Id(id) => Customer::retrieve(stripe_client, &id, &[]).await?,
867 Expandable::Object(customer) => *customer,
868 };
869
870 let Some(email) = customer.email else {
871 return Ok(None);
872 };
873
874 let Some(user) = app.db.get_user_by_email(&email).await? else {
875 return Ok(None);
876 };
877
878 let billing_customer = app
879 .db
880 .create_billing_customer(&CreateBillingCustomerParams {
881 user_id: user.id,
882 stripe_customer_id: customer.id.to_string(),
883 })
884 .await?;
885
886 Ok(Some(billing_customer))
887}
888
889const SYNC_LLM_USAGE_WITH_STRIPE_INTERVAL: Duration = Duration::from_secs(60);
890
891pub fn sync_llm_usage_with_stripe_periodically(app: Arc<AppState>) {
892 let Some(stripe_billing) = app.stripe_billing.clone() else {
893 log::warn!("failed to retrieve Stripe billing object");
894 return;
895 };
896 let Some(llm_db) = app.llm_db.clone() else {
897 log::warn!("failed to retrieve LLM database");
898 return;
899 };
900
901 let executor = app.executor.clone();
902 executor.spawn_detached({
903 let executor = executor.clone();
904 async move {
905 loop {
906 sync_with_stripe(&app, &llm_db, &stripe_billing)
907 .await
908 .context("failed to sync LLM usage to Stripe")
909 .trace_err();
910 executor.sleep(SYNC_LLM_USAGE_WITH_STRIPE_INTERVAL).await;
911 }
912 }
913 });
914}
915
916async fn sync_with_stripe(
917 app: &Arc<AppState>,
918 llm_db: &Arc<LlmDatabase>,
919 stripe_billing: &Arc<StripeBilling>,
920) -> anyhow::Result<()> {
921 let events = llm_db.get_billing_events().await?;
922 let user_ids = events
923 .iter()
924 .map(|(event, _)| event.user_id)
925 .collect::<HashSet<UserId>>();
926 let stripe_subscriptions = app.db.get_active_billing_subscriptions(user_ids).await?;
927
928 for (event, model) in events {
929 let Some((stripe_db_customer, stripe_db_subscription)) =
930 stripe_subscriptions.get(&event.user_id)
931 else {
932 tracing::warn!(
933 user_id = event.user_id.0,
934 "Registered billing event for user who is not a Stripe customer. Billing events should only be created for users who are Stripe customers, so this is a mistake on our side."
935 );
936 continue;
937 };
938 let stripe_subscription_id: stripe::SubscriptionId = stripe_db_subscription
939 .stripe_subscription_id
940 .parse()
941 .context("failed to parse stripe subscription id from db")?;
942 let stripe_customer_id: stripe::CustomerId = stripe_db_customer
943 .stripe_customer_id
944 .parse()
945 .context("failed to parse stripe customer id from db")?;
946
947 let stripe_model = stripe_billing.register_model(&model).await?;
948 stripe_billing
949 .subscribe_to_model(&stripe_subscription_id, &stripe_model)
950 .await?;
951 stripe_billing
952 .bill_model_usage(&stripe_customer_id, &stripe_model, &event)
953 .await?;
954 llm_db.consume_billing_event(event.id).await?;
955 }
956
957 Ok(())
958}