142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
/**
|
|
* Functions to interact with Credorax
|
|
* This is based on the Credorax ePower Payment API Specification.
|
|
* @see {@url http://epower.credorax.com/home}
|
|
* The API version at the time this file was cared is 4.15 Rev 1 Jan 2016
|
|
*/
|
|
'use strict';
|
|
|
|
var Q = require('q');
|
|
var _ = require('lodash');
|
|
var config = require(global.configFile);
|
|
var errors = require(global.pathPrefix + '../utils/acquirers/acquirer_errors.js');
|
|
var log = require(global.pathPrefix + 'log.js');
|
|
var sms = require(global.pathPrefix + 'sms.js');
|
|
var credorax = require(global.pathPrefix + 'credorax.js');
|
|
|
|
module.exports = {
|
|
invalidateToken: invalidateToken,
|
|
payTokenised: payTokenised
|
|
};
|
|
|
|
/**
|
|
* Attempts to invalidate the token with Credorax
|
|
*
|
|
* @param {string} token - The token to be invalidated
|
|
* @param {string} merchantID - The merchant ID the card was tokenised with
|
|
* @param {string} cipher - The merchant cipher the card was tokenised with
|
|
* @param {string} accountID - The accountID for creating a tracking id
|
|
*
|
|
* @returns {promise} - A promise that resolves on success, or rejects on fail
|
|
*/
|
|
function invalidateToken(token, merchantID, cipher, accountID) {
|
|
//
|
|
// Check if we have anything to invalidate
|
|
//
|
|
if (!token) {
|
|
return Q.resolve();
|
|
}
|
|
|
|
//
|
|
// Setup the API request to invalidate a token.
|
|
//
|
|
var command = {
|
|
'O': '16', // Code 16 is Block Token (see page 12 of API docs)
|
|
a1: 'DA' + accountID, // Request ID (unique number defined by us)
|
|
g1: token // The token to be invalidated
|
|
};
|
|
|
|
return Q.nfcall(
|
|
credorax.CredoraxFunction,
|
|
command,
|
|
merchantID,
|
|
cipher)
|
|
.then(function(response) {
|
|
if (response.z2 === '0') {
|
|
return Q.resolve();
|
|
} else {
|
|
return Q.resolve(response);
|
|
}
|
|
})
|
|
.catch(function(error) {
|
|
credorax.commsFailure('webConsole.onCommunicationFailure');
|
|
return Q.reject({name: errors.CREDORAX_DOWN});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Makes a payment from customer to merchant using the specified acquirer. This
|
|
* requires the card to be pre-tokenised by the selected acquirer.
|
|
*
|
|
* @param {Object} transaction - The transaction being completed
|
|
* @param {Object} customerAccount - The customer's payment account
|
|
* @param {string} customerIP - The IP address that the customer connects from
|
|
* @param {Object} merchantAccount - The merchant's payment account
|
|
*
|
|
* @returns {Promise} - A promise that resolves on success, or rejects on fail
|
|
*/
|
|
function payTokenised(transaction, customerAccount, customerIP, merchantAccount) {
|
|
//
|
|
// Setup the API request
|
|
//
|
|
var billingDescriptor =
|
|
'COMCARDE *' +
|
|
_.truncate(transaction.MerchantDisplayName, {length: 13});
|
|
var command = {
|
|
'O': '11', // Code 11 is "Use Token - Sale"
|
|
'a1': transaction._id.toString(), // Use the ID as the RequestID
|
|
'a4': transaction.TotalAmount,
|
|
'd1': customerIP,
|
|
'g1': customerAccount.Token,
|
|
'i2': billingDescriptor
|
|
};
|
|
|
|
//
|
|
// Make the call
|
|
//
|
|
return Q.nfcall(
|
|
credorax.CredoraxFunction,
|
|
command,
|
|
merchantAccount.AcquirerMerchantID,
|
|
merchantAccount.AcquirerCipher
|
|
).then(
|
|
function(response) {
|
|
if (response.z2 === '0') {
|
|
//
|
|
// Success
|
|
//
|
|
return Q.resolve({
|
|
reference: response.z13,
|
|
authCode: response.z4,
|
|
riskScore: response.z5,
|
|
avsResponse: response.z9,
|
|
responseID: response.z1,
|
|
saleTime: new Date()
|
|
});
|
|
} else if (response.z3.indexOf('Card is expired') !== -1) {
|
|
//
|
|
// Specific error: card is expired
|
|
//
|
|
return Q.reject({
|
|
name: errors.CARD_EXPIRED,
|
|
reason: response.z3
|
|
});
|
|
} else {
|
|
//
|
|
// Other unspecified errors
|
|
//
|
|
return Q.reject({
|
|
name: errors.PAYMENT_FAILED_UNSPECIFIED,
|
|
reason: response.z3
|
|
});
|
|
}
|
|
},
|
|
function(error) {
|
|
credorax.commsFailure('webConsole.onCommunicationFailure');
|
|
return Q.reject({
|
|
name: errors.CREDORAX_DOWN,
|
|
reason: error
|
|
});
|
|
});
|
|
}
|