Skip to content

Commit 8eba440

Browse files
authored
chore: Webhook events handling (#27)
1 parent 74ef212 commit 8eba440

File tree

3 files changed

+90
-68
lines changed

3 files changed

+90
-68
lines changed

apps/event-system/services/api/clients.service.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,74 @@ module.exports = {
164164
},
165165
},
166166

167-
getByCustomerId: {
167+
updateOnInvoicePaymentSuccess: {
168168
params: {
169169
customerId: 'string',
170+
endDate: 'number',
170171
},
172+
171173
async handler(ctx: any) {
172174
try {
173175
const client = await this.adapter.findOne({
174176
'billing.customerId': ctx.params.customerId,
175177
});
176178

177-
return client;
179+
const updateDoc = await this.adapter.updateById(
180+
client._id,
181+
{
182+
$set: {
183+
'billing.subscription.valid': true,
184+
'billing.subscription.reason': null,
185+
'billing.subscription.endDate': ctx.params.endDate,
186+
},
187+
},
188+
(doc: any) => {
189+
return doc;
190+
}
191+
);
192+
193+
if (!updateDoc) {
194+
return await ctx.call('error.404');
195+
}
196+
197+
return updateDoc;
178198
} catch (error) {
199+
return await ctx.call('error.404');
200+
}
201+
},
202+
},
203+
204+
updateOnInvoicePaymentFailed: {
205+
params: {
206+
customerId: 'string',
207+
},
208+
209+
async handler(ctx: any) {
210+
try {
211+
const client = await this.adapter.findOne({
212+
'billing.customerId': ctx.params.customerId,
213+
});
214+
215+
const updateDoc = await this.adapter.updateById(
216+
client._id,
217+
{
218+
$set: {
219+
'billing.subscription.valid': false,
220+
'billing.subscription.reason': 'payment-failed',
221+
},
222+
},
223+
(doc: any) => {
224+
return doc;
225+
}
226+
);
227+
228+
if (!updateDoc) {
179229
return await ctx.call('error.404');
230+
}
231+
232+
return updateDoc;
233+
} catch (error) {
234+
return await ctx.call('error.404');
180235
}
181236
},
182237
},

apps/event-system/services/stripe/stripe-webhook.service.ts

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -124,81 +124,48 @@ export default {
124124
const invoicePaymentFailed = event.data.object;
125125

126126
const failedInvoiceClient = await ctx.broker.call(
127-
'v1.clients.getByCustomerId',
127+
'v1.clients.updateOnInvoicePaymentFailed',
128128
{
129129
customerId: invoicePaymentFailed?.customer,
130130
}
131131
);
132132

133-
if (failedInvoiceClient) {
134-
const failedBilling = {
135-
...failedInvoiceClient?.billing,
136-
subscription: {
137-
...failedInvoiceClient?.billing?.subscription,
138-
valid: false,
139-
reason: 'payment_failed',
140-
},
141-
};
142-
143-
const updatedFailedClient = await ctx.broker.call(
144-
'v1.clients.updateBillingByCustomerId',
145-
{
146-
customerId: invoicePaymentFailed?.customer,
147-
billing: failedBilling,
148-
}
149-
);
150-
151-
await ctx.broker.call('v1.tracking.public.track', {
152-
path: 't',
153-
data: {
154-
event: 'Failed Invoice Payment',
155-
properties: invoicePaymentFailed,
156-
userId: updatedFailedClient?.author?._id,
157-
},
158-
});
159-
}
160-
133+
await ctx.broker.call('v1.tracking.public.track', {
134+
path: 't',
135+
data: {
136+
event: 'Failed Invoice Payment',
137+
properties: invoicePaymentFailed,
138+
userId: failedInvoiceClient?.author?._id,
139+
},
140+
});
161141
break;
162142

163143
case 'invoice.payment_succeeded':
164-
setTimeout(async () => {
165-
const invoicePaymentSucceeded = event.data.object;
144+
145+
const invoicePaymentSucceeded = event.data.object;
166146

167-
const succeededInvoiceClient = await ctx.broker.call(
168-
'v1.clients.getByCustomerId',
169-
{
170-
customerId: invoicePaymentSucceeded?.customer,
171-
}
172-
);
173-
174-
if (succeededInvoiceClient) {
175-
const succeededBilling = {
176-
...succeededInvoiceClient?.billing,
177-
subscription: {
178-
...succeededInvoiceClient?.billing?.subscription,
179-
valid: true,
180-
reason: null,
181-
},
182-
};
183-
184-
const updatedSucceededClient = await ctx.broker.call(
185-
'v1.clients.updateBillingByCustomerId',
186-
{
187-
customerId: invoicePaymentSucceeded?.customer,
188-
billing: succeededBilling,
189-
}
190-
);
191-
192-
await ctx.broker.call('v1.tracking.public.track', {
193-
path: 't',
194-
data: {
195-
event: 'Successful Invoice Payment',
196-
properties: invoicePaymentSucceeded,
197-
userId: updatedSucceededClient?.author?._id,
198-
},
199-
});
147+
const subscription = await stripe.subscriptions.retrieve(
148+
invoicePaymentSucceeded?.subscription as string
149+
);
150+
151+
const succeededInvoiceClient = await ctx.broker.call(
152+
'v1.clients.updateOnInvoicePaymentSuccess',
153+
{
154+
customerId: invoicePaymentSucceeded?.customer,
155+
endDate: subscription?.current_period_end,
200156
}
201-
}, 5000);
157+
);
158+
159+
await ctx.broker.call('v1.tracking.public.track', {
160+
path: 't',
161+
data: {
162+
event: 'Successful Invoice Payment',
163+
properties: invoicePaymentSucceeded,
164+
userId: succeededInvoiceClient?.author?._id,
165+
},
166+
});
167+
168+
202169
break;
203170
}
204171

packages/connections/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@integrationos/authkit-node",
3-
"version": "1.0.7",
3+
"version": "1.0.9",
44
"description": "Secure token generation for IntegrationOS AuthKit",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)