feat(server): ai trial support (#9031)

This commit is contained in:
liuyi 2024-12-05 18:39:59 +09:00 committed by GitHub
parent 5bf8ed1095
commit 4c39b89b98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 4 deletions

View File

@ -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}',
}),

View File

@ -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,
},

View File

@ -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],
},
},
});