UNPKG

westpac-payway

Version:

Process payments via Westpac PayWay API

112 lines (92 loc) 2.98 kB
var https = require('https'); var qs = require('querystring'); var fs = require('fs'); var _ = require('underscore'); var test = { number: '4564710000000004', expiryYear: '19', expiryMonth: '02', cvn:'847', merchant: 'TEST' }; var PayWay = function(options) { this.ca = options.ca; this.eci = options.eci || 'SSL'; this.merchant = options.merchant; this.username = options.username; this.password = options.password; this.passphrase = options.passphrase; this.certificate = options.certificate; this.testing = options.testing || false; this.currency = options.currency || 'AUD'; this.defaults = { host: 'ccapi.client.qvalent.com', path: '/payway/ccapi', port: 443, method: 'POST', ca: [this.ca], pfx: this.certificate, passphrase: this.passphrase, agent: false }; }; PayWay.prototype.purchase = function(name, number, expiryMonth, expiryYear, cvn, amount, reference, callback) { var options = _.clone(this.defaults); var params = { 'order.type': 'capture', 'customer.username': this.username, 'customer.password': this.password, 'customer.merchant': this.testing ? test.merchant : this.merchant, 'card.PAN': this.testing ? test.number : number, 'card.CVN': this.testing ? test.cvn : cvn, 'card.expiryYear': this.testing ? test.expiryYear : expiryYear, 'card.expiryMonth': this.testing ? test.expiryMonth : expiryMonth, 'card.cardHolderName': name, 'order.amount': amount, 'customer.orderNumber': reference, 'card.currency': this.currency, 'order.ECI': this.eci }; this.request(options, params, callback); }; PayWay.prototype.request = function(options, params, callback) { var result = ''; var data = qs.stringify(params); var parseResult = this.parse; options.headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': data.length }; var request = https.request(options, function(res) { res.on('data', function(chunk) { result += chunk; }); res.on('end', function() { if(res.statusCode !== 200) { var err = new Error('Error communicating with the payment gateway.'); console.log('here'); return callback(err); } return parseResult(result, callback); }); }); request.on('error', function(err) { return callback(err); }); request.write(data); request.end(); }; PayWay.prototype.parse = function(data, callback) { var parsed = qs.parse(data); var result = { approved: parsed['response.summaryCode'] === '0', code: parsed['response.responseCode'], text: parsed['response.text'], receipt: parsed['response.receiptNo'], error: parsed['response.summaryCode'] === '2' }; return callback(null, result); }; exports.createClient = function(options) { return new PayWay(options); };