From 4c39b89b989fb457587d690b210149fcccdfc4ed Mon Sep 17 00:00:00 2001 From: liuyi Date: Thu, 5 Dec 2024 18:39:59 +0900 Subject: [PATCH] feat(server): ai trial support (#9031) --- .../server/src/plugins/payment/manager/user.ts | 15 +++++++++++++++ .../src/plugins/payment/manager/workspace.ts | 1 + .../server/src/plugins/payment/resolver.ts | 10 ++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/backend/server/src/plugins/payment/manager/user.ts b/packages/backend/server/src/plugins/payment/manager/user.ts index 32b20c570d..fa1558ecad 100644 --- a/packages/backend/server/src/plugins/payment/manager/user.ts +++ b/packages/backend/server/src/plugins/payment/manager/user.ts @@ -148,6 +148,15 @@ export class UserSubscriptionManager extends SubscriptionManager { return { allow_promotion_codes: true }; })(); + const trials = (() => { + if (lookupKey.plan === SubscriptionPlan.AI && !strategy.aiSubscribed) { + return { + trial_period_days: 7, + } as Stripe.Checkout.SessionCreateParams.SubscriptionData; + } + return undefined; + })(); + // mode: 'subscription' or 'payment' for lifetime and onetime payment const mode = lookupKey.recurring === SubscriptionRecurring.Lifetime || @@ -169,11 +178,17 @@ export class UserSubscriptionManager extends SubscriptionManager { quantity: 1, }, ], + customer_update: { + name: 'auto', + }, tax_id_collection: { enabled: true, }, ...discounts, ...mode, + subscription_data: { + ...trials, + }, success_url: this.url.link(params.successCallbackLink, { session_id: '{CHECKOUT_SESSION_ID}', }), diff --git a/packages/backend/server/src/plugins/payment/manager/workspace.ts b/packages/backend/server/src/plugins/payment/manager/workspace.ts index 5eba7ae036..4c0c424487 100644 --- a/packages/backend/server/src/plugins/payment/manager/workspace.ts +++ b/packages/backend/server/src/plugins/payment/manager/workspace.ts @@ -115,6 +115,7 @@ export class WorkspaceSubscriptionManager extends SubscriptionManager { success_url: this.url.link(params.successCallbackLink), customer: customer.stripeCustomerId, subscription_data: { + trial_period_days: 15, metadata: { workspaceId: args.workspaceId, }, diff --git a/packages/backend/server/src/plugins/payment/resolver.ts b/packages/backend/server/src/plugins/payment/resolver.ts index d71399fba3..e0c78c8e9b 100644 --- a/packages/backend/server/src/plugins/payment/resolver.ts +++ b/packages/backend/server/src/plugins/payment/resolver.ts @@ -279,7 +279,7 @@ export class SubscriptionResolver { throw new FailedToCheckout(); } - return session; + return session.url; } @Mutation(() => String, { @@ -322,7 +322,7 @@ export class SubscriptionResolver { return this.service.cancelSubscription( { - targetId: user.id, + userId: user.id, // @ts-expect-error exam inside plan, }, @@ -363,7 +363,7 @@ export class SubscriptionResolver { return this.service.resumeSubscription( { - targetId: user.id, + userId: user.id, // @ts-expect-error exam inside plan, }, @@ -433,7 +433,9 @@ export class UserSubscriptionResolver { const subscriptions = await this.db.subscription.findMany({ where: { targetId: user.id, - status: SubscriptionStatus.Active, + status: { + in: [SubscriptionStatus.Active, SubscriptionStatus.Trialing], + }, }, });