billing.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. import { Stripe } from "stripe"
  2. import { Database, eq, sql } from "./drizzle"
  3. import { BillingTable, LiteTable, PaymentTable, SubscriptionTable, UsageTable } from "./schema/billing.sql"
  4. import { Actor } from "./actor"
  5. import { fn } from "./util/fn"
  6. import { z } from "zod"
  7. import { Resource } from "@opencode-ai/console-resource"
  8. import { Identifier } from "./identifier"
  9. import { centsToMicroCents } from "./util/price"
  10. import { User } from "./user"
  11. import { BlackData } from "./black"
  12. import { LiteData } from "./lite"
  13. export namespace Billing {
  14. export const ITEM_CREDIT_NAME = "opencode credits"
  15. export const ITEM_FEE_NAME = "processing fee"
  16. export const RELOAD_AMOUNT = 20
  17. export const RELOAD_AMOUNT_MIN = 10
  18. export const RELOAD_TRIGGER = 5
  19. export const RELOAD_TRIGGER_MIN = 5
  20. export const stripe = () =>
  21. new Stripe(Resource.STRIPE_SECRET_KEY.value, {
  22. apiVersion: "2025-03-31.basil",
  23. httpClient: Stripe.createFetchHttpClient(),
  24. })
  25. export const get = async () => {
  26. return Database.use(async (tx) =>
  27. tx
  28. .select()
  29. .from(BillingTable)
  30. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  31. .then((r) => r[0]),
  32. )
  33. }
  34. export const payments = async () => {
  35. return await Database.use((tx) =>
  36. tx
  37. .select()
  38. .from(PaymentTable)
  39. .where(eq(PaymentTable.workspaceID, Actor.workspace()))
  40. .orderBy(sql`${PaymentTable.timeCreated} DESC`)
  41. .limit(100),
  42. )
  43. }
  44. export const usages = async (page = 0, pageSize = 50) => {
  45. return await Database.use((tx) =>
  46. tx
  47. .select()
  48. .from(UsageTable)
  49. .where(eq(UsageTable.workspaceID, Actor.workspace()))
  50. .orderBy(sql`${UsageTable.timeCreated} DESC`)
  51. .limit(pageSize)
  52. .offset(page * pageSize),
  53. )
  54. }
  55. export const calculateFeeInCents = (x: number) => {
  56. // math: x = total - (total * 0.044 + 0.30)
  57. // math: x = total * (1-0.044) - 0.30
  58. // math: (x + 0.30) / 0.956 = total
  59. return Math.round(((x + 30) / 0.956) * 0.044 + 30)
  60. }
  61. export const reload = async () => {
  62. const billing = await Database.use((tx) =>
  63. tx
  64. .select({
  65. customerID: BillingTable.customerID,
  66. paymentMethodID: BillingTable.paymentMethodID,
  67. reloadAmount: BillingTable.reloadAmount,
  68. })
  69. .from(BillingTable)
  70. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  71. .then((rows) => rows[0]),
  72. )
  73. const customerID = billing.customerID
  74. const paymentMethodID = billing.paymentMethodID
  75. const amountInCents = (billing.reloadAmount ?? Billing.RELOAD_AMOUNT) * 100
  76. try {
  77. const draft = await Billing.stripe().invoices.create({
  78. customer: customerID!,
  79. auto_advance: false,
  80. default_payment_method: paymentMethodID!,
  81. collection_method: "charge_automatically",
  82. currency: "usd",
  83. metadata: {
  84. workspaceID: Actor.workspace(),
  85. amount: amountInCents.toString(),
  86. },
  87. })
  88. await Billing.stripe().invoiceItems.create({
  89. amount: amountInCents,
  90. currency: "usd",
  91. customer: customerID!,
  92. invoice: draft.id!,
  93. description: ITEM_CREDIT_NAME,
  94. })
  95. await Billing.stripe().invoiceItems.create({
  96. amount: calculateFeeInCents(amountInCents),
  97. currency: "usd",
  98. customer: customerID!,
  99. invoice: draft.id!,
  100. description: ITEM_FEE_NAME,
  101. })
  102. await Billing.stripe().invoices.finalizeInvoice(draft.id!)
  103. await Billing.stripe().invoices.pay(draft.id!, {
  104. off_session: true,
  105. payment_method: paymentMethodID!,
  106. })
  107. } catch (e: any) {
  108. console.error(e)
  109. await Database.use((tx) =>
  110. tx
  111. .update(BillingTable)
  112. .set({
  113. reload: false,
  114. reloadError: e.message ?? "Payment failed.",
  115. timeReloadError: sql`now()`,
  116. })
  117. .where(eq(BillingTable.workspaceID, Actor.workspace())),
  118. )
  119. return
  120. }
  121. }
  122. export const grantCredit = async (workspaceID: string, dollarAmount: number) => {
  123. const amountInMicroCents = centsToMicroCents(dollarAmount * 100)
  124. await Database.transaction(async (tx) => {
  125. await tx
  126. .update(BillingTable)
  127. .set({
  128. balance: sql`${BillingTable.balance} + ${amountInMicroCents}`,
  129. })
  130. .where(eq(BillingTable.workspaceID, workspaceID))
  131. await tx.insert(PaymentTable).values({
  132. workspaceID,
  133. id: Identifier.create("payment"),
  134. amount: amountInMicroCents,
  135. enrichment: {
  136. type: "credit",
  137. },
  138. })
  139. })
  140. return amountInMicroCents
  141. }
  142. export const setMonthlyLimit = fn(z.number(), async (input) => {
  143. return await Database.use((tx) =>
  144. tx
  145. .update(BillingTable)
  146. .set({
  147. monthlyLimit: input,
  148. })
  149. .where(eq(BillingTable.workspaceID, Actor.workspace())),
  150. )
  151. })
  152. export const generateCheckoutUrl = fn(
  153. z.object({
  154. successUrl: z.string(),
  155. cancelUrl: z.string(),
  156. amount: z.number().optional(),
  157. }),
  158. async (input) => {
  159. const user = Actor.assert("user")
  160. const { successUrl, cancelUrl, amount } = input
  161. if (amount !== undefined && amount < Billing.RELOAD_AMOUNT_MIN) {
  162. throw new Error(`Amount must be at least $${Billing.RELOAD_AMOUNT_MIN}`)
  163. }
  164. const email = await User.getAuthEmail(user.properties.userID)
  165. const customer = await Billing.get()
  166. const amountInCents = (amount ?? customer.reloadAmount ?? Billing.RELOAD_AMOUNT) * 100
  167. const session = await Billing.stripe().checkout.sessions.create({
  168. mode: "payment",
  169. billing_address_collection: "required",
  170. line_items: [
  171. {
  172. price_data: {
  173. currency: "usd",
  174. product_data: { name: ITEM_CREDIT_NAME },
  175. unit_amount: amountInCents,
  176. },
  177. quantity: 1,
  178. },
  179. {
  180. price_data: {
  181. currency: "usd",
  182. product_data: { name: ITEM_FEE_NAME },
  183. unit_amount: calculateFeeInCents(amountInCents),
  184. },
  185. quantity: 1,
  186. },
  187. ],
  188. ...(customer.customerID
  189. ? {
  190. customer: customer.customerID,
  191. customer_update: {
  192. name: "auto",
  193. address: "auto",
  194. },
  195. }
  196. : {
  197. customer_email: email!,
  198. customer_creation: "always",
  199. }),
  200. currency: "usd",
  201. invoice_creation: {
  202. enabled: true,
  203. },
  204. payment_method_options: {
  205. card: {
  206. setup_future_usage: "on_session",
  207. },
  208. },
  209. //payment_method_data: {
  210. // allow_redisplay: "always",
  211. //},
  212. tax_id_collection: {
  213. enabled: true,
  214. },
  215. metadata: {
  216. workspaceID: Actor.workspace(),
  217. amount: amountInCents.toString(),
  218. },
  219. success_url: successUrl,
  220. cancel_url: cancelUrl,
  221. })
  222. return session.url
  223. },
  224. )
  225. export const generateLiteCheckoutUrl = fn(
  226. z.object({
  227. successUrl: z.string(),
  228. cancelUrl: z.string(),
  229. method: z.enum(["alipay", "upi"]).optional(),
  230. }),
  231. async (input) => {
  232. const user = Actor.assert("user")
  233. const { successUrl, cancelUrl, method } = input
  234. const email = await User.getAuthEmail(user.properties.userID)
  235. const billing = await Billing.get()
  236. if (billing.subscriptionID) throw new Error("Already subscribed to Black")
  237. if (billing.liteSubscriptionID) throw new Error("Already subscribed to Lite")
  238. const createSession = () =>
  239. Billing.stripe().checkout.sessions.create({
  240. mode: "subscription",
  241. discounts: [{ coupon: LiteData.firstMonthCoupon(email!) }],
  242. ...(billing.customerID
  243. ? {
  244. customer: billing.customerID,
  245. customer_update: {
  246. name: "auto",
  247. address: "auto",
  248. },
  249. }
  250. : {
  251. customer_email: email!,
  252. }),
  253. ...(() => {
  254. if (method === "alipay") {
  255. return {
  256. line_items: [{ price: LiteData.priceID(), quantity: 1 }],
  257. payment_method_types: ["alipay"],
  258. adaptive_pricing: {
  259. enabled: false,
  260. },
  261. }
  262. }
  263. if (method === "upi") {
  264. return {
  265. line_items: [
  266. {
  267. price_data: {
  268. currency: "inr",
  269. product: LiteData.productID(),
  270. recurring: {
  271. interval: "month",
  272. interval_count: 1,
  273. },
  274. unit_amount: LiteData.priceInr(),
  275. },
  276. quantity: 1,
  277. },
  278. ],
  279. payment_method_types: ["upi"] as any,
  280. adaptive_pricing: {
  281. enabled: false,
  282. },
  283. }
  284. }
  285. return {
  286. line_items: [{ price: LiteData.priceID(), quantity: 1 }],
  287. billing_address_collection: "required",
  288. }
  289. })(),
  290. tax_id_collection: {
  291. enabled: true,
  292. },
  293. success_url: successUrl,
  294. cancel_url: cancelUrl,
  295. subscription_data: {
  296. metadata: {
  297. workspaceID: Actor.workspace(),
  298. userID: user.properties.userID,
  299. type: "lite",
  300. },
  301. },
  302. })
  303. try {
  304. const session = await createSession()
  305. return session.url
  306. } catch (e: any) {
  307. if (
  308. e.type !== "StripeInvalidRequestError" ||
  309. !e.message.includes("You cannot combine currencies on a single customer")
  310. )
  311. throw e
  312. // get pending payment intent
  313. const intents = await Billing.stripe().paymentIntents.search({
  314. query: `-status:'canceled' AND -status:'processing' AND -status:'succeeded' AND customer:'${billing.customerID}'`,
  315. })
  316. if (intents.data.length === 0) throw e
  317. for (const intent of intents.data) {
  318. // get checkout session
  319. const sessions = await Billing.stripe().checkout.sessions.list({
  320. customer: billing.customerID!,
  321. payment_intent: intent.id,
  322. })
  323. // delete pending payment intent
  324. await Billing.stripe().checkout.sessions.expire(sessions.data[0].id)
  325. }
  326. const session = await createSession()
  327. return session.url
  328. }
  329. },
  330. )
  331. export const generateSessionUrl = fn(
  332. z.object({
  333. returnUrl: z.string(),
  334. }),
  335. async (input) => {
  336. const { returnUrl } = input
  337. const customer = await Billing.get()
  338. if (!customer?.customerID) {
  339. throw new Error("No stripe customer ID")
  340. }
  341. const session = await Billing.stripe().billingPortal.sessions.create({
  342. customer: customer.customerID,
  343. return_url: returnUrl,
  344. })
  345. return session.url
  346. },
  347. )
  348. export const generateReceiptUrl = fn(
  349. z.object({
  350. paymentID: z.string(),
  351. }),
  352. async (input) => {
  353. const { paymentID } = input
  354. const intent = await Billing.stripe().paymentIntents.retrieve(paymentID)
  355. if (!intent.latest_charge) throw new Error("No charge found")
  356. const charge = await Billing.stripe().charges.retrieve(intent.latest_charge as string)
  357. if (!charge.receipt_url) throw new Error("No receipt URL found")
  358. return charge.receipt_url
  359. },
  360. )
  361. export const subscribeBlack = fn(
  362. z.object({
  363. seats: z.number(),
  364. coupon: z.string().optional(),
  365. }),
  366. async ({ seats, coupon }) => {
  367. const user = Actor.assert("user")
  368. const billing = await Database.use((tx) =>
  369. tx
  370. .select({
  371. customerID: BillingTable.customerID,
  372. paymentMethodID: BillingTable.paymentMethodID,
  373. subscriptionID: BillingTable.subscriptionID,
  374. subscriptionPlan: BillingTable.subscriptionPlan,
  375. timeSubscriptionSelected: BillingTable.timeSubscriptionSelected,
  376. })
  377. .from(BillingTable)
  378. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  379. .then((rows) => rows[0]),
  380. )
  381. if (!billing) throw new Error("Billing record not found")
  382. if (!billing.timeSubscriptionSelected) throw new Error("Not selected for subscription")
  383. if (billing.subscriptionID) throw new Error("Already subscribed")
  384. if (!billing.customerID) throw new Error("No customer ID")
  385. if (!billing.paymentMethodID) throw new Error("No payment method")
  386. if (!billing.subscriptionPlan) throw new Error("No subscription plan")
  387. const subscription = await Billing.stripe().subscriptions.create({
  388. customer: billing.customerID,
  389. default_payment_method: billing.paymentMethodID,
  390. items: [{ price: BlackData.planToPriceID({ plan: billing.subscriptionPlan }) }],
  391. metadata: {
  392. workspaceID: Actor.workspace(),
  393. },
  394. })
  395. await Database.transaction(async (tx) => {
  396. await tx
  397. .update(BillingTable)
  398. .set({
  399. subscriptionID: subscription.id,
  400. subscription: {
  401. status: "subscribed",
  402. coupon,
  403. seats,
  404. plan: billing.subscriptionPlan!,
  405. },
  406. subscriptionPlan: null,
  407. timeSubscriptionBooked: null,
  408. timeSubscriptionSelected: null,
  409. })
  410. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  411. await tx.insert(SubscriptionTable).values({
  412. workspaceID: Actor.workspace(),
  413. id: Identifier.create("subscription"),
  414. userID: user.properties.userID,
  415. })
  416. })
  417. return subscription.id
  418. },
  419. )
  420. export const unsubscribeBlack = fn(
  421. z.object({
  422. subscriptionID: z.string(),
  423. }),
  424. async ({ subscriptionID }) => {
  425. const workspaceID = await Database.use((tx) =>
  426. tx
  427. .select({ workspaceID: BillingTable.workspaceID })
  428. .from(BillingTable)
  429. .where(eq(BillingTable.subscriptionID, subscriptionID))
  430. .then((rows) => rows[0]?.workspaceID),
  431. )
  432. if (!workspaceID) throw new Error("Workspace ID not found for subscription")
  433. await Database.transaction(async (tx) => {
  434. await tx
  435. .update(BillingTable)
  436. .set({ subscriptionID: null, subscription: null })
  437. .where(eq(BillingTable.workspaceID, workspaceID))
  438. await tx.delete(SubscriptionTable).where(eq(SubscriptionTable.workspaceID, workspaceID))
  439. })
  440. },
  441. )
  442. export const unsubscribeLite = fn(
  443. z.object({
  444. subscriptionID: z.string(),
  445. }),
  446. async ({ subscriptionID }) => {
  447. const workspaceID = await Database.use((tx) =>
  448. tx
  449. .select({ workspaceID: BillingTable.workspaceID })
  450. .from(BillingTable)
  451. .where(eq(BillingTable.liteSubscriptionID, subscriptionID))
  452. .then((rows) => rows[0]?.workspaceID),
  453. )
  454. if (!workspaceID) throw new Error("Workspace ID not found for subscription")
  455. await Database.transaction(async (tx) => {
  456. await tx
  457. .update(BillingTable)
  458. .set({ liteSubscriptionID: null, lite: null })
  459. .where(eq(BillingTable.workspaceID, workspaceID))
  460. await tx.delete(LiteTable).where(eq(LiteTable.workspaceID, workspaceID))
  461. })
  462. },
  463. )
  464. }