Skip to content

Commit f69b19e

Browse files
authored
Fix zero value HMAC signature (Adyen#126)
1 parent c0d7a1d commit f69b19e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Adyen/Util/HmacSignature.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private function getNotificationDataToSign($params)
6666
$originalReference = (!empty($params['originalReference'])) ? $params['originalReference'] : "";
6767
$merchantAccountCode = (!empty($params['merchantAccountCode'])) ? $params['merchantAccountCode'] : "";
6868
$merchantReference = (!empty($params['merchantReference'])) ? $params['merchantReference'] : "";
69-
$value = (!empty($params['amount']['value'])) ? $params['amount']['value'] : "";
69+
// `empty` treats too many value types as empty. `isset` should prevent some of these cases.
70+
$value = (isset($params['amount']['value'])) ? $params['amount']['value'] : "";
7071
$currency = (!empty($params['amount']['currency'])) ? $params['amount']['currency'] : "";
7172
$eventCode = (!empty($params['eventCode'])) ? $params['eventCode'] : "";
7273
$success = (!empty($params['success'])) ? $params['success'] : "";

tests/Adyen/Util/HmacSignatureTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,32 @@ public function testNotificationRequestItemHmac()
5656
$this->fail('Unexpected exception');
5757
}
5858
}
59+
public function testHmacSignatureForRefundWithZeroValue()
60+
{
61+
$params = json_decode('{
62+
"pspReference": "7914073381342284",
63+
"merchantAccountCode": "TestMerchant",
64+
"merchantReference": "TestPayment-1407325143704",
65+
"amount": {
66+
"value": 0,
67+
"currency": "EUR"
68+
},
69+
"eventCode": "REFUND",
70+
"success": "true"
71+
}', true);
72+
$key = "44782DEF547AAA06C910C43932B1EB0C71FC68D9D0C057550C48EC2ACF6BA056";
73+
$hmac = new HmacSignature();
74+
try {
75+
$hmacCalculation = $hmac->calculateNotificationHMAC($key, $params);
76+
$this->assertNotEmpty($hmacCalculation);
77+
$this->assertEquals("J7HhsgZo5KwqdB7LFZJV6rfQgp+RqC2kuYyw/3x3w+8=", $hmacCalculation);
78+
$params['additionalData'] = array(
79+
'hmacSignature' => $hmacCalculation
80+
);
81+
$hmacValidate = $hmac->isValidNotificationHMAC($key, $params);
82+
$this->assertTrue($hmacValidate);
83+
} catch (AdyenException $e) {
84+
$this->fail('Unexpected exception');
85+
}
86+
}
5987
}

0 commit comments

Comments
 (0)