diff options
| author | Frank <[email protected]> | 2025-09-23 17:58:26 -0400 |
|---|---|---|
| committer | Frank <[email protected]> | 2025-09-23 17:58:27 -0400 |
| commit | edd6198999e956f9cccde0d67b8719b6b68188e3 (patch) | |
| tree | 36073c8a2efca29381f5be6c4d58e6e5f271e5a5 /packages/console/app/src/routes | |
| parent | c3b2c27997261b07db8efb3e0a4819dccb90a8b8 (diff) | |
| download | opencode-edd6198999e956f9cccde0d67b8719b6b68188e3.tar.gz opencode-edd6198999e956f9cccde0d67b8719b6b68188e3.zip | |
zen: refund
Diffstat (limited to 'packages/console/app/src/routes')
| -rw-r--r-- | packages/console/app/src/routes/stripe/webhook.ts | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/packages/console/app/src/routes/stripe/webhook.ts b/packages/console/app/src/routes/stripe/webhook.ts index 920966286..009a525c7 100644 --- a/packages/console/app/src/routes/stripe/webhook.ts +++ b/packages/console/app/src/routes/stripe/webhook.ts @@ -1,6 +1,6 @@ import { Billing } from "@opencode/console-core/billing.js" import type { APIEvent } from "@solidjs/start/server" -import { Database, eq, sql } from "@opencode/console-core/drizzle/index.js" +import { and, Database, eq, sql } from "@opencode/console-core/drizzle/index.js" import { BillingTable, PaymentTable } from "@opencode/console-core/schema/billing.sql.js" import { Identifier } from "@opencode/console-core/identifier.js" import { centsToMicroCents } from "@opencode/console-core/util/price.js" @@ -91,6 +91,39 @@ export async function POST(input: APIEvent) { }) }) } + if (body.type === "charge.refunded") { + const customerID = body.data.object.customer as string + const paymentIntentID = body.data.object.payment_intent as string + if (!customerID) throw new Error("Customer ID not found") + if (!paymentIntentID) throw new Error("Payment ID not found") + + const workspaceID = await Database.use((tx) => + tx + .select({ + workspaceID: BillingTable.workspaceID, + }) + .from(BillingTable) + .where(eq(BillingTable.customerID, customerID)) + .then((rows) => rows[0]?.workspaceID), + ) + if (!workspaceID) throw new Error("Workspace ID not found") + + await Database.transaction(async (tx) => { + await tx + .update(PaymentTable) + .set({ + timeRefunded: new Date(body.created * 1000), + }) + .where(and(eq(PaymentTable.paymentID, paymentIntentID), eq(PaymentTable.workspaceID, workspaceID))) + + await tx + .update(BillingTable) + .set({ + balance: sql`${BillingTable.balance} - ${centsToMicroCents(Billing.CHARGE_AMOUNT)}`, + }) + .where(eq(BillingTable.workspaceID, workspaceID)) + }) + } console.log("finished handling") |
