Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Last active July 9, 2018 13:28
Show Gist options
  • Save mytharcher/41cd5ea79f400bd2c1c2e64c2b7ae61c to your computer and use it in GitHub Desktop.
Save mytharcher/41cd5ea79f400bd2c1c2e64c2b7ae61c to your computer and use it in GitHub Desktop.
Royal Mail API test
ROYAL_MAIL_API_CLIENT_ID=
ROYAL_MAIL_API_CLIENT_SECRET=
ROYAL_MAIL_API_APP_ID=
ROYAL_MAIL_API_USER_ID=
ROYAL_MAIL_API_PASSWORD=
require('dotenv').load();
const crypto = require('crypto');
const request = require('axios');
const moment = require('moment');
const { pack } = require('php-pack');
function sha1(str) {
return crypto.createHash('sha1').update(str).digest('hex');
}
function base64(str) {
return Buffer.from(str).toString('base64');
}
const nonce = parseInt(`${Math.random()}`.substr(-8), 10);
console.log('nonce: ', nonce);
const encodedNonce = base64(nonce.toString());
console.log('nonce(base64): ', encodedNonce);
const created = `${moment().utc().format('YYYY-MM-DDTHH:mm:ss')}Z`;
// const created = '2016-07-13T19:53:55.685Z';
console.log('date: ', created);
const buffer = Buffer.concat([pack('A*', nonce), pack('A*', created), pack('H*', sha1(process.env.ROYAL_MAIL_API_PASSWORD))]);
const passwordDigest = base64(pack('H*', sha1(buffer)));
console.log('password(encoded): ', passwordDigest);
const headers = {
'Accept': 'application/soap+xml',
'Content-Type': 'text/xml',
'SoapAction': 'createShipment',
'X-IBM-Client-Id': process.env.ROYAL_MAIL_API_CLIENT_ID,
'X-IBM-Client-Secret': process.env.ROYAL_MAIL_API_CLIENT_SECRET
};
const shipmentType = 'Delivery';
const serviceOccurrence = 1;
const serviceType = 1;
const serviceCode = 'CRL';
const serviceFormat = 'P';
const shippingDate = moment().utc().add(2, 'weeks').format('YYYY-MM-DD');
// in PHP sample code
const body = `<soapenv:Envelope xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.royalmailgroup.com/integration/core/V1" xmlns:v2="http://www.royalmailgroup.com/api/ship/V2">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>${process.env.ROYAL_MAIL_API_USER_ID}</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">${passwordDigest}</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">${encodedNonce}</wsse:Nonce>
<wsu:Created>${created}</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<v2:createShipmentRequest>
<v2:integrationHeader>
<v1:dateTime>${created}</v1:dateTime>
<v1:version>2</v1:version>
<v1:identification>
<v1:applicationId>${process.env.ROYAL_MAIL_API_APP_ID}</v1:applicationId>
<v1:transactionId>${Date.now()}</v1:transactionId>
</v1:identification>
</v2:integrationHeader>
<v2:requestedShipment>
<v2:shipmentType>
<code>${shipmentType}</code>
</v2:shipmentType>
<v2:serviceOccurrence>${serviceOccurrence}</v2:serviceOccurrence>
<v2:serviceType>
<code>${serviceType}</code>
</v2:serviceType>
<v2:serviceOffering>
<serviceOfferingCode>
<code>${serviceCode}</code>
</serviceOfferingCode>
</v2:serviceOffering>
<v2:serviceFormat>
<serviceFormatCode>
<code>${serviceFormat}</code>
</serviceFormatCode>
</v2:serviceFormat>
<v2:shippingDate>${shippingDate}</v2:shippingDate>
<v2:recipientContact>
<v2:name>whatever</v2:name>
</v2:recipientContact>
<v2:recipientAddress>
<buildingNumber>13</buildingNumber>
<addressLine1>AddressLine 1</addressLine1>
<postTown>LONDON</postTown>
<postcode>EC1A 1BB</postcode>
<country>
<countryCode>
<code>GB</code>
</countryCode>
</country>
</v2:recipientAddress>
<v2:items>
<v2:item>
<v2:numberOfItems>1</v2:numberOfItems>
<v2:weight>
<unitOfMeasure>
<unitOfMeasureCode>
<code>g</code>
</unitOfMeasureCode>
</unitOfMeasure>
<value>2000</value>
</v2:weight>
</v2:item>
</v2:items>
<v2:senderReference>sendersReference</v2:senderReference>
</v2:requestedShipment>
</v2:createShipmentRequest>
</soapenv:Body>
</soapenv:Envelope>`;
request.post('https://api.royalmail.net/shipping/v2', body, { headers }).then(res => {
console.log(res.data);
}).catch(ex => {
console.error(ex.response.status, ex.response.data);
});
{
"name": "royal-mail-test",
"version": "0.0.1",
"dependencies": {
"axios": "^0.18.0",
"dotenv": "^4.0.0",
"moment": "^2.19.2",
"php-pack": "^1.0.2-release"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment