Refactor usage reporting for metered subscriptions
This commit is contained in:
@@ -39,7 +39,6 @@ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=
|
|||||||
NEXT_PUBLIC_ENABLE_ACCOUNT_DELETION=true
|
NEXT_PUBLIC_ENABLE_ACCOUNT_DELETION=true
|
||||||
NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_BILLING=true
|
NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_BILLING=true
|
||||||
NEXT_PUBLIC_ENABLE_ORGANIZATION_DELETION=true
|
NEXT_PUBLIC_ENABLE_ORGANIZATION_DELETION=true
|
||||||
NEXT_PUBLIC_ENABLE_ORGANIZATION_INVITATIONS=true
|
|
||||||
NEXT_PUBLIC_ENABLE_ORGANIZATION_BILLING=true
|
NEXT_PUBLIC_ENABLE_ORGANIZATION_BILLING=true
|
||||||
|
|
||||||
# LOCALES
|
# LOCALES
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export const ReportBillingUsageSchema = z.object({
|
export const ReportBillingUsageSchema = z.object({
|
||||||
subscriptionId: z.string(),
|
subscriptionItemId: z.string(),
|
||||||
usage: z.object({
|
usage: z.object({
|
||||||
quantity: z.number(),
|
quantity: z.number(),
|
||||||
|
action: z.enum(['increment', 'set']),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -121,6 +121,15 @@ export class LemonSqueezyBillingStrategyService
|
|||||||
const { error } = await cancelSubscription(params.subscriptionId);
|
const { error } = await cancelSubscription(params.subscriptionId);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
Logger.error(
|
||||||
|
{
|
||||||
|
name: 'billing.lemon-squeezy',
|
||||||
|
subscriptionId: params.subscriptionId,
|
||||||
|
error: error.message,
|
||||||
|
},
|
||||||
|
'Failed to cancel subscription',
|
||||||
|
);
|
||||||
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,21 +206,22 @@ export class LemonSqueezyBillingStrategyService
|
|||||||
Logger.info(
|
Logger.info(
|
||||||
{
|
{
|
||||||
name: 'billing.lemon-squeezy',
|
name: 'billing.lemon-squeezy',
|
||||||
subscriptionItemId: params.subscriptionId,
|
subscriptionItemId: params.subscriptionItemId,
|
||||||
},
|
},
|
||||||
'Reporting usage...',
|
'Reporting usage...',
|
||||||
);
|
);
|
||||||
|
|
||||||
const { error } = await createUsageRecord({
|
const { error } = await createUsageRecord({
|
||||||
quantity: params.usage.quantity,
|
quantity: params.usage.quantity,
|
||||||
subscriptionItemId: params.subscriptionId,
|
subscriptionItemId: params.subscriptionItemId,
|
||||||
|
action: params.usage.action,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
Logger.error(
|
Logger.error(
|
||||||
{
|
{
|
||||||
name: 'billing.lemon-squeezy',
|
name: 'billing.lemon-squeezy',
|
||||||
subscriptionItemId: params.subscriptionId,
|
subscriptionItemId: params.subscriptionItemId,
|
||||||
error: error.message,
|
error: error.message,
|
||||||
},
|
},
|
||||||
'Failed to report usage',
|
'Failed to report usage',
|
||||||
@@ -223,7 +233,7 @@ export class LemonSqueezyBillingStrategyService
|
|||||||
Logger.info(
|
Logger.info(
|
||||||
{
|
{
|
||||||
name: 'billing.lemon-squeezy',
|
name: 'billing.lemon-squeezy',
|
||||||
subscriptionItemId: params.subscriptionId,
|
subscriptionItemId: params.subscriptionItemId,
|
||||||
},
|
},
|
||||||
'Usage reported successfully',
|
'Usage reported successfully',
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -187,9 +187,13 @@ export class StripeBillingStrategyService
|
|||||||
async reportUsage(params: z.infer<typeof ReportBillingUsageSchema>) {
|
async reportUsage(params: z.infer<typeof ReportBillingUsageSchema>) {
|
||||||
const stripe = await this.stripeProvider();
|
const stripe = await this.stripeProvider();
|
||||||
|
|
||||||
await stripe.subscriptionItems.createUsageRecord(params.subscriptionId, {
|
await stripe.subscriptionItems.createUsageRecord(
|
||||||
quantity: params.usage.quantity,
|
params.subscriptionItemId,
|
||||||
});
|
{
|
||||||
|
quantity: params.usage.quantity,
|
||||||
|
action: params.usage.action,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user