Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
1,538 linesCodeBlameRaw
1//! The billing service: what g1t's compute costs, charged to the workspace
2//! it ran for at what g1t pays plus 20%.
3//!
4//! One paid plan, "g1t" (see `features`): $21 a month per workspace, never
5//! per person, with $10 of usage included, deployments, and more private
6//! storage. The forge is free for everyone; compute needs the plan or a
7//! card check (see `compute` and `cards`). Before anything that costs money
8//! starts, the service that starts it reserves its estimate here; when it
9//! is done, what it cost goes on the ledger plus the margin, drawn first
10//! from what the plan includes, a trial or g1t's pools (see `credits`).
11//! Every change is a ledger entry, and a balance is always the sum of its
12//! ledger.
13//!
14//! Without a card processor configured the service says so and charges
15//! nothing, so that g1t still runs where billing has not been set up.
16//!
17//! Reached only through service bindings; see `g1t_contracts::billing` for
18//! the methods and their arguments.
19
20mod accounts;
21mod budget;
22mod charged;
23mod cards;
24mod closing;
25mod compute;
26mod costs;
27mod cycle;
28mod detach;
29mod margin;
30mod platform;
31mod pricing;
32mod report;
33mod details;
34mod credits;
35mod grants;
36mod overages;
37mod requests;
38mod storage;
39mod invoices;
40mod sales;
41mod statement;
42mod webhooks;
43mod features;
44mod keeper;
45mod limits;
46mod rename;
47mod reset;
48mod retention;
49mod stripe;
50mod stripe_sync;
51mod subscriptions;
52mod tax;
53
54use g1t_contracts::billing::*;
55use g1t_contracts::time::rfc3339;
56use g1t_contracts::{FailureCode, Outcome, new_id};
57use g1t_kit::{args, now_ms, reply, rpc_method};
58use serde::Deserialize;
59use worker::wasm_bindgen::JsValue;
60use worker::{Context, D1Database, Env, MessageBatch, MessageExt, Request, Response, Result, ScheduleContext, ScheduledEvent, event};
61use futures_util::future::{try_join, try_join5};
62
63use stripe::Stripe;
64
65/// Prepaying: $25 at the least; by card up to $10,000 at a time, and by
66/// bank transfer from $1,000 to $100,000.
67const MIN_TOP_UP_CENTS: u32 = 2_500;
68const MAX_TOP_UP_CENTS: u32 = 1_000_000;
69const MIN_BANK_TRANSFER_CENTS: u32 = 100_000;
70const MAX_BANK_TRANSFER_CENTS: u32 = 10_000_000;
71const LEDGER_PAGE: u32 = 100;
72/// What a cost g1t trusts (the price book's) is charged at: plus the
73/// margin, rounded up to a whole millionth.
74pub fn margin_on(cost_micros: i64, margin_percent: u32) -> i64 {
75 let cost = i128::from(cost_micros.max(0));
76 let charge = (cost * i128::from(100 + margin_percent) + 99) / 100;
77 i64::try_from(charge).unwrap_or(i64::MAX)
78}
79
80fn optional(value: Option<&str>) -> JsValue {
81 value.map_or(JsValue::NULL, JsValue::from)
82}
83
84#[derive(Deserialize)]
85struct AccountRow {
86 balance_micros: i64,
87 customer_id: Option<String>,
88}
89
90#[derive(Deserialize)]
91struct LedgerRow {
92 id: String,
93 kind: EntryKind,
94 amount_micros: i64,
95 description: String,
96 repo: Option<String>,
97 number: Option<u32>,
98 task: Option<String>,
99 model: Option<String>,
100 created_by: Option<String>,
101 created_at: String,
102 billed_to: Option<String>,
103 #[serde(default)]
104 workspace: Option<String>,
105 #[serde(default)]
106 credit_micros: Option<i64>,
107 #[serde(default)]
108 trial_micros: Option<i64>,
109 #[serde(default)]
110 oss_micros: Option<i64>,
111 #[serde(default)]
112 given_micros: Option<i64>,
113 #[serde(default)]
114 credit_kind: Option<String>,
115 #[serde(default)]
116 discount_micros: Option<i64>,
117}
118
119impl From<LedgerRow> for LedgerEntry {
120 fn from(row: LedgerRow) -> Self {
121 LedgerEntry {
122 id: row.id,
123 kind: row.kind,
124 amount_micros: row.amount_micros,
125 description: row.description,
126 repo: row.repo,
127 number: row.number,
128 task: row.task,
129 model: row.model,
130 billed_to: row.billed_to.unwrap_or_else(|| "g1t".to_owned()),
131 created_by: row.created_by,
132 created_at: row.created_at,
133 workspace: row.workspace,
134 credit_micros: row.credit_micros.unwrap_or(0),
135 trial_micros: row.trial_micros.unwrap_or(0),
136 oss_micros: row.oss_micros.unwrap_or(0),
137 given_micros: row.given_micros.unwrap_or(0),
138 credit_kind: row.credit_kind.as_deref().and_then(CreditKind::parse),
139 discount_micros: row.discount_micros.unwrap_or(0),
140 }
141 }
142}
143
144#[derive(Deserialize)]
145struct CheckoutRow {
146 workspace: String,
147 created_by: String,
148 #[serde(default)]
149 fee_cents: Option<u32>,
150}
151
152/// A payment page started, as `checkouts` keeps it.
153pub(crate) struct NewCheckout<'a> {
154 pub id: &'a str,
155 pub workspace: &'a str,
156 /// What it pays for, in cents: the prepayment or the plan's price; 0
157 /// for a card check.
158 pub amount_cents: u32,
159 /// The card fee on top: always 0 now that g1t's prices include it; the
160 /// column keeps what pages from before were opened with.
161 pub fee_cents: u32,
162 pub created_by: &'a str,
163 /// None for a prepayment; `plan`, `security` or `card_check` otherwise
164 /// (`ai_credit` on pages from before AI credit was retired).
165 pub feature: Option<&'a str>,
166}
167
168/// The one insert every payment page goes through, every column named, so
169/// a column added to `checkouts` with no default is caught by
170/// `tests::every_checkout_insert_fills_the_table` rather than by a 500.
171/// A page asked for twice in Stripe's idempotency window is the same page:
172/// the row the first made stands, and the second is handed the same page.
173pub(crate) const CHECKOUT_INSERT: &str = "INSERT OR IGNORE INTO checkouts (id, workspace, amount_cents, fee_cents, created_by, created_at, feature, status)
174 VALUES (?, ?, ?, ?, ?, ?, ?, 'open')";
175
176impl Billing {
177 pub(crate) async fn record_checkout(&self, c: &NewCheckout<'_>) -> Result<()> {
178 self.db
179 .prepare(CHECKOUT_INSERT)
180 .bind(&[
181 c.id.into(),
182 c.workspace.into(),
183 c.amount_cents.into(),
184 c.fee_cents.into(),
185 c.created_by.into(),
186 rfc3339(now_ms()).into(),
187 optional(c.feature),
188 ])?
189 .run()
190 .await?;
191 Ok(())
192 }
193}
194
195/// Lifts a workspace's stop (`?1`) for a declined card or a dispute, unless
196/// a dispute is still open or what a lost one took back is still unpaid.
197pub(crate) const CLEAR_STOP: &str = "UPDATE limits SET autopay_failed_at = NULL, autopay_error = NULL
198 WHERE workspace = ?1 AND dispute_owed_micros = 0
199 AND NOT EXISTS (SELECT 1 FROM disputes WHERE disputes.workspace = ?1 AND disputes.status = 'open')";
200
201/// Whether D1 refused a write because the row is there already: a ledger
202/// entry for a payment someone else credited first (`ledger.reference` is
203/// unique), so it is never credited twice.
204pub(crate) fn is_duplicate(error: &worker::Error) -> bool {
205 error.to_string().contains("UNIQUE constraint failed")
206}
207
208/// A row an `UPDATE … RETURNING` touched.
209#[derive(Deserialize)]
210struct Touched {
211 #[allow(dead_code)]
212 id: String,
213}
214
215struct Billing {
216 db: D1Database,
217 /// Absent when no card processor is configured.
218 stripe: Option<Stripe>,
219 /// Whether g1t charges for usage (`CHARGING`): every "is this charged"
220 /// decision keys on it, never on whether the card processor's key is
221 /// set. On without a key, usage is still recorded and free workspaces
222 /// are held to their limits (fail closed); off, nothing is gated or
223 /// charged (a self-hosted g1t).
224 charging: bool,
225 /// The destination's signing secret from Stripe (`STRIPE_WEBHOOK_SECRET`);
226 /// without it no event is believed.
227 webhook_secret: Option<String>,
228 margin_percent: u32,
229 /// While g1t is being built out, nothing is charged (`FREE_WHILE_BUILDING`).
230 free: bool,
231 /// Whether new workspaces get trial credit (`TRIAL_WORKSPACE_MICROS`
232 /// and `TRIAL_MONTHLY_POOL_MICROS` both above zero).
233 trials_on: bool,
234 /// The plans' and pools' numbers; see `credits`.
235 plans: credits::Config,
236 /// The repos service: which repositories are public, what private ones
237 /// hold, and their git operations. Absent where it is not bound.
238 repos: Option<worker::Fetcher>,
239 /// The packages service: what each workspace's packages hold.
240 packages: Option<worker::Fetcher>,
241 /// The identity service, which emails owners. Absent where it is not
242 /// bound.
243 identity: Option<worker::Fetcher>,
244 /// How far unpaid usage may go; see `limits`.
245 ceilings: limits::Ceilings,
246 /// The caps on what g1t pays for itself; see `budget`.
247 caps: budget::Caps,
248 /// The worker's bindings, for emailing staff (`EMAIL`).
249 env: Env,
250}
251
252impl Billing {
253 fn status(&self) -> Status {
254 Status {
255 enabled: self.stripe.is_some(),
256 live: self.stripe.as_ref().is_some_and(Stripe::live),
257 free: self.free,
258 }
259 }
260
261 async fn row(&self, workspace: &str) -> Result<Option<AccountRow>> {
262 self.db
263 .prepare("SELECT balance_micros, customer_id FROM accounts WHERE workspace = ?")
264 .bind(&[workspace.into()])?
265 .first::<AccountRow>(None)
266 .await
267 }
268
269 async fn standing(&self, workspace: &str) -> Result<Account> {
270 // The card as last synced (stripe_sync.rs), not asked of Stripe.
271 let (row, card) = try_join(self.row(workspace), self.saved_card(workspace)).await?;
272 Ok(Account {
273 workspace: workspace.to_owned(),
274 balance_micros: row.map_or(0, |row| row.balance_micros),
275 status: self.status(),
276 margin_percent: self.margin_percent,
277 card,
278 })
279 }
280
281 /// The workspace's customer at Stripe, made the first time one is needed.
282 pub(crate) async fn customer_for(&self, workspace: &str) -> Result<String> {
283 let Some(stripe) = &self.stripe else {
284 return Err(worker::Error::RustError("payments are not set up".into()));
285 };
286 if let Some(customer) = self.row(workspace).await?.and_then(|row| row.customer_id) {
287 return Ok(customer);
288 }
289 let email = self.owner_email(workspace).await;
290 let customer = stripe.create_customer(workspace, email.as_deref()).await?;
291 // Two asking at once: the first customer saved stays, and both use it.
292 self.db
293 .prepare(
294 "INSERT INTO accounts (workspace, balance_micros, customer_id, created_at) VALUES (?1, 0, ?2, ?3)
295 ON CONFLICT (workspace) DO UPDATE SET customer_id = COALESCE(customer_id, ?2)",
296 )
297 .bind(&[workspace.into(), customer.as_str().into(), rfc3339(now_ms()).into()])?
298 .run()
299 .await?;
300 Ok(self.row(workspace).await?.and_then(|row| row.customer_id).unwrap_or(customer))
301 }
302
303 /// An owner's email, for a new Stripe customer's receipts and billing
304 /// page sign-in. None when identity cannot say; the owners can add one
305 /// on the billing page.
306 async fn owner_email(&self, workspace: &str) -> Option<String> {
307 let identity = self.identity.as_ref()?;
308 let args = g1t_contracts::identity::SlugArgs { slug: workspace.to_owned() };
309 match g1t_kit::call::<_, Option<g1t_contracts::identity::AdminWorkspaceDetail>>(identity, "admin_workspace", &args).await {
310 Ok(found) => found?.members.into_iter().find(|m| m.role == g1t_contracts::Role::Owner && m.email.is_some()).and_then(|m| m.email),
311 Err(error) => {
312 worker::console_error!("{workspace}: no owner email for its Stripe customer: {error}");
313 None
314 }
315 }
316 }
317
318 /// Stripe's hosted billing page for the workspace. Owners only.
319 async fn billing_portal(&self, a: BillingPortalArgs) -> Result<Outcome<Checkout>> {
320 let workspace = a.workspace.to_lowercase();
321 if !a.actor.manages_billing(&workspace) {
322 return Ok(Outcome::fail(FailureCode::Forbidden, "Only an owner or a billing manager can manage the workspace's billing."));
323 }
324 let Some(stripe) = &self.stripe else {
325 return Ok(Outcome::fail(FailureCode::Conflict, "Payments are not set up on this g1t."));
326 };
327 let customer = match self.customer_for(&workspace).await {
328 Ok(customer) => customer,
329 Err(error) => return Ok(Outcome::fail(FailureCode::Conflict, crate::stripe::friendly(&error))),
330 };
331 match stripe.portal_session(&customer, &a.return_url).await {
332 Ok(url) => Ok(Outcome::Ok(Checkout { url })),
333 Err(error) if stripe::is_missing(&error) => {
334 // The customer was removed at Stripe: a new one next time.
335 self.forget_customer(&workspace).await?;
336 Ok(Outcome::fail(FailureCode::Conflict, "Stripe no longer had this workspace's customer. Try again."))
337 }
338 Err(error) => Ok(Outcome::fail(FailureCode::Conflict, crate::stripe::friendly(&error))),
339 }
340 }
341
342 /// Adds a ledger entry and moves the balance by the same amount, as
343 /// one write.
344 #[allow(clippy::too_many_arguments)]
345 async fn enter(
346 &self,
347 workspace: &str,
348 kind: EntryKind,
349 amount_micros: i64,
350 description: &str,
351 reference: &str,
352 cost_micros: Option<i64>,
353 created_by: Option<&str>,
354 customer: Option<&str>,
355 ) -> Result<()> {
356 let statements = self.entry_statements(workspace, kind, amount_micros, description, reference, cost_micros, created_by, customer)?;
357 let kind = match kind {
358 EntryKind::TopUp => "top_up",
359 EntryKind::Usage => "usage",
360 };
361 self.db.batch(statements).await?;
362 self.after_entry(workspace, kind, amount_micros).await
363 }
364
365 /// The two writes of a ledger entry (the entry, and the balance moved
366 /// by as much), for a caller that makes them part of a larger batch.
367 /// A reference entered before fails the whole batch.
368 #[allow(clippy::too_many_arguments)]
369 pub(crate) fn entry_statements(
370 &self,
371 workspace: &str,
372 kind: EntryKind,
373 amount_micros: i64,
374 description: &str,
375 reference: &str,
376 cost_micros: Option<i64>,
377 created_by: Option<&str>,
378 customer: Option<&str>,
379 ) -> Result<Vec<worker::D1PreparedStatement>> {
380 let now = now_ms();
381 let timestamp = rfc3339(now);
382 let kind = match kind {
383 EntryKind::TopUp => "top_up",
384 EntryKind::Usage => "usage",
385 };
386 Ok(vec![
387 self.db
388 .prepare(
389 "INSERT INTO ledger
390 (id, workspace, kind, amount_micros, description, cost_micros, reference, created_by, created_at, billed_to)
391 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'g1t')",
392 )
393 .bind(&[
394 new_id("led", now).into(),
395 workspace.into(),
396 kind.into(),
397 // D1 takes numbers as doubles, which hold every
398 // amount this service will see exactly.
399 (amount_micros as f64).into(),
400 description.into(),
401 cost_micros.map_or(JsValue::NULL, |cost| (cost as f64).into()),
402 reference.into(),
403 optional(created_by),
404 timestamp.as_str().into(),
405 ])?,
406 self.db
407 .prepare(
408 "INSERT INTO accounts (workspace, balance_micros, customer_id, created_at)
409 VALUES (?1, ?2, ?3, ?4)
410 ON CONFLICT (workspace) DO UPDATE SET
411 balance_micros = balance_micros + ?2,
412 customer_id = COALESCE(?3, customer_id)",
413 )
414 .bind(&[
415 workspace.into(),
416 (amount_micros as f64).into(),
417 optional(customer),
418 timestamp.as_str().into(),
419 ])?,
420 ])
421 }
422
423 /// What follows an entry entered on its own.
424 async fn after_entry(&self, workspace: &str, kind: &str, amount_micros: i64) -> Result<()> {
425 // Money in clears a card declined at the limit. A refund or a lost
426 // dispute is a top-up of less than nothing: money out, which never
427 // clears it. A stop for a dispute lifts only once no dispute is open
428 // and what lost ones took back is paid again, over one payment or
429 // several (`dispute_owed_micros`): a smaller, unrelated payment
430 // leaves it.
431 if kind == "top_up" && amount_micros > 0 {
432 self.db
433 .batch(vec![
434 self.db
435 .prepare(
436 "UPDATE limits SET dispute_owed_micros = MAX(dispute_owed_micros - ?2, 0)
437 WHERE workspace = ?1 AND dispute_owed_micros > 0",
438 )
439 .bind(&[workspace.into(), (amount_micros as f64).into()])?,
440 self.db.prepare(CLEAR_STOP).bind(&[workspace.into()])?,
441 ])
442 .await?;
443 }
444 Ok(())
445 }
446
447 async fn account(&self, a: AccountArgs) -> Result<Outcome<Account>> {
448 let workspace = a.workspace.to_lowercase();
449 if !a.viewer.is_some_and(|viewer| viewer.is_member(&workspace)) {
450 return Ok(members_only());
451 }
452 Ok(Outcome::Ok(self.standing(&workspace).await?))
453 }
454
455 async fn ledger(&self, a: AccountArgs) -> Result<Outcome<Vec<LedgerEntry>>> {
456 let workspace = a.workspace.to_lowercase();
457 if !a.viewer.is_some_and(|viewer| viewer.is_member(&workspace)) {
458 return Ok(members_only());
459 }
460 let rows = self
461 .db
462 .prepare("SELECT * FROM ledger WHERE workspace = ? ORDER BY id DESC LIMIT ?")
463 .bind(&[workspace.into(), LEDGER_PAGE.into()])?
464 .all()
465 .await?
466 .results::<LedgerRow>()?;
467 Ok(Outcome::Ok(
468 rows.into_iter().map(LedgerEntry::from).collect(),
469 ))
470 }
471
472 async fn usage(&self, a: UsageArgs) -> Result<Outcome<Usage>> {
473 let workspace = a.workspace.to_lowercase();
474 if !a.viewer.is_some_and(|viewer| viewer.is_member(&workspace)) {
475 return Ok(members_only());
476 }
477 #[derive(serde::Deserialize)]
478 struct SliceRow {
479 key: Option<String>,
480 micros: Option<i64>,
481 runs: Option<u32>,
482 }
483 // While g1t is free, what was used at cost is what there is to show.
484 // With a discount, usage at its price, so a 100% discount still
485 // shows what the workspace would pay.
486 // Every slice measures usage at price (statement::PRICE_SQL), the one
487 // figure every page shows as usage, whatever paid for it.
488 let discount_percent = self.terms_of(&workspace).await?.percent_off();
489 let measure = if self.free { "COALESCE(cost_micros, 0)" } else { crate::statement::PRICE_SQL };
490 let slices = |key: &str, limit: u32| {
491 format!(
492 "SELECT {key} AS key, SUM({measure}) AS micros, COUNT(*) AS runs FROM ledger
493 WHERE workspace = ?1 AND kind = 'usage' AND created_at >= ?2
494 GROUP BY 1 ORDER BY micros DESC LIMIT {limit}"
495 )
496 };
497 let query = |sql: String| {
498 let db = &self.db;
499 let workspace = workspace.clone();
500 let since = a.since.clone();
501 async move {
502 let rows = db
503 .prepare(sql)
504 .bind(&[workspace.into(), since.into()])?
505 .all()
506 .await?
507 .results::<SliceRow>()?;
508 Ok::<Vec<UsageSlice>, worker::Error>(
509 rows.into_iter()
510 .map(|row| UsageSlice {
511 key: row.key.unwrap_or_else(|| "other".to_owned()),
512 micros: row.micros.unwrap_or_default(),
513 runs: row.runs.unwrap_or_default(),
514 })
515 .collect(),
516 )
517 }
518 };
519 #[derive(serde::Deserialize)]
520 struct Totals {
521 spent: Option<i64>,
522 cost: Option<i64>,
523 provider: Option<i64>,
524 runs: Option<u32>,
525 added: Option<i64>,
526 discount: Option<i64>,
527 covered: Option<i64>,
528 price: Option<i64>,
529 }
530 let totals = async {
531 self.db
532 .prepare(format!(
533 "SELECT
534 -SUM(CASE WHEN kind = 'usage' THEN amount_micros END) AS spent,
535 SUM(CASE WHEN kind = 'usage' THEN COALESCE(credit_micros, 0) + COALESCE(trial_micros, 0)
536 + COALESCE(oss_micros, 0) + COALESCE(given_micros, 0) END) AS covered,
537 SUM(CASE WHEN kind = 'usage' THEN {price} END) AS price,",
538 price = crate::statement::PRICE_SQL
539 ) + "
540 SUM(CASE WHEN kind = 'usage' AND COALESCE(billed_to, 'g1t') = 'g1t' THEN cost_micros END) AS cost,
541 SUM(CASE WHEN kind = 'usage' AND billed_to = 'workspace' THEN cost_micros END) AS provider,
542 SUM(CASE WHEN kind = 'usage' AND " + crate::statement::RUN_SQL + " THEN 1 ELSE 0 END) AS runs,
543 SUM(CASE WHEN kind = 'top_up' THEN amount_micros END) AS added,
544 SUM(CASE WHEN kind = 'usage' THEN discount_micros END) AS discount
545 FROM ledger WHERE workspace = ?1 AND created_at >= ?2",
546 )
547 .bind(&[workspace.as_str().into(), a.since.as_str().into()])?
548 .first::<Totals>(None)
549 .await
550 };
551 // The totals and the five slices read the same rows independently,
552 // so they go to D1 at once: one round trip of waiting, not six.
553 let (totals, (by_day, by_task, by_repo, by_pull, by_model)) = try_join(
554 totals,
555 try_join5(
556 query(slices("substr(created_at, 1, 10) || '/' || COALESCE(task, 'other')", 400)),
557 query(slices("task", 20)),
558 query(slices("repo", 20)),
559 query(slices("repo || '#' || number", 10)),
560 query(slices("model", 10)),
561 ),
562 )
563 .await?;
564 let totals = totals.unwrap_or(Totals {
565 spent: None,
566 cost: None,
567 provider: None,
568 runs: None,
569 added: None,
570 discount: None,
571 covered: None,
572 price: None,
573 });
574 Ok(Outcome::Ok(Usage {
575 spent_micros: totals.spent.unwrap_or_default(),
576 // What the included usage, the trial, a pool or g1t paid, as the
577 // entries record it.
578 covered_micros: totals.covered.unwrap_or_default(),
579 price_micros: if self.free { totals.cost.unwrap_or_default() } else { totals.price.unwrap_or_default() },
580 cost_micros: totals.cost.unwrap_or_default(),
581 provider_micros: totals.provider.unwrap_or_default(),
582 used_micros: totals.cost.unwrap_or_default() + totals.provider.unwrap_or_default(),
583 free: self.free,
584 discount_micros: totals.discount.unwrap_or_default(),
585 discount_percent: (discount_percent > 0).then_some(discount_percent),
586 runs: totals.runs.unwrap_or_default(),
587 added_micros: totals.added.unwrap_or_default(),
588 by_day,
589 by_task,
590 by_repo,
591 by_pull,
592 by_model,
593 since: a.since,
594 }))
595 }
596
597 /// `checkout`: prepays usage, by card or (from $1,000) bank transfer.
598 async fn checkout(&self, a: CheckoutArgs) -> Result<Outcome<Checkout>> {
599 let workspace = a.workspace.to_lowercase();
600 if !a.actor.manages_billing(&workspace) {
601 return Ok(Outcome::fail(
602 FailureCode::Forbidden,
603 "Only an owner or a billing manager can prepay for a workspace.",
604 ));
605 }
606 let Some(stripe) = &self.stripe else {
607 return Ok(Outcome::fail(
608 FailureCode::Conflict,
609 "Payments are not set up on this g1t yet.",
610 ));
611 };
612 let bank_transfer = a.method.as_deref() == Some("bank_transfer");
613 if let Err(why) = prepay_amount(a.amount_cents, bank_transfer) {
614 return Ok(Outcome::fail(FailureCode::Invalid, why));
615 }
616 // A bank transfer needs a customer for its account details.
617 let customer = if bank_transfer {
618 match self.customer_for(&workspace).await {
619 Ok(customer) => Some(customer),
620 Err(error) => return Ok(Outcome::fail(FailureCode::Conflict, stripe::friendly(&error))),
621 }
622 } else {
623 self.row(&workspace).await?.and_then(|row| row.customer_id)
624 };
625 // No card fee: what is prepaid is what is paid, before tax.
626 let started = match stripe.start_checkout(&workspace, a.amount_cents, customer.as_deref(), &a.return_url, bank_transfer).await {
627 // A customer saved under another Stripe account: start afresh.
628 Err(error) if customer.is_some() && stripe::is_missing(&error) => {
629 self.forget_customer(&workspace).await?;
630 let customer = if bank_transfer { self.customer_for(&workspace).await.ok() } else { None };
631 stripe.start_checkout(&workspace, a.amount_cents, customer.as_deref(), &a.return_url, bank_transfer).await
632 }
633 other => other,
634 };
635 self.page_opened(started, &workspace, a.amount_cents, 0, &a.actor.username, None).await
636 }
637
638 /// A payment page Stripe started (or refused), recorded in `checkouts`
639 /// and handed back; any failure as a sentence for the page, never a
640 /// raw error.
641 pub(crate) async fn page_opened(
642 &self,
643 started: Result<stripe::Session>,
644 workspace: &str,
645 amount_cents: u32,
646 fee_cents: u32,
647 created_by: &str,
648 feature: Option<&str>,
649 ) -> Result<Outcome<Checkout>> {
650 let session = match started {
651 Ok(session) => session,
652 Err(error) => {
653 worker::console_error!("{workspace}: Stripe refused a payment page ({}): {error}", feature.unwrap_or("prepay"));
654 return Ok(Outcome::fail(FailureCode::Conflict, stripe::friendly(&error)));
655 }
656 };
657 let Some(url) = session.url.clone() else {
658 return Ok(Outcome::fail(FailureCode::Conflict, "Stripe returned no payment page. Try again in a minute."));
659 };
660 let recorded = self
661 .record_checkout(&NewCheckout { id: &session.id, workspace, amount_cents, fee_cents, created_by, feature })
662 .await;
663 if let Err(error) = recorded {
664 worker::console_error!("{workspace}: a payment page could not be recorded: {error}");
665 return Ok(Outcome::fail(FailureCode::Conflict, "g1t could not keep track of the payment page. Nothing was charged; try again."));
666 }
667 Ok(Outcome::Ok(Checkout { url }))
668 }
669
670 /// Credits a payment if the processor says it was made and it has not
671 /// been credited before. The amount credited is what the processor
672 /// says was paid, not what anyone here remembers asking for.
673 async fn confirm(&self, a: ConfirmArgs) -> Result<Outcome<Account>> {
674 let workspace = a.workspace.to_lowercase();
675 if !a.viewer.is_some_and(|viewer| viewer.is_member(&workspace)) {
676 return Ok(members_only());
677 }
678 let (Some(stripe), Some(checkout)) = (
679 &self.stripe,
680 self.db
681 .prepare(
682 // A prepayment only: a plan's or a card check's page is
683 // settled where it was started, never credited as money
684 // paid in advance.
685 "SELECT workspace, created_by, fee_cents FROM checkouts
686 WHERE id = ? AND workspace = ? AND status = 'open' AND feature IS NULL",
687 )
688 .bind(&[a.session.as_str().into(), workspace.as_str().into()])?
689 .first::<CheckoutRow>(None)
690 .await?,
691 ) else {
692 // Unknown, someone else's, or already credited: nothing to do.
693 return Ok(Outcome::Ok(self.standing(&workspace).await?));
694 };
695 let session = match stripe.session(&a.session).await {
696 Ok(session) => session,
697 Err(error) => return Ok(Outcome::fail(FailureCode::Conflict, crate::stripe::friendly(&error))),
698 };
699 if session.payment_status == "paid" && session.amount_total.is_some() {
700 // The credit first, then the page marked paid: entered once
701 // (the ledger's reference is the page), however many confirm it
702 // at once, and never a page marked paid with no credit if the
703 // second write fails; the next to look marks it.
704 self.credit_prepayment(&checkout.workspace, &session, checkout.fee_cents.unwrap_or(0), &checkout.created_by).await?;
705 self.db
706 .prepare("UPDATE checkouts SET status = 'paid' WHERE id = ? AND status = 'open'")
707 .bind(&[a.session.as_str().into()])?
708 .run()
709 .await?;
710 }
711 Ok(Outcome::Ok(self.standing(&workspace).await?))
712 }
713
714 /// Enters a paid prepayment page: what its lines came to before tax,
715 /// less any card fee (only pages opened before the fee was folded into
716 /// prices had one), as credit; its tax and fee kept apart. Once per
717 /// page, by the ledger's unique reference: a page credited already
718 /// enters nothing (0). The credit in cents.
719 pub(crate) async fn credit_prepayment(&self, workspace: &str, session: &stripe::Session, fee_cents: u32, by: &str) -> Result<i64> {
720 let seen = self.db.prepare("SELECT id FROM ledger WHERE reference = ?").bind(&[session.id.as_str().into()])?.first::<Touched>(None).await?;
721 if seen.is_some() {
722 return Ok(0);
723 }
724 let fee = i64::from(fee_cents);
725 let cents = tax::prepay_credit_cents(session.before_tax_cents(), fee);
726 let tax_cents = session.tax_cents();
727 let description = format!("Paid in advance{}", tax::paid_with(tax::Extras { tax_cents, fee_cents: fee }));
728 let entered = self
729 .enter(
730 workspace,
731 EntryKind::TopUp,
732 cents * MICROS_PER_DOLLAR / 100,
733 &description,
734 &session.id,
735 None,
736 Some(by),
737 session.customer.as_deref(),
738 )
739 .await;
740 match entered {
741 Ok(()) => {}
742 // Credited a moment ago by whoever confirmed it alongside.
743 Err(error) if is_duplicate(&error) => return Ok(0),
744 Err(error) => return Err(error),
745 }
746 self.record_extras(workspace, &session.id, session.payment_intent.as_deref(), tax::Extras { tax_cents, fee_cents: fee }, None)
747 .await?;
748 Ok(cents)
749 }
750
751 /// Drops a saved customer the card processor no longer knows.
752 pub(crate) async fn forget_customer(&self, workspace: &str) -> Result<()> {
753 self.db
754 .prepare(
755 "UPDATE accounts SET customer_id = NULL, card_brand = NULL, card_last4 = NULL, card_exp_month = NULL,
756 card_exp_year = NULL, card_synced_at = NULL WHERE workspace = ?",
757 )
758 .bind(&[workspace.into()])?
759 .run()
760 .await?;
761 Ok(())
762 }
763}
764
765impl Billing {
766 /// Records how long a sandbox ran, with its cost and its charge: every
767 /// second, from the first, at the price book's price; on its own CPU
768 /// when it reports it. Settles its reservation, if it names one.
769 async fn record_sandbox(&self, a: RecordSandboxArgs) -> Result<Outcome<bool>> {
770 // Self-hosted time is recorded wherever g1t runs, for its minutes;
771 // anything else only where there is a bill to put it on.
772 if (!self.charging && !a.self_hosted) || a.seconds == 0 {
773 return Ok(Outcome::Ok(false));
774 }
775 let workspace = a.workspace.to_lowercase();
776 let seen = self
777 .db
778 .prepare("SELECT id FROM ledger WHERE reference = ?")
779 .bind(&[a.reference.as_str().into()])?
780 .first::<Touched>(None)
781 .await?;
782 if seen.is_some() {
783 return Ok(Outcome::Ok(false));
784 }
785 let now = now_ms();
786 let timestamp = rfc3339(now);
787 let seconds = i64::from(a.seconds);
788 // On the workspace's own machine: its minutes go on usage, at $0,
789 // and whatever was reserved for it is given back.
790 if a.self_hosted {
791 let description = format!("{}: {} of self-hosted runner time, $0", a.description, duration(seconds));
792 self.db
793 .prepare(
794 "INSERT INTO ledger
795 (id, workspace, kind, amount_micros, description, repo, task,
796 cost_micros, reference, created_at, billed_to, credit_micros, trial_micros, oss_micros, given_micros, quantity)
797 VALUES (?, ?, 'usage', 0, ?, ?, 'self_hosted', 0, ?, ?, 'workspace', 0, 0, 0, 0, ?)
798 ON CONFLICT (reference) DO NOTHING",
799 )
800 .bind(&[
801 new_id("led", now).into(),
802 workspace.as_str().into(),
803 description.as_str().into(),
804 optional(a.repo.as_deref()),
805 a.reference.as_str().into(),
806 timestamp.as_str().into(),
807 (seconds as f64).into(),
808 ])?
809 .run()
810 .await?;
811 if let Some(reservation) = &a.reservation_id {
812 self.settle_reservation(SettleArgs { reservation_id: reservation.clone(), actual_micros: 0 }).await?;
813 }
814 return Ok(Outcome::Ok(true));
815 }
816 // From the price book, which follows what Cloudflare bills g1t. A
817 // sandbox is the same container as a build, so without a row it is
818 // a build second's cost plus the margin.
819 let (cost_per_second, _) = self.price("sandbox_second").await?.unwrap_or_else(|| {
820 let cost = deployment_costs::MICROS_PER_BUILD_SECOND as f64;
821 (cost, Price::price_for(cost, self.margin_percent))
822 });
823 // The machine it ran on, from the catalogue by its `runs-on` label;
824 // the standard one otherwise. What it cost g1t: a larger machine's
825 // memory and disk cost more each second, and without its own CPU it
826 // is costed at its vCPUs as busy as the standard machine's.
827 let instance = a.instance.as_deref().and_then(g1t_contracts::actions::instance_named).unwrap_or(g1t_contracts::actions::STANDARD_INSTANCE);
828 let base_scale = keeper::base_scale(instance.memory_gib, instance.disk_gb);
829 let cost_scale = keeper::busy_scale(&instance);
830 let vcpus = instance.vcpu.max(4.0);
831 // Its own CPU when the sandbox reports it; otherwise the average.
832 let parts = match a.cpu_seconds.filter(|cpu| cpu.is_finite() && *cpu >= 0.0) {
833 Some(cpu) => match (self.price("sandbox_base_second").await?, self.price("sandbox_cpu_second").await?) {
834 (Some((base, _)), Some((vcpu, _))) => {
835 Some((keeper::run_cost(seconds, cpu.min(seconds as f64 * vcpus), base * base_scale, vcpu), cpu))
836 }
837 _ => None,
838 },
839 None => None,
840 };
841 let cost = parts.map_or(seconds as f64 * cost_per_second * cost_scale, |(cost, _)| cost).ceil() as i64;
842 // Charged by the job: its minutes, rounded up, at the machine's rate
843 // (never under what it cost g1t plus the margin).
844 let (minutes, price) = keeper::runner_charge(seconds, &instance, cost, self.margin_percent);
845 let (charge, terms_note, discount) = self.charged(&workspace, price).await?;
846 let eligible = credits::eligible_for(a.kind, a.repo.as_deref());
847 let month = credits::month_of(&timestamp);
848 let drawn = self.draw(&workspace, charge, &month, &eligible).await?;
849 let charge = charge - drawn.total();
850 let description = format!(
851 "{}: {}, {} on the {} machine{terms_note}{}",
852 a.description,
853 duration(seconds),
854 keeper::minutes_text(minutes),
855 instance.machine,
856 drawn.note()
857 );
858 // The price versions it was charged at (pricing.rs).
859 let meters: &[&str] = if parts.is_some() { &["sandbox_base_second", "sandbox_cpu_second"] } else { &["sandbox_second"] };
860 let mut versions = Vec::new();
861 for meter in meters {
862 versions.extend(self.version_now(meter).await?);
863 }
864 let price_version = versions.join(",");
865 // The ledger's unique reference decides between duplicate
866 // deliveries: the entry goes in, and the balance moves, only for the
867 // first; a later one gives back what it drew.
868 let id = new_id("led", now);
869 let written = self
870 .db
871 .batch(vec![
872 self.db
873 .prepare(
874 "INSERT INTO ledger
875 (id, workspace, kind, amount_micros, description, repo, task,
876 cost_micros, reference, created_at, billed_to, credit_micros, trial_micros, oss_micros, given_micros, price_version, quantity, compute, machine)
877 VALUES (?, ?, 'usage', ?, ?, ?, 'sandbox', ?, ?, ?, 'g1t', ?, ?, ?, ?, ?, ?, ?, ?)
878 ON CONFLICT (reference) DO NOTHING RETURNING id",
879 )
880 .bind(&[
881 id.as_str().into(),
882 workspace.as_str().into(),
883 (-(charge as f64)).into(),
884 description.as_str().into(),
885 optional(a.repo.as_deref()),
886 (cost as f64).into(),
887 a.reference.as_str().into(),
888 timestamp.as_str().into(),
889 (drawn.credit as f64).into(),
890 (drawn.trial as f64).into(),
891 (drawn.oss as f64).into(),
892 (drawn.given as f64).into(),
893 optional(Some(price_version.as_str()).filter(|v| !v.is_empty())),
894 // Runner minutes, as charged; lines with no machine
895 // (before minutes) hold seconds.
896 (minutes as f64).into(),
897 optional(a.kind.map(ComputeKind::as_str)),
898 instance.machine.into(),
899 ])?,
900 self.balance_if_posted(&workspace, charge, &id)?,
901 ])
902 .await
903 .and_then(|results| storage::posted(&results));
904 let repo = eligible.repo.as_deref();
905 match written {
906 Ok(true) => {}
907 Ok(false) => {
908 self.undraw(&workspace, &month, &drawn, repo).await?;
909 return Ok(Outcome::Ok(false));
910 }
911 Err(error) => {
912 self.undraw(&workspace, &month, &drawn, repo).await?;
913 return Err(error);
914 }
915 }
916 self.record_discount(&a.reference, discount).await?;
917 self.count_spend(&workspace, cost, charge, &drawn).await;
918 if let Some(reservation) = &a.reservation_id {
919 self.settle_reservation(SettleArgs { reservation_id: reservation.clone(), actual_micros: cost }).await?;
920 }
921 Ok(Outcome::Ok(true))
922 }
923}
924
925/// Whether g1t charges, from `CHARGING`: `on` or `off`; unset (or
926/// anything else), whether the card processor's key is set, as before the
927/// switch.
928pub(crate) fn charging_from(var: Option<&str>, has_key: bool) -> bool {
929 match var.map(str::trim) {
930 Some("on") => true,
931 Some("off") => false,
932 _ => has_key,
933 }
934}
935
936/// Whether an amount may be prepaid: $25 at the least by card, and from
937/// $1,000 by bank transfer.
938pub(crate) fn prepay_amount(cents: u32, bank_transfer: bool) -> std::result::Result<(), String> {
939 let (min, max) = if bank_transfer { (MIN_BANK_TRANSFER_CENTS, MAX_BANK_TRANSFER_CENTS) } else { (MIN_TOP_UP_CENTS, MAX_TOP_UP_CENTS) };
940 if (min..=max).contains(&cents) {
941 return Ok(());
942 }
943 Err(if bank_transfer {
944 format!("Prepay between ${} and ${} by bank transfer.", min / 100, group(max / 100))
945 } else {
946 format!("Prepay between ${} and ${} by card; from $1,000, a bank transfer works too.", min / 100, group(max / 100))
947 })
948}
949
950/// `10,000` for 10000.
951fn group(n: u32) -> String {
952 let digits = n.to_string();
953 let mut out = String::new();
954 for (i, c) in digits.chars().enumerate() {
955 if i > 0 && (digits.len() - i).is_multiple_of(3) {
956 out.push(',');
957 }
958 out.push(c);
959 }
960 out
961}
962
963#[cfg(test)]
964/// What `seconds` of sandbox time are charged at `price_per_second`, in
965/// millionths of a dollar: every second, rounded up to the next millionth.
966fn sandbox_charge(seconds: i64, price_per_second: f64) -> i64 {
967 (seconds as f64 * price_per_second).ceil() as i64
968}
969
970/// `1h 2m`, `3m 12s` or `40s`.
971fn duration(seconds: i64) -> String {
972 let (h, m, s) = (seconds / 3600, seconds % 3600 / 60, seconds % 60);
973 if h > 0 {
974 format!("{h}h {m}m")
975 } else if m > 0 {
976 format!("{m}m {s}s")
977 } else {
978 format!("{s}s")
979 }
980}
981
982impl Billing {
983 /// What a workspace is charged for something that would be `base`:
984 /// nothing while g1t is free, or as its account's terms say. With a
985 /// note for the statement when it differs.
986 /// A charge at cost plus the margin (`base`) on the account's terms:
987 /// what is charged, the note for the statement, and what a discount
988 /// gave away below `base`. That last is written on the entry
989 /// (`record_discount`) so the reconciliation counts it as given, never
990 /// as margin lost: a sold charge is worth at least its cost plus the
991 /// margin.
992 pub(crate) async fn charged(&self, workspace: &str, base: i64) -> Result<(i64, String, i64)> {
993 if self.free {
994 return Ok((0, " (free while g1t is being built out)".to_owned(), 0));
995 }
996 let terms = self.terms_of(workspace).await?;
997 let (charge, discount) = terms.discounted(base);
998 let note = match terms.discount_label() {
999 Some(label) if base > 0 => format!(" ({label})"),
1000 _ => String::new(),
1001 };
1002 Ok((charge, note, discount))
1003 }
1004
1005 /// What a discount gave away on an entry, below cost plus the margin
1006 /// (less than nothing on a correction down).
1007 pub(crate) async fn record_discount(&self, reference: &str, micros: i64) -> Result<()> {
1008 if micros == 0 {
1009 return Ok(());
1010 }
1011 self.db
1012 .prepare("UPDATE ledger SET discount_micros = ? WHERE reference = ?")
1013 .bind(&[(micros as f64).into(), reference.into()])?
1014 .run()
1015 .await?;
1016 Ok(())
1017 }
1018}
1019
1020fn members_only<T>() -> Outcome<T> {
1021 Outcome::fail(
1022 FailureCode::Forbidden,
1023 "Only members can see a workspace's billing.",
1024 )
1025}
1026
1027impl Billing {
1028 fn from_env(env: &Env) -> Result<Self> {
1029 let stripe = env
1030 .secret("STRIPE_SECRET_KEY")
1031 .ok()
1032 .map(|key| key.to_string())
1033 .filter(|key| !key.is_empty())
1034 .map(Stripe::new);
1035 let charging = charging_from(env.var("CHARGING").ok().map(|v| v.to_string()).as_deref(), stripe.is_some());
1036 if charging && stripe.is_none() {
1037 worker::console_error!(
1038 "CHARGING is on but STRIPE_SECRET_KEY is not set: usage is recorded, free workspaces are held to their limits, and nothing can be paid"
1039 );
1040 }
1041 Ok(Billing {
1042 db: env.d1("DB")?,
1043 stripe,
1044 charging,
1045 webhook_secret: env
1046 .secret("STRIPE_WEBHOOK_SECRET")
1047 .ok()
1048 .map(|secret| secret.to_string().trim().to_owned())
1049 .filter(|secret| !secret.is_empty()),
1050 margin_percent: env
1051 .var("MARGIN_PERCENT")
1052 .ok()
1053 .and_then(|percent| percent.to_string().parse().ok())
1054 .unwrap_or(20),
1055 free: env.var("FREE_WHILE_BUILDING").is_ok_and(|v| v.to_string() == "true"),
1056 ceilings: limits::Ceilings::from_env(env),
1057 trials_on: {
1058 let plans = credits::Config::from_env(env);
1059 plans.trial_workspace_micros > 0 && plans.trial_monthly_pool_micros > 0
1060 },
1061 plans: credits::Config::from_env(env),
1062 repos: env.service("REPOS").ok(),
1063 packages: env.service("PACKAGES").ok(),
1064 identity: env.service("IDENTITY").ok(),
1065 caps: budget::Caps::from_env(env),
1066 env: env.clone(),
1067 })
1068 }
1069}
1070
1071/// The fifteen-minute steps, each on its own: one failing never skips the
1072/// rest.
1073async fn tick(billing: &Billing, env: &Env) {
1074 // Stripe events billing never received, handled now.
1075 match billing.replay_events().await {
1076 Ok(done) => worker::console_log!("stripe replay: {done}"),
1077 Err(error) => worker::console_error!("replaying Stripe events failed: {error}"),
1078 }
1079 // This month's users, read once for autopay and the warnings.
1080 let users = if billing.charging {
1081 match billing.month_users().await {
1082 Ok(users) => Some(users),
1083 Err(error) => {
1084 worker::console_error!("reading this month's users failed: {error}");
1085 None
1086 }
1087 }
1088 } else {
1089 None
1090 };
1091 if let Some(users) = &users
1092 && let Err(error) = billing.autopay(users).await {
1093 worker::console_error!("paying at the limit failed: {error}");
1094 }
1095 // Last month's metered usage (scans, embeddings, storage) goes on the
1096 // ledger before the month is closed and invoiced.
1097 if let Err(error) = billing.charge_pending().await {
1098 worker::console_error!("charging last month's metered usage failed: {error}");
1099 }
1100 if let Err(error) = billing.close_months().await {
1101 worker::console_error!("closing the month failed: {error}");
1102 }
1103 if let Err(error) = billing.invoice_enterprises().await {
1104 worker::console_error!("invoicing enterprises failed: {error}");
1105 }
1106 if let Some(users) = &users
1107 && let Ok(identity) = env.service("IDENTITY")
1108 && let Err(error) = billing.warn_limits(&identity, users).await {
1109 worker::console_error!("warning owners failed: {error}");
1110 }
1111}
1112
1113/// Every fifteen minutes: runs settled, Stripe replayed, cards charged at
1114/// the limit, months closed, owners warned. Once a day, at
1115/// [`keeper::DAILY`], in an invocation of its own: the slower checks
1116/// against Stripe and Cloudflare, and measuring.
1117///
1118/// Each step logs its own failure and the next still runs. The spend
1119/// breaker and budget alerts go first: a step that runs out of CPU ends
1120/// the invocation, and must never take the alerts with it.
1121#[event(scheduled)]
1122async fn scheduled(event: ScheduledEvent, env: Env, _ctx: ScheduleContext) {
1123 let Ok(billing) = Billing::from_env(&env) else {
1124 return;
1125 };
1126 // Comped budgets' alerts, and a tripped breaker staff were not told of.
1127 if let Err(error) = billing.watch_spend().await {
1128 worker::console_error!("watching g1t's own spend failed: {error}");
1129 }
1130 let keeper = keeper::Keeper::from_env(&env);
1131 // Once an hour, at the quarter past: what Cloudflare counted for the
1132 // whole platform in the hour before, against its thresholds
1133 // (platform.rs).
1134 if event.cron() == keeper::QUARTER_HOURLY && platform::hourly_due(now_ms()) {
1135 match billing.watch_platform(&keeper).await {
1136 Ok((read, breached)) => worker::console_log!("platform watch: {read} metrics, {breached} breaches"),
1137 Err(error) => worker::console_error!("watching platform usage failed: {error}"),
1138 }
1139 }
1140 let daily = event.cron() == keeper::DAILY;
1141 if !daily {
1142 tick(&billing, &env).await;
1143 }
1144 // Once a day: Stripe's endpoint kept listening to billing's events and
1145 // enabled, and saved cards and plans not read in a while read again.
1146 if daily {
1147 match billing.keep_endpoint("billing").await {
1148 Ok(done) => worker::console_log!("stripe endpoint: {done}"),
1149 Err(error) => worker::console_error!("keeping Stripe's endpoint failed: {error}"),
1150 }
1151 match billing.refresh_from_stripe().await {
1152 Ok(done) => worker::console_log!("stripe refresh: {done}"),
1153 Err(error) => worker::console_error!("refreshing from Stripe failed: {error}"),
1154 }
1155 }
1156 // Once a day, and at once if the costs were never checked: check every
1157 // cost against what Cloudflare billed.
1158 if (daily || billing.never_checked().await.unwrap_or(false))
1159 && let Err(error) = billing.reconcile(&keeper).await {
1160 worker::console_error!("checking costs against Cloudflare failed: {error}");
1161 }
1162 // Once a day: credit from g1t past its expiry stops counting
1163 // (grants.rs), before the day is reconciled.
1164 if daily {
1165 match billing.expire_credits().await {
1166 Ok(closed) => worker::console_log!("credits expired: {closed}"),
1167 Err(error) => worker::console_error!("expiring credits failed: {error}"),
1168 }
1169 }
1170 // Once a day: what Cloudflare charged, reconciled against what g1t
1171 // counted and charged; prices whose day has come; margin alerts
1172 // (margin.rs). After the keeper, so its proposals are in.
1173 if daily {
1174 match billing.costs_daily(&env, &keeper).await {
1175 Ok(run) => worker::console_log!("costs: {} lines, {} days, {} proposals, {} alerts", run.lines, run.days, run.proposals, run.alerts),
1176 Err(error) => worker::console_error!("reconciling costs failed: {error}"),
1177 }
1178 }
1179 // Once a day: what each workspace's private repositories hold, its git
1180 // operations, Deployments plans from before the g1t plan set to end,
1181 // and old reservations cleared.
1182 if daily {
1183 if let Err(error) = billing.measure_packages().await {
1184 worker::console_error!("measuring package storage failed: {error}");
1185 }
1186 if let Err(error) = billing.measure_storage().await {
1187 worker::console_error!("measuring storage failed: {error}");
1188 }
1189 if let Err(error) = billing.measure_git().await {
1190 worker::console_error!("measuring git operations failed: {error}");
1191 }
1192 if let Err(error) = billing.retire_deployments_plans().await {
1193 worker::console_error!("ending Deployments plans failed: {error}");
1194 }
1195 if let Err(error) = billing.retire_security_activations().await {
1196 worker::console_error!("ending Security and quality activations failed: {error}");
1197 }
1198 if let Err(error) = billing.sweep_reservations().await {
1199 worker::console_error!("clearing reservations failed: {error}");
1200 }
1201 // The AI Gateway's request log, from when there was one, still
1202 // keeps only its last 30 days until it is empty.
1203 let cutoff = rfc3339(now_ms().saturating_sub(30 * 24 * 60 * 60 * 1000));
1204 let cleared = match billing.db.prepare("DELETE FROM gateway_requests WHERE created_at < ?").bind(&[cutoff.into()]) {
1205 Ok(statement) => statement.run().await.map(|_| ()),
1206 Err(error) => Err(error),
1207 };
1208 if let Err(error) = cleared {
1209 worker::console_error!("clearing old AI Gateway requests failed: {error}");
1210 }
1211 }
1212}
1213
1214/// Events from the bus, on billing's own queue: only a workspace's rename
1215/// matters here (see `rename`).
1216#[event(queue)]
1217async fn queue(batch: MessageBatch<g1t_contracts::events::Event>, env: Env, _ctx: Context) -> Result<()> {
1218 let billing = Billing::from_env(&env)?;
1219 let identity = env.service("IDENTITY").ok();
1220 for message in batch.messages()? {
1221 // A repository transferred: its share of the open-source pool this
1222 // month follows it. What it was charged stays with the workspace
1223 // it was charged to; usage from now on is charged to the new one.
1224 if g1t_kit::transfer::on_event(&env, &billing.db, message.body(), closing::TRANSFERRED).await? {
1225 message.ack();
1226 continue;
1227 }
1228 billing.on_event(identity.as_ref(), message.body()).await?;
1229 message.ack();
1230 }
1231 Ok(())
1232}
1233
1234#[event(fetch)]
1235async fn fetch(mut request: Request, env: Env, _ctx: Context) -> Result<Response> {
1236 let Some(method) = rpc_method(&request) else {
1237 return Response::error("Not found", 404);
1238 };
1239 // A replica near the caller when it asks for one (crates/kit/src/d1.rs);
1240 // what other services call (can_start, start_run) asks for none.
1241 let (db, served) = g1t_kit::d1::open(&env, "DB", &request)?;
1242 let body: serde_json::Value = request.json().await?;
1243 let mut billing = Billing::from_env(&env)?;
1244 billing.db = db;
1245 let answered = match method.as_str() {
1246 "status" => reply(&billing.status()),
1247 "account" => reply(&billing.account(args(body)?).await?),
1248 "ledger" => reply(&billing.ledger(args(body)?).await?),
1249 "credits" => reply(&billing.credits(args(body)?).await?),
1250 "usage" => reply(&billing.usage(args(body)?).await?),
1251 "usage_report" => reply(&billing.usage_report(args(body)?).await?),
1252 "set_budget" => reply(&billing.set_budget(args(body)?).await?),
1253 "billing_details" => reply(&billing.billing_details(args(body)?).await?),
1254 "set_billing_details" => reply(&billing.set_billing_details(args(body)?).await?),
1255 "checkout" => reply(&billing.checkout(args(body)?).await?),
1256 "confirm" => reply(&billing.confirm(args(body)?).await?),
1257 "trial" => reply(&billing.trial(args(body)?).await?),
1258 "features" => reply(&billing.features(args(body)?).await?),
1259 "subscribe" => reply(&billing.subscribe(args(body)?).await?),
1260 "confirm_subscription" => reply(&billing.confirm_subscription(args(body)?).await?),
1261 "cancel_subscription" => reply(&billing.cancel_subscription(args(body)?).await?),
1262 "close_workspace" => reply(&billing.close_workspace(args(body)?).await?),
1263 "has_feature" => reply(&billing.has_feature(args(body)?).await?),
1264 "free_workspaces" => reply(&billing.free_workspaces(args(body)?).await?),
1265 "charge_feature" => reply(&billing.charge_feature(args(body)?).await?),
1266 "record_sandbox" => reply(&billing.record_sandbox(args(body)?).await?),
1267 "limit" => reply(&billing.limit(args(body)?).await?),
1268 "check_limit" => reply(&billing.check_limit(args(body)?).await?),
1269 "set_spend_limit" => reply(&billing.set_spend_limit(args(body)?).await?),
1270 "prices" => reply(&billing.prices().await?),
1271 "billing_portal" => reply(&billing.billing_portal(args(body)?).await?),
1272 "admin_billing_link" => reply(&billing.admin_billing_link(args(body)?).await?),
1273 "admin_stripe" => reply(&billing.admin_stripe(args(body)?).await?),
1274 "admin_enterprise_billing" => reply(&billing.admin_enterprise_billing(args(body)?).await?),
1275 "admin_enterprise_address" => reply(&billing.admin_enterprise_address(args(body)?).await?),
1276 "admin_invoice_enterprise" => reply(&billing.admin_invoice_enterprise(args(body)?).await?),
1277 "stripe_webhook" => reply(&billing.stripe_webhook(args(body)?).await?),
1278 "invoices" => reply(&billing.invoices(args(body)?).await?),
1279 "statement" => reply(&billing.statement(args(body)?).await?),
1280 "statement_entries" => reply(&billing.statement_entries(args(body)?).await?),
1281 "usage_meters" => reply(&billing.usage_meters(args(body)?).await?),
1282 "admin_workspace_invoices" => {
1283 let a: AdminWorkspaceInvoicesArgs = args(body)?;
1284 reply(&billing.workspace_invoices(&a.workspace.to_lowercase()).await?)
1285 }
1286 "admin_signals" => reply(&billing.admin_signals(args(body)?).await?),
1287 "admin_overview" => reply(&billing.admin_overview(args(body)?).await?),
1288 "admin_sales" => reply(&billing.admin_sales(args(body)?).await?),
1289 "admin_set_sales" => reply(&billing.admin_set_sales(args(body)?).await?),
1290 "admin_add_note" => reply(&billing.admin_add_note(args(body)?).await?),
1291 "admin_invoices" => reply(&billing.admin_invoices(args(body)?).await?),
1292 "admin_audit" => reply(&billing.admin_audit(args(body)?).await?),
1293 // A staff change made in another service, for sudo's audit log:
1294 // identity's restores and purges of deleted workspaces.
1295 "admin_log" => {
1296 let a: AdminLogArgs = args(body)?;
1297 billing
1298 .audit(&accounts::own_account(&a.workspace), &a.action, &a.detail, &a.by)
1299 .await?;
1300 reply(&true)
1301 }
1302 "note_pending" => reply(&billing.note_pending(args(body)?).await?),
1303 "admin_accounts" => reply(&billing.admin_accounts(args(body)?).await?),
1304 "admin_account" => reply(&billing.admin_account(args(body)?).await?),
1305 "admin_set_terms" => reply(&billing.admin_set_terms(args(body)?).await?),
1306 "admin_create_enterprise" => reply(&billing.admin_create_enterprise(args(body)?).await?),
1307 "admin_attach" => reply(&billing.admin_attach(args(body)?).await?),
1308 "admin_credit" => reply(&billing.admin_credit(args(body)?).await?),
1309 "admin_credits" => reply(&billing.admin_credits(args(body)?).await?),
1310 "admin_revoke_credit" => reply(&billing.admin_revoke_credit(args(body)?).await?),
1311 "admin_reset_billing" => reply(&billing.admin_reset_billing(&env, args(body)?).await?),
1312 "admin_detach_test_stripe" => reply(&billing.admin_detach_test_stripe(args(body)?).await?),
1313 "admin_set_allowances" => reply(&billing.admin_set_allowances(args(body)?).await?),
1314 "entitlements" => reply(&billing.entitlements(args(body)?).await?),
1315 "audit_retention" => reply(&billing.audit_retention(args(body)?).await?),
1316 "reserve" => reply(&billing.reserve(args(body)?).await?),
1317 "settle" => reply(&billing.settle_reservation(args(body)?).await?),
1318 "card_check" => reply(&billing.card_check(args(body)?).await?),
1319 "confirm_card_check" => reply(&billing.confirm_card_check(args(body)?).await?),
1320 "request_limit" => reply(&billing.request_limit(args(body)?).await?),
1321 "limit_requests" => reply(&billing.limit_requests(args(body)?).await?),
1322 "confirm_spike" => reply(&billing.confirm_spike(args(body)?).await?),
1323 "admin_limit_requests" => reply(&billing.admin_limit_requests(args(body)?).await?),
1324 "admin_decide_limit_request" => reply(&billing.admin_decide_limit_request(args(body)?).await?),
1325 "admin_overages" => reply(&billing.admin_overages(args(body)?).await?),
1326 "admin_goodwill" => reply(&billing.admin_goodwill(args(body)?).await?),
1327 "admin_velocity" => reply(&billing.admin_velocity(args(body)?).await?),
1328 "admin_record_payment" => reply(&billing.admin_record_payment(args(body)?).await?),
1329 "admin_costs" => reply(&billing.admin_costs(args(body)?, keeper::Keeper::from_env(&env).can_read_bill()).await?),
1330 "admin_cost_alerts" => reply(&billing.admin_cost_alerts(args(body)?).await?),
1331 "admin_spend_caps" => reply(&billing.spend_caps().await?),
1332 "admin_lift_breaker" => reply(&billing.admin_lift_breaker(args(body)?).await?),
1333 // Platform pauses (src/platform.rs): read by every service that
1334 // honours one, kept 30 seconds in each isolate.
1335 "platform_pause" => reply(&billing.pause_now().await),
1336 "admin_platform_guard" => reply(&billing.platform_guard(&keeper::Keeper::from_env(&env)).await?),
1337 "admin_set_pause" => reply(&billing.admin_set_pause(args(body)?, &keeper::Keeper::from_env(&env)).await?),
1338 "admin_decide_proposal" => reply(&billing.admin_decide_proposal(args(body)?).await?),
1339 "admin_set_cost_settings" => reply(&billing.admin_set_cost_settings(args(body)?).await?),
1340 "admin_set_cost_mapping" => reply(&billing.admin_set_cost_mapping(args(body)?).await?),
1341 "admin_run_costs" => reply(&billing.admin_run_costs(&env, args(body)?).await?),
1342 _ => Response::error("Unknown method", 404),
1343 };
1344 served.finish(answered)
1345}
1346
1347#[cfg(test)]
1348mod tests {
1349 use super::*;
1350
1351 #[test]
1352 fn charging_is_the_switch_not_the_key() {
1353 // On: charged and gated whether or not the key is set, so a lost
1354 // secret fails closed rather than admitting everyone for free.
1355 assert!(charging_from(Some("on"), false));
1356 assert!(charging_from(Some("on"), true));
1357 // Off: a self-hosted g1t charges nothing, key or not.
1358 assert!(!charging_from(Some("off"), false));
1359 assert!(!charging_from(Some("off"), true));
1360 // Unset: as before the switch, whether the key is set.
1361 assert!(charging_from(None, true));
1362 assert!(!charging_from(None, false));
1363 assert!(!charging_from(Some(""), false));
1364 }
1365
1366 #[test]
1367 fn sandbox_time_is_charged_from_the_first_second() {
1368 // 21 millionths a second at cost, plus 20%.
1369 let price = Price::price_for(21.0, 20);
1370 assert_eq!(sandbox_charge(1, price), 26);
1371 assert_eq!(sandbox_charge(60, price), 1_512);
1372 assert_eq!(sandbox_charge(0, price), 0);
1373 }
1374
1375 #[test]
1376 fn prepaying_starts_at_twenty_five_dollars_and_bank_transfers_at_a_thousand() {
1377 assert!(prepay_amount(2_500, false).is_ok());
1378 assert!(prepay_amount(2_499, false).is_err());
1379 assert!(prepay_amount(10_000, false).is_ok());
1380 assert!(prepay_amount(1_000_000, false).is_ok());
1381 assert_eq!(prepay_amount(1_000_001, false).unwrap_err(), "Prepay between $25 and $10,000 by card; from $1,000, a bank transfer works too.");
1382 assert!(prepay_amount(99_999, true).is_err());
1383 assert!(prepay_amount(100_000, true).is_ok());
1384 }
1385
1386 #[test]
1387 fn durations_read_plainly() {
1388 assert_eq!(duration(40), "40s");
1389 assert_eq!(duration(192), "3m 12s");
1390 assert_eq!(duration(3720), "1h 2m");
1391 }
1392
1393 /// Every migration, in order, as D1 applies them.
1394 const MIGRATIONS: &[&str] = &[
1395 include_str!("../migrations/0001_init.sql"),
1396 include_str!("../migrations/0002_own_provider.sql"),
1397 include_str!("../migrations/0003_subscriptions.sql"),
1398 include_str!("../migrations/0004_sandbox_time.sql"),
1399 include_str!("../migrations/0005_limits.sql"),
1400 include_str!("../migrations/0006_prices.sql"),
1401 include_str!("../migrations/0007_pending_usage.sql"),
1402 include_str!("../migrations/0008_accounts.sql"),
1403 include_str!("../migrations/0009_autopay.sql"),
1404 include_str!("../migrations/0010_month_close.sql"),
1405 include_str!("../migrations/0011_limit_warnings.sql"),
1406 include_str!("../migrations/0012_stripe_webhooks.sql"),
1407 include_str!("../migrations/0013_invoices_trust_sales.sql"),
1408 include_str!("../migrations/0014_custom_domain_price.sql"),
1409 include_str!("../migrations/0015_sandbox_at_cost.sql"),
1410 include_str!("../migrations/0016_plans_and_pools.sql"),
1411 include_str!("../migrations/0017_flagon_comped.sql"),
1412 include_str!("../migrations/0018_one_plan.sql"),
1413 include_str!("../migrations/0019_closed_workspaces.sql"),
1414 include_str!("../migrations/0020_syntaqx_standard.sql"),
1415 include_str!("../migrations/0021_no_quotas.sql"),
1416 include_str!("../migrations/0022_costs_and_margin.sql"),
1417 include_str!("../migrations/0023_one_operation_mapping.sql"),
1418 include_str!("../migrations/0024_spend_caps.sql"),
1419 include_str!("../migrations/0025_run_tier.sql"),
1420 include_str!("../migrations/0026_ledger_by_workspace_time.sql"),
1421 include_str!("../migrations/0027_stripe_cache.sql"),
1422 include_str!("../migrations/0028_drop_stripe_webhooks.sql"),
1423 include_str!("../migrations/0029_audit_retention.sql"),
1424 include_str!("../migrations/0030_token_usage.sql"),
1425 include_str!("../migrations/0031_package_storage.sql"),
1426 include_str!("../migrations/0032_workspace_value.sql"),
1427 include_str!("../migrations/0033_given_away.sql"),
1428 include_str!("../migrations/0034_given_by_why.sql"),
1429 include_str!("../migrations/0035_cf_subscriptions.sql"),
1430 include_str!("../migrations/0036_model_costs_in_full.sql"),
1431 include_str!("../migrations/0037_security_activation.sql"),
1432 include_str!("../migrations/0038_staff_credits.sql"),
1433 include_str!("../migrations/0039_discounts_not_comped.sql"),
1434 include_str!("../migrations/0040_ai_credit.sql"),
1435 include_str!("../migrations/0041_agent_rate_own_key.sql"),
1436 include_str!("../migrations/0042_agent_rate_weights.sql"),
1437 include_str!("../migrations/0043_tax_and_card_fees.sql"),
1438 include_str!("../migrations/0044_cache_reads_count_a_tenth.sql"),
1439 include_str!("../migrations/0045_gateway.sql"),
1440 include_str!("../migrations/0046_reset_costs.sql"),
1441 include_str!("../migrations/0047_gateway_formats.sql"),
1442 include_str!("../migrations/0048_model_catalogue.sql"),
1443 include_str!("../migrations/0049_superseded_proposals.sql"),
1444 include_str!("../migrations/0050_ledger_usage_by_time.sql"),
1445 include_str!("../migrations/0051_platform_guardrails.sql"),
1446 include_str!("../migrations/0052_cloudflare_cycle.sql"),
1447 include_str!("../migrations/0053_models_at_cost.sql"),
1448 include_str!("../migrations/0054_agent_attribution.sql"),
1449 include_str!("../migrations/0055_app_containers.sql"),
1450 ];
1451
1452 /// The columns of `table` after the migrations: each with whether an
1453 /// insert must give it (NOT NULL, no default).
1454 fn columns(table: &str) -> Vec<(String, bool)> {
1455 let mut out = vec![];
1456 for sql in MIGRATIONS {
1457 let sql: String = sql.lines().map(|l| l.split("--").next().unwrap_or("")).collect::<Vec<_>>().join("\n");
1458 if let Some(start) = sql.find(&format!("CREATE TABLE {table} (")) {
1459 let body = &sql[start + format!("CREATE TABLE {table} (").len()..];
1460 let body = &body[..body.find(");").unwrap()];
1461 for column in body.split(",\n") {
1462 let column = column.trim();
1463 let name = column.split_whitespace().next().unwrap_or("").to_owned();
1464 if name.is_empty() || name == "PRIMARY" {
1465 continue;
1466 }
1467 let required = column.contains("NOT NULL") && !column.contains("DEFAULT") && !column.contains("PRIMARY KEY");
1468 out.push((name, required || column.contains("PRIMARY KEY")));
1469 }
1470 }
1471 for statement in sql.split(';') {
1472 let s = statement.trim();
1473 if let Some(rest) = s.strip_prefix(&format!("ALTER TABLE {table} ADD COLUMN ")) {
1474 let name = rest.split_whitespace().next().unwrap().to_owned();
1475 out.push((name, rest.contains("NOT NULL") && !rest.contains("DEFAULT")));
1476 }
1477 }
1478 }
1479 out
1480 }
1481
1482 /// The columns an `INSERT [OR IGNORE] INTO table (…)` names.
1483 fn inserted(sql: &str, table: &str) -> Vec<String> {
1484 let start = sql.find(&format!("INTO {table} (")).unwrap() + format!("INTO {table} (").len();
1485 sql[start..start + sql[start..].find(')').unwrap()].split(',').map(|c| c.trim().to_owned()).collect()
1486 }
1487
1488
1489 #[test]
1490 fn every_checkout_insert_fills_the_table() {
1491 // The plan's, the activation's, a card check's and a prepayment's
1492 // pages all go through one insert, and it names every
1493 // column the table needs and none it lacks: a missing NOT NULL
1494 // column, or one from a migration not yet applied, was a 500.
1495 let table = columns("checkouts");
1496 let names: Vec<&str> = table.iter().map(|(n, _)| n.as_str()).collect();
1497 assert_eq!(names, ["id", "workspace", "amount_cents", "created_by", "status", "created_at", "feature", "fee_cents"]);
1498 let given = inserted(CHECKOUT_INSERT, "checkouts");
1499 for column in &given {
1500 assert!(names.contains(&column.as_str()), "the insert names {column}, which checkouts does not have");
1501 }
1502 for (column, required) in &table {
1503 assert!(!required || given.contains(column), "checkouts needs {column}, which the insert leaves out");
1504 }
1505 // As many values as columns.
1506 let values = CHECKOUT_INSERT.split("VALUES").nth(1).unwrap();
1507 assert_eq!(values.matches('?').count() + values.matches("'open'").count(), given.len());
1508 // And no page writes a row of its own any more.
1509 for (file, source) in [
1510 ("lib.rs", include_str!("lib.rs")),
1511 ("features.rs", include_str!("features.rs")),
1512 ("cards.rs", include_str!("cards.rs")),
1513 ] {
1514 let inserts = source.matches(concat!("INTO ", "checkouts (")).count();
1515 assert!(inserts <= usize::from(file == "lib.rs"), "{file} inserts into checkouts on its own");
1516 }
1517 // The same page handed out twice (Stripe's idempotency window) is
1518 // one row, never a failed insert.
1519 assert!(CHECKOUT_INSERT.starts_with("INSERT OR IGNORE INTO checkouts"));
1520 }
1521
1522 #[test]
1523 fn a_payment_credited_alongside_is_never_credited_twice() {
1524 assert!(is_duplicate(&worker::Error::RustError("D1_ERROR: UNIQUE constraint failed: ledger.reference: SQLITE_CONSTRAINT".into())));
1525 assert!(!is_duplicate(&worker::Error::RustError("D1_ERROR: network".into())));
1526 // The reference every credit is entered under is unique.
1527 assert!(include_str!("../migrations/0001_init.sql").contains("reference TEXT NOT NULL UNIQUE"));
1528 }
1529
1530 #[test]
1531 fn a_new_ledger_column_has_a_default() {
1532 // The ledger is written from a dozen places; a column without a
1533 // default would break every one that does not name it.
1534 for (column, required) in columns("ledger") {
1535 assert!(!required || ["id", "workspace", "kind", "amount_micros", "description", "reference", "created_at"].contains(&column.as_str()), "{column}");
1536 }
1537 }
1538}