* Filter out metered line items from billing schema This update refines the process of creating a billing schema by filtering out metered line items. The change is necessary as metered line items can be shared across different plans, potentially causing conflicts or duplicates in the schema. * Update packages to newer versions This update upgrades several packages across multiple project files to their latest version. These packages include "supabase-js", "react-query", "react-hook-form", and "pnpm". The commit ensures the project is up-to-date with recent package versions, potentially benefiting from bug fixes, new features, and performance improvements. * Add subscription retrieval in billing services Added a function to retrieve subscription info in both Stripe and LemonSqueezy billing services. To accomplish this, new methods were added to related services and types. This allows querying specific subscription data based on its id, and throws an error if it fails. Furthermore, PayloadBuilder classes were created to systematically build the subscription payload. * Remove account ID retrieval from Lemon Squeezy billing service The code that was querying the database to fetch the accountId has been removed from lemon-squeezy-billing-strategy.service.ts. It was unnecessary as the Lemon Squeezy API does not provide account ID and therefore it is always left empty. Also, adjustments have been made in billing-strategy-provider.service.ts to reflect that the target account ID can be optional. * Extract 'next' parameter from callback URL The update allows for the extraction of the 'next' parameter from the callback URL. If such a parameter is available, it is subsequently added to the search parameters. The enhancement improves URL parameter handling in the authentication callback service. * Refactor URL redirection in auth-callback service The update simplifies the redirection logic in the authentication callback service. This is achieved by setting the url pathname directly to the redirect path, instead of first setting it to the callback parameter. Moreover, the code handling the 'next' path has also been streamlined, setting the url pathname to the next path when available.
144 lines
4.1 KiB
TypeScript
144 lines
4.1 KiB
TypeScript
import { z } from 'zod';
|
|
|
|
import { BillingProviderSchema } from '@kit/billing';
|
|
import {
|
|
CancelSubscriptionParamsSchema,
|
|
CreateBillingCheckoutSchema,
|
|
CreateBillingPortalSessionSchema,
|
|
QueryBillingUsageSchema,
|
|
ReportBillingUsageSchema,
|
|
RetrieveCheckoutSessionSchema,
|
|
UpdateSubscriptionParamsSchema,
|
|
} from '@kit/billing/schema';
|
|
|
|
import { BillingGatewayFactoryService } from './billing-gateway-factory.service';
|
|
|
|
export function createBillingGatewayService(
|
|
provider: z.infer<typeof BillingProviderSchema>,
|
|
) {
|
|
return new BillingGatewayService(provider);
|
|
}
|
|
|
|
/**
|
|
* @description The billing gateway service to interact with the billing provider of choice (e.g. Stripe)
|
|
* @class BillingGatewayService
|
|
* @param {BillingProvider} provider - The billing provider to use
|
|
* @example
|
|
*
|
|
* const provider = 'stripe';
|
|
* const billingGatewayService = new BillingGatewayService(provider);
|
|
*/
|
|
class BillingGatewayService {
|
|
constructor(
|
|
private readonly provider: z.infer<typeof BillingProviderSchema>,
|
|
) {}
|
|
|
|
/**
|
|
* Creates a checkout session for billing.
|
|
*
|
|
* @param {CreateBillingCheckoutSchema} params - The parameters for creating the checkout session.
|
|
*
|
|
*/
|
|
async createCheckoutSession(
|
|
params: z.infer<typeof CreateBillingCheckoutSchema>,
|
|
) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = CreateBillingCheckoutSchema.parse(params);
|
|
|
|
return strategy.createCheckoutSession(payload);
|
|
}
|
|
|
|
/**
|
|
* Retrieves the checkout session from the specified provider.
|
|
*
|
|
* @param {RetrieveCheckoutSessionSchema} params - The parameters to retrieve the checkout session.
|
|
*/
|
|
async retrieveCheckoutSession(
|
|
params: z.infer<typeof RetrieveCheckoutSessionSchema>,
|
|
) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = RetrieveCheckoutSessionSchema.parse(params);
|
|
|
|
return strategy.retrieveCheckoutSession(payload);
|
|
}
|
|
|
|
/**
|
|
* Creates a billing portal session for the specified parameters.
|
|
*
|
|
* @param {CreateBillingPortalSessionSchema} params - The parameters to create the billing portal session.
|
|
*/
|
|
async createBillingPortalSession(
|
|
params: z.infer<typeof CreateBillingPortalSessionSchema>,
|
|
) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = CreateBillingPortalSessionSchema.parse(params);
|
|
|
|
return strategy.createBillingPortalSession(payload);
|
|
}
|
|
|
|
/**
|
|
* Cancels a subscription.
|
|
*
|
|
* @param {CancelSubscriptionParamsSchema} params - The parameters for cancelling the subscription.
|
|
*/
|
|
async cancelSubscription(
|
|
params: z.infer<typeof CancelSubscriptionParamsSchema>,
|
|
) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = CancelSubscriptionParamsSchema.parse(params);
|
|
|
|
return strategy.cancelSubscription(payload);
|
|
}
|
|
|
|
/**
|
|
* Reports the usage of the billing.
|
|
* @description This is used to report the usage of the billing to the provider.
|
|
* @param params
|
|
*/
|
|
async reportUsage(params: z.infer<typeof ReportBillingUsageSchema>) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = ReportBillingUsageSchema.parse(params);
|
|
|
|
return strategy.reportUsage(payload);
|
|
}
|
|
|
|
/**
|
|
* @name queryUsage
|
|
* @description Queries the usage of the metered billing.
|
|
* @param params
|
|
*/
|
|
async queryUsage(params: z.infer<typeof QueryBillingUsageSchema>) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = QueryBillingUsageSchema.parse(params);
|
|
|
|
return strategy.queryUsage(payload);
|
|
}
|
|
|
|
/**
|
|
* Updates a subscription with the specified parameters.
|
|
* @param params
|
|
*/
|
|
async updateSubscriptionItem(
|
|
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
|
|
) {
|
|
const strategy = await this.getStrategy();
|
|
const payload = UpdateSubscriptionParamsSchema.parse(params);
|
|
|
|
return strategy.updateSubscriptionItem(payload);
|
|
}
|
|
|
|
/**
|
|
* Retrieves a subscription from the provider.
|
|
* @param subscriptionId
|
|
*/
|
|
async getSubscription(subscriptionId: string) {
|
|
const strategy = await this.getStrategy();
|
|
|
|
return strategy.getSubscription(subscriptionId);
|
|
}
|
|
|
|
private getStrategy() {
|
|
return BillingGatewayFactoryService.GetProviderStrategy(this.provider);
|
|
}
|
|
}
|