Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions src/Adyen/Util/HmacSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,15 @@ public function calculateNotificationHMAC($hmacKey, $params)
*/
private function getNotificationDataToSign($params)
{
$pspReference = (!empty($params['pspReference'])) ? $params['pspReference'] : "";
$originalReference = (!empty($params['originalReference'])) ? $params['originalReference'] : "";
$merchantAccountCode = (!empty($params['merchantAccountCode'])) ? $params['merchantAccountCode'] : "";
$merchantReference = (!empty($params['merchantReference'])) ? $params['merchantReference'] : "";
// `empty` treats too many value types as empty. `isset` should prevent some of these cases.
$value = (isset($params['amount']['value'])) ? $params['amount']['value'] : "";
$currency = (!empty($params['amount']['currency'])) ? $params['amount']['currency'] : "";
$eventCode = (!empty($params[self::EVENT_CODE])) ? $params[self::EVENT_CODE] : "";
$success = (!empty($params['success'])) ? $params['success'] : "";

$dataToSign = array(
$pspReference,
$originalReference,
$merchantAccountCode,
$merchantReference,
$value,
$currency,
$eventCode,
$success
$params['pspReference'] ?? "",
$params['originalReference'] ?? "",
$params['merchantAccountCode'] ?? "",
$params['merchantReference'] ?? "",
$params['amount']['value'] ?? "",
$params['amount']['currency'] ?? "",
$params[self::EVENT_CODE] ?? "",
$params['success'] ?? ""
);

return implode(":", $dataToSign);
Expand Down
73 changes: 73 additions & 0 deletions tests/Unit/Util/HmacSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,79 @@ public function testHmacSignatureEscaping()
}
}

public function testMerchantReferenceZeroIsNotTreatedAsEmpty()
{
$params = json_decode('{
"pspReference": "7914073381342284",
"merchantAccountCode": "TestMerchant",
"merchantReference": "0",
"amount": {
"value": 1130,
"currency": "EUR"
},
"eventCode": "AUTHORISATION",
"success": "true"
}', true);
$key = "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00";
$hmac = new HmacSignature();
$hmacCalculation = $hmac->calculateNotificationHMAC($key, $params);
$this->assertEquals("FxamuQrBMUCLGp4jK2kwgsURDS1pD6NjMXLZz5Ls1nI=", $hmacCalculation);

$params['additionalData'] = array('hmacSignature' => $hmacCalculation);
$this->assertTrue($hmac->isValidNotificationHMAC($key, $params));
}

/**
* @dataProvider zeroStringFieldProvider
*/
public function testFieldContainingZeroDoesNotCollideWithEmptyField($field, $expectedHmac)
{
$key = "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00";
$hmac = new HmacSignature();

$withZero = $this->notificationWithField($field, "0");
$withEmpty = $this->notificationWithField($field, "");

$zeroHmac = $hmac->calculateNotificationHMAC($key, $withZero);
$emptyHmac = $hmac->calculateNotificationHMAC($key, $withEmpty);

$this->assertEquals($expectedHmac, $zeroHmac);
$this->assertNotEquals($emptyHmac, $zeroHmac);
}

public static function zeroStringFieldProvider()
{
return array(
'pspReference' => array('pspReference', 'jh8PoPf6ROKe7ji5v20s41Fb/TEzRTItgID1qHY240Q='),
'originalReference' => array('originalReference', '3GHN2RNrz0GHIdbdfN55JqDebcNObEmRG79Bs6Fpx80='),
'merchantAccountCode' => array('merchantAccountCode', 'C17xGwk4PJHQMS/PDA2ho+tGRCvCXMTQ0u+CiuXbJlQ='),
'merchantReference' => array('merchantReference', 'FxamuQrBMUCLGp4jK2kwgsURDS1pD6NjMXLZz5Ls1nI='),
'currency' => array('currency', 'V3hcM/Zbi+UyxllxicWpEYIiWSAwWQ7kEFU/NzjRfPQ='),
'eventCode' => array('eventCode', 'z5vWmtlmAQL4a0h2wfdnjHjknlkTP4v8vnfNr0/FFzg='),
);
}

private function notificationWithField($field, $value)
{
$params = array(
'pspReference' => '7914073381342284',
'originalReference' => '',
'merchantAccountCode' => 'TestMerchant',
'merchantReference' => 'TestPayment-1407325143704',
'amount' => array('value' => 1130, 'currency' => 'EUR'),
'eventCode' => 'AUTHORISATION',
'success' => 'true',
);

if ($field === 'currency' || $field === 'value') {
$params['amount'][$field] = $value;
} else {
$params[$field] = $value;
}

return $params;
}

public function testIsHmacSupportedEventCode()
{
$params = json_decode('{
Expand Down