node-ccavenue
Version:
Node Module for integrating CCAvenue Payment gateway in Node.js
1 lines • 2.79 kB
JavaScript
;Object.defineProperty(exports,'__esModule',{value:!0});var _slicedToArray=function(){function a(a,b){var c=[],d=!0,e=!1,f=void 0;try{for(var g,h=a[Symbol.iterator]();!(d=(g=h.next()).done)&&(c.push(g.value),!(b&&c.length===b));d=!0);}catch(a){e=!0,f=a}finally{try{!d&&h['return']&&h['return']()}finally{if(e)throw f}}return c}return function(b,c){if(Array.isArray(b))return b;if(Symbol.iterator in Object(b))return a(b,c);throw new TypeError('Invalid attempt to destructure non-iterable instance')}}(),_createClass=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,'value'in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}var _require=require('crypto'),createHash=_require.createHash,createCipheriv=_require.createCipheriv,createDecipheriv=_require.createDecipheriv,initOptions={},Configure=function(){function a(b){_classCallCheck(this,a),initOptions=b||{}}return _createClass(a,[{key:'validate',value:function validate(a){return!!(initOptions&&initOptions[a])}},{key:'throwError',value:function throwError(a){throw new Error(a+' is required to perform this action')}},{key:'encrypt',value:function encrypt(a){if(this.validate('working_key')&&a){var b=initOptions,c=b.working_key,d=createHash('md5');d.update(c);var e=d.digest(),f=createCipheriv('aes-128-cbc',e,'\0\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F'),g=f.update(a,'utf8','hex');return g+=f.final('hex'),g}return a?(this.throwError('Working Key'),!1):(this.throwError('Plain text'),!1)}},{key:'decrypt',value:function decrypt(a){if(this.validate('working_key')&&a){var b=initOptions,c=b.working_key,d=createHash('md5');d.update(c);var e=d.digest(),f=createDecipheriv('aes-128-cbc',e,'\0\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F'),g=f.update(a,'hex','utf8');return g+=f.final('utf8'),g}return a?(this.throwError('Working Key'),!1):(this.throwError('Encrypted text'),!1)}},{key:'redirectResponseToJson',value:function redirectResponseToJson(a){if(a){var b=this.decrypt(a),c=b.split('&'),d=JSON.stringify(c),e=d.replace(/['"]+/g,''),f=e.replace(/[[\]]/g,'');return f.split(',').reduce(function(a,b){return b=b.split('='),a[b[0]]=b[1],a},{})}this.throwError('CCAvenue encrypted response')}},{key:'getEncryptedOrder',value:function getEncryptedOrder(a){if(this.validate('merchant_id')&&a){var b='merchant_id='+initOptions.merchant_id;return b+=Object.entries(a).map(function(a){var b=_slicedToArray(a,2),c=b[0],d=b[1];return'&'+c+'='+d}).join(''),this.encrypt(b)}a?this.throwError('Merchant ID'):this.throwError('Order Params')}}]),a}();exports.Configure=Configure;