47 add stripe checkout endpoint (#4147)
* 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 * Add checkout endpoint * Remove resolver * Merge controllers * Fix security issue * Handle missing url error * Add workspaceId in checkout metadata
This commit is contained in:
@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common';
|
||||
import Stripe from 'stripe';
|
||||
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
import { StripeService } from 'src/core/billing/stripe/stripe.service';
|
||||
|
||||
export type PriceData = Partial<
|
||||
Record<Stripe.Price.Recurring.Interval, Stripe.Price>
|
||||
@ -10,10 +11,16 @@ export type PriceData = Partial<
|
||||
export enum AvailableProduct {
|
||||
BasePlan = 'base-plan',
|
||||
}
|
||||
|
||||
export enum RecurringInterval {
|
||||
MONTH = 'month',
|
||||
YEAR = 'year',
|
||||
}
|
||||
@Injectable()
|
||||
export class BillingService {
|
||||
constructor(private readonly environmentService: EnvironmentService) {}
|
||||
constructor(
|
||||
private readonly stripeService: StripeService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
) {}
|
||||
|
||||
getProductStripeId(product: AvailableProduct) {
|
||||
if (product === AvailableProduct.BasePlan) {
|
||||
@ -21,6 +28,14 @@ export class BillingService {
|
||||
}
|
||||
}
|
||||
|
||||
async getProductPrices(stripeProductId: string) {
|
||||
const productPrices = await this.stripeService.stripe.prices.search({
|
||||
query: `product: '${stripeProductId}'`,
|
||||
});
|
||||
|
||||
return this.formatProductPrices(productPrices.data);
|
||||
}
|
||||
|
||||
formatProductPrices(prices: Stripe.Price[]) {
|
||||
const result: PriceData = {};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user