From 9efb061a7ef4a21bc16de307fb22262da5d8a1e2 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Sat, 1 Oct 2022 11:45:30 -0400 Subject: [PATCH] Handle PNID not found error on all subscription types --- src/util.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/util.js b/src/util.js index 6729b2a..cfde20d 100644 --- a/src/util.js +++ b/src/util.js @@ -263,25 +263,30 @@ async function handleStripeEvent(event) { const pid = Number(customer.metadata.pnid_pid); const pnid = await database.PNID.findOne({ pid }); - if (!pnid && subscription.status !== 'canceled' && subscription.status !== 'unpaid') { - // PNID does not exist. Abort and refund! - logger.error(`PNID PID ${pid} does not exist! Found on Stripe user ${customer.id}! Refunding order`); + if (!pnid) { + // PNID does not exist + if (subscription.status !== 'canceled' && subscription.status !== 'unpaid') { + // Abort and refund! + logger.error(`PNID PID ${pid} does not exist! Found on Stripe user ${customer.id}! Refunding order`); - await stripe.subscriptions.del(subscription.id); + await stripe.subscriptions.del(subscription.id); - const invoice = await stripe.invoices.retrieve(subscription.latest_invoice); - await stripe.refunds.create({ - payment_intent: invoice.payment_intent - }); - - try { - await mailer.sendMail({ - to: customer.email, - subject: 'Pretendo Network Subscription Failed - PNID Not Found', - text: `Your recent subscription to Pretendo Network has failed.\nThis is due to the provided PNID not being found. The subscription has been canceled and refunded. Please contact Jon immediately.\nStripe Customer ID: ${customer.id}\nPNID PID: ${pid}` + const invoice = await stripe.invoices.retrieve(subscription.latest_invoice); + await stripe.refunds.create({ + payment_intent: invoice.payment_intent }); - } catch (error) { - logger.error(`Error sending email | ${customer.id}, ${customer.email} | - ${error.message}`); + + try { + await mailer.sendMail({ + to: customer.email, + subject: 'Pretendo Network Subscription Failed - PNID Not Found', + text: `Your recent subscription to Pretendo Network has failed.\nThis is due to the provided PNID not being found. The subscription has been canceled and refunded. Please contact Jon immediately.\nStripe Customer ID: ${customer.id}\nPNID PID: ${pid}` + }); + } catch (error) { + logger.error(`Error sending email | ${customer.id}, ${customer.email} | - ${error.message}`); + } + } else { + logger.error(`PNID PID ${pid} does not exist! Found on Stripe user ${customer.id}!`); } return;