billing.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { Stripe } from "stripe"
  2. import { Database, eq, sql } from "./drizzle"
  3. import { BillingTable, 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. export namespace Billing {
  13. export const ITEM_CREDIT_NAME = "opencode credits"
  14. export const ITEM_FEE_NAME = "processing fee"
  15. export const RELOAD_AMOUNT = 20
  16. export const RELOAD_AMOUNT_MIN = 10
  17. export const RELOAD_TRIGGER = 5
  18. export const RELOAD_TRIGGER_MIN = 5
  19. export const stripe = () =>
  20. new Stripe(Resource.STRIPE_SECRET_KEY.value, {
  21. apiVersion: "2025-03-31.basil",
  22. httpClient: Stripe.createFetchHttpClient(),
  23. })
  24. export const get = async () => {
  25. return Database.use(async (tx) =>
  26. tx
  27. .select()
  28. .from(BillingTable)
  29. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  30. .then((r) => r[0]),
  31. )
  32. }
  33. export const payments = async () => {
  34. return await Database.use((tx) =>
  35. tx
  36. .select()
  37. .from(PaymentTable)
  38. .where(eq(PaymentTable.workspaceID, Actor.workspace()))
  39. .orderBy(sql`${PaymentTable.timeCreated} DESC`)
  40. .limit(100),
  41. )
  42. }
  43. export const usages = async (page = 0, pageSize = 50) => {
  44. return await Database.use((tx) =>
  45. tx
  46. .select()
  47. .from(UsageTable)
  48. .where(eq(UsageTable.workspaceID, Actor.workspace()))
  49. .orderBy(sql`${UsageTable.timeCreated} DESC`)
  50. .limit(pageSize)
  51. .offset(page * pageSize),
  52. )
  53. }
  54. export const calculateFeeInCents = (x: number) => {
  55. // math: x = total - (total * 0.044 + 0.30)
  56. // math: x = total * (1-0.044) - 0.30
  57. // math: (x + 0.30) / 0.956 = total
  58. return Math.round(((x + 30) / 0.956) * 0.044 + 30)
  59. }
  60. export const reload = async () => {
  61. const billing = await Database.use((tx) =>
  62. tx
  63. .select({
  64. customerID: BillingTable.customerID,
  65. paymentMethodID: BillingTable.paymentMethodID,
  66. reloadAmount: BillingTable.reloadAmount,
  67. })
  68. .from(BillingTable)
  69. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  70. .then((rows) => rows[0]),
  71. )
  72. const customerID = billing.customerID
  73. const paymentMethodID = billing.paymentMethodID
  74. const amountInCents = (billing.reloadAmount ?? Billing.RELOAD_AMOUNT) * 100
  75. try {
  76. const draft = await Billing.stripe().invoices.create({
  77. customer: customerID!,
  78. auto_advance: false,
  79. default_payment_method: paymentMethodID!,
  80. collection_method: "charge_automatically",
  81. currency: "usd",
  82. metadata: {
  83. workspaceID: Actor.workspace(),
  84. amount: amountInCents.toString(),
  85. },
  86. })
  87. await Billing.stripe().invoiceItems.create({
  88. amount: amountInCents,
  89. currency: "usd",
  90. customer: customerID!,
  91. invoice: draft.id!,
  92. description: ITEM_CREDIT_NAME,
  93. })
  94. await Billing.stripe().invoiceItems.create({
  95. amount: calculateFeeInCents(amountInCents),
  96. currency: "usd",
  97. customer: customerID!,
  98. invoice: draft.id!,
  99. description: ITEM_FEE_NAME,
  100. })
  101. await Billing.stripe().invoices.finalizeInvoice(draft.id!)
  102. await Billing.stripe().invoices.pay(draft.id!, {
  103. off_session: true,
  104. payment_method: paymentMethodID!,
  105. })
  106. } catch (e: any) {
  107. console.error(e)
  108. await Database.use((tx) =>
  109. tx
  110. .update(BillingTable)
  111. .set({
  112. reload: false,
  113. reloadError: e.message ?? "Payment failed.",
  114. timeReloadError: sql`now()`,
  115. })
  116. .where(eq(BillingTable.workspaceID, Actor.workspace())),
  117. )
  118. return
  119. }
  120. }
  121. export const grantCredit = async (workspaceID: string, dollarAmount: number) => {
  122. const amountInMicroCents = centsToMicroCents(dollarAmount * 100)
  123. await Database.transaction(async (tx) => {
  124. await tx
  125. .update(BillingTable)
  126. .set({
  127. balance: sql`${BillingTable.balance} + ${amountInMicroCents}`,
  128. })
  129. .where(eq(BillingTable.workspaceID, workspaceID))
  130. await tx.insert(PaymentTable).values({
  131. workspaceID,
  132. id: Identifier.create("payment"),
  133. amount: amountInMicroCents,
  134. enrichment: {
  135. type: "credit",
  136. },
  137. })
  138. })
  139. return amountInMicroCents
  140. }
  141. export const setMonthlyLimit = fn(z.number(), async (input) => {
  142. return await Database.use((tx) =>
  143. tx
  144. .update(BillingTable)
  145. .set({
  146. monthlyLimit: input,
  147. })
  148. .where(eq(BillingTable.workspaceID, Actor.workspace())),
  149. )
  150. })
  151. export const generateCheckoutUrl = fn(
  152. z.object({
  153. successUrl: z.string(),
  154. cancelUrl: z.string(),
  155. amount: z.number().optional(),
  156. }),
  157. async (input) => {
  158. const user = Actor.assert("user")
  159. const { successUrl, cancelUrl, amount } = input
  160. if (amount !== undefined && amount < Billing.RELOAD_AMOUNT_MIN) {
  161. throw new Error(`Amount must be at least $${Billing.RELOAD_AMOUNT_MIN}`)
  162. }
  163. const email = await User.getAuthEmail(user.properties.userID)
  164. const customer = await Billing.get()
  165. const amountInCents = (amount ?? customer.reloadAmount ?? Billing.RELOAD_AMOUNT) * 100
  166. const session = await Billing.stripe().checkout.sessions.create({
  167. mode: "payment",
  168. billing_address_collection: "required",
  169. line_items: [
  170. {
  171. price_data: {
  172. currency: "usd",
  173. product_data: { name: ITEM_CREDIT_NAME },
  174. unit_amount: amountInCents,
  175. },
  176. quantity: 1,
  177. },
  178. {
  179. price_data: {
  180. currency: "usd",
  181. product_data: { name: ITEM_FEE_NAME },
  182. unit_amount: calculateFeeInCents(amountInCents),
  183. },
  184. quantity: 1,
  185. },
  186. ],
  187. ...(customer.customerID
  188. ? {
  189. customer: customer.customerID,
  190. customer_update: {
  191. name: "auto",
  192. address: "auto",
  193. },
  194. }
  195. : {
  196. customer_email: email!,
  197. customer_creation: "always",
  198. }),
  199. currency: "usd",
  200. invoice_creation: {
  201. enabled: true,
  202. },
  203. payment_intent_data: {
  204. setup_future_usage: "on_session",
  205. },
  206. payment_method_types: ["card"],
  207. payment_method_data: {
  208. allow_redisplay: "always",
  209. },
  210. tax_id_collection: {
  211. enabled: true,
  212. },
  213. metadata: {
  214. workspaceID: Actor.workspace(),
  215. amount: amountInCents.toString(),
  216. },
  217. success_url: successUrl,
  218. cancel_url: cancelUrl,
  219. })
  220. return session.url
  221. },
  222. )
  223. export const generateSessionUrl = fn(
  224. z.object({
  225. returnUrl: z.string(),
  226. }),
  227. async (input) => {
  228. const { returnUrl } = input
  229. const customer = await Billing.get()
  230. if (!customer?.customerID) {
  231. throw new Error("No stripe customer ID")
  232. }
  233. const session = await Billing.stripe().billingPortal.sessions.create({
  234. customer: customer.customerID,
  235. return_url: returnUrl,
  236. })
  237. return session.url
  238. },
  239. )
  240. export const generateReceiptUrl = fn(
  241. z.object({
  242. paymentID: z.string(),
  243. }),
  244. async (input) => {
  245. const { paymentID } = input
  246. const intent = await Billing.stripe().paymentIntents.retrieve(paymentID)
  247. if (!intent.latest_charge) throw new Error("No charge found")
  248. const charge = await Billing.stripe().charges.retrieve(intent.latest_charge as string)
  249. if (!charge.receipt_url) throw new Error("No receipt URL found")
  250. return charge.receipt_url
  251. },
  252. )
  253. export const subscribe = fn(
  254. z.object({
  255. seats: z.number(),
  256. coupon: z.string().optional(),
  257. }),
  258. async ({ seats, coupon }) => {
  259. const user = Actor.assert("user")
  260. const billing = await Database.use((tx) =>
  261. tx
  262. .select({
  263. customerID: BillingTable.customerID,
  264. paymentMethodID: BillingTable.paymentMethodID,
  265. subscriptionID: BillingTable.subscriptionID,
  266. subscriptionPlan: BillingTable.subscriptionPlan,
  267. timeSubscriptionSelected: BillingTable.timeSubscriptionSelected,
  268. })
  269. .from(BillingTable)
  270. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  271. .then((rows) => rows[0]),
  272. )
  273. if (!billing) throw new Error("Billing record not found")
  274. if (!billing.timeSubscriptionSelected) throw new Error("Not selected for subscription")
  275. if (billing.subscriptionID) throw new Error("Already subscribed")
  276. if (!billing.customerID) throw new Error("No customer ID")
  277. if (!billing.paymentMethodID) throw new Error("No payment method")
  278. if (!billing.subscriptionPlan) throw new Error("No subscription plan")
  279. const subscription = await Billing.stripe().subscriptions.create({
  280. customer: billing.customerID,
  281. default_payment_method: billing.paymentMethodID,
  282. items: [{ price: BlackData.planToPriceID({ plan: billing.subscriptionPlan }) }],
  283. metadata: {
  284. workspaceID: Actor.workspace(),
  285. },
  286. })
  287. await Database.transaction(async (tx) => {
  288. await tx
  289. .update(BillingTable)
  290. .set({
  291. subscriptionID: subscription.id,
  292. subscription: {
  293. status: "subscribed",
  294. coupon,
  295. seats,
  296. plan: billing.subscriptionPlan!,
  297. },
  298. subscriptionPlan: null,
  299. timeSubscriptionBooked: null,
  300. timeSubscriptionSelected: null,
  301. })
  302. .where(eq(BillingTable.workspaceID, Actor.workspace()))
  303. await tx.insert(SubscriptionTable).values({
  304. workspaceID: Actor.workspace(),
  305. id: Identifier.create("subscription"),
  306. userID: user.properties.userID,
  307. })
  308. })
  309. return subscription.id
  310. },
  311. )
  312. }