In my previouse blog Use nodejs soap to block supplier invoice by call soap api in s/4 hana cloud,we can payment block supplier invoice with local application. Today I want to blog down developing and deploying a rest api application on BTP Cloud foundry to realize the same function.On BTP,we need to use destination to call soap api in s/4 hana cloud . To to this , we need to replace the default httpClient used in soap client library .
For developing and deploying MTA application , please refer to the blog. The folowing is the how is the MTA application look like .
We need to replace the httpClient used SOAP client with sap-cf-axios to utilize destination . The following is the js code .
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// const cds = require('@sap/cds');
const core = require('@sap-cloud-sdk/core')
const short = require('short-uuid');
const uuid = require('uuid');
const SapCfAxios = require('sap-cf-axios').default;
const { retrieveJwt } = require('@sap-cloud-sdk/core');
const { desc } = require('@sap-cloud-sdk/core');
url = '/sap/bc/srt/scs_ext/sap/journalentrybulkchangerequest_';
const xssec = require('@sap/xssec');
const passport = require('passport');
const soap = require('soap');
const xsenv = require('@sap/xsenv');
xsenv.loadEnv();
const services = xsenv.getServices({
uaa: { tag: 'xsuaa' }
});
passport.use('JWT', new xssec.JWTStrategy(services.uaa));
app.use(passport.initialize());
app.use(passport.authenticate('JWT', {
session: false
}));
app.use(bodyParser.json());
var invoiceblock = function(req,res){
if (req.authInfo.checkScope('$XSAPPNAME.User')) {
const destinationName = "O5P";
const axios = SapCfAxios(destinationName);
var httpClient1 = {
request: function (url, data, callback, exheaders, exoptions) {
axios({
method: 'POST',
url: url,
data: data,
headers: exheaders
}).then((result) => {
callback(null, result, result.data);
}).catch((e) => {
callback(e);
});
}
}
var options = {
httpClient:httpClient1,
forceSoap12Headers: true
};
soap.createClient('./external/JOURNALENTRYBULKCHANGEREQUEST_.wsdl',options,function(err,client){
if(err){
console.log('soap client creation failed');
console.log(err);
res.status(500).send( err.toString())}
else{
client.setEndpoint(url);
var sid =short.generate();
var id = uuid.v1();
var date1 =new Date().toISOString();
var args = {'MessageHeader':{'ID':sid,'UUID':id,'CreationDateTime':date1},'JournalEntryHeader':{'MessageHeader':{'ID':sid,'CreationDateTime':date1},'HeaderKey':{'AccountingDocument':'5100000050','CompanyCode':'1310','FiscalYear':'2022'},'DocumentHeaderTextChange':{'DocumentHeaderText':'hello1','FieldValueChangeIsRequested':true}},'JournalEntryDebtorCreditorItem':{'MessageHeader':{'ID':sid,'CreationDateTime':date1},'ItemKey':{'AccountingDocument':'5100000050','CompanyCode':'1310','FiscalYear':'2022','AccountingDocumentItemID':'1'},'PaymentBlockingReasonCodeChange':{'PaymentBlockingReasonCode':'A','FieldValueChangeIsRequested':true}}};
client.addSoapHeader({MessageID:id},'http://www.w3.org/2005/08/addressing','wsa','http://www.w3.org/2005/08/addressing');
client.JournalEntryBulkChangeRequest_In(args,function(err,result){
if(err){
console.log('soap call failed');
console.log(err);
res.status(500).send(err.toString());}else{res.status(200).send('Journal Entry sent successfully!');}
});}
});
} else {
res.status(403).send('Forbidden');}}
app.get('/srv/grir',invoiceblock);
app.get('/srv', function (req, res) {
if (req.authInfo.checkScope('$XSAPPNAME.User')) {
res.status(200).send('mtasoap2');
} else {
res.status(403).send('Forbidden');
}
});
const port = process.env.PORT || 5001;
app.listen(port, function () {
console.info('Listening on http://localhost:' + port);
});
The following is the code for package.json
{
"name": "mtasoap2-srv",
"engines": {
"node": "14.x"
},
"dependencies": {
"@sap-cloud-sdk/core": "1.54.2",
"@sap/cloud-sdk-vdm-sales-order-service": "2.0.0",
"@sap/xsenv": "3.2.2",
"@sap/xssec": "3.2.13",
"body-parser": "1.20.0",
"express": "4.18.1",
"passport": "0.6.0",
"sap-cf-axios": "^0.3.5",
"short-uuid": "^4.2.0",
"soap": "^0.43.0",
"uuid": "^8.3.2"
},
"scripts": {
"start": "node server.js"
}
}
The following is the testing effect :