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:
@@ -2,6 +2,7 @@ import {
|
||||
cancelSubscription,
|
||||
createUsageRecord,
|
||||
getCheckout,
|
||||
updateSubscriptionItem,
|
||||
} from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import 'server-only';
|
||||
import { z } from 'zod';
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
CreateBillingPortalSessionSchema,
|
||||
ReportBillingUsageSchema,
|
||||
RetrieveCheckoutSessionSchema,
|
||||
UpdateSubscriptionParamsSchema,
|
||||
} from '@kit/billing/schema';
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
|
||||
@@ -240,4 +242,35 @@ export class LemonSqueezyBillingStrategyService
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async updateSubscription(
|
||||
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
|
||||
) {
|
||||
const ctx = {
|
||||
name: 'billing.lemon-squeezy',
|
||||
...params,
|
||||
};
|
||||
|
||||
Logger.info(ctx, 'Updating subscription...');
|
||||
|
||||
const { error } = await updateSubscriptionItem(params.subscriptionItemId, {
|
||||
quantity: params.quantity,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
Logger.error(
|
||||
{
|
||||
...ctx,
|
||||
error,
|
||||
},
|
||||
'Failed to update subscription',
|
||||
);
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
Logger.info(ctx, 'Subscription updated successfully');
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { getOrder, getVariant } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { createHmac, timingSafeEqual } from 'crypto';
|
||||
|
||||
import { BillingWebhookHandlerService } from '@kit/billing';
|
||||
import {
|
||||
BillingConfig,
|
||||
BillingWebhookHandlerService,
|
||||
getLineItemTypeById,
|
||||
} from '@kit/billing';
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
@@ -35,6 +39,8 @@ export class LemonSqueezyWebhookHandlerService
|
||||
|
||||
private readonly namespace = 'billing.lemon-squeezy';
|
||||
|
||||
constructor(private readonly config: BillingConfig) {}
|
||||
|
||||
/**
|
||||
* @description Verifies the webhook signature - should throw an error if the signature is invalid
|
||||
*/
|
||||
@@ -307,6 +313,7 @@ export class LemonSqueezyWebhookHandlerService
|
||||
product_id: item.product,
|
||||
variant_id: item.variant,
|
||||
price_amount: item.unitAmount,
|
||||
type: getLineItemTypeById(this.config, item.id),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user