41 update subscription when workspace member changes 2 (#4252)

* Add loader and disabling on checkout button

* Add Stripe Subscription Item id to subscriptionItem entity

* Handle create and delete workspace members

* Update billing webhook

* Make stripe attribute private

* Fixing webhook error

* Clean migration

* Cancel subscription when deleting workspace

* Fix test

* Add freetrial

* Update navigate after signup

* Add automatic tax collection
This commit is contained in:
martmull
2024-03-01 17:29:28 +01:00
committed by GitHub
parent aa7ead3e8c
commit 8f6200be7d
22 changed files with 436 additions and 131 deletions

View File

@ -29,7 +29,7 @@ export class BillingController {
@Res() res: Response,
) {
if (!req.rawBody) {
res.status(400).send('Missing raw body');
res.status(400).end();
return;
}
@ -38,27 +38,23 @@ export class BillingController {
req.rawBody,
);
if (event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_UPDATED) {
if (event.data.object.status !== 'active') {
res.status(402).send('Payment did not succeeded');
return;
}
if (
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_CREATED ||
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_UPDATED
) {
const workspaceId = event.data.object.metadata?.workspaceId;
if (!workspaceId) {
res.status(404).send('Missing workspaceId in webhook event metadata');
res.status(404).end();
return;
}
await this.billingService.createBillingSubscription(
await this.billingService.upsertBillingSubscription(
workspaceId,
event.data,
);
res.status(200).send('Subscription successfully updated');
}
res.status(200).end();
}
}

View File

@ -8,16 +8,24 @@ import { BillingSubscription } from 'src/core/billing/entities/billing-subscript
import { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subscription-item.entity';
import { Workspace } from 'src/core/workspace/workspace.entity';
import { BillingResolver } from 'src/core/billing/billing.resolver';
import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity';
import { BillingWorkspaceMemberListener } from 'src/core/billing/listeners/billing-workspace-member.listener';
@Module({
imports: [
StripeModule,
TypeOrmModule.forFeature(
[BillingSubscription, BillingSubscriptionItem, Workspace],
[
BillingSubscription,
BillingSubscriptionItem,
Workspace,
FeatureFlagEntity,
],
'core',
),
],
controllers: [BillingController],
providers: [BillingService, BillingResolver],
providers: [BillingService, BillingResolver, BillingWorkspaceMemberListener],
exports: [BillingService],
})
export class BillingModule {}

View File

@ -11,13 +11,13 @@ import { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subsc
import { Workspace } from 'src/core/workspace/workspace.entity';
import { ProductPriceEntity } from 'src/core/billing/dto/product-price.entity';
import { User } from 'src/core/user/user.entity';
import { assert } from 'src/utils/assert';
export enum AvailableProduct {
BasePlan = 'base-plan',
}
export enum WebhookEvent {
CUSTOMER_SUBSCRIPTION_CREATED = 'customer.subscription.created',
CUSTOMER_SUBSCRIPTION_UPDATED = 'customer.subscription.updated',
}
@ -42,9 +42,8 @@ export class BillingService {
}
async getProductPrices(stripeProductId: string) {
const productPrices = await this.stripeService.stripe.prices.search({
query: `product: '${stripeProductId}'`,
});
const productPrices =
await this.stripeService.getProductPrices(stripeProductId);
return this.formatProductPrices(productPrices.data);
}
@ -74,63 +73,101 @@ export class BillingService {
return Object.values(result).sort((a, b) => a.unitAmount - b.unitAmount);
}
async checkout(user: User, priceId: string, successUrlPath?: string) {
const frontBaseUrl = this.environmentService.getFrontBaseUrl();
const session = await this.stripeService.stripe.checkout.sessions.create({
line_items: [
{
price: priceId,
quantity: 1,
},
],
mode: 'subscription',
subscription_data: {
metadata: {
workspaceId: user.defaultWorkspace.id,
},
},
customer_email: user.email,
success_url: successUrlPath
? frontBaseUrl + successUrlPath
: frontBaseUrl,
cancel_url: frontBaseUrl,
async getBillingSubscription(workspaceId: string) {
return await this.billingSubscriptionRepository.findOneOrFail({
where: { workspaceId },
relations: ['billingSubscriptionItems'],
});
assert(session.url, 'Error: missing checkout.session.url');
this.logger.log(`Stripe Checkout Session Url Redirection: ${session.url}`);
return session.url;
}
async createBillingSubscription(
async getBillingSubscriptionItem(
workspaceId: string,
data: Stripe.CustomerSubscriptionUpdatedEvent.Data,
stripeProductId = this.environmentService.getBillingStripeBasePlanProductId(),
) {
const billingSubscription = this.billingSubscriptionRepository.create({
workspaceId: workspaceId,
stripeCustomerId: data.object.customer as string,
stripeSubscriptionId: data.object.id,
status: data.object.status,
const billingSubscription = await this.getBillingSubscription(workspaceId);
const billingSubscriptionItem =
billingSubscription.billingSubscriptionItems.filter(
(billingSubscriptionItem) =>
billingSubscriptionItem.stripeProductId === stripeProductId,
)?.[0];
if (!billingSubscriptionItem) {
throw new Error(
`Cannot find billingSubscriptionItem for product ${stripeProductId} for workspace ${workspaceId}`,
);
}
return billingSubscriptionItem;
}
async checkout(user: User, priceId: string, successUrlPath?: string) {
const frontBaseUrl = this.environmentService.getFrontBaseUrl();
const successUrl = successUrlPath
? frontBaseUrl + successUrlPath
: frontBaseUrl;
return await this.stripeService.createCheckoutSession(
user,
priceId,
successUrl,
frontBaseUrl,
);
}
async deleteSubscription(workspaceId: string) {
const subscriptionToCancel =
await this.billingSubscriptionRepository.findOneBy({
workspaceId,
});
if (subscriptionToCancel) {
await this.stripeService.cancelSubscription(
subscriptionToCancel.stripeSubscriptionId,
);
await this.billingSubscriptionRepository.delete(subscriptionToCancel.id);
}
}
async upsertBillingSubscription(
workspaceId: string,
data:
| Stripe.CustomerSubscriptionUpdatedEvent.Data
| Stripe.CustomerSubscriptionCreatedEvent.Data,
) {
await this.billingSubscriptionRepository.upsert(
{
workspaceId: workspaceId,
stripeCustomerId: data.object.customer as string,
stripeSubscriptionId: data.object.id,
status: data.object.status,
},
{
conflictPaths: ['stripeSubscriptionId'],
skipUpdateIfNoValuesChanged: true,
},
);
await this.workspaceRepository.update(workspaceId, {
subscriptionStatus: data.object.status,
});
await this.billingSubscriptionRepository.save(billingSubscription);
const billingSubscription = await this.getBillingSubscription(workspaceId);
for (const item of data.object.items.data) {
const billingSubscriptionItem =
this.billingSubscriptionItemRepository.create({
await this.billingSubscriptionItemRepository.upsert(
data.object.items.data.map((item) => {
return {
billingSubscriptionId: billingSubscription.id,
stripeProductId: item.price.product as string,
stripePriceId: item.price.id,
stripeSubscriptionItemId: item.id,
quantity: item.quantity,
});
await this.billingSubscriptionItemRepository.save(
billingSubscriptionItem,
);
}
await this.workspaceRepository.update(workspaceId, {
subscriptionStatus: 'active',
});
};
}),
{
conflictPaths: ['stripeSubscriptionItemId', 'billingSubscriptionId'],
skipUpdateIfNoValuesChanged: true,
},
);
}
}

View File

@ -4,12 +4,21 @@ import {
Entity,
ManyToOne,
PrimaryGeneratedColumn,
Unique,
UpdateDateColumn,
} from 'typeorm';
import { BillingSubscription } from 'src/core/billing/entities/billing-subscription.entity';
@Entity({ name: 'billingSubscriptionItem', schema: 'core' })
@Unique('IndexOnBillingSubscriptionIdAndStripeProductIdUnique', [
'billingSubscriptionId',
'stripeProductId',
])
@Unique('IndexOnBillingSubscriptionIdAndStripeSubscriptionItemIdUnique', [
'billingSubscriptionId',
'stripeSubscriptionItemId',
])
export class BillingSubscriptionItem {
@PrimaryGeneratedColumn('uuid')
id: string;
@ -41,6 +50,9 @@ export class BillingSubscriptionItem {
@Column({ nullable: false })
stripePriceId: string;
@Column({ nullable: false })
stripeSubscriptionItemId: string;
@Column({ nullable: false })
quantity: number;
}

View File

@ -0,0 +1,59 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface';
import { BillingService } from 'src/core/billing/billing.service';
import { UserWorkspaceService } from 'src/core/user-workspace/user-workspace.service';
import {
FeatureFlagEntity,
FeatureFlagKeys,
} from 'src/core/feature-flag/feature-flag.entity';
import { StripeService } from 'src/core/billing/stripe/stripe.service';
export type UpdateSubscriptionJobData = { workspaceId: string };
@Injectable()
export class UpdateSubscriptionJob
implements MessageQueueJob<UpdateSubscriptionJobData>
{
protected readonly logger = new Logger(UpdateSubscriptionJob.name);
constructor(
private readonly billingService: BillingService,
private readonly userWorkspaceService: UserWorkspaceService,
private readonly stripeService: StripeService,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
) {}
async handle(data: UpdateSubscriptionJobData): Promise<void> {
const isSelfBillingEnabled = await this.featureFlagRepository.findOneBy({
workspaceId: data.workspaceId,
key: FeatureFlagKeys.IsSelfBillingEnabled,
value: true,
});
if (!isSelfBillingEnabled) {
return;
}
const workspaceMembersCount =
await this.userWorkspaceService.getWorkspaceMemberCount(data.workspaceId);
if (workspaceMembersCount <= 0) {
return;
}
const billingSubscriptionItem =
await this.billingService.getBillingSubscriptionItem(data.workspaceId);
await this.stripeService.updateSubscriptionItem(
billingSubscriptionItem.stripeSubscriptionItemId,
workspaceMembersCount,
);
this.logger.log(
`Updating workspace ${data.workspaceId} subscription quantity to ${workspaceMembersCount} members`,
);
}
}

View File

@ -0,0 +1,50 @@
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { OnEvent } from '@nestjs/event-emitter';
import { Repository } from 'typeorm';
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service';
import {
FeatureFlagEntity,
FeatureFlagKeys,
} from 'src/core/feature-flag/feature-flag.entity';
import { ObjectRecordCreateEvent } from 'src/integrations/event-emitter/types/object-record-create.event';
import { WorkspaceMemberObjectMetadata } from 'src/workspace/workspace-sync-metadata/standard-objects/workspace-member.object-metadata';
import {
UpdateSubscriptionJob,
UpdateSubscriptionJobData,
} from 'src/core/billing/jobs/update-subscription.job';
@Injectable()
export class BillingWorkspaceMemberListener {
constructor(
@Inject(MessageQueue.billingQueue)
private readonly messageQueueService: MessageQueueService,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
) {}
@OnEvent('workspaceMember.created')
@OnEvent('workspaceMember.deleted')
async handleCreateOrDeleteEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberObjectMetadata>,
) {
const isSelfBillingFeatureFlag = await this.featureFlagRepository.findOneBy(
{
key: FeatureFlagKeys.IsSelfBillingEnabled,
value: true,
workspaceId: payload.workspaceId,
},
);
if (!isSelfBillingFeatureFlag) {
return;
}
await this.messageQueueService.add<UpdateSubscriptionJobData>(
UpdateSubscriptionJob.name,
{ workspaceId: payload.workspaceId },
);
}
}

View File

@ -1,12 +1,15 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import Stripe from 'stripe';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
import { User } from 'src/core/user/user.entity';
import { assert } from 'src/utils/assert';
@Injectable()
export class StripeService {
public readonly stripe: Stripe;
protected readonly logger = new Logger(StripeService.name);
private readonly stripe: Stripe;
constructor(private readonly environmentService: EnvironmentService) {
this.stripe = new Stripe(
@ -25,4 +28,53 @@ export class StripeService {
webhookSecret,
);
}
async getProductPrices(stripeProductId: string) {
return this.stripe.prices.search({
query: `product: '${stripeProductId}'`,
});
}
async updateSubscriptionItem(stripeItemId: string, quantity: number) {
await this.stripe.subscriptionItems.update(stripeItemId, { quantity });
}
async cancelSubscription(stripeSubscriptionId: string) {
await this.stripe.subscriptions.cancel(stripeSubscriptionId);
}
async createCheckoutSession(
user: User,
priceId: string,
successUrl?: string,
cancelUrl?: string,
) {
const session = await this.stripe.checkout.sessions.create({
line_items: [
{
price: priceId,
quantity: 1,
},
],
mode: 'subscription',
subscription_data: {
metadata: {
workspaceId: user.defaultWorkspace.id,
},
trial_period_days:
this.environmentService.getBillingFreeTrialDurationInDays(),
},
automatic_tax: { enabled: true },
tax_id_collection: { enabled: true },
customer_email: user.email,
success_url: successUrl,
cancel_url: cancelUrl,
});
assert(session.url, 'Error: missing checkout.session.url');
this.logger.log(`Stripe Checkout Session Url Redirection: ${session.url}`);
return session.url;
}
}