Skip to content

Commit

Permalink
fix: Stripe Invoice payment failed (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
krish221997 authored Aug 21, 2024
1 parent a6ac2e3 commit f3bf8c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 21 additions & 0 deletions apps/event-system/services/api/clients.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,27 @@ module.exports = {
},
},

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

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

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

updateBillingByCustomerId: {
params: {
customerId: 'string',
Expand Down
21 changes: 15 additions & 6 deletions apps/event-system/services/stripe/stripe-webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,18 @@ export default {
case 'invoice.payment_failed':
const invoicePaymentFailed = event.data.object;

const failedInvoiceClient = await ctx.broker.call(
'v1.clients.updateOnInvoicePaymentFailed',
const currentSubscription = await stripe.subscriptions.retrieve(
invoicePaymentFailed?.subscription as string
);

if (currentSubscription?.status !== 'active') {
await ctx.broker.call('v1.clients.updateOnInvoicePaymentFailed', {
customerId: invoicePaymentFailed?.customer,
});
}

const currentClient = await ctx.broker.call(
'v1.clients.getByCustomerId',
{
customerId: invoicePaymentFailed?.customer,
}
Expand All @@ -135,13 +145,12 @@ export default {
data: {
event: 'Failed Invoice Payment',
properties: invoicePaymentFailed,
userId: failedInvoiceClient?.author?._id,
userId: currentClient?.author?._id,
},
});
break;

case 'invoice.payment_succeeded':

const invoicePaymentSucceeded = event.data.object;

const subscription = await stripe.subscriptions.retrieve(
Expand All @@ -155,7 +164,7 @@ export default {
endDate: subscription?.current_period_end,
}
);

await ctx.broker.call('v1.tracking.public.track', {
path: 't',
data: {
Expand All @@ -165,7 +174,6 @@ export default {
},
});


break;
}

Expand All @@ -183,3 +191,4 @@ export default {
},
} as ServiceSchema;


0 comments on commit f3bf8c6

Please sign in to comment.