Implement updateSubscription feature and refactor billing services
This commit introduces the updateSubscription method to the BillingStrategyProviderService, ensuring that subscriptions can be updated within the billing core. Additionally, a refactor has been applied to the BillingGatewayFactoryService and stripe-billing-strategy.service to improve error handling and the robustness of subscription updates. Logging in the webhook route has been adjusted for clarity and the data model has been enhanced.
This commit is contained in:
@@ -103,6 +103,34 @@ export const PlanSchema = z
|
||||
message: 'Line item IDs must be unique',
|
||||
path: ['lineItems'],
|
||||
},
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
if (data.paymentType === 'one-time') {
|
||||
const meteredItems = data.lineItems.filter(
|
||||
(item) => item.type === 'metered',
|
||||
);
|
||||
|
||||
return meteredItems.length === 0;
|
||||
}
|
||||
},
|
||||
{
|
||||
message: 'One-time plans must not have metered line items',
|
||||
path: ['paymentType', 'lineItems'],
|
||||
},
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
if (data.paymentType === 'one-time') {
|
||||
const baseItems = data.lineItems.filter((item) => item.type !== 'base');
|
||||
|
||||
return baseItems.length === 0;
|
||||
}
|
||||
},
|
||||
{
|
||||
message: 'One-time plans must not have non-base line items',
|
||||
path: ['paymentType', 'lineItems'],
|
||||
},
|
||||
);
|
||||
|
||||
const ProductSchema = z
|
||||
@@ -259,3 +287,20 @@ export function getProductPlanPairByVariantId(
|
||||
|
||||
throw new Error('Plan not found');
|
||||
}
|
||||
|
||||
export function getLineItemTypeById(
|
||||
config: z.infer<typeof BillingSchema>,
|
||||
id: string,
|
||||
) {
|
||||
for (const product of config.products) {
|
||||
for (const plan of product.plans) {
|
||||
for (const lineItem of plan.lineItems) {
|
||||
if (lineItem.type === id) {
|
||||
return lineItem.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Line Item with ID ${id} not found`);
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@ export * from './create-biling-portal-session.schema';
|
||||
export * from './retrieve-checkout-session.schema';
|
||||
export * from './cancel-subscription-params.schema';
|
||||
export * from './report-billing-usage.schema';
|
||||
export * from './update-subscription-params.schema';
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const UpdateSubscriptionParamsSchema = z.object({
|
||||
subscriptionId: z.string().min(1),
|
||||
subscriptionItemId: z.string().min(1),
|
||||
quantity: z.number().min(1),
|
||||
});
|
||||
@@ -4,9 +4,10 @@ import {
|
||||
CancelSubscriptionParamsSchema,
|
||||
CreateBillingCheckoutSchema,
|
||||
CreateBillingPortalSessionSchema,
|
||||
ReportBillingUsageSchema,
|
||||
RetrieveCheckoutSessionSchema,
|
||||
UpdateSubscriptionParamsSchema,
|
||||
} from '../schema';
|
||||
import { ReportBillingUsageSchema } from '../schema';
|
||||
|
||||
export abstract class BillingStrategyProviderService {
|
||||
abstract createBillingPortalSession(
|
||||
@@ -44,4 +45,10 @@ export abstract class BillingStrategyProviderService {
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
}>;
|
||||
|
||||
abstract updateSubscription(
|
||||
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
}>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user