Guest User

Untitled

a guest
Dec 8th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. "use strict";
  2.  
  3. var port = 2345;
  4. var targetServer = 'https://aserver.com';
  5.  
  6. var soap = require('soap');
  7. var http = require('http');
  8. var request = require("request");
  9.  
  10. var myService = {
  11. Pfs: {
  12. PfsSoap: {
  13. abc: function(args, callback, header, req) {
  14. var options = { method: 'POST',
  15. url: targetServer + '/api/v2/login',
  16. headers:
  17. { 'cache-control': 'no-cache',
  18. 'Content-Type': 'application/json' },
  19. body: { username: 'username', password: 'password' },
  20. json: true };
  21.  
  22. request(options, function (error, response, body) {
  23. if (error) throw new Error(error);
  24.  
  25. callback({
  26. "Result": {
  27. "tns:MsgHeader": {
  28. "tns:MessageType": "Response",
  29. "tns:ContentVersion": "1.0"
  30. },
  31. "tns:ResponseId": args.requestId//,
  32. // "ENVELOPE-RECEIVED": args
  33. }
  34. });
  35.  
  36.  
  37. });
  38. },
  39. xyz: function(args, callback, header, req) {
  40. /****
  41. * The outer function calls the server with login credentials
  42. **/
  43. var options = { method: 'POST',
  44. url: targetServer + '/api/v2/login',
  45. headers:
  46. { 'cache-control': 'no-cache',
  47. 'Content-Type': 'application/json' },
  48. body: {
  49. username: header.AuthenticationHeader.SenderId,
  50. password: header.AuthenticationHeader.Password
  51. },
  52. json: true };
  53.  
  54. request(options, function (error, response, outerFunctionReturn) {
  55. /****
  56. * Handle authorisation failure here. Error is not for bad credentials.
  57. **/
  58. if (error) {
  59. throw new Error(error);
  60. }
  61. if(!outerFunctionReturn.authToken){ // if this doesn't exist then the credentails failed
  62. callback({
  63. "Result": {
  64. "tns:MsgHeader": {
  65. "tns:MessageType": "Response",
  66. "tns:ContentVersion": "1.0"
  67. },
  68. "tns:ResponseStatus": {
  69. "tns:ResponseResult": "Failure",
  70. "tns:ResponseCode": "401",
  71. "tns:ResponseMessage": ""
  72. },
  73. "tns:ResponseId": args.requestId
  74. }
  75. });
  76. } else {
  77. /****
  78. * The inner function calls the server with the bearer token to test order
  79. **/
  80. var options = { method: 'PUT',
  81. url: targetServer + '/api//v2/orders',
  82. headers:
  83. {
  84. 'cache-control': 'no-cache',
  85. Authorization: outerFunctionReturn.authToken,
  86. 'Content-Type': 'application/json' },
  87. body:
  88. { customerEmail: 'someone@gmail.com' },
  89. json: true };
  90.  
  91. request(options, function (error, response, innerFunctionReturn) {
  92. if (error) {
  93. throw new Error(error);
  94. }
  95. /****
  96. * At this point the login is successful and we have successfully fetched the needed data.
  97. * This callback builds the return SOAP envelope.
  98. **/
  99. callback({
  100. "LoadCardResult": {
  101. "tns:MsgHeader": {
  102. "tns:MessageType": "LoadCardResponse",
  103. "tns:ContentVersion": "1.0"
  104. },
  105. "tns:ResponseStatus": {
  106. "tns:ResponseResult": "Success",
  107. "tns:ResponseCode": innerFunctionReturn.barcode,
  108. "tns:ResponseMessage": ""
  109. },
  110. "tns:CardValue": {
  111. "tns:Amount": "1000",
  112. "tns:CurrencyCode": "EUR",
  113. "tns:EndBalance": "0"
  114. },
  115. "tns:ResponseId": outerFunctionReturn.vendorId/*args.requestId*/
  116. }
  117. });
  118. });
  119. }//END innerFunctionReturn
  120. });
  121. }
  122. }
  123. }
  124. };
  125.  
  126. var xml = require('fs').readFileSync('Service.wsdl','utf8');
  127. var server = http.createServer(function(request,response) {
  128. console.log('Request ' + request.url);
  129. response.end("404: Not Found: " + request.url);
  130. });
  131. console.log('Started server on port ' + port);
  132. server.listen(port);
  133.  
  134. soap.listen(server,'/wsdl',myService, xml);
Add Comment
Please, Sign In to add comment