Palk Quick Example [Node.js]

Example on how to post the salaries and withholdings or adding amounts for base salaries:

var CryptoJS = require('crypto-js');
var request = require('request');

var reqJson = {
  ContractCode: '4',
  Typecode: 11,
  Tariff: 5.6,
  Amount: 152,
  DepCode: 'SL',
  Month: 5,
  Year: 2018,
  DocName: ' ',
};

var ApiId = 'Your-api-id';
var ApiKey = 'Your-api-key';
function pad2(n) {
  return n > 9 ? '' + n : '0' + n;
}
function getTimestamp() {
  var d = new Date();
  var yyyy = d.getFullYear();
  var MM = pad2(d.getMonth() + 1);
  var dd = pad2(d.getDate());
  var HH = pad2(d.getHours());
  var mm = pad2(d.getMinutes());
  var ss = pad2(d.getSeconds());
  return yyyy + MM + dd + HH + mm + ss;
}
var timestamp = getTimestamp();
var dataString = ApiId + timestamp + JSON.stringify(reqJson);
var hash = CryptoJS.HmacSHA256(dataString, ApiKey);
var signature = CryptoJS.enc.Base64.stringify(hash);
console.log(dataString);
console.log(hash);
console.log(signature);
var url = 'https://palk.merit.ee/api/v1/sendsalary' +
  '?ApiId=' + ApiId + '&timestamp=' + timestamp + '&signature=' +
  signature;
request({
  url: url,
  method: 'POST',
  json: true,
  headers: { 'content-type': 'application/json', },
  body: reqJson,
 },
 
function (request, response) {
  console.log('Status code: ', response.statusCode, ' -- ',
response.statusMessage);
  console.log('Headers: ', response.headers['content-type']);
  console.log('Body: ', response.body);
 }
); 

Example on how to post absences

var CryptoJS = require('crypto-js');
var request = require('request');

var reqJson = {
  ContractCode: '45805282756',
  Typecode: 1,
  StartDate: '2017-05-01',
  AbsenceDays: 10,
  Month: 5,
  Year: 2017,
  DocName: 'X',
};
var ApiId = 'Your ApiId ';
var ApiKey = 'Your ApiKey ';
function pad2(n) {
  return n > 9 ? '' + n : '0' + n;
}
function getTimestamp() {
  var d = new Date();
  var yyyy = d.getFullYear();
  var MM = pad2(d.getMonth() + 1);
  var dd = pad2(d.getDate());
  var HH = pad2(d.getHours());
  var mm = pad2(d.getMinutes());
  var ss = pad2(d.getSeconds());
  return yyyy + MM + dd + HH + mm + ss;
}
var timestamp = getTimestamp();
var dataString = ApiId + timestamp + JSON.stringify(reqJson);
var hash = CryptoJS.HmacSHA256(dataString, ApiKey);
var signature = CryptoJS.enc.Base64.stringify(hash);
console.log(dataString);
console.log(hash);
console.log(signature);
var url = 'https://palk.merit.ee/api/v1/sendabsence' +
  '?ApiId=' + ApiId + '&timestamp=' + timestamp + '&signature=' +
  signature;
request({
    url: url,
    method: 'POST',
    json: true,
    headers: { 'content-type': 'application/json', },
    body: reqJson,
  },
  function (request, response) {
    console.log('Status code: ', response.statusCode, ' -- ',
  response.statusMessage);
    console.log('Headers: ', response.headers['content-type']);
    console.log('Body: ', response.body);
  }
 );