-
Notifications
You must be signed in to change notification settings - Fork 501
Payment transactions #990
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
Payment transactions #990
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b604a3a
transactions
BilalG1 3445951
small transaction fixes
BilalG1 aae139c
transaction table update
BilalG1 db16ac2
Merge branch 'dev' into payment-transactions
BilalG1 bb92c8e
remove log
BilalG1 5b1a8c5
fix lint err
BilalG1 ad79275
Merge dev into payment-transactions
N2D4 323e40a
wip
BilalG1 6a36d07
fix tests
BilalG1 47141ce
Merge remote-tracking branch 'origin/dev' into payment-transactions
BilalG1 b30fa8c
fix tests
BilalG1 ba0eaa2
rm extra test
BilalG1 42e3d83
rm unused
BilalG1 bfa5685
remove unnecessary
BilalG1 bd9d850
remove unneeded
BilalG1 dc4b53b
Merge dev into payment-transactions
N2D4 11ddac1
Merge dev into payment-transactions
N2D4 ec0c18e
Merge dev into payment-transactions
N2D4 6fce497
Merge remote-tracking branch 'origin/dev' into payment-transactions
BilalG1 6914e5a
Merge dev into payment-transactions
N2D4 420a508
Merge dev into payment-transactions
N2D4 9d6d035
Merge dev into payment-transactions
N2D4 b3e2670
Merge dev into payment-transactions
N2D4 740b941
add more transaction tests
BilalG1 4693a82
Merge branch 'dev' into payment-transactions
BilalG1 d1953ce
Merge dev into payment-transactions
N2D4 e77f688
Merge dev into payment-transactions
N2D4 c914d11
Merge dev into payment-transactions
N2D4 19dfcff
subscription renewal transactions (#1005)
BilalG1 31a924f
webhook fix
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
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
apps/backend/prisma/migrations/20251107182739_subscription_invoice/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,19 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "SubscriptionInvoice" ( | ||
| "id" UUID NOT NULL, | ||
| "tenancyId" UUID NOT NULL, | ||
| "stripeSubscriptionId" TEXT NOT NULL, | ||
| "stripeInvoiceId" TEXT NOT NULL, | ||
| "isSubscriptionCreationInvoice" BOOLEAN NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "SubscriptionInvoice_pkey" PRIMARY KEY ("tenancyId","id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "SubscriptionInvoice_tenancyId_stripeInvoiceId_key" ON "SubscriptionInvoice"("tenancyId", "stripeInvoiceId"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "SubscriptionInvoice" ADD CONSTRAINT "SubscriptionInvoice_tenancyId_stripeSubscriptionId_fkey" FOREIGN KEY ("tenancyId", "stripeSubscriptionId") REFERENCES "Subscription"("tenancyId", "stripeSubscriptionId") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
|
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, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.