max user limits + check plan active on webhook + flow tweaks
This commit is contained in:
@@ -28,14 +28,22 @@ export default defineEventHandler(async (event) => {
|
||||
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
|
||||
|
||||
let subscription = stripeEvent.data.object as Stripe.Subscription;
|
||||
|
||||
const userService = new UserAccountService();
|
||||
|
||||
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
|
||||
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
|
||||
|
||||
console.log(`updating stripe sub details subscription.current_period_end:${subscription.current_period_end}, subscription.id:${subscription.id}`);
|
||||
userService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends)
|
||||
if(subscription.status == 'active'){
|
||||
const sub_item = subscription.items.data.find(item => item?.object && item?.object == 'subscription_item')
|
||||
|
||||
const stripe_product_id = sub_item?.plan.product?.toString(); // TODO - is the product ever a product object and in that case should I check for deleted?
|
||||
if(!stripe_product_id){
|
||||
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
|
||||
}
|
||||
|
||||
const userService = new UserAccountService();
|
||||
|
||||
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
|
||||
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
|
||||
|
||||
console.log(`updating stripe sub details subscription.current_period_end:${subscription.current_period_end}, subscription.id:${subscription.id}, stripe_product_id:${stripe_product_id}`);
|
||||
userService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends, stripe_product_id);
|
||||
}
|
||||
}
|
||||
return `handled ${stripeEvent.type}.`;
|
||||
});
|
||||
Reference in New Issue
Block a user