|
93 | 93 | this.agent = new superagent.agent(); |
94 | 94 | } |
95 | 95 |
|
| 96 | + /** |
| 97 | + * The filepath where reports are downloaded |
| 98 | + */ |
| 99 | + this.downloadFilePath = ''; |
96 | 100 | }; |
97 | 101 |
|
98 | 102 | /** |
|
356 | 360 | exports.prototype.setConfiguration = function (configObject) { |
357 | 361 |
|
358 | 362 | this.merchantConfig = new AuthenticationSDK.MerchantConfig(configObject); |
| 363 | + this.constants = AuthenticationSDK.Constants; |
| 364 | + this.logger = AuthenticationSDK.Logger.getLogger(this.merchantConfig); |
359 | 365 | } |
360 | 366 |
|
361 | 367 | /** |
|
367 | 373 | */ |
368 | 374 | exports.prototype.callAuthenticationHeader = function (httpMethod, requestTarget, requestBody, headerParams) { |
369 | 375 |
|
370 | | - var Constants = AuthenticationSDK.Constants; |
371 | | - |
372 | 376 | this.merchantConfig.setRequestTarget(requestTarget); |
373 | 377 | this.merchantConfig.setRequestType(httpMethod) |
374 | 378 | this.merchantConfig.setRequestJsonData(requestBody); |
375 | | - var logger = AuthenticationSDK.Logger.getLogger(this.merchantConfig); |
376 | | - var token = AuthenticationSDK.Authorization.getToken(this.merchantConfig, logger); |
| 379 | + |
| 380 | + this.logger.info("Authentication Type : " + this.merchantConfig.getAuthenticationType()); |
| 381 | + this.logger.info(this.constants.REQUEST_TYPE + ' : ' + httpMethod.toUpperCase()); |
377 | 382 |
|
378 | | - if (this.merchantConfig.getAuthenticationType() === Constants.JWT) { |
| 383 | + var token = AuthenticationSDK.Authorization.getToken(this.merchantConfig, this.logger); |
| 384 | + |
| 385 | + if (this.merchantConfig.getAuthenticationType() === this.constants.JWT) { |
379 | 386 | token = "Bearer " + token; |
380 | 387 | headerParams['Authorization'] = token; |
| 388 | + this.logger.info(this.constants.AUTHORIZATION + ' : ' + token); |
381 | 389 | } |
382 | | - else if (this.merchantConfig.getAuthenticationType() === Constants.HTTP) { |
| 390 | + else if (this.merchantConfig.getAuthenticationType() === this.constants.HTTP) { |
383 | 391 | var date = new Date(Date.now()).toUTCString(); |
384 | 392 |
|
385 | | - if (httpMethod === "POST" || httpMethod === "PATCH" || httpMethod === "PUT") { |
386 | | - var digest = AuthenticationSDK.PayloadDigest.generateDigest(this.merchantConfig, logger); |
387 | | - digest = Constants.SIGNATURE_ALGORITHAM + digest; |
388 | | - logger.info(Constants.DIGEST + " : " + digest); |
| 393 | + if (httpMethod.toLowerCase() === this.constants.POST |
| 394 | + || httpMethod.toLowerCase() === this.constants.PATCH |
| 395 | + || httpMethod.toLowerCase() === this.constants.PUT) { |
| 396 | + var digest = AuthenticationSDK.PayloadDigest.generateDigest(this.merchantConfig, this.logger); |
| 397 | + digest = this.constants.SIGNATURE_ALGORITHAM + digest; |
| 398 | + this.logger.info(this.constants.DIGEST + " : " + digest); |
389 | 399 | headerParams['digest'] = digest; |
390 | | - logger.info('digest : ' + headerParams['digest']); |
391 | 400 | } |
392 | 401 |
|
393 | 402 | headerParams['v-c-merchant-id'] = this.merchantConfig.getMerchantID(); |
394 | 403 | headerParams['date'] = date; |
395 | 404 | headerParams['host'] = this.merchantConfig.getRequestHost(); |
396 | 405 | headerParams['signature'] = token; |
397 | | - headerParams['User-Agent'] = "Mozilla/5.0"; |
398 | | - |
399 | | - logger.info("v-c-merchant-id : " + this.merchantConfig.getMerchantID()); |
400 | | - logger.info("date : " + date); |
401 | | - logger.info("host : " + this.merchantConfig.getRequestHost()); |
402 | | - logger.info("signature : " + token); |
403 | | - logger.info("User-Agent : " + headerParams['User-Agent']); |
404 | | - logger.info("End > ==============================") |
| 406 | + headerParams['User-Agent'] = this.constants.USER_AGENT_VALUE; |
| 407 | + |
| 408 | + this.logger.info("v-c-merchant-id : " + this.merchantConfig.getMerchantID()); |
| 409 | + this.logger.info("date : " + date); |
| 410 | + this.logger.info("host : " + this.merchantConfig.getRequestHost()); |
| 411 | + this.logger.info("signature : " + token); |
| 412 | + this.logger.info("User-Agent : " + headerParams['User-Agent']); |
| 413 | + this.logger.info(this.constants.END_TRANSACTION); |
405 | 414 | } |
406 | 415 |
|
407 | 416 | return headerParams; |
|
444 | 453 | this.applyAuthToRequest(request, authNames); |
445 | 454 |
|
446 | 455 | // set query parameters |
447 | | - if (httpMethod.toUpperCase() === 'GET' && this.cache === false) { |
| 456 | + if (httpMethod.toLowerCase() === this.constants.GET && this.cache === false) { |
448 | 457 | queryParams['_'] = new Date().getTime(); |
449 | 458 | } |
450 | 459 | request.query(this.normalizeParams(queryParams)); |
|
454 | 463 | */ |
455 | 464 | var requestTarget = this.buildRequestTarget(path, pathParams, queryParams); |
456 | 465 |
|
457 | | - if (httpMethod.toUpperCase() === 'POST' || httpMethod === 'PATCH' || httpMethod === 'PUT') { |
| 466 | + if (httpMethod.toLowerCase() === this.constants.POST |
| 467 | + || httpMethod.toLowerCase() === this.constants.PATCH |
| 468 | + || httpMethod.toLowerCase() === this.constants.PUT) { |
458 | 469 | bodyParam = JSON.stringify(bodyParam, null, 0); |
459 | 470 | } |
460 | 471 | headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams); |
|
475 | 486 | request.type('application/json'); |
476 | 487 | } |
477 | 488 |
|
478 | | - |
479 | | - |
480 | 489 | if (contentType === 'application/x-www-form-urlencoded') { |
481 | 490 | request.send(querystring.stringify(this.normalizeParams(formParams))); |
482 | 491 | } else if (contentType == 'multipart/form-data') { |
|
501 | 510 | /* Code for downloading file from stream */ |
502 | 511 | if (accept === 'application/xml') { |
503 | 512 | var fs = require('fs'); |
504 | | - var path = require('path'); |
505 | | - var fileName; |
506 | | - if (queryParams['reportName']) |
507 | | - fileName = queryParams['reportName'] + '.xml'; |
508 | | - else |
509 | | - fileName = "FileIdentifier.csv"; |
510 | | - var filePath = path.join(this.merchantConfig.getKeysDirectory(), fileName); |
511 | | - var stream = fs.createWriteStream(path.resolve(filePath)); |
| 513 | + var stream = fs.createWriteStream(this.downloadFilePath); |
512 | 514 | request.send().pipe(stream); |
513 | 515 | request._endCalled = false; |
514 | 516 | } |
|
551 | 553 | }; |
552 | 554 |
|
553 | 555 | /** |
554 | | - * @ghari |
555 | 556 | * Build request target required for the signature generation |
556 | 557 | * @param {String} path |
557 | 558 | * @param {Object} pathParams |
|
0 commit comments