|
13 | 13 | * |
14 | 14 | */ |
15 | 15 |
|
16 | | -(function(root, factory) { |
| 16 | +(function (root, factory) { |
17 | 17 | if (typeof define === 'function' && define.amd) { |
18 | 18 | // AMD. Register as an anonymous module. |
19 | 19 | define(['superagent', 'querystring'], factory); |
|
27 | 27 | } |
28 | 28 | root.CyberSource.ApiClient = factory(root.superagent, root.querystring); |
29 | 29 | } |
30 | | -}(this, function(superagent, querystring) { |
| 30 | +}(this, function (superagent, querystring) { |
31 | 31 | 'use strict'; |
32 | 32 |
|
33 | 33 | /** |
|
42 | 42 | * @alias module:ApiClient |
43 | 43 | * @class |
44 | 44 | */ |
45 | | - var exports = function() { |
| 45 | + var exports = function () { |
46 | 46 | /** |
47 | 47 | * The base URL against which to resolve every API call's (relative) path. |
48 | 48 | * @type {String} |
|
100 | 100 | * @param param The actual parameter. |
101 | 101 | * @returns {String} The string representation of <code>param</code>. |
102 | 102 | */ |
103 | | - exports.prototype.paramToString = function(param) { |
| 103 | + exports.prototype.paramToString = function (param) { |
104 | 104 | if (param == undefined || param == null) { |
105 | 105 | return ''; |
106 | 106 | } |
|
117 | 117 | * @param {Object} pathParams The parameter values to append. |
118 | 118 | * @returns {String} The encoded path with parameter values substituted. |
119 | 119 | */ |
120 | | - exports.prototype.buildUrl = function(path, pathParams) { |
| 120 | + exports.prototype.buildUrl = function (path, pathParams) { |
121 | 121 | if (!path.match(/^\//)) { |
122 | 122 | path = '/' + path; |
123 | 123 | } |
124 | 124 | var url = this.basePath + path; |
125 | 125 | var _this = this; |
126 | | - url = url.replace(/\{([\w-]+)\}/g, function(fullMatch, key) { |
| 126 | + url = url.replace(/\{([\w-]+)\}/g, function (fullMatch, key) { |
127 | 127 | var value; |
128 | 128 | if (pathParams.hasOwnProperty(key)) { |
129 | 129 | value = _this.paramToString(pathParams[key]); |
|
146 | 146 | * @param {String} contentType The MIME content type to check. |
147 | 147 | * @returns {Boolean} <code>true</code> if <code>contentType</code> represents JSON, otherwise <code>false</code>. |
148 | 148 | */ |
149 | | - exports.prototype.isJsonMime = function(contentType) { |
| 149 | + exports.prototype.isJsonMime = function (contentType) { |
150 | 150 | return Boolean(contentType != null && contentType.match(/^application\/json(;.*)?$/i)); |
151 | 151 | }; |
152 | 152 |
|
|
155 | 155 | * @param {Array.<String>} contentTypes |
156 | 156 | * @returns {String} The chosen content type, preferring JSON. |
157 | 157 | */ |
158 | | - exports.prototype.jsonPreferredMime = function(contentTypes) { |
| 158 | + exports.prototype.jsonPreferredMime = function (contentTypes) { |
159 | 159 | for (var i = 0; i < contentTypes.length; i++) { |
160 | 160 | if (this.isJsonMime(contentTypes[i])) { |
161 | 161 | return contentTypes[i]; |
|
169 | 169 | * @param param The parameter to check. |
170 | 170 | * @returns {Boolean} <code>true</code> if <code>param</code> represents a file. |
171 | 171 | */ |
172 | | - exports.prototype.isFileParam = function(param) { |
| 172 | + exports.prototype.isFileParam = function (param) { |
173 | 173 | // fs.ReadStream in Node.js and Electron (but not in runtime like browserify) |
174 | 174 | if (typeof require === 'function') { |
175 | 175 | var fs; |
176 | 176 | try { |
177 | 177 | fs = require('fs'); |
178 | | - } catch (err) {} |
| 178 | + } catch (err) { } |
179 | 179 | if (fs && fs.ReadStream && param instanceof fs.ReadStream) { |
180 | 180 | return true; |
181 | 181 | } |
|
205 | 205 | * @param {Object.<String, Object>} params The parameters as object properties. |
206 | 206 | * @returns {Object.<String, Object>} normalized parameters. |
207 | 207 | */ |
208 | | - exports.prototype.normalizeParams = function(params) { |
| 208 | + exports.prototype.normalizeParams = function (params) { |
209 | 209 | var newParams = {}; |
210 | 210 | for (var key in params) { |
211 | 211 | if (params.hasOwnProperty(key) && params[key] != undefined && params[key] != null) { |
|
286 | 286 | * @param {Object} request The request object created by a <code>superagent()</code> call. |
287 | 287 | * @param {Array.<String>} authNames An array of authentication method names. |
288 | 288 | */ |
289 | | - exports.prototype.applyAuthToRequest = function(request, authNames) { |
| 289 | + exports.prototype.applyAuthToRequest = function (request, authNames) { |
290 | 290 | var _this = this; |
291 | | - authNames.forEach(function(authName) { |
| 291 | + authNames.forEach(function (authName) { |
292 | 292 | var auth = _this.authentications[authName]; |
293 | 293 | switch (auth.type) { |
294 | 294 | case 'basic': |
|
313 | 313 | break; |
314 | 314 | case 'oauth2': |
315 | 315 | if (auth.accessToken) { |
316 | | - request.set({'Authorization': 'Bearer ' + auth.accessToken}); |
| 316 | + request.set({ 'Authorization': 'Bearer ' + auth.accessToken }); |
317 | 317 | } |
318 | 318 | break; |
319 | 319 | default: |
|
345 | 345 | return exports.convertToType(data, returnType); |
346 | 346 | }; |
347 | 347 |
|
| 348 | + // Code added by Infosys dev team |
| 349 | + |
| 350 | + var AuthenticationSDK = require('cybersource-rest-auth'); |
348 | 351 | /** |
349 | | - * @ghari |
| 352 | + * This method will set the merchantConfig object global |
| 353 | + * |
| 354 | + * @param {Configuration} configObject merchantConfiguration properties. |
350 | 355 | */ |
351 | | - |
352 | | - |
353 | | - |
| 356 | + exports.prototype.setConfiguration = function (configObject) { |
| 357 | + |
| 358 | + this.merchantConfig = new AuthenticationSDK.MerchantConfig(configObject); |
| 359 | + } |
| 360 | + |
354 | 361 | /** |
355 | 362 | * This method is to generate headers for http and jwt authentication. |
356 | 363 | * |
|
360 | 367 | */ |
361 | 368 | exports.prototype.callAuthenticationHeader = function (httpMethod, requestTarget, requestBody, headerParams) { |
362 | 369 |
|
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'); |
367 | 370 | var Constants = AuthenticationSDK.Constants; |
368 | 371 |
|
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); |
378 | 377 |
|
379 | | - if (merchantConfig.getAuthenticationType() === Constants.JWT) { |
| 378 | + if (this.merchantConfig.getAuthenticationType() === Constants.JWT) { |
380 | 379 | token = "Bearer " + token; |
381 | 380 | headerParams['Authorization'] = token; |
382 | 381 | } |
383 | | - else if (merchantConfig.getAuthenticationType() === Constants.HTTP) { |
| 382 | + else if (this.merchantConfig.getAuthenticationType() === Constants.HTTP) { |
384 | 383 | var date = new Date(Date.now()).toUTCString(); |
385 | 384 |
|
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); |
388 | 387 | digest = Constants.SIGNATURE_ALGORITHAM + digest; |
389 | 388 | logger.info(Constants.DIGEST + " : " + digest); |
390 | 389 | headerParams['digest'] = digest; |
| 390 | + logger.info('digest : ' + headerParams['digest']); |
391 | 391 | } |
392 | 392 |
|
393 | | - headerParams['v-c-merchant-id'] = merchantConfig.getMerchantID(); |
| 393 | + headerParams['v-c-merchant-id'] = this.merchantConfig.getMerchantID(); |
394 | 394 | headerParams['date'] = date; |
395 | | - headerParams['host'] = merchantConfig.getRequestHost(); |
| 395 | + headerParams['host'] = this.merchantConfig.getRequestHost(); |
396 | 396 | headerParams['signature'] = token; |
397 | 397 | headerParams['User-Agent'] = "Mozilla/5.0"; |
398 | 398 |
|
399 | | - logger.info("v-c-merchant-id : " + merchantConfig.getMerchantID()); |
| 399 | + logger.info("v-c-merchant-id : " + this.merchantConfig.getMerchantID()); |
400 | 400 | logger.info("date : " + date); |
401 | | - logger.info("host : " + merchantConfig.getRequestHost()); |
| 401 | + logger.info("host : " + this.merchantConfig.getRequestHost()); |
402 | 402 | logger.info("signature : " + token); |
403 | 403 | logger.info("User-Agent : " + headerParams['User-Agent']); |
404 | 404 | logger.info("End > ==============================") |
|
433 | 433 | * @returns {Object} The SuperAgent request object. |
434 | 434 | */ |
435 | 435 | 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) { |
438 | 438 |
|
439 | 439 | var _this = this; |
440 | 440 | var url = this.buildUrl(path, pathParams); |
|
445 | 445 |
|
446 | 446 | // set query parameters |
447 | 447 | if (httpMethod.toUpperCase() === 'GET' && this.cache === false) { |
448 | | - queryParams['_'] = new Date().getTime(); |
| 448 | + queryParams['_'] = new Date().getTime(); |
449 | 449 | } |
450 | 450 | request.query(this.normalizeParams(queryParams)); |
451 | 451 |
|
452 | 452 | /** |
453 | | - * @ghari |
| 453 | + *added by infosys team, to generate requestTarget with pathParam |
454 | 454 | */ |
455 | | - var requestTarget = this.buildRequestTarget(path, pathParams); |
| 455 | + var requestTarget = this.buildRequestTarget(path, pathParams, queryParams); |
456 | 456 |
|
457 | | - if (httpMethod.toUpperCase() === 'POST') { |
| 457 | + if (httpMethod.toUpperCase() === 'POST' || httpMethod === 'PATCH' || httpMethod === 'PUT') { |
458 | 458 | bodyParam = JSON.stringify(bodyParam, null, 0); |
459 | 459 | } |
460 | 460 | headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams); |
|
468 | 468 | var contentType = this.jsonPreferredMime(contentTypes); |
469 | 469 | if (contentType) { |
470 | 470 | // 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') { |
472 | 472 | request.type(contentType); |
473 | 473 | } |
474 | 474 | } else if (!request.header['Content-Type']) { |
475 | 475 | request.type('application/json'); |
476 | 476 | } |
477 | 477 |
|
| 478 | + |
| 479 | + |
478 | 480 | if (contentType === 'application/x-www-form-urlencoded') { |
479 | 481 | request.send(querystring.stringify(this.normalizeParams(formParams))); |
480 | 482 | } else if (contentType == 'multipart/form-data') { |
|
505 | 507 | } |
506 | 508 |
|
507 | 509 | // Attach previously saved cookies, if enabled |
508 | | - if (this.enableCookies){ |
| 510 | + if (this.enableCookies) { |
509 | 511 | if (typeof window === 'undefined') { |
510 | 512 | this.agent.attachCookies(request); |
511 | 513 | } |
|
514 | 516 | } |
515 | 517 | } |
516 | 518 |
|
517 | | - |
518 | | - request.end(function(error, response) { |
| 519 | + request.end(function (error, response) { |
519 | 520 | if (callback) { |
520 | 521 | var data = null; |
521 | 522 | if (!error) { |
522 | 523 | try { |
523 | 524 | data = _this.deserialize(response, returnType); |
524 | | - if (_this.enableCookies && typeof window === 'undefined'){ |
| 525 | + if (_this.enableCookies && typeof window === 'undefined') { |
525 | 526 | _this.agent.saveCookies(response); |
526 | 527 | } |
527 | 528 | } catch (err) { |
|
536 | 537 | }; |
537 | 538 |
|
538 | 539 | /** |
539 | | - * @ghari |
| 540 | + * |
540 | 541 | * Build request target required for the signature generation |
541 | 542 | * @param {String} path |
542 | 543 | * @param {Object} pathParams |
543 | 544 | */ |
544 | | - exports.prototype.buildRequestTarget = function (path, pathParams) { |
| 545 | + exports.prototype.buildRequestTarget = function (path, pathParams, queryParams) { |
545 | 546 | if (!path.match(/^\//)) { |
546 | 547 | path = '/' + path; |
547 | 548 | } |
|
556 | 557 | } |
557 | 558 | return encodeURIComponent(value); |
558 | 559 | }); |
| 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 | + } |
559 | 576 | return requestTarget; |
560 | 577 | }; |
561 | 578 |
|
|
564 | 581 | * @param {String} str The date value as a string. |
565 | 582 | * @returns {Date} The parsed date object. |
566 | 583 | */ |
567 | | - exports.parseDate = function(str) { |
| 584 | + exports.parseDate = function (str) { |
568 | 585 | return new Date(str.replace(/T/i, ' ')); |
569 | 586 | }; |
570 | 587 |
|
|
577 | 594 | * all properties on <code>data<code> will be converted to this type. |
578 | 595 | * @returns An instance of the specified type or null or undefined if data is null or undefined. |
579 | 596 | */ |
580 | | - exports.convertToType = function(data, type) { |
| 597 | + exports.convertToType = function (data, type) { |
581 | 598 | if (data === null || data === undefined) |
582 | 599 | return data |
583 | 600 |
|
|
593 | 610 | case 'Date': |
594 | 611 | return this.parseDate(String(data)); |
595 | 612 | case 'Blob': |
596 | | - return data; |
| 613 | + return data; |
597 | 614 | default: |
598 | 615 | if (type === Object) { |
599 | 616 | // generic object, return directly |
|
604 | 621 | } else if (Array.isArray(type)) { |
605 | 622 | // for array type like: ['String'] |
606 | 623 | var itemType = type[0]; |
607 | | - return data.map(function(item) { |
| 624 | + return data.map(function (item) { |
608 | 625 | return exports.convertToType(item, itemType); |
609 | 626 | }); |
610 | 627 | } else if (typeof type === 'object') { |
|
638 | 655 | * @param data {Object|Array} The REST data. |
639 | 656 | * @param obj {Object|Array} The target object or array. |
640 | 657 | */ |
641 | | - exports.constructFromObject = function(data, obj, itemType) { |
| 658 | + exports.constructFromObject = function (data, obj, itemType) { |
642 | 659 | if (Array.isArray(data)) { |
643 | 660 | for (var i = 0; i < data.length; i++) { |
644 | 661 | if (data.hasOwnProperty(i)) |
|
0 commit comments