From 3d8768f06e9d864b234ea3a9ee3c6af8893b216d Mon Sep 17 00:00:00 2001 From: jay-dee7 Date: Wed, 6 Nov 2024 14:14:43 +0530 Subject: [PATCH] feat: Start trial on signup Signed-off-by: jay-dee7 --- package-lock.json | 4 +-- src/controllers/admin/webhook.ts | 1 - src/services/track/admin/account-submitter.ts | 27 ++++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 95a5b797..cf310193 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@cheqd/studio", - "version": "3.4.0", + "version": "3.5.0-develop.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@cheqd/studio", - "version": "3.4.0", + "version": "3.5.0-develop.2", "license": "Apache-2.0", "dependencies": { "@cheqd/did-provider-cheqd": "^4.2.1-develop.1", diff --git a/src/controllers/admin/webhook.ts b/src/controllers/admin/webhook.ts index 5ae8f75b..e252add3 100644 --- a/src/controllers/admin/webhook.ts +++ b/src/controllers/admin/webhook.ts @@ -106,7 +106,6 @@ export class WebhookController { severity: 'info', } satisfies INotifyMessage); - console.log('fetching stripe data'); const [product, stripeCustomer] = await Promise.all([ stripe.products.retrieve(data.productId), stripe.customers.retrieve(data.paymentProviderId), diff --git a/src/services/track/admin/account-submitter.ts b/src/services/track/admin/account-submitter.ts index 87cc544a..73b3c6fb 100644 --- a/src/services/track/admin/account-submitter.ts +++ b/src/services/track/admin/account-submitter.ts @@ -1,6 +1,6 @@ import Stripe from 'stripe'; import type { IObserver } from '../types.js'; -import { OperationNameEnum } from '../../../types/constants.js'; +import { MaxAllowedTrialPeriodDays, OperationNameEnum } from '../../../types/constants.js'; import type { INotifyMessage } from '../../../types/track.js'; import { EventTracker } from '../tracker.js'; import { StatusCodes } from 'http-status-codes'; @@ -65,6 +65,11 @@ export class PortalAccountCreateSubmitter implements IObserver { name: data.name, paymentProviderId: stripeCustomer.id, }); + + if (process.env.STRIPE_TRIAL_PLAN_PRICING_ID) { + // Start a trial for the user + await this.startTrialForPlan(stripe, stripeCustomer.id, MaxAllowedTrialPeriodDays); + } } } catch (error) { this.notify({ @@ -76,4 +81,24 @@ export class PortalAccountCreateSubmitter implements IObserver { } as INotifyMessage); } } + + async startTrialForPlan(stripe: Stripe, paymentProviderId: string, trialPeriodDays: number = 30) { + const subscriptionResponse = await stripe.subscriptions.create({ + customer: paymentProviderId, + items: [{ price: process.env.STRIPE_TRIAL_PLAN_PRICING_ID, quantity: 1 }], + payment_settings: { + save_default_payment_method: 'on_subscription', + }, + trial_period_days: trialPeriodDays, + trial_settings: { + end_behavior: { + missing_payment_method: 'cancel', + }, + }, + }); + + if (subscriptionResponse.lastResponse.statusCode !== StatusCodes.OK) { + throw new Error(`Failed to start trial plan`); + } + } }