diff options
Diffstat (limited to 'packages/console/app')
3 files changed, 41 insertions, 2 deletions
diff --git a/packages/console/app/src/component/workspace/payment-section.module.css b/packages/console/app/src/component/workspace/payment-section.module.css index ea8e2ed42..2e1afe78b 100644 --- a/packages/console/app/src/component/workspace/payment-section.module.css +++ b/packages/console/app/src/component/workspace/payment-section.module.css @@ -40,6 +40,10 @@ &[data-slot="payment-amount"] { color: var(--color-text); + + &[data-refunded="true"] { + text-decoration: line-through; + } } } diff --git a/packages/console/app/src/component/workspace/payment-section.tsx b/packages/console/app/src/component/workspace/payment-section.tsx index 826ec7a55..5cdb0cd78 100644 --- a/packages/console/app/src/component/workspace/payment-section.tsx +++ b/packages/console/app/src/component/workspace/payment-section.tsx @@ -85,7 +85,9 @@ export function PaymentSection() { {formatDateForTable(date)} </td> <td data-slot="payment-id">{payment.id}</td> - <td data-slot="payment-amount">${((payment.amount ?? 0) / 100000000).toFixed(2)}</td> + <td data-slot="payment-amount" data-refunded={!!payment.timeRefunded}> + ${((payment.amount ?? 0) / 100000000).toFixed(2)} + </td> <td data-slot="payment-receipt"> <button onClick={async () => { 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") |
