-
Notifications
You must be signed in to change notification settings - Fork 501
subscription and otp refunds #1006
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
BilalG1
merged 11 commits into
payment-subscription-renewal-transactions
from
transaction-refunds
Nov 18, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e88365
subscription and otp refunds
BilalG1 e9aebbd
remove unnecessary transaction types
BilalG1 5a7b49d
lint and type fixes
BilalG1 6918507
Merge remote-tracking branch 'origin/payment-subscription-renewal-tra…
BilalG1 156b5e0
wip transactions
BilalG1 5e1b6d6
Merge branch 'payment-subscription-renewal-transactions' into transac…
BilalG1 6618268
Merge branch 'transaction-refunds' of https://github.com/stack-auth/s…
BilalG1 e96b769
fix lint
BilalG1 34f1653
fix refund tests
BilalG1 f452275
disable test mode refunds on transaction table
BilalG1 ed0ebe8
show transaction refunds in table (#1013)
BilalG1 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
apps/backend/prisma/migrations/20251107210602_one_time_payment_refunds/migration.sql
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,3 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "OneTimePurchase" ADD COLUMN "refundedAt" TIMESTAMP(3); | ||
|
|
3 changes: 3 additions & 0 deletions
3
apps/backend/prisma/migrations/20251112215249_subscription_refunds/migration.sql
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,3 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "Subscription" ADD COLUMN "refundedAt" TIMESTAMP(3); | ||
|
|
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
123 changes: 123 additions & 0 deletions
123
apps/backend/src/app/api/latest/internal/payments/transactions/refund/route.tsx
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,123 @@ | ||
| import { getStripeForAccount } from "@/lib/stripe"; | ||
| import { getPrismaClientForTenancy } from "@/prisma-client"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { KnownErrors } from "@stackframe/stack-shared/dist/known-errors"; | ||
| import { adaptSchema, adminAuthTypeSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors"; | ||
| import { SubscriptionStatus } from "@prisma/client"; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adminAuthTypeSchema.defined(), | ||
| project: adaptSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }).defined(), | ||
| body: yupObject({ | ||
| type: yupString().oneOf(["subscription", "one-time-purchase"]).defined(), | ||
| id: yupString().defined(), | ||
| }).defined() | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| success: yupBoolean().defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth, body }) => { | ||
| const prisma = await getPrismaClientForTenancy(auth.tenancy); | ||
| if (body.type === "subscription") { | ||
| const subscription = await prisma.subscription.findUnique({ | ||
| where: { tenancyId_id: { tenancyId: auth.tenancy.id, id: body.id } }, | ||
| select: { refundedAt: true }, | ||
| }); | ||
| if (!subscription) { | ||
| throw new KnownErrors.SubscriptionInvoiceNotFound(body.id); | ||
| } | ||
| if (subscription.refundedAt) { | ||
| throw new KnownErrors.SubscriptionAlreadyRefunded(body.id); | ||
| } | ||
| const subscriptionInvoices = await prisma.subscriptionInvoice.findMany({ | ||
| where: { | ||
| tenancyId: auth.tenancy.id, | ||
| isSubscriptionCreationInvoice: true, | ||
| subscription: { | ||
| tenancyId: auth.tenancy.id, | ||
| id: body.id, | ||
| } | ||
| } | ||
| }); | ||
| if (subscriptionInvoices.length === 0) { | ||
| throw new KnownErrors.SubscriptionInvoiceNotFound(body.id); | ||
| } | ||
| if (subscriptionInvoices.length > 1) { | ||
| throw new StackAssertionError("Multiple subscription creation invoices found for subscription", { subscriptionId: body.id }); | ||
| } | ||
| const subscriptionInvoice = subscriptionInvoices[0]; | ||
| const stripe = await getStripeForAccount({ tenancy: auth.tenancy }); | ||
| const invoice = await stripe.invoices.retrieve(subscriptionInvoice.stripeInvoiceId, { expand: ["payments"] }); | ||
| const payments = invoice.payments?.data; | ||
| if (!payments || payments.length === 0) { | ||
| throw new StackAssertionError("Invoice has no payments", { invoiceId: subscriptionInvoice.stripeInvoiceId }); | ||
| } | ||
| const paidPayment = payments.find((payment) => payment.status === "paid"); | ||
| if (!paidPayment) { | ||
| throw new StackAssertionError("Invoice has no paid payment", { invoiceId: subscriptionInvoice.stripeInvoiceId }); | ||
| } | ||
| const paymentIntentId = paidPayment.payment.payment_intent; | ||
| if (!paymentIntentId || typeof paymentIntentId !== "string") { | ||
| throw new StackAssertionError("Payment has no payment intent", { invoiceId: subscriptionInvoice.stripeInvoiceId }); | ||
| } | ||
| await stripe.refunds.create({ payment_intent: paymentIntentId }); | ||
| await prisma.subscription.update({ | ||
| where: { tenancyId_id: { tenancyId: auth.tenancy.id, id: body.id } }, | ||
| data: { | ||
| status: SubscriptionStatus.canceled, | ||
| cancelAtPeriodEnd: true, | ||
| currentPeriodEnd: new Date(), | ||
| refundedAt: new Date(), | ||
| }, | ||
| }); | ||
| } else { | ||
| const purchase = await prisma.oneTimePurchase.findUnique({ | ||
| where: { tenancyId_id: { tenancyId: auth.tenancy.id, id: body.id } }, | ||
| }); | ||
| if (!purchase) { | ||
| throw new KnownErrors.OneTimePurchaseNotFound(body.id); | ||
| } | ||
| if (purchase.refundedAt) { | ||
| throw new KnownErrors.OneTimePurchaseAlreadyRefunded(body.id); | ||
| } | ||
| if (purchase.creationSource === "TEST_MODE") { | ||
| throw new KnownErrors.TestModePurchaseNonRefundable(); | ||
| } | ||
| const stripe = await getStripeForAccount({ tenancy: auth.tenancy }); | ||
| if (!purchase.stripePaymentIntentId) { | ||
| throw new KnownErrors.OneTimePurchaseNotFound(body.id); | ||
| } | ||
| await stripe.refunds.create({ | ||
| payment_intent: purchase.stripePaymentIntentId, | ||
| metadata: { | ||
| tenancyId: auth.tenancy.id, | ||
| purchaseId: purchase.id, | ||
| }, | ||
| }); | ||
| await prisma.oneTimePurchase.update({ | ||
| where: { tenancyId_id: { tenancyId: auth.tenancy.id, id: body.id } }, | ||
| data: { refundedAt: new Date() }, | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| success: true, | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Empty
refund.createdwebhook handler has no implementationPrompt To Fix With AI