46 add stripe product endpoint (#4133)
* Add self billing feature flag * Add two core tables for billing * Remove useless imports * Remove graphql decorators * Rename subscriptionProduct table * WIP: Add stripe config * Add controller to get product prices * Add billing service * Remove unecessary package * Simplify stripe service * Code review returns * Use nestjs param * Rename subscription to basePlan * Rename env variable
This commit is contained in:
43
packages/twenty-server/src/core/billing/billing.service.ts
Normal file
43
packages/twenty-server/src/core/billing/billing.service.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import Stripe from 'stripe';
|
||||
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
|
||||
export type PriceData = Partial<
|
||||
Record<Stripe.Price.Recurring.Interval, Stripe.Price>
|
||||
>;
|
||||
export enum AvailableProduct {
|
||||
BasePlan = 'base-plan',
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BillingService {
|
||||
constructor(private readonly environmentService: EnvironmentService) {}
|
||||
|
||||
getProductStripeId(product: AvailableProduct) {
|
||||
if (product === AvailableProduct.BasePlan) {
|
||||
return this.environmentService.getBillingStripeBasePlanProductId();
|
||||
}
|
||||
}
|
||||
|
||||
formatProductPrices(prices: Stripe.Price[]) {
|
||||
const result: PriceData = {};
|
||||
|
||||
prices.forEach((item) => {
|
||||
const recurringInterval = item.recurring?.interval;
|
||||
|
||||
if (!recurringInterval) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!result[recurringInterval] ||
|
||||
item.created > (result[recurringInterval]?.created || 0)
|
||||
) {
|
||||
result[recurringInterval] = item;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user