Skip to content

Commit

Permalink
chore: Webhook events handling (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
krish221997 authored Aug 14, 2024
1 parent 74ef212 commit 8eba440
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 68 deletions.
59 changes: 57 additions & 2 deletions apps/event-system/services/api/clients.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,74 @@ module.exports = {
},
},

getByCustomerId: {
updateOnInvoicePaymentSuccess: {
params: {
customerId: 'string',
endDate: 'number',
},

async handler(ctx: any) {
try {
const client = await this.adapter.findOne({
'billing.customerId': ctx.params.customerId,
});

return client;
const updateDoc = await this.adapter.updateById(
client._id,
{
$set: {
'billing.subscription.valid': true,
'billing.subscription.reason': null,
'billing.subscription.endDate': ctx.params.endDate,
},
},
(doc: any) => {
return doc;
}
);

if (!updateDoc) {
return await ctx.call('error.404');
}

return updateDoc;
} catch (error) {
return await ctx.call('error.404');
}
},
},

updateOnInvoicePaymentFailed: {
params: {
customerId: 'string',
},

async handler(ctx: any) {
try {
const client = await this.adapter.findOne({
'billing.customerId': ctx.params.customerId,
});

const updateDoc = await this.adapter.updateById(
client._id,
{
$set: {
'billing.subscription.valid': false,
'billing.subscription.reason': 'payment-failed',
},
},
(doc: any) => {
return doc;
}
);

if (!updateDoc) {
return await ctx.call('error.404');
}

return updateDoc;
} catch (error) {
return await ctx.call('error.404');
}
},
},
Expand Down
97 changes: 32 additions & 65 deletions apps/event-system/services/stripe/stripe-webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,81 +124,48 @@ export default {
const invoicePaymentFailed = event.data.object;

const failedInvoiceClient = await ctx.broker.call(
'v1.clients.getByCustomerId',
'v1.clients.updateOnInvoicePaymentFailed',
{
customerId: invoicePaymentFailed?.customer,
}
);

if (failedInvoiceClient) {
const failedBilling = {
...failedInvoiceClient?.billing,
subscription: {
...failedInvoiceClient?.billing?.subscription,
valid: false,
reason: 'payment_failed',
},
};

const updatedFailedClient = await ctx.broker.call(
'v1.clients.updateBillingByCustomerId',
{
customerId: invoicePaymentFailed?.customer,
billing: failedBilling,
}
);

await ctx.broker.call('v1.tracking.public.track', {
path: 't',
data: {
event: 'Failed Invoice Payment',
properties: invoicePaymentFailed,
userId: updatedFailedClient?.author?._id,
},
});
}

await ctx.broker.call('v1.tracking.public.track', {
path: 't',
data: {
event: 'Failed Invoice Payment',
properties: invoicePaymentFailed,
userId: failedInvoiceClient?.author?._id,
},
});
break;

case 'invoice.payment_succeeded':
setTimeout(async () => {
const invoicePaymentSucceeded = event.data.object;

const invoicePaymentSucceeded = event.data.object;

const succeededInvoiceClient = await ctx.broker.call(
'v1.clients.getByCustomerId',
{
customerId: invoicePaymentSucceeded?.customer,
}
);

if (succeededInvoiceClient) {
const succeededBilling = {
...succeededInvoiceClient?.billing,
subscription: {
...succeededInvoiceClient?.billing?.subscription,
valid: true,
reason: null,
},
};

const updatedSucceededClient = await ctx.broker.call(
'v1.clients.updateBillingByCustomerId',
{
customerId: invoicePaymentSucceeded?.customer,
billing: succeededBilling,
}
);

await ctx.broker.call('v1.tracking.public.track', {
path: 't',
data: {
event: 'Successful Invoice Payment',
properties: invoicePaymentSucceeded,
userId: updatedSucceededClient?.author?._id,
},
});
const subscription = await stripe.subscriptions.retrieve(
invoicePaymentSucceeded?.subscription as string
);

const succeededInvoiceClient = await ctx.broker.call(
'v1.clients.updateOnInvoicePaymentSuccess',
{
customerId: invoicePaymentSucceeded?.customer,
endDate: subscription?.current_period_end,
}
}, 5000);
);

await ctx.broker.call('v1.tracking.public.track', {
path: 't',
data: {
event: 'Successful Invoice Payment',
properties: invoicePaymentSucceeded,
userId: succeededInvoiceClient?.author?._id,
},
});


break;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/connections/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@integrationos/authkit-node",
"version": "1.0.7",
"version": "1.0.9",
"description": "Secure token generation for IntegrationOS AuthKit",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit 8eba440

Please sign in to comment.