OneSpan Sign Developer: Callback Event Notification – Part 5

Duo Liang,

In the previous entries in this series, we showcased how to setup a web application and respond to the OneSpan Sign callback notifications. In this blog, we will achieve the same goal with Node.js by following the recommended flows covered in Part 2

Callback Event Notification in Action

Before beginning a Node.js project, be sure you complete the following:

  • Register for Event Notifications: Before the OneSpan Sign system can notify you of an event, you must turn on notifications. Follow this feature guide to learn how to register. In our example, we will simply register from the web portal.
  • Node.js: Node.js is available for Windows, Mac, and Linux. Download the installer.
  • Download Ngrok

The Code

Download the complete source code. Feel free to directly import the solution or copy all necessary classes to your existing project.

The main entrance of the project is the listener.js file, where we start the server and listen to port 3800 for connections.

app.listen(portToListenOn, () => {
  console.log(`Listening for OneSpan Sign notifications on port ${portToListenOn}. Started ${new Date().toString()}`);
});

We will expose an API route to handle callback requests sent from OneSpan Sign. See the code below to process the event notification.

app.post('/event',  (req, res) => {
  const callbakKey = req.get('Authorization');
  const payload = req.body;

  console.log('callback key: '+callbakKey);
  console.log('payload: '+ JSON.stringify(payload));

  if(payload.name == 'PACKAGE_COMPLETE'){
  	oss.downloadZippedDocument(payload);
  }
  console.log('it\'s here');
  res.send('OK');
});

Once all documents in a package have been signed and the package completed, OneSpan Sign will make a POST request to the registered URL with the following example JSON payload: 

{
  "@class": "com.silanis.esl.packages.event.ESLProcessEvent",
  "name": "PACKAGE_COMPLETE",
  "sessionUser": "0787be84-f095-44c7-ba00-787093df86fc",
  "packageId": "KHiRfOXgKK0gpVWwwpFOSNy6o34=",
  "message": null,
  "documentId": null
}

In the code above, any POST request targeting the “/event” route will be handled by the service function below:

const ossFunctions = module.exports = 
{
  downloadZippedDocument: async function (payload) 
  {	
  	const options = 
  	{ 
  		method: 'GET',
		url: API_URL+'/packages/' + payload.packageId + '/documents/zip',
		headers: 
		{ 	
		    accept: 'application/zip',
		    authorization: 'Basic ' + API_KEY}
	};

	const file_path = FILE_SAVE_PATH+'/package_' + payload.packageId + '_'+ Date.now()+'.zip';

	request(options)
	  .pipe(fs.createWriteStream(file_path))
	  .on('close', function () {
	    console.log('File written!');
	  });	
  }
};

In this example, we will simply download the signed document in a zip file and store it to a location of your choice. 

Perform a Test

Run your local server with Node.js by opening a command prompt and changing the current directory where you saved your "listener.js" file. Then, enter:

node listener.js

Open a command prompt and change the directory to the location where you saved your Ngrok executable. Enter the following command:

ngrok http [port] -host-header="localhost:[port]"

Next, login to your OneSpan Sign account, browse to the Admin page, and modify the domain of your callback URL. 

Finally, create, send, and complete a test package. You should be able to view the signed documents as a zipped file in the location of your choice. The moment OneSpan Sign sends a callback notification to your listener, you’ll see the message below at your output console. 

8-10-1

If you have any questions regarding this blog or anything else concerning integrating OneSpan Sign into your application, visit the Developer Community Forums. Your feedback matters to us!

Browse the Full Blog series:

•    Callback Event Notification – Part1
•    Callback Event Notification – Part2
•    Callback Event Notification – Part3
•    Callback Event Notification – Part4

Duo Liang is a Technical Evangelist and Partner Integrations Developer at OneSpan where he creates and maintains integration guides and code shares, helps customers and partners integrate OneSpan products into their applications, and builds integrations within third party platforms.