Files
myeasycms-v2/packages/billing/gateway/src/server/services/billing-gateway/billing-gateway.service.ts
giancarlo 5b837d2fa8 Update UI style and enhance billing services
Several changes have been made in this commit. Firstly, updates have been made to the site-header-account-section and the pricing-table components to enhance UI aesthetics. Secondly, billing services have been significantly improved. A new method for retrieving plan information by an ID has been introduced. This method is available for all strategy services, including Stripe and Lemon-Squeezy. Furthermore, the way context and logging are handled during the billing process has been streamlined for better readability and efficiency.
2024-04-11 10:29:52 +08:00

143 lines
3.9 KiB
TypeScript

import 'server-only';
import { z } from 'zod';
import { BillingProviderSchema } from '@kit/billing';
import {
CancelSubscriptionParamsSchema,
CreateBillingCheckoutSchema,
CreateBillingPortalSessionSchema,
ReportBillingUsageSchema,
RetrieveCheckoutSessionSchema,
UpdateSubscriptionParamsSchema,
} from '@kit/billing/schema';
import { BillingGatewayFactoryService } from './billing-gateway-factory.service';
/**
* @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);
*/
export 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 BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
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 BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
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 BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
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 BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
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 BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
const payload = ReportBillingUsageSchema.parse(params);
return strategy.reportUsage(payload);
}
/**
* Updates a subscription with the specified parameters.
* @param params
*/
async updateSubscriptionItem(
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
) {
const strategy = await BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
const payload = UpdateSubscriptionParamsSchema.parse(params);
return strategy.updateSubscription(payload);
}
/**
* Retrieves a plan by the specified plan ID.
* @param planId
*/
async getPlanById(planId: string) {
const strategy = await BillingGatewayFactoryService.GetProviderStrategy(
this.provider,
);
return strategy.getPlanById(planId);
}
}