|
1 | 1 | import { Stripe } from "stripe" |
2 | | -import { Database, eq, sql } from "./drizzle" |
| 2 | +import { and, Database, eq, sql } from "./drizzle" |
3 | 3 | import { BillingTable, PaymentTable, UsageTable } from "./schema/billing.sql" |
4 | 4 | import { Actor } from "./actor" |
5 | 5 | import { fn } from "./util/fn" |
6 | 6 | import { z } from "zod" |
7 | | -import { User } from "./user" |
8 | 7 | import { Resource } from "@opencode/console-resource" |
9 | 8 | import { Identifier } from "./identifier" |
10 | 9 | import { centsToMicroCents } from "./util/price" |
| 10 | +import { UserTable } from "./schema/user.sql" |
| 11 | +import { AccountTable } from "./schema/account.sql" |
11 | 12 |
|
12 | 13 | export namespace Billing { |
13 | 14 | export const CHARGE_NAME = "opencode credits" |
@@ -168,10 +169,19 @@ export namespace Billing { |
168 | 169 | cancelUrl: z.string(), |
169 | 170 | }), |
170 | 171 | async (input) => { |
171 | | - const account = Actor.assert("user") |
| 172 | + const user = Actor.assert("user") |
172 | 173 | const { successUrl, cancelUrl } = input |
173 | 174 |
|
174 | | - const user = await User.fromID(account.properties.userID) |
| 175 | + const email = await Database.use((tx) => |
| 176 | + tx |
| 177 | + .select({ |
| 178 | + email: AccountTable.email, |
| 179 | + }) |
| 180 | + .from(UserTable) |
| 181 | + .innerJoin(AccountTable, eq(UserTable.accountID, AccountTable.id)) |
| 182 | + .where(and(eq(UserTable.id, user.properties.userID), eq(UserTable.workspaceID, Actor.workspace()))) |
| 183 | + .then((rows) => rows[0]?.email), |
| 184 | + ) |
175 | 185 | const customer = await Billing.get() |
176 | 186 | const session = await Billing.stripe().checkout.sessions.create({ |
177 | 187 | mode: "payment", |
@@ -206,7 +216,7 @@ export namespace Billing { |
206 | 216 | }, |
207 | 217 | } |
208 | 218 | : { |
209 | | - customer_email: user.email!, |
| 219 | + customer_email: email, |
210 | 220 | customer_creation: "always", |
211 | 221 | }), |
212 | 222 | currency: "usd", |
|
0 commit comments