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:
@@ -9,6 +9,7 @@ import {
|
||||
CreateBillingPortalSessionSchema,
|
||||
ReportBillingUsageSchema,
|
||||
RetrieveCheckoutSessionSchema,
|
||||
UpdateSubscriptionParamsSchema,
|
||||
} from '@kit/billing/schema';
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
|
||||
@@ -198,6 +199,52 @@ export class StripeBillingStrategyService
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async updateSubscription(
|
||||
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
|
||||
) {
|
||||
const stripe = await this.stripeProvider();
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.stripe',
|
||||
...params,
|
||||
},
|
||||
'Updating subscription...',
|
||||
);
|
||||
|
||||
try {
|
||||
await stripe.subscriptions.update(params.subscriptionId, {
|
||||
items: [
|
||||
{
|
||||
id: params.subscriptionItemId,
|
||||
quantity: params.quantity,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.stripe',
|
||||
...params,
|
||||
},
|
||||
'Subscription updated successfully',
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
Logger.error(
|
||||
{
|
||||
name: 'billing.stripe',
|
||||
...params,
|
||||
error: e,
|
||||
},
|
||||
'Failed to update subscription',
|
||||
);
|
||||
|
||||
throw new Error('Failed to update subscription');
|
||||
}
|
||||
}
|
||||
|
||||
private async stripeProvider(): Promise<Stripe> {
|
||||
return createStripeClient();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user