-
Notifications
You must be signed in to change notification settings - Fork 501
payouts tab #1065
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
payouts tab #1065
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9272bc2
payouts tab
BilalG1 e8aa3f7
Merge remote-tracking branch 'origin/dev' into payouts-tab
BilalG1 a3fd753
Merge branch 'dev' into payouts-tab
BilalG1 5545238
empty
BilalG1 1bbb395
cancel subscription endpoint (#1067)
BilalG1 0c63a62
Merge branch 'dev' into payouts-tab
BilalG1 6dde5e4
Merge branch 'dev' into payouts-tab
BilalG1 bcf0dfe
Merge branch 'dev' into payouts-tab
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
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
138 changes: 138 additions & 0 deletions
138
.../src/app/api/latest/payments/products/[customer_type]/[customer_id]/[product_id]/route.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,138 @@ | ||
| import { ensureProductIdOrInlineProduct, getOwnedProductsForCustomer } from "@/lib/payments"; | ||
| import { getPrismaClientForTenancy } from "@/prisma-client"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, clientOrHigherAuthTypeSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { KnownErrors } from "@stackframe/stack-shared"; | ||
| import { StackAssertionError, StatusError, captureError, throwErr } from "@stackframe/stack-shared/dist/utils/errors"; | ||
| import { SubscriptionStatus } from "@/generated/prisma/client"; | ||
| import { getStripeForAccount } from "@/lib/stripe"; | ||
| import { typedToUppercase } from "@stackframe/stack-shared/dist/utils/strings"; | ||
| import { ensureUserTeamPermissionExists } from "@/lib/request-checks"; | ||
|
|
||
| export const DELETE = createSmartRouteHandler({ | ||
| metadata: { | ||
| summary: "Cancel a customer's subscription product", | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: clientOrHigherAuthTypeSchema.defined(), | ||
| project: adaptSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }).defined(), | ||
| params: yupObject({ | ||
| customer_type: yupString().oneOf(["user", "team", "custom"]).defined(), | ||
| customer_id: yupString().defined(), | ||
| product_id: yupString().defined(), | ||
| }).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| success: yupBoolean().oneOf([true]).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth, params }, fullReq) => { | ||
| if (auth.type === "client") { | ||
| const currentUser = fullReq.auth?.user; | ||
| if (!currentUser) { | ||
| throw new KnownErrors.UserAuthenticationRequired(); | ||
| } | ||
| if (params.customer_type === "user") { | ||
| if (params.customer_id !== currentUser.id) { | ||
| throw new StatusError(StatusError.Forbidden, "Clients can only cancel their own subscriptions."); | ||
| } | ||
| } else if (params.customer_type === "team") { | ||
| const prisma = await getPrismaClientForTenancy(auth.tenancy); | ||
| await ensureUserTeamPermissionExists(prisma, { | ||
| tenancy: auth.tenancy, | ||
| teamId: params.customer_id, | ||
| userId: currentUser.id, | ||
| permissionId: "team_admin", | ||
| errorType: "required", | ||
| recursive: true, | ||
| }); | ||
| } else { | ||
| throw new StatusError(StatusError.Forbidden, "Clients can only cancel user or team subscriptions they control."); | ||
| } | ||
| } | ||
|
|
||
| const prisma = await getPrismaClientForTenancy(auth.tenancy); | ||
| const product = await ensureProductIdOrInlineProduct(auth.tenancy, auth.type, params.product_id, undefined); | ||
| if (params.customer_type !== product.customerType) { | ||
| throw new KnownErrors.ProductCustomerTypeDoesNotMatch( | ||
| params.product_id, | ||
| params.customer_id, | ||
| product.customerType, | ||
| params.customer_type, | ||
| ); | ||
| } | ||
|
|
||
| const ownedProducts = await getOwnedProductsForCustomer({ | ||
| prisma, | ||
| tenancy: auth.tenancy, | ||
| customerType: params.customer_type, | ||
| customerId: params.customer_id, | ||
| }); | ||
| const ownedProductsForProduct = ownedProducts.filter((p) => p.id === params.product_id); | ||
| if (ownedProductsForProduct.length === 0) { | ||
| throw new StatusError(400, "Customer does not have this product."); | ||
| } | ||
| if (ownedProductsForProduct.some((product) => product.type === "one_time")) { | ||
| throw new StatusError(400, "This product is a one time purchase and cannot be canceled."); | ||
| } | ||
|
|
||
| const subscriptions = await prisma.subscription.findMany({ | ||
| where: { | ||
| tenancyId: auth.tenancy.id, | ||
| customerType: typedToUppercase(params.customer_type), | ||
| customerId: params.customer_id, | ||
| productId: params.product_id, | ||
| status: { in: [SubscriptionStatus.active, SubscriptionStatus.trialing] }, | ||
| }, | ||
| }); | ||
| if (subscriptions.length === 0) { | ||
| captureError("cancel-subscription-missing", new StackAssertionError( | ||
| "Owned subscription product missing active/trialing subscription record.", | ||
| { | ||
| customerType: params.customer_type, | ||
| customerId: params.customer_id, | ||
| productId: params.product_id, | ||
| }, | ||
| )); | ||
| throw new StatusError(400, "This subscription cannot be canceled."); | ||
| } | ||
|
|
||
| const hasStripeSubscription = subscriptions.some((subscription) => subscription.stripeSubscriptionId); | ||
| const stripe = hasStripeSubscription ? await getStripeForAccount({ tenancy: auth.tenancy }) : undefined; | ||
| for (const subscription of subscriptions) { | ||
| if (subscription.stripeSubscriptionId) { | ||
| const stripeClient = stripe ?? throwErr(500, "Stripe client missing for subscription cancellation."); | ||
| await stripeClient.subscriptions.cancel(subscription.stripeSubscriptionId); | ||
| continue; | ||
| } | ||
| await prisma.subscription.update({ | ||
| where: { | ||
| tenancyId_id: { | ||
| tenancyId: auth.tenancy.id, | ||
| id: subscription.id, | ||
| }, | ||
| }, | ||
| data: { | ||
| status: SubscriptionStatus.canceled, | ||
| currentPeriodEnd: new Date(), | ||
| cancelAtPeriodEnd: true, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| 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
16 changes: 16 additions & 0 deletions
16
...ashboard/src/app/(main)/(protected)/projects/[projectId]/payments/payouts/page-client.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,16 @@ | ||
| "use client"; | ||
|
|
||
| import { ConnectPayouts } from "@stripe/react-connect-js"; | ||
| import { PageLayout } from "../../page-layout"; | ||
| import { StripeConnectProvider } from "@/components/payments/stripe-connect-provider"; | ||
|
|
||
| export default function PageClient() { | ||
|
|
||
| return ( | ||
| <PageLayout title="Payouts"> | ||
| <StripeConnectProvider> | ||
| <ConnectPayouts /> | ||
| </StripeConnectProvider> | ||
BilalG1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </PageLayout> | ||
| ); | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
apps/dashboard/src/app/(main)/(protected)/projects/[projectId]/payments/payouts/page.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,9 @@ | ||
| "use client"; | ||
BilalG1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
BilalG1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import PageClient from "./page-client"; | ||
|
|
||
| export default function Page() { | ||
| return ( | ||
| <PageClient /> | ||
| ); | ||
| } | ||
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.