From 9e5074f7c710065a370c4852af78ba0a92bda317 Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Thu, 14 Nov 2019 21:43:29 +1030 Subject: [PATCH] Authorize.net fixes --- routes/payments/authorizenet.js | 13 +++++++------ views/partials/payments/authorizenet.hbs | 11 ++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/routes/payments/authorizenet.js b/routes/payments/authorizenet.js index 7f96275..e231bed 100644 --- a/routes/payments/authorizenet.js +++ b/routes/payments/authorizenet.js @@ -36,7 +36,7 @@ router.post('/checkout_action', (req, res, next) => { }; axios.post(authorizeUrl, chargeJson, { responseType: 'text' }) - .then((response) => { + .then(async(response) => { // This is crazy but the Authorize.net API returns a string with BOM and totally // screws the JSON response being parsed. So many hours wasted! const txn = JSON.parse(stripBom(response.data)).transactionResponse; @@ -76,10 +76,8 @@ router.post('/checkout_action', (req, res, next) => { }; // insert order into DB - db.orders.insertOne(orderDoc, (err, newDoc) => { - if(err){ - console.info(err.stack); - } + try{ + const newDoc = await db.orders.insertOne(orderDoc); // get the new ID const newId = newDoc.insertedId; @@ -129,7 +127,10 @@ router.post('/checkout_action', (req, res, next) => { res.status(400).json({ err: true, orderId: newId }); } }); - }); + }catch(ex){ + console.log('Error sending payment to API', ex); + res.status(400).json({ err: 'Your payment has declined. Please try again' }); + } }) .catch((err) => { console.log('Error sending payment to API', err); diff --git a/views/partials/payments/authorizenet.hbs b/views/partials/payments/authorizenet.hbs index 3e06e6d..fb19c60 100644 --- a/views/partials/payments/authorizenet.hbs +++ b/views/partials/payments/authorizenet.hbs @@ -1,14 +1,11 @@ -{{#ifCond paymentConfig.mode '==' 'test'}} {{else}} - -{{/ifCond}}