-
Notifications
You must be signed in to change notification settings - Fork 633
feat: cancel subscriptions at the end of the period #2944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { insertAuditLogs } from "@/lib/audit"; | ||
| import { type Quotas, db, eq, schema } from "@/lib/db"; | ||
| import { stripeEnv } from "@/lib/env"; | ||
| import Stripe from "stripe"; | ||
|
|
||
| export const runtime = "nodejs"; | ||
|
|
||
| export const POST = async (req: Request): Promise<Response> => { | ||
| const signature = req.headers.get("stripe-signature"); | ||
| if (!signature) { | ||
| throw new Error("Signature missing"); | ||
| } | ||
|
|
||
| const e = stripeEnv(); | ||
|
|
||
| if (!e) { | ||
| throw new Error("stripe env variables are not set up"); | ||
| } | ||
|
|
||
| const stripe = new Stripe(stripeEnv()!.STRIPE_SECRET_KEY, { | ||
| apiVersion: "2023-10-16", | ||
| typescript: true, | ||
| }); | ||
|
|
||
| const event = stripe.webhooks.constructEvent( | ||
| await req.text(), | ||
| signature, | ||
| e.STRIPE_WEBHOOK_SECRET, | ||
| ); | ||
|
|
||
| switch (event.type) { | ||
| case "customer.subscription.deleted": { | ||
| const sub = event.data.object as Stripe.Subscription; | ||
|
|
||
| const ws = await db.query.workspaces.findFirst({ | ||
| where: (table, { and, eq, isNull }) => | ||
| and(eq(table.stripeSubscriptionId, sub.id), isNull(table.deletedAtM)), | ||
| with: { | ||
| auditLogBuckets: { | ||
| where: (table, { eq }) => eq(table.name, "unkey_mutations"), | ||
| }, | ||
| }, | ||
| }); | ||
| if (!ws) { | ||
| throw new Error("workspace does not exist"); | ||
| } | ||
| await db | ||
| .update(schema.workspaces) | ||
| .set({ | ||
| stripeSubscriptionId: null, | ||
| }) | ||
| .where(eq(schema.workspaces.id, ws.id)); | ||
|
|
||
| const freeTierQuotas: Omit<Quotas, "workspaceId"> = { | ||
| requestsPerMonth: 150_000, | ||
| logsRetentionDays: 7, | ||
| auditLogsRetentionDays: 30, | ||
| team: false, | ||
| }; | ||
| await db | ||
| .insert(schema.quotas) | ||
| .values({ | ||
| workspaceId: ws.id, | ||
| ...freeTierQuotas, | ||
| }) | ||
| .onDuplicateKeyUpdate({ | ||
| set: freeTierQuotas, | ||
| }); | ||
|
|
||
| await insertAuditLogs(db, ws.auditLogBuckets[0].id, { | ||
| workspaceId: ws.id, | ||
| actor: { | ||
| type: "system", | ||
| id: "stripe", | ||
| }, | ||
| event: "workspace.update", | ||
| description: "Cancelled subscription.", | ||
| resources: [], | ||
| context: { | ||
| location: "", | ||
| userAgent: undefined, | ||
| }, | ||
| }); | ||
| break; | ||
| } | ||
|
|
||
| default: | ||
| console.error("Incoming stripe event, that should not be received", event.type); | ||
| break; | ||
| } | ||
| return new Response("OK"); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
apps/dashboard/lib/trpc/routers/stripe/uncancelSubscription.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { stripeEnv } from "@/lib/env"; | ||
| import { TRPCError } from "@trpc/server"; | ||
| import Stripe from "stripe"; | ||
| import { auth, t } from "../../trpc"; | ||
| export const uncancelSubscription = t.procedure.use(auth).mutation(async ({ ctx }) => { | ||
| const e = stripeEnv(); | ||
| if (!e) { | ||
| throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: "Stripe is not set up" }); | ||
| } | ||
|
|
||
| const stripe = new Stripe(e.STRIPE_SECRET_KEY, { | ||
| apiVersion: "2023-10-16", | ||
| typescript: true, | ||
| }); | ||
|
|
||
| if (!ctx.workspace.stripeCustomerId) { | ||
| throw new TRPCError({ | ||
| code: "PRECONDITION_FAILED", | ||
| message: "Workspace doesn't have a stripe customer id.", | ||
| }); | ||
| } | ||
| if (!ctx.workspace.stripeSubscriptionId) { | ||
| throw new TRPCError({ | ||
| code: "PRECONDITION_FAILED", | ||
| message: "Workspace doesn't have a stripe subscrption id.", | ||
| }); | ||
| } | ||
|
|
||
| await stripe.subscriptions.update(ctx.workspace.stripeSubscriptionId, { | ||
| cancel_at_period_end: false, | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.