Skip to content

Commit e9c1908

Browse files
committed
fix: verification of hmac when completing purchase
1 parent 646f4be commit e9c1908

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/Messages/CompletePurchaseResponse.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Omnipay\EveryPay\Exceptions\MismatchException;
1010
use Omnipay\Common\Message\RedirectResponseInterface;
1111
use Omnipay\EveryPay\Exceptions\PaymentFailedException;
12-
use Omnipay\EveryPay\Support\SignedDataOptions;
1312

1413
/**
1514
* Response
@@ -54,7 +53,7 @@ public function validateResponse()
5453
throw new MismatchException('Order reference returned by gateway does not match');
5554
}
5655

57-
if ($this->everyPayRequestHmac() !== $this->data['request']['hmac']) {
56+
if (!$this->isAuthentic()) {
5857
throw new MismatchException('Invalid HMAC signature in the incoming request');
5958
}
6059

@@ -75,21 +74,12 @@ public function getCardToken()
7574
return CardToken::make($this->data['request']);
7675
}
7776

78-
private function everyPayRequestHmac()
77+
private function isAuthentic()
7978
{
80-
$options = SignedDataOptions::gateway(
81-
$this->request->getSecret()
82-
)->dontInclude([
83-
'utf8',
84-
'hmac',
85-
'_method',
86-
'authenticity_token'
87-
]);
88-
89-
return SignedData::make(
79+
return SignedData::verify(
9080
$this->data['request'],
91-
$options
92-
)['hmac'];
81+
$this->request->getSecret()
82+
);
9383
}
9484

9585
public function getTransactionReference()

src/Support/SignedData.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ public static function make(array $data, SignedDataOptions $options)
2424
->toArray();
2525
}
2626

27+
public static function verify(array $data, $secret)
28+
{
29+
$candidateData = [];
30+
$hmacFields = explode(',', $data['hmac_fields']);
31+
32+
foreach ($hmacFields as $field) {
33+
$candidateData[] = $field . '=' . $data[$field];
34+
}
35+
36+
$candidateHmac = hash_hmac(
37+
'sha1',
38+
implode('&', $candidateData),
39+
$secret
40+
);
41+
42+
return $data['hmac'] === $candidateHmac;
43+
}
44+
2745
public function sign()
2846
{
2947
$hmacPayload = [];

0 commit comments

Comments
 (0)