Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- var port = 2345;
- var targetServer = 'https://aserver.com';
- var soap = require('soap');
- var http = require('http');
- var request = require("request");
- var myService = {
- Pfs: {
- PfsSoap: {
- abc: function(args, callback, header, req) {
- var options = { method: 'POST',
- url: targetServer + '/api/v2/login',
- headers:
- { 'cache-control': 'no-cache',
- 'Content-Type': 'application/json' },
- body: { username: 'username', password: 'password' },
- json: true };
- request(options, function (error, response, body) {
- if (error) throw new Error(error);
- callback({
- "Result": {
- "tns:MsgHeader": {
- "tns:MessageType": "Response",
- "tns:ContentVersion": "1.0"
- },
- "tns:ResponseId": args.requestId//,
- // "ENVELOPE-RECEIVED": args
- }
- });
- });
- },
- xyz: function(args, callback, header, req) {
- /****
- * The outer function calls the server with login credentials
- **/
- var options = { method: 'POST',
- url: targetServer + '/api/v2/login',
- headers:
- { 'cache-control': 'no-cache',
- 'Content-Type': 'application/json' },
- body: {
- username: header.AuthenticationHeader.SenderId,
- password: header.AuthenticationHeader.Password
- },
- json: true };
- request(options, function (error, response, outerFunctionReturn) {
- /****
- * Handle authorisation failure here. Error is not for bad credentials.
- **/
- if (error) {
- throw new Error(error);
- }
- if(!outerFunctionReturn.authToken){ // if this doesn't exist then the credentails failed
- callback({
- "Result": {
- "tns:MsgHeader": {
- "tns:MessageType": "Response",
- "tns:ContentVersion": "1.0"
- },
- "tns:ResponseStatus": {
- "tns:ResponseResult": "Failure",
- "tns:ResponseCode": "401",
- "tns:ResponseMessage": ""
- },
- "tns:ResponseId": args.requestId
- }
- });
- } else {
- /****
- * The inner function calls the server with the bearer token to test order
- **/
- var options = { method: 'PUT',
- url: targetServer + '/api//v2/orders',
- headers:
- {
- 'cache-control': 'no-cache',
- Authorization: outerFunctionReturn.authToken,
- 'Content-Type': 'application/json' },
- body:
- { customerEmail: 'someone@gmail.com' },
- json: true };
- request(options, function (error, response, innerFunctionReturn) {
- if (error) {
- throw new Error(error);
- }
- /****
- * At this point the login is successful and we have successfully fetched the needed data.
- * This callback builds the return SOAP envelope.
- **/
- callback({
- "LoadCardResult": {
- "tns:MsgHeader": {
- "tns:MessageType": "LoadCardResponse",
- "tns:ContentVersion": "1.0"
- },
- "tns:ResponseStatus": {
- "tns:ResponseResult": "Success",
- "tns:ResponseCode": innerFunctionReturn.barcode,
- "tns:ResponseMessage": ""
- },
- "tns:CardValue": {
- "tns:Amount": "1000",
- "tns:CurrencyCode": "EUR",
- "tns:EndBalance": "0"
- },
- "tns:ResponseId": outerFunctionReturn.vendorId/*args.requestId*/
- }
- });
- });
- }//END innerFunctionReturn
- });
- }
- }
- }
- };
- var xml = require('fs').readFileSync('Service.wsdl','utf8');
- var server = http.createServer(function(request,response) {
- console.log('Request ' + request.url);
- response.end("404: Not Found: " + request.url);
- });
- console.log('Started server on port ' + port);
- server.listen(port);
- soap.listen(server,'/wsdl',myService, xml);
Add Comment
Please, Sign In to add comment