Skip to content
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

feat: Start trial on signup - [DEV-4560] #622

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/controllers/admin/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
27 changes: 26 additions & 1 deletion src/services/track/admin/account-submitter.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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({
Expand All @@ -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`);
}
}
}
Loading