Skip to content

Commit 86dbc5a

Browse files
author
Hari
committed
Commiting BT apis
1 parent d7f3e16 commit 86dbc5a

File tree

291 files changed

+15752
-6110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

291 files changed

+15752
-6110
lines changed

src/ApiClient.js

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*/
1515

16-
(function(root, factory) {
16+
(function (root, factory) {
1717
if (typeof define === 'function' && define.amd) {
1818
// AMD. Register as an anonymous module.
1919
define(['superagent', 'querystring'], factory);
@@ -27,7 +27,7 @@
2727
}
2828
root.CyberSource.ApiClient = factory(root.superagent, root.querystring);
2929
}
30-
}(this, function(superagent, querystring) {
30+
}(this, function (superagent, querystring) {
3131
'use strict';
3232

3333
/**
@@ -42,7 +42,7 @@
4242
* @alias module:ApiClient
4343
* @class
4444
*/
45-
var exports = function() {
45+
var exports = function () {
4646
/**
4747
* The base URL against which to resolve every API call's (relative) path.
4848
* @type {String}
@@ -100,7 +100,7 @@
100100
* @param param The actual parameter.
101101
* @returns {String} The string representation of <code>param</code>.
102102
*/
103-
exports.prototype.paramToString = function(param) {
103+
exports.prototype.paramToString = function (param) {
104104
if (param == undefined || param == null) {
105105
return '';
106106
}
@@ -117,13 +117,13 @@
117117
* @param {Object} pathParams The parameter values to append.
118118
* @returns {String} The encoded path with parameter values substituted.
119119
*/
120-
exports.prototype.buildUrl = function(path, pathParams) {
120+
exports.prototype.buildUrl = function (path, pathParams) {
121121
if (!path.match(/^\//)) {
122122
path = '/' + path;
123123
}
124124
var url = this.basePath + path;
125125
var _this = this;
126-
url = url.replace(/\{([\w-]+)\}/g, function(fullMatch, key) {
126+
url = url.replace(/\{([\w-]+)\}/g, function (fullMatch, key) {
127127
var value;
128128
if (pathParams.hasOwnProperty(key)) {
129129
value = _this.paramToString(pathParams[key]);
@@ -146,7 +146,7 @@
146146
* @param {String} contentType The MIME content type to check.
147147
* @returns {Boolean} <code>true</code> if <code>contentType</code> represents JSON, otherwise <code>false</code>.
148148
*/
149-
exports.prototype.isJsonMime = function(contentType) {
149+
exports.prototype.isJsonMime = function (contentType) {
150150
return Boolean(contentType != null && contentType.match(/^application\/json(;.*)?$/i));
151151
};
152152

@@ -155,7 +155,7 @@
155155
* @param {Array.<String>} contentTypes
156156
* @returns {String} The chosen content type, preferring JSON.
157157
*/
158-
exports.prototype.jsonPreferredMime = function(contentTypes) {
158+
exports.prototype.jsonPreferredMime = function (contentTypes) {
159159
for (var i = 0; i < contentTypes.length; i++) {
160160
if (this.isJsonMime(contentTypes[i])) {
161161
return contentTypes[i];
@@ -169,13 +169,13 @@
169169
* @param param The parameter to check.
170170
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
171171
*/
172-
exports.prototype.isFileParam = function(param) {
172+
exports.prototype.isFileParam = function (param) {
173173
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
174174
if (typeof require === 'function') {
175175
var fs;
176176
try {
177177
fs = require('fs');
178-
} catch (err) {}
178+
} catch (err) { }
179179
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
180180
return true;
181181
}
@@ -205,7 +205,7 @@
205205
* @param {Object.<String, Object>} params The parameters as object properties.
206206
* @returns {Object.<String, Object>} normalized parameters.
207207
*/
208-
exports.prototype.normalizeParams = function(params) {
208+
exports.prototype.normalizeParams = function (params) {
209209
var newParams = {};
210210
for (var key in params) {
211211
if (params.hasOwnProperty(key) && params[key] != undefined && params[key] != null) {
@@ -286,9 +286,9 @@
286286
* @param {Object} request The request object created by a <code>superagent()</code> call.
287287
* @param {Array.<String>} authNames An array of authentication method names.
288288
*/
289-
exports.prototype.applyAuthToRequest = function(request, authNames) {
289+
exports.prototype.applyAuthToRequest = function (request, authNames) {
290290
var _this = this;
291-
authNames.forEach(function(authName) {
291+
authNames.forEach(function (authName) {
292292
var auth = _this.authentications[authName];
293293
switch (auth.type) {
294294
case 'basic':
@@ -313,7 +313,7 @@
313313
break;
314314
case 'oauth2':
315315
if (auth.accessToken) {
316-
request.set({'Authorization': 'Bearer ' + auth.accessToken});
316+
request.set({ 'Authorization': 'Bearer ' + auth.accessToken });
317317
}
318318
break;
319319
default:
@@ -345,12 +345,19 @@
345345
return exports.convertToType(data, returnType);
346346
};
347347

348+
// Code added by Infosys dev team
349+
350+
var AuthenticationSDK = require('cybersource-rest-auth');
348351
/**
349-
* @ghari
352+
* This method will set the merchantConfig object global
353+
*
354+
* @param {Configuration} configObject merchantConfiguration properties.
350355
*/
351-
352-
353-
356+
exports.prototype.setConfiguration = function (configObject) {
357+
358+
this.merchantConfig = new AuthenticationSDK.MerchantConfig(configObject);
359+
}
360+
354361
/**
355362
* This method is to generate headers for http and jwt authentication.
356363
*
@@ -360,45 +367,38 @@
360367
*/
361368
exports.prototype.callAuthenticationHeader = function (httpMethod, requestTarget, requestBody, headerParams) {
362369

363-
var path = require('path');
364-
var filePath = path.resolve('Data/Configuration.js');
365-
var Configuration = require(filePath);
366-
var AuthenticationSDK = require('cybersource-rest-auth');
367370
var Constants = AuthenticationSDK.Constants;
368371

369-
// reading cybs.json from Configuration module
370-
var configObject = new Configuration();
371-
var merchantConfig = new AuthenticationSDK.MerchantConfig(configObject);
372-
373-
merchantConfig.setRequestTarget(requestTarget);
374-
merchantConfig.setRequestType(httpMethod)
375-
merchantConfig.setRequestJsonData(requestBody);
376-
var logger = AuthenticationSDK.Logger.getLogger(merchantConfig);
377-
var token = AuthenticationSDK.Authorization.getToken(merchantConfig, logger);
372+
this.merchantConfig.setRequestTarget(requestTarget);
373+
this.merchantConfig.setRequestType(httpMethod)
374+
this.merchantConfig.setRequestJsonData(requestBody);
375+
var logger = AuthenticationSDK.Logger.getLogger(this.merchantConfig);
376+
var token = AuthenticationSDK.Authorization.getToken(this.merchantConfig, logger);
378377

379-
if (merchantConfig.getAuthenticationType() === Constants.JWT) {
378+
if (this.merchantConfig.getAuthenticationType() === Constants.JWT) {
380379
token = "Bearer " + token;
381380
headerParams['Authorization'] = token;
382381
}
383-
else if (merchantConfig.getAuthenticationType() === Constants.HTTP) {
382+
else if (this.merchantConfig.getAuthenticationType() === Constants.HTTP) {
384383
var date = new Date(Date.now()).toUTCString();
385384

386-
if (httpMethod === "POST") {
387-
var digest = AuthenticationSDK.PayloadDigest.generateDigest(merchantConfig, logger);
385+
if (httpMethod === "POST" || httpMethod === "PATCH" || httpMethod === "PUT") {
386+
var digest = AuthenticationSDK.PayloadDigest.generateDigest(this.merchantConfig, logger);
388387
digest = Constants.SIGNATURE_ALGORITHAM + digest;
389388
logger.info(Constants.DIGEST + " : " + digest);
390389
headerParams['digest'] = digest;
390+
logger.info('digest : ' + headerParams['digest']);
391391
}
392392

393-
headerParams['v-c-merchant-id'] = merchantConfig.getMerchantID();
393+
headerParams['v-c-merchant-id'] = this.merchantConfig.getMerchantID();
394394
headerParams['date'] = date;
395-
headerParams['host'] = merchantConfig.getRequestHost();
395+
headerParams['host'] = this.merchantConfig.getRequestHost();
396396
headerParams['signature'] = token;
397397
headerParams['User-Agent'] = "Mozilla/5.0";
398398

399-
logger.info("v-c-merchant-id : " + merchantConfig.getMerchantID());
399+
logger.info("v-c-merchant-id : " + this.merchantConfig.getMerchantID());
400400
logger.info("date : " + date);
401-
logger.info("host : " + merchantConfig.getRequestHost());
401+
logger.info("host : " + this.merchantConfig.getRequestHost());
402402
logger.info("signature : " + token);
403403
logger.info("User-Agent : " + headerParams['User-Agent']);
404404
logger.info("End > ==============================")
@@ -433,8 +433,8 @@
433433
* @returns {Object} The SuperAgent request object.
434434
*/
435435
exports.prototype.callApi = function callApi(path, httpMethod, pathParams,
436-
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
437-
returnType, callback) {
436+
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
437+
returnType, callback) {
438438

439439
var _this = this;
440440
var url = this.buildUrl(path, pathParams);
@@ -445,16 +445,16 @@
445445

446446
// set query parameters
447447
if (httpMethod.toUpperCase() === 'GET' && this.cache === false) {
448-
queryParams['_'] = new Date().getTime();
448+
queryParams['_'] = new Date().getTime();
449449
}
450450
request.query(this.normalizeParams(queryParams));
451451

452452
/**
453-
* @ghari
453+
*added by infosys team, to generate requestTarget with pathParam
454454
*/
455-
var requestTarget = this.buildRequestTarget(path, pathParams);
455+
var requestTarget = this.buildRequestTarget(path, pathParams, queryParams);
456456

457-
if (httpMethod.toUpperCase() === 'POST') {
457+
if (httpMethod.toUpperCase() === 'POST' || httpMethod === 'PATCH' || httpMethod === 'PUT') {
458458
bodyParam = JSON.stringify(bodyParam, null, 0);
459459
}
460460
headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams);
@@ -468,13 +468,15 @@
468468
var contentType = this.jsonPreferredMime(contentTypes);
469469
if (contentType) {
470470
// Issue with superagent and multipart/form-data (https://github.com/visionmedia/superagent/issues/746)
471-
if(contentType != 'multipart/form-data') {
471+
if (contentType != 'multipart/form-data') {
472472
request.type(contentType);
473473
}
474474
} else if (!request.header['Content-Type']) {
475475
request.type('application/json');
476476
}
477477

478+
479+
478480
if (contentType === 'application/x-www-form-urlencoded') {
479481
request.send(querystring.stringify(this.normalizeParams(formParams)));
480482
} else if (contentType == 'multipart/form-data') {
@@ -505,7 +507,7 @@
505507
}
506508

507509
// Attach previously saved cookies, if enabled
508-
if (this.enableCookies){
510+
if (this.enableCookies) {
509511
if (typeof window === 'undefined') {
510512
this.agent.attachCookies(request);
511513
}
@@ -514,14 +516,13 @@
514516
}
515517
}
516518

517-
518-
request.end(function(error, response) {
519+
request.end(function (error, response) {
519520
if (callback) {
520521
var data = null;
521522
if (!error) {
522523
try {
523524
data = _this.deserialize(response, returnType);
524-
if (_this.enableCookies && typeof window === 'undefined'){
525+
if (_this.enableCookies && typeof window === 'undefined') {
525526
_this.agent.saveCookies(response);
526527
}
527528
} catch (err) {
@@ -536,12 +537,12 @@
536537
};
537538

538539
/**
539-
* @ghari
540+
*
540541
* Build request target required for the signature generation
541542
* @param {String} path
542543
* @param {Object} pathParams
543544
*/
544-
exports.prototype.buildRequestTarget = function (path, pathParams) {
545+
exports.prototype.buildRequestTarget = function (path, pathParams, queryParams) {
545546
if (!path.match(/^\//)) {
546547
path = '/' + path;
547548
}
@@ -556,6 +557,22 @@
556557
}
557558
return encodeURIComponent(value);
558559
});
560+
561+
// added by infosys team, to generate requestTarget with queryParams
562+
if (Object.keys(queryParams).length !== 0) {
563+
var queryFlag = false;
564+
var queryArray = [];
565+
Object.keys(queryParams).forEach(function (prop) {
566+
var val = queryParams[prop];
567+
568+
if (val !== undefined) {
569+
queryArray[prop] = val;
570+
queryFlag = true;
571+
}
572+
});
573+
if (queryFlag)
574+
requestTarget = requestTarget + '?' + querystring.stringify(queryArray);
575+
}
559576
return requestTarget;
560577
};
561578

@@ -564,7 +581,7 @@
564581
* @param {String} str The date value as a string.
565582
* @returns {Date} The parsed date object.
566583
*/
567-
exports.parseDate = function(str) {
584+
exports.parseDate = function (str) {
568585
return new Date(str.replace(/T/i, ' '));
569586
};
570587

@@ -577,7 +594,7 @@
577594
* all properties on <code>data<code> will be converted to this type.
578595
* @returns An instance of the specified type or null or undefined if data is null or undefined.
579596
*/
580-
exports.convertToType = function(data, type) {
597+
exports.convertToType = function (data, type) {
581598
if (data === null || data === undefined)
582599
return data
583600

@@ -593,7 +610,7 @@
593610
case 'Date':
594611
return this.parseDate(String(data));
595612
case 'Blob':
596-
return data;
613+
return data;
597614
default:
598615
if (type === Object) {
599616
// generic object, return directly
@@ -604,7 +621,7 @@
604621
} else if (Array.isArray(type)) {
605622
// for array type like: ['String']
606623
var itemType = type[0];
607-
return data.map(function(item) {
624+
return data.map(function (item) {
608625
return exports.convertToType(item, itemType);
609626
});
610627
} else if (typeof type === 'object') {
@@ -638,7 +655,7 @@
638655
* @param data {Object|Array} The REST data.
639656
* @param obj {Object|Array} The target object or array.
640657
*/
641-
exports.constructFromObject = function(data, obj, itemType) {
658+
exports.constructFromObject = function (data, obj, itemType) {
642659
if (Array.isArray(data)) {
643660
for (var i = 0; i < data.length; i++) {
644661
if (data.hasOwnProperty(i))

0 commit comments

Comments
 (0)