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 customer_id =
253 if let Some(existing_customer) = app.db.get_billing_customer_by_user_id(user.id).await? {
254 CustomerId::from_str(&existing_customer.stripe_customer_id)
255 .context("failed to parse customer ID")?
256 } else {
257 let customer = Customer::create(
258 &stripe_client,
259 CreateCustomer {
260 email: user.email_address.as_deref(),
261 ..Default::default()
262 },
263 )
264 .await?;
265
266 customer.id
267 };
268
269 let default_model = llm_db.model(rpc::LanguageModelProvider::Anthropic, "claude-3-5-sonnet")?;
270 let stripe_model = stripe_billing.register_model(default_model).await?;
271 let success_url = format!(
272 "{}/account?checkout_complete=1",
273 app.config.zed_dot_dev_url()
274 );
275 let checkout_session_url = stripe_billing
276 .checkout(customer_id, &user.github_login, &stripe_model, &success_url)
277 .await?;
278 Ok(Json(CreateBillingSubscriptionResponse {
279 checkout_session_url,
280 }))
281}
282
283#[derive(Debug, PartialEq, Deserialize)]
284#[serde(rename_all = "snake_case")]
285enum ManageSubscriptionIntent {
286 /// The user intends to cancel their subscription.
287 Cancel,
288 /// The user intends to stop the cancellation of their subscription.
289 StopCancellation,
290}
291
292#[derive(Debug, Deserialize)]
293struct ManageBillingSubscriptionBody {
294 github_user_id: i32,
295 intent: ManageSubscriptionIntent,
296 /// The ID of the subscription to manage.
297 subscription_id: BillingSubscriptionId,
298}
299
300#[derive(Debug, Serialize)]
301struct ManageBillingSubscriptionResponse {
302 billing_portal_session_url: Option<String>,
303}
304
305/// Initiates a Stripe customer portal session for managing a billing subscription.
306async fn manage_billing_subscription(
307 Extension(app): Extension<Arc<AppState>>,
308 extract::Json(body): extract::Json<ManageBillingSubscriptionBody>,
309) -> Result<Json<ManageBillingSubscriptionResponse>> {
310 let user = app
311 .db
312 .get_user_by_github_user_id(body.github_user_id)
313 .await?
314 .ok_or_else(|| anyhow!("user not found"))?;
315
316 let Some(stripe_client) = app.stripe_client.clone() else {
317 log::error!("failed to retrieve Stripe client");
318 Err(Error::http(
319 StatusCode::NOT_IMPLEMENTED,
320 "not supported".into(),
321 ))?
322 };
323
324 let customer = app
325 .db
326 .get_billing_customer_by_user_id(user.id)
327 .await?
328 .ok_or_else(|| anyhow!("billing customer not found"))?;
329 let customer_id = CustomerId::from_str(&customer.stripe_customer_id)
330 .context("failed to parse customer ID")?;
331
332 let subscription = app
333 .db
334 .get_billing_subscription_by_id(body.subscription_id)
335 .await?
336 .ok_or_else(|| anyhow!("subscription not found"))?;
337
338 if body.intent == ManageSubscriptionIntent::StopCancellation {
339 let subscription_id = SubscriptionId::from_str(&subscription.stripe_subscription_id)
340 .context("failed to parse subscription ID")?;
341
342 let updated_stripe_subscription = Subscription::update(
343 &stripe_client,
344 &subscription_id,
345 stripe::UpdateSubscription {
346 cancel_at_period_end: Some(false),
347 ..Default::default()
348 },
349 )
350 .await?;
351
352 app.db
353 .update_billing_subscription(
354 subscription.id,
355 &UpdateBillingSubscriptionParams {
356 stripe_cancel_at: ActiveValue::set(
357 updated_stripe_subscription
358 .cancel_at
359 .and_then(|cancel_at| DateTime::from_timestamp(cancel_at, 0))
360 .map(|time| time.naive_utc()),
361 ),
362 ..Default::default()
363 },
364 )
365 .await?;
366
367 return Ok(Json(ManageBillingSubscriptionResponse {
368 billing_portal_session_url: None,
369 }));
370 }
371
372 let flow = match body.intent {
373 ManageSubscriptionIntent::Cancel => CreateBillingPortalSessionFlowData {
374 type_: CreateBillingPortalSessionFlowDataType::SubscriptionCancel,
375 after_completion: Some(CreateBillingPortalSessionFlowDataAfterCompletion {
376 type_: stripe::CreateBillingPortalSessionFlowDataAfterCompletionType::Redirect,
377 redirect: Some(CreateBillingPortalSessionFlowDataAfterCompletionRedirect {
378 return_url: format!("{}/account", app.config.zed_dot_dev_url()),
379 }),
380 ..Default::default()
381 }),
382 subscription_cancel: Some(
383 stripe::CreateBillingPortalSessionFlowDataSubscriptionCancel {
384 subscription: subscription.stripe_subscription_id,
385 retention: None,
386 },
387 ),
388 ..Default::default()
389 },
390 ManageSubscriptionIntent::StopCancellation => unreachable!(),
391 };
392
393 let mut params = CreateBillingPortalSession::new(customer_id);
394 params.flow_data = Some(flow);
395 let return_url = format!("{}/account", app.config.zed_dot_dev_url());
396 params.return_url = Some(&return_url);
397
398 let session = BillingPortalSession::create(&stripe_client, params).await?;
399
400 Ok(Json(ManageBillingSubscriptionResponse {
401 billing_portal_session_url: Some(session.url),
402 }))
403}
404
405/// The amount of time we wait in between each poll of Stripe events.
406///
407/// This value should strike a balance between:
408/// 1. Being short enough that we update quickly when something in Stripe changes
409/// 2. Being long enough that we don't eat into our rate limits.
410///
411/// As a point of reference, the Sequin folks say they have this at **500ms**:
412///
413/// > We poll the Stripe /events endpoint every 500ms per account
414/// >
415/// > — https://blog.sequinstream.com/events-not-webhooks/
416const POLL_EVENTS_INTERVAL: Duration = Duration::from_secs(5);
417
418/// The maximum number of events to return per page.
419///
420/// We set this to 100 (the max) so we have to make fewer requests to Stripe.
421///
422/// > Limit can range between 1 and 100, and the default is 10.
423const EVENTS_LIMIT_PER_PAGE: u64 = 100;
424
425/// The number of pages consisting entirely of already-processed events that we
426/// will see before we stop retrieving events.
427///
428/// This is used to prevent over-fetching the Stripe events API for events we've
429/// already seen and processed.
430const NUMBER_OF_ALREADY_PROCESSED_PAGES_BEFORE_WE_STOP: usize = 4;
431
432/// Polls the Stripe events API periodically to reconcile the records in our
433/// database with the data in Stripe.
434pub fn poll_stripe_events_periodically(app: Arc<AppState>, rpc_server: Arc<Server>) {
435 let Some(stripe_client) = app.stripe_client.clone() else {
436 log::warn!("failed to retrieve Stripe client");
437 return;
438 };
439
440 let executor = app.executor.clone();
441 executor.spawn_detached({
442 let executor = executor.clone();
443 async move {
444 loop {
445 poll_stripe_events(&app, &rpc_server, &stripe_client)
446 .await
447 .log_err();
448
449 executor.sleep(POLL_EVENTS_INTERVAL).await;
450 }
451 }
452 });
453}
454
455async fn poll_stripe_events(
456 app: &Arc<AppState>,
457 rpc_server: &Arc<Server>,
458 stripe_client: &stripe::Client,
459) -> anyhow::Result<()> {
460 fn event_type_to_string(event_type: EventType) -> String {
461 // Calling `to_string` on `stripe::EventType` members gives us a quoted string,
462 // so we need to unquote it.
463 event_type.to_string().trim_matches('"').to_string()
464 }
465
466 let event_types = [
467 EventType::CustomerCreated,
468 EventType::CustomerUpdated,
469 EventType::CustomerSubscriptionCreated,
470 EventType::CustomerSubscriptionUpdated,
471 EventType::CustomerSubscriptionPaused,
472 EventType::CustomerSubscriptionResumed,
473 EventType::CustomerSubscriptionDeleted,
474 ]
475 .into_iter()
476 .map(event_type_to_string)
477 .collect::<Vec<_>>();
478
479 let mut pages_of_already_processed_events = 0;
480 let mut unprocessed_events = Vec::new();
481
482 log::info!(
483 "Stripe events: starting retrieval for {}",
484 event_types.join(", ")
485 );
486 let mut params = ListEvents::new();
487 params.types = Some(event_types.clone());
488 params.limit = Some(EVENTS_LIMIT_PER_PAGE);
489
490 let mut event_pages = stripe::Event::list(&stripe_client, ¶ms)
491 .await?
492 .paginate(params);
493
494 loop {
495 let processed_event_ids = {
496 let event_ids = event_pages
497 .page
498 .data
499 .iter()
500 .map(|event| event.id.as_str())
501 .collect::<Vec<_>>();
502 app.db
503 .get_processed_stripe_events_by_event_ids(&event_ids)
504 .await?
505 .into_iter()
506 .map(|event| event.stripe_event_id)
507 .collect::<Vec<_>>()
508 };
509
510 let mut processed_events_in_page = 0;
511 let events_in_page = event_pages.page.data.len();
512 for event in &event_pages.page.data {
513 if processed_event_ids.contains(&event.id.to_string()) {
514 processed_events_in_page += 1;
515 log::debug!("Stripe events: already processed '{}', skipping", event.id);
516 } else {
517 unprocessed_events.push(event.clone());
518 }
519 }
520
521 if processed_events_in_page == events_in_page {
522 pages_of_already_processed_events += 1;
523 }
524
525 if event_pages.page.has_more {
526 if pages_of_already_processed_events >= NUMBER_OF_ALREADY_PROCESSED_PAGES_BEFORE_WE_STOP
527 {
528 log::info!("Stripe events: stopping, saw {pages_of_already_processed_events} pages of already-processed events");
529 break;
530 } else {
531 log::info!("Stripe events: retrieving next page");
532 event_pages = event_pages.next(&stripe_client).await?;
533 }
534 } else {
535 break;
536 }
537 }
538
539 log::info!("Stripe events: unprocessed {}", unprocessed_events.len());
540
541 // Sort all of the unprocessed events in ascending order, so we can handle them in the order they occurred.
542 unprocessed_events.sort_by(|a, b| a.created.cmp(&b.created).then_with(|| a.id.cmp(&b.id)));
543
544 for event in unprocessed_events {
545 let event_id = event.id.clone();
546 let processed_event_params = CreateProcessedStripeEventParams {
547 stripe_event_id: event.id.to_string(),
548 stripe_event_type: event_type_to_string(event.type_),
549 stripe_event_created_timestamp: event.created,
550 };
551
552 // If the event has happened too far in the past, we don't want to
553 // process it and risk overwriting other more-recent updates.
554 //
555 // 1 day was chosen arbitrarily. This could be made longer or shorter.
556 let one_day = Duration::from_secs(24 * 60 * 60);
557 let a_day_ago = Utc::now() - one_day;
558 if a_day_ago.timestamp() > event.created {
559 log::info!(
560 "Stripe events: event '{}' is more than {one_day:?} old, marking as processed",
561 event_id
562 );
563 app.db
564 .create_processed_stripe_event(&processed_event_params)
565 .await?;
566
567 return Ok(());
568 }
569
570 let process_result = match event.type_ {
571 EventType::CustomerCreated | EventType::CustomerUpdated => {
572 handle_customer_event(app, stripe_client, event).await
573 }
574 EventType::CustomerSubscriptionCreated
575 | EventType::CustomerSubscriptionUpdated
576 | EventType::CustomerSubscriptionPaused
577 | EventType::CustomerSubscriptionResumed
578 | EventType::CustomerSubscriptionDeleted => {
579 handle_customer_subscription_event(app, rpc_server, stripe_client, event).await
580 }
581 _ => Ok(()),
582 };
583
584 if let Some(()) = process_result
585 .with_context(|| format!("failed to process event {event_id} successfully"))
586 .log_err()
587 {
588 app.db
589 .create_processed_stripe_event(&processed_event_params)
590 .await?;
591 }
592 }
593
594 Ok(())
595}
596
597async fn handle_customer_event(
598 app: &Arc<AppState>,
599 _stripe_client: &stripe::Client,
600 event: stripe::Event,
601) -> anyhow::Result<()> {
602 let EventObject::Customer(customer) = event.data.object else {
603 bail!("unexpected event payload for {}", event.id);
604 };
605
606 log::info!("handling Stripe {} event: {}", event.type_, event.id);
607
608 let Some(email) = customer.email else {
609 log::info!("Stripe customer has no email: skipping");
610 return Ok(());
611 };
612
613 let Some(user) = app.db.get_user_by_email(&email).await? else {
614 log::info!("no user found for email: skipping");
615 return Ok(());
616 };
617
618 if let Some(existing_customer) = app
619 .db
620 .get_billing_customer_by_stripe_customer_id(&customer.id)
621 .await?
622 {
623 app.db
624 .update_billing_customer(
625 existing_customer.id,
626 &UpdateBillingCustomerParams {
627 // For now we just leave the information as-is, as it is not
628 // likely to change.
629 ..Default::default()
630 },
631 )
632 .await?;
633 } else {
634 app.db
635 .create_billing_customer(&CreateBillingCustomerParams {
636 user_id: user.id,
637 stripe_customer_id: customer.id.to_string(),
638 })
639 .await?;
640 }
641
642 Ok(())
643}
644
645async fn handle_customer_subscription_event(
646 app: &Arc<AppState>,
647 rpc_server: &Arc<Server>,
648 stripe_client: &stripe::Client,
649 event: stripe::Event,
650) -> anyhow::Result<()> {
651 let EventObject::Subscription(subscription) = event.data.object else {
652 bail!("unexpected event payload for {}", event.id);
653 };
654
655 log::info!("handling Stripe {} event: {}", event.type_, event.id);
656
657 let billing_customer =
658 find_or_create_billing_customer(app, stripe_client, subscription.customer)
659 .await?
660 .ok_or_else(|| anyhow!("billing customer not found"))?;
661
662 if let Some(existing_subscription) = app
663 .db
664 .get_billing_subscription_by_stripe_subscription_id(&subscription.id)
665 .await?
666 {
667 app.db
668 .update_billing_subscription(
669 existing_subscription.id,
670 &UpdateBillingSubscriptionParams {
671 billing_customer_id: ActiveValue::set(billing_customer.id),
672 stripe_subscription_id: ActiveValue::set(subscription.id.to_string()),
673 stripe_subscription_status: ActiveValue::set(subscription.status.into()),
674 stripe_cancel_at: ActiveValue::set(
675 subscription
676 .cancel_at
677 .and_then(|cancel_at| DateTime::from_timestamp(cancel_at, 0))
678 .map(|time| time.naive_utc()),
679 ),
680 stripe_cancellation_reason: ActiveValue::set(
681 subscription
682 .cancellation_details
683 .and_then(|details| details.reason)
684 .map(|reason| reason.into()),
685 ),
686 },
687 )
688 .await?;
689 } else {
690 // If the user already has an active billing subscription, ignore the
691 // event and return an `Ok` to signal that it was processed
692 // successfully.
693 //
694 // There is the possibility that this could cause us to not create a
695 // subscription in the following scenario:
696 //
697 // 1. User has an active subscription A
698 // 2. User cancels subscription A
699 // 3. User creates a new subscription B
700 // 4. We process the new subscription B before the cancellation of subscription A
701 // 5. User ends up with no subscriptions
702 //
703 // In theory this situation shouldn't arise as we try to process the events in the order they occur.
704 if app
705 .db
706 .has_active_billing_subscription(billing_customer.user_id)
707 .await?
708 {
709 log::info!(
710 "user {user_id} already has an active subscription, skipping creation of subscription {subscription_id}",
711 user_id = billing_customer.user_id,
712 subscription_id = subscription.id
713 );
714 return Ok(());
715 }
716
717 app.db
718 .create_billing_subscription(&CreateBillingSubscriptionParams {
719 billing_customer_id: billing_customer.id,
720 stripe_subscription_id: subscription.id.to_string(),
721 stripe_subscription_status: subscription.status.into(),
722 })
723 .await?;
724 }
725
726 // When the user's subscription changes, we want to refresh their LLM tokens
727 // to either grant/revoke access.
728 rpc_server
729 .refresh_llm_tokens_for_user(billing_customer.user_id)
730 .await;
731
732 Ok(())
733}
734
735#[derive(Debug, Deserialize)]
736struct GetMonthlySpendParams {
737 github_user_id: i32,
738}
739
740#[derive(Debug, Serialize)]
741struct GetMonthlySpendResponse {
742 monthly_free_tier_spend_in_cents: u32,
743 monthly_free_tier_allowance_in_cents: u32,
744 monthly_spend_in_cents: u32,
745}
746
747async fn get_monthly_spend(
748 Extension(app): Extension<Arc<AppState>>,
749 Query(params): Query<GetMonthlySpendParams>,
750) -> Result<Json<GetMonthlySpendResponse>> {
751 let user = app
752 .db
753 .get_user_by_github_user_id(params.github_user_id)
754 .await?
755 .ok_or_else(|| anyhow!("user not found"))?;
756
757 let Some(llm_db) = app.llm_db.clone() else {
758 return Err(Error::http(
759 StatusCode::NOT_IMPLEMENTED,
760 "LLM database not available".into(),
761 ));
762 };
763
764 let free_tier = user
765 .custom_llm_monthly_allowance_in_cents
766 .map(|allowance| Cents(allowance as u32))
767 .unwrap_or(FREE_TIER_MONTHLY_SPENDING_LIMIT);
768
769 let spending_for_month = llm_db
770 .get_user_spending_for_month(user.id, Utc::now())
771 .await?;
772
773 let free_tier_spend = Cents::min(spending_for_month, free_tier);
774 let monthly_spend = spending_for_month.saturating_sub(free_tier);
775
776 Ok(Json(GetMonthlySpendResponse {
777 monthly_free_tier_spend_in_cents: free_tier_spend.0,
778 monthly_free_tier_allowance_in_cents: free_tier.0,
779 monthly_spend_in_cents: monthly_spend.0,
780 }))
781}
782
783impl From<SubscriptionStatus> for StripeSubscriptionStatus {
784 fn from(value: SubscriptionStatus) -> Self {
785 match value {
786 SubscriptionStatus::Incomplete => Self::Incomplete,
787 SubscriptionStatus::IncompleteExpired => Self::IncompleteExpired,
788 SubscriptionStatus::Trialing => Self::Trialing,
789 SubscriptionStatus::Active => Self::Active,
790 SubscriptionStatus::PastDue => Self::PastDue,
791 SubscriptionStatus::Canceled => Self::Canceled,
792 SubscriptionStatus::Unpaid => Self::Unpaid,
793 SubscriptionStatus::Paused => Self::Paused,
794 }
795 }
796}
797
798impl From<CancellationDetailsReason> for StripeCancellationReason {
799 fn from(value: CancellationDetailsReason) -> Self {
800 match value {
801 CancellationDetailsReason::CancellationRequested => Self::CancellationRequested,
802 CancellationDetailsReason::PaymentDisputed => Self::PaymentDisputed,
803 CancellationDetailsReason::PaymentFailed => Self::PaymentFailed,
804 }
805 }
806}
807
808/// Finds or creates a billing customer using the provided customer.
809async fn find_or_create_billing_customer(
810 app: &Arc<AppState>,
811 stripe_client: &stripe::Client,
812 customer_or_id: Expandable<Customer>,
813) -> anyhow::Result<Option<billing_customer::Model>> {
814 let customer_id = match &customer_or_id {
815 Expandable::Id(id) => id,
816 Expandable::Object(customer) => customer.id.as_ref(),
817 };
818
819 // If we already have a billing customer record associated with the Stripe customer,
820 // there's nothing more we need to do.
821 if let Some(billing_customer) = app
822 .db
823 .get_billing_customer_by_stripe_customer_id(customer_id)
824 .await?
825 {
826 return Ok(Some(billing_customer));
827 }
828
829 // If all we have is a customer ID, resolve it to a full customer record by
830 // hitting the Stripe API.
831 let customer = match customer_or_id {
832 Expandable::Id(id) => Customer::retrieve(stripe_client, &id, &[]).await?,
833 Expandable::Object(customer) => *customer,
834 };
835
836 let Some(email) = customer.email else {
837 return Ok(None);
838 };
839
840 let Some(user) = app.db.get_user_by_email(&email).await? else {
841 return Ok(None);
842 };
843
844 let billing_customer = app
845 .db
846 .create_billing_customer(&CreateBillingCustomerParams {
847 user_id: user.id,
848 stripe_customer_id: customer.id.to_string(),
849 })
850 .await?;
851
852 Ok(Some(billing_customer))
853}
854
855const SYNC_LLM_USAGE_WITH_STRIPE_INTERVAL: Duration = Duration::from_secs(60);
856
857pub fn sync_llm_usage_with_stripe_periodically(app: Arc<AppState>) {
858 let Some(stripe_billing) = app.stripe_billing.clone() else {
859 log::warn!("failed to retrieve Stripe billing object");
860 return;
861 };
862 let Some(llm_db) = app.llm_db.clone() else {
863 log::warn!("failed to retrieve LLM database");
864 return;
865 };
866
867 let executor = app.executor.clone();
868 executor.spawn_detached({
869 let executor = executor.clone();
870 async move {
871 loop {
872 sync_with_stripe(&app, &llm_db, &stripe_billing)
873 .await
874 .context("failed to sync LLM usage to Stripe")
875 .trace_err();
876 executor.sleep(SYNC_LLM_USAGE_WITH_STRIPE_INTERVAL).await;
877 }
878 }
879 });
880}
881
882async fn sync_with_stripe(
883 app: &Arc<AppState>,
884 llm_db: &Arc<LlmDatabase>,
885 stripe_billing: &Arc<StripeBilling>,
886) -> anyhow::Result<()> {
887 let events = llm_db.get_billing_events().await?;
888 let user_ids = events
889 .iter()
890 .map(|(event, _)| event.user_id)
891 .collect::<HashSet<UserId>>();
892 let stripe_subscriptions = app.db.get_active_billing_subscriptions(user_ids).await?;
893
894 for (event, model) in events {
895 let Some((stripe_db_customer, stripe_db_subscription)) =
896 stripe_subscriptions.get(&event.user_id)
897 else {
898 tracing::warn!(
899 user_id = event.user_id.0,
900 "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."
901 );
902 continue;
903 };
904 let stripe_subscription_id: stripe::SubscriptionId = stripe_db_subscription
905 .stripe_subscription_id
906 .parse()
907 .context("failed to parse stripe subscription id from db")?;
908 let stripe_customer_id: stripe::CustomerId = stripe_db_customer
909 .stripe_customer_id
910 .parse()
911 .context("failed to parse stripe customer id from db")?;
912
913 let stripe_model = stripe_billing.register_model(&model).await?;
914 stripe_billing
915 .subscribe_to_model(&stripe_subscription_id, &stripe_model)
916 .await?;
917 stripe_billing
918 .bill_model_usage(&stripe_customer_id, &stripe_model, &event)
919 .await?;
920 llm_db.consume_billing_event(event.id).await?;
921 }
922
923 Ok(())
924}