expressCart/routes/payments/authorizenet.js

143 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-02-05 07:39:42 +10:00
const express = require('express');
const axios = require('axios');
const stripBom = require('strip-bom');
const common = require('../../lib/common');
2019-06-15 15:58:19 +10:00
const { indexOrders } = require('../../lib/indexing');
2018-02-05 07:39:42 +10:00
const router = express.Router();
// The homepage of the site
router.post('/checkout_action', (req, res, next) => {
const db = req.app.db;
2018-02-23 03:41:24 +10:00
const config = req.app.config;
2018-02-05 07:39:42 +10:00
const authorizenetConfig = common.getPaymentConfig();
let authorizeUrl = 'https://api.authorize.net/xml/v1/request.api';
if(authorizenetConfig.mode === 'test'){
authorizeUrl = 'https://apitest.authorize.net/xml/v1/request.api';
}
const chargeJson = {
createTransactionRequest: {
merchantAuthentication: {
name: authorizenetConfig.loginId,
transactionKey: authorizenetConfig.transactionKey
},
transactionRequest: {
transactionType: 'authCaptureTransaction',
amount: req.session.totalCartAmount,
payment: {
opaqueData: {
dataDescriptor: req.body.opaqueData.dataDescriptor,
dataValue: req.body.opaqueData.dataValue
}
}
}
}
};
2019-06-15 14:51:01 +10:00
axios.post(authorizeUrl, chargeJson, { responseType: 'text' })
2019-11-14 21:13:29 +10:00
.then(async(response) => {
2018-02-05 07:39:42 +10:00
// 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;
if(!txn){
console.log('Declined request payload', chargeJson);
console.log('Declined response payload', response.data);
2019-06-15 14:51:01 +10:00
res.status(400).json({ err: 'Your payment has declined. Please try again' });
2018-02-05 07:39:42 +10:00
return;
}
// order status if approved
let orderStatus = 'Paid';
if(txn && txn.responseCode !== '1'){
console.log('Declined response payload', response.data);
orderStatus = 'Declined';
}
2019-07-12 18:06:34 +10:00
const orderDoc = {
2018-02-05 07:39:42 +10:00
orderPaymentId: txn.transHash,
orderPaymentGateway: 'AuthorizeNet',
orderPaymentMessage: 'Your payment was successfully completed',
orderTotal: req.session.totalCartAmount,
2019-12-30 17:35:43 +10:00
orderItemCount: req.session.totalCartItems,
orderProductCount: req.session.totalCartProducts,
orderEmail: req.session.customerEmail,
orderFirstname: req.session.customerFirstname,
orderLastname: req.session.customerLastname,
orderAddr1: req.session.customerAddress1,
orderAddr2: req.session.customerAddress2,
orderCountry: req.session.customerCountry,
orderState: req.session.customerState,
orderPostcode: req.session.customerPostcode,
orderPhoneNumber: req.session.customerPhone,
orderComment: req.session.orderComment,
2018-02-05 07:39:42 +10:00
orderStatus: orderStatus,
orderDate: new Date(),
2020-01-02 15:23:39 +10:00
orderProducts: req.session.cart,
orderType: 'Single'
2018-02-05 07:39:42 +10:00
};
// insert order into DB
2019-11-14 21:13:29 +10:00
try{
const newDoc = await db.orders.insertOne(orderDoc);
2018-02-05 07:39:42 +10:00
// get the new ID
2019-11-04 16:39:05 +10:00
const newId = newDoc.insertedId;
2018-02-05 07:39:42 +10:00
// add to lunr index
2019-06-15 15:56:51 +10:00
indexOrders(req.app)
2018-02-05 07:39:42 +10:00
.then(() => {
// if approved, send email etc
if(orderStatus === 'Paid'){
// set the results
req.session.messageType = 'success';
req.session.message = 'Your payment was successfully completed';
req.session.paymentEmailAddr = newDoc.ops[0].orderEmail;
req.session.paymentApproved = true;
req.session.paymentDetails = `<p><strong>Order ID: </strong>${newId}</p>
2020-01-01 14:24:17 +10:00
<p><strong>Transaction ID: </strong>${orderDoc.orderPaymentId}</p>`;
2018-02-05 07:39:42 +10:00
// set payment results for email
2019-07-12 18:06:34 +10:00
const paymentResults = {
2018-02-05 07:39:42 +10:00
message: req.session.message,
messageType: req.session.messageType,
paymentEmailAddr: req.session.paymentEmailAddr,
paymentApproved: true,
paymentDetails: req.session.paymentDetails
};
// clear the cart
if(req.session.cart){
2020-01-02 15:23:39 +10:00
common.emptyCart(req, res, 'function');
2018-02-05 07:39:42 +10:00
}
// send the email with the response
// TODO: Should fix this to properly handle result
common.sendEmail(req.session.paymentEmailAddr, `Your payment with ${config.cartTitle}`, common.getEmailTemplate(paymentResults));
// redirect to outcome
2019-06-15 14:51:01 +10:00
res.status(200).json({ orderId: newId });
2018-02-05 07:39:42 +10:00
}else{
// redirect to failure
req.session.messageType = 'danger';
req.session.message = 'Your payment has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = `<p><strong>Order ID: </strong>${newId}
</p><p><strong>Transaction ID: </strong> ${txn.transHash}</p>`;
2019-06-15 14:51:01 +10:00
res.status(400).json({ err: true, orderId: newId });
2018-02-05 07:39:42 +10:00
}
});
2019-11-14 21:13:29 +10:00
}catch(ex){
console.log('Error sending payment to API', ex);
res.status(400).json({ err: 'Your payment has declined. Please try again' });
}
2018-02-05 07:39:42 +10:00
})
.catch((err) => {
console.log('Error sending payment to API', err);
2019-06-15 14:51:01 +10:00
res.status(400).json({ err: 'Your payment has declined. Please try again' });
2018-02-05 07:39:42 +10:00
});
});
module.exports = router;