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:
martmull
2024-02-24 17:19:51 +01:00
committed by GitHub
parent c434d1edb5
commit c96e210ef1
4 changed files with 135 additions and 50 deletions

View File

@ -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 = {};