Skip to content

Commit 8ec2228

Browse files
author
dormin
committed
add description field to notification
change hmac logic to calculate on raw body support for arbitrary fields in notification
1 parent 76eeb77 commit 8ec2228

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

sample/callback.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Riskified\DecisionNotification\Model;
2424

2525
# Replace with the 'shop domain' of your account in Riskified
26-
$domain = "busteco.com";
26+
$domain = "test.com";
2727

2828
# Replace with the 'auth token' listed in the Riskified web app under the 'Settings' Tab
2929
$authToken = "bde6c2dce1657b1197cbebb10e4423b3560a3a6b";
@@ -32,9 +32,9 @@
3232

3333
$signature = new Signature\HttpDataSignature();
3434

35-
$headers = array('X-Riskified-Hmac-Sha256:4e17669551be731365461a27bf50d6886f11f2fd95ba88c74d401d0328909a63');
36-
$body = 'id=1&status=approved';
35+
$headers = array('X-Riskified-Hmac-Sha256:6bccbd8fbeeb2b95b553ada025a8d018b6d5182792f1db4fcc4186e7bf6c3c0f');
36+
$body = 'id=1&description=all%20good&status=approved';
3737

3838
$notification = new Model\Notification($signature, $headers, $body);
3939

40-
print "Order $notification->id changed to status $notification->status";
40+
print "Order #$notification->id changed to status '$notification->status' with message '$notification->description'\n";

sample/order_webhook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Riskified\OrderWebhook\Transport;
2626

2727
# Replace with the 'shop domain' of your account in Riskified
28-
$domain = "test.pass.com";
28+
$domain = "test.com";
2929

3030
# Replace with the 'auth token' listed in the Riskified web app under the 'Settings' Tab
3131
$authToken = "1388add8a99252fc1a4974de471e73cd";

src/Riskified/DecisionNotification/Model/Notification.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
class Notification {
2525

2626
/**
27-
* @var Order ID
27+
* @var string Order ID
2828
*/
2929
public $id;
3030
/**
31-
* @var Order Status
31+
* @var string Status of Order
3232
*/
3333
public $status;
34+
/**
35+
* @var string Description of Decision
36+
*/
37+
public $description;
3438

3539
protected $signature;
3640
protected $headers;
@@ -41,7 +45,7 @@ class Notification {
4145
* Inits and validates the request.
4246
* @param $signature Signature An instance of a Signature class that handles authentication
4347
* @param $headers array A list of HTTP Headers as strings
44-
* @param $body string The body of the Request
48+
* @param $body string The raw body of the Request
4549
* @throws NotificationException on issues with the request
4650
*/
4751
public function __construct($signature, $headers, $body) {
@@ -50,7 +54,7 @@ public function __construct($signature, $headers, $body) {
5054
$this->body = $body;
5155

5256
$this->parse_headers();
53-
$this->parse_body($body);
57+
$this->parse_body();
5458
$this->test_authorization();
5559
}
5660

@@ -64,7 +68,8 @@ protected function parse_headers() {
6468
list ($key, $value) = explode(':', $header);
6569
if (!$key || !$value)
6670
throw new Exception\BadHeaderException($this->headers, $this->body, $header);
67-
$this->headers_map[trim($key)] = trim($value);
71+
$header = str_replace('-', '_', strtoupper(trim($key)));
72+
$this->headers_map[$header] = trim($value);
6873
}
6974
}
7075

@@ -75,7 +80,7 @@ protected function parse_headers() {
7580
protected function test_authorization() {
7681
$signature = $this->signature;
7782
$remote_hmac = $this->headers_map[$signature::HMAC_HEADER_NAME];
78-
$local_hmac = $signature->calc_hmac($this->data_string());
83+
$local_hmac = $signature->calc_hmac($this->body);
7984
if ($remote_hmac != $local_hmac)
8085
throw new Exception\AuthorizationException($this->headers, $this->body, $local_hmac, $remote_hmac);
8186
}
@@ -90,14 +95,7 @@ protected function parse_body() {
9095
if (!$vars['id'] || !$vars['status'])
9196
throw new Exception\BadPostParametersException($this->headers, $this->body);
9297

93-
$this->id = $vars['id'];
94-
$this->status = $vars['status'];
95-
}
96-
97-
/**
98-
* @return string sorted param string for hashing
99-
*/
100-
protected function data_string() {
101-
return "id=$this->id&status=$this->status";
98+
foreach($vars as $key => $value)
99+
$this->$key = $value;
102100
}
103101
}

0 commit comments

Comments
 (0)