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