Last active
August 29, 2015 14:19
-
-
Save diverted247/aae527168c15728f25de to your computer and use it in GitHub Desktop.
Recurly Callback API Server in Hapi.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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