Skip to content

Instantly share code, notes, and snippets.

@pepakriz
Created February 16, 2016 10:06
Show Gist options
  • Save pepakriz/43ef17fe0d0acba4e57a to your computer and use it in GitHub Desktop.
Save pepakriz/43ef17fe0d0acba4e57a to your computer and use it in GitHub Desktop.
{
"event":"onPay",
"data":{
"order":{
"created":"2016-02-16 10:44:39"
},
"reservations":[
{
"slotId":"a43f8d2b-fee0-4755-90b0-c562f9c73d60"
}
]
}
}
<?php
use React\Socket;
use React\Http;
use React\EventLoop;
require __DIR__ . '/vendor/autoload.php';
const HEADER_SIGNATURE = 'X-Hele-Signature';
const HEADER_SIGNATURE_ALGO = 'X-Hele-Signature-Algo';
const HEADER_HELE_NOTIFICATION_ID = 'X-Hele-Notification-Id';
$secret = 'abcXYZ';
$port = getenv('PORT') ?: 6543;
$loop = EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);
$http->on('request', function (Http\Request $request, Http\Response $response) use ($loop, $secret) {
$requestBody = '';
$contentLength = (int) $request->getHeaders()['Content-Length'];
$receivedData = 0;
$request->on('data', function ($data) use ($loop, $request, $response, $secret, & $requestBody, & $receivedData, $contentLength) {
$requestBody .= $data;
$receivedData += strlen($data);
if ($receivedData >= $contentLength) {
$signature = $request->getHeaders()[HEADER_SIGNATURE];
$algo = $request->getHeaders()[HEADER_SIGNATURE_ALGO];
$notificationId = $request->getHeaders()[HEADER_HELE_NOTIFICATION_ID];
if ($signature !== hash_hmac($algo, $requestBody, $secret)) {
$response->writeHead(403);
$response->end('Invalid signature');
return;
}
$data = json_decode($requestBody, JSON_OBJECT_AS_ARRAY);
...
$response->writeHead(200);
$response->end('Success');
}
});
});
$socket->listen($port);
$loop->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment