/** * @fileOverview Worldpay transaction controller * * The functions here pick the data required for the transaction. * The returned will be response readied or a HttpError. No massaging should occur here. */ 'use strict'; const _ = require('lodash'); const payDirectly = require('./acquirers/worldpay/pay-directly/payment'); const HttpError = require('../common/HttpError'); const commonErrorHandler = require('./common/errorHandler'); const log = require('../../utils/logging.js')(__filename, 'payments:direct:worldpay'); /** * Common error formatting * * @param {Object} req - Express request object (for log-related info) * @param {Object} res - Express response object * @param {Error} error - an object which inherits from Error * @param {Object} logInfo - The base log info for the request * @private */ function handleError(req, res, error, logInfo) { let additionalResponse; // // If this is an HttpError we can get additional information for the response // if (error instanceof HttpError) { _.defaults(logInfo, { extraInfo: _.pick(error.acquirerInfo, ['httpStatusCode', 'customCode', 'message']) }); additionalResponse = { response: error.worldpayResponse }; } return commonErrorHandler( res, error, { req, log, message: 'Unsuccessful payment request', logInfo }, additionalResponse ); } /** * Make a payment (not to be confused with making a worldpay payment with a saved instrument!). * * @param {Object} req - Express request object * @param {Object} res - Express response object */ function worldpayPayment(req, res) { /** * Initial details of the request we are about to make */ const payeeInfo = _.pick(req.swagger.params.body.value.paymentInstrument.payer, ['email', 'firstName', 'lastName']); const logInfo = _.merge( {}, payeeInfo, { totalAmount: req.swagger.params.body.value.amount.value, currency: 'GBP' }); return payDirectly.payment(req.body) .then((outcome) => { /** * Successful response, so log the extra info we need. */ logInfo.worldpayOrderCode = outcome.transaction.id; logInfo.cardSchemeName = outcome.additionalInfo.cardSchemeName; logInfo.riskScore = outcome.additionalInfo.riskScore; log.info(req, 'Successful payment', logInfo); /** * Need to filter the response to just the transaction object. */ const response = _.pick(outcome, 'transaction'); return res.status(200).json(response); }) .catch((error) => { return handleError(req, res, error, logInfo); }); } module.exports = { worldpayPayment };