Skip to content

Instantly share code, notes, and snippets.

@diverted247
Last active August 29, 2015 14:19
Show Gist options
  • Save diverted247/aae527168c15728f25de to your computer and use it in GitHub Desktop.
Save diverted247/aae527168c15728f25de to your computer and use it in GitHub Desktop.
Recurly Callback API Server in Hapi.js
The Recurly Webhook api calls a remote server passing data about user events. This server captures the calls and account_code to enable updating remote data. Typically you would use the Recurly REST API to validate any webhook call from the API itself.
SETUP
$ npm install
$ node server.js
$ source text.sh # posts data.xml to local server simulating webhook callbacks
<?xml version="1.0" encoding="UTF-8"?>
<closed_invoice_notification>
<account>
<account_code>[email protected]</account_code>
<username nil="true"></username>
<email>[email protected]</email>
<first_name>Ted</first_name>
<last_name>Patrick</last_name>
<company_name nil="true"></company_name>
</account>
</closed_invoice_notification>
{
"name": "Recurly_webhooks_server_hapi",
"version": "1.0.0",
"description": "Recurly API Server in Hapijs",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ted Patrick <[email protected]> (http://light.ly)",
"license": "ISC",
"dependencies": {
"hapi": "^8.4.0",
"xml2js": "^0.4.6"
}
}
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: process.env.PORT || 8888 });
var parseString = require('xml2js').parseString;
server.route({
method: 'POST',
path: '/api/recurly_webhooks/',
config: {
auth: false,
payload:{
allow:'application/xml',
parse:false
},
handler: function (request, reply) {
reply('1');
parseString( request.payload.toString() , { explicitArray:false,explicitRoot:false }, function( err , result ){
console.log( result.account.account_code );
//Call the Recurly API with account_code to sync any account detail.
});
}
}
});
server.start(function () {
console.log('Server running at:', server.info.uri);
});
curl -X POST --header "Content-Type:application/xml" -d @data.xml http://localhost:8888/api/recurly_webhooks/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment