The open source coding agent.
TypeScript
zen: refund
Frankedd6198999e9
diff --git a/infra/console.ts b/infra/console.tsindex 74c478aeea6..bd0af2b1529 100644--- a/infra/console.ts+++ b/infra/console.ts@@ -75,6 +75,7 @@ export const stripeWebhook = new WebhookEndpoint("StripeWebhookEndpoint", {"checkout.session.async_payment_succeeded","checkout.session.completed","checkout.session.expired",+ "charge.refunded","customer.created","customer.deleted","customer.updated",@@ -93,9 +94,6 @@ export const stripeWebhook = new WebhookEndpoint("StripeWebhookEndpoint", {"customer.subscription.resumed","customer.subscription.trial_will_end","customer.subscription.updated",- "customer.tax_id.created",- "customer.tax_id.deleted",- "customer.tax_id.updated",],})diff --git a/packages/console/app/src/component/workspace/payment-section.module.css b/packages/console/app/src/component/workspace/payment-section.module.cssindex ea8e2ed42a1..2e1afe78bcc 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.tsxindex 826ec7a5577..5cdb0cd7872 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"><buttononClick={async () => {diff --git a/packages/console/app/src/routes/stripe/webhook.ts b/packages/console/app/src/routes/stripe/webhook.tsindex 920966286d4..009a525c749 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")diff --git a/packages/console/core/migrations/0016_cold_la_nuit.sql b/packages/console/core/migrations/0016_cold_la_nuit.sqlnew file mode 100644index 00000000000..860beb2e705--- /dev/null+++ b/packages/console/core/migrations/0016_cold_la_nuit.sql@@ -0,0 +1 @@+ALTER TABLE `payment` ADD `time_refunded` timestamp(3);\ No newline at end of filediff --git a/packages/console/core/migrations/meta/0016_snapshot.json b/packages/console/core/migrations/meta/0016_snapshot.jsonnew file mode 100644index 00000000000..fec3c559524--- /dev/null+++ b/packages/console/core/migrations/meta/0016_snapshot.json@@ -0,0 +1,681 @@+{+ "version": "5",+ "dialect": "mysql",+ "id": "45b67fb4-77ce-4aa2-b883-1971429c69f5",+ "prevId": "7f3989cb-3e8b-430e-a0f5-f87051d1d824",+ "tables": {+ "account": {+ "name": "account",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "email": {+ "name": "email",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "email": {+ "name": "email",+ "columns": [+ "email"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "billing": {+ "name": "billing",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "customer_id": {+ "name": "customer_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "payment_method_id": {+ "name": "payment_method_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "payment_method_last4": {+ "name": "payment_method_last4",+ "type": "varchar(4)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "balance": {+ "name": "balance",+ "type": "bigint",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "monthly_limit": {+ "name": "monthly_limit",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "monthly_usage": {+ "name": "monthly_usage",+ "type": "bigint",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_monthly_usage_updated": {+ "name": "time_monthly_usage_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "reload": {+ "name": "reload",+ "type": "boolean",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "reload_error": {+ "name": "reload_error",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_reload_error": {+ "name": "time_reload_error",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_reload_locked_till": {+ "name": "time_reload_locked_till",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "global_customer_id": {+ "name": "global_customer_id",+ "columns": [+ "customer_id"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "billing_workspace_id_id_pk": {+ "name": "billing_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "payment": {+ "name": "payment",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "customer_id": {+ "name": "customer_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "payment_id": {+ "name": "payment_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "amount": {+ "name": "amount",+ "type": "bigint",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_refunded": {+ "name": "time_refunded",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "payment_workspace_id_id_pk": {+ "name": "payment_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "usage": {+ "name": "usage",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "model": {+ "name": "model",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "provider": {+ "name": "provider",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "input_tokens": {+ "name": "input_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "output_tokens": {+ "name": "output_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "reasoning_tokens": {+ "name": "reasoning_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cache_read_tokens": {+ "name": "cache_read_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cache_write_5m_tokens": {+ "name": "cache_write_5m_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cache_write_1h_tokens": {+ "name": "cache_write_1h_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cost": {+ "name": "cost",+ "type": "bigint",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "usage_workspace_id_id_pk": {+ "name": "usage_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "key": {+ "name": "key",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "actor": {+ "name": "actor",+ "type": "json",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "old_name": {+ "name": "old_name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "key": {+ "name": "key",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_used": {+ "name": "time_used",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "global_key": {+ "name": "global_key",+ "columns": [+ "key"+ ],+ "isUnique": true+ },+ "name": {+ "name": "name",+ "columns": [+ "workspace_id",+ "name"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "key_workspace_id_id_pk": {+ "name": "key_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "user": {+ "name": "user",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "email": {+ "name": "email",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_seen": {+ "name": "time_seen",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "color": {+ "name": "color",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "user_email": {+ "name": "user_email",+ "columns": [+ "workspace_id",+ "email"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "user_workspace_id_id_pk": {+ "name": "user_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "workspace": {+ "name": "workspace",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "slug": {+ "name": "slug",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "slug": {+ "name": "slug",+ "columns": [+ "slug"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "workspace_id": {+ "name": "workspace_id",+ "columns": [+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ }+ },+ "views": {},+ "_meta": {+ "schemas": {},+ "tables": {},+ "columns": {}+ },+ "internal": {+ "tables": {},+ "indexes": {}+ }+}\ No newline at end of filediff --git a/packages/console/core/migrations/meta/_journal.json b/packages/console/core/migrations/meta/_journal.jsonindex 659d3c7fc76..390b8640eb1 100644--- a/packages/console/core/migrations/meta/_journal.json+++ b/packages/console/core/migrations/meta/_journal.json@@ -113,6 +113,13 @@"when": 1758428484500,"tag": "0015_cloudy_revanche","breakpoints": true+ },+ {+ "idx": 16,+ "version": "5",+ "when": 1758663086739,+ "tag": "0016_cold_la_nuit",+ "breakpoints": true}]-}+}\ No newline at end of filediff --git a/packages/console/core/src/schema/billing.sql.ts b/packages/console/core/src/schema/billing.sql.tsindex 5bec4e90095..f7da84ab7d9 100644--- a/packages/console/core/src/schema/billing.sql.ts+++ b/packages/console/core/src/schema/billing.sql.ts@@ -30,6 +30,7 @@ export const PaymentTable = mysqlTable(customerID: varchar("customer_id", { length: 255 }),paymentID: varchar("payment_id", { length: 255 }),amount: bigint("amount", { mode: "number" }).notNull(),+ timeRefunded: utc("time_refunded"),},(table) => [...workspaceIndexes(table)],)