In order to authorise payments you need signature, which is calculated as follows:
- Sort alphabetically, in ascending order, by order parameters.
- Combine the parameters as follows:
parameter1=value1¶meter2=value2...parameterN=valueN. Save the result to a variable (hereinafter referred to asbody). - Calculate the signature using the
sha224,sha256,sha384orsha512hashing method, e.g.sha256(body + serviceKey. Save the hashing result to a variable (hereinafter referred to assignature). - Add the hashing method used after a semicolon to the calculated signature:
signature + “;sha256”.
Example of signature calculation
/**
* @param array $orderData
* @param string $serviceKey
* @param string $hashMethod
*
* @return string
*/
function createSignature($orderData, $serviceKey, $hashMethod)
{
$data = prepareData($orderData);
return hash($hashMethod, $data . $serviceKey);
}
/**
* @param array $data
* @param string $prefix
*
* @return string
*/
function prepareData(
$data,
$prefix = ''
) {
ksort($data);
$hashData = [];
foreach($data as $key => $value) {
if($prefix) {
$key = $prefix . '[' . $key . ']';
}
if(is_array($value)) {
$hashData[] = prepareData($value, $key);
} else {
$hashData[] = $key . '=' . $value;
}
}
return implode('&', $hashData);
}
$hashMethod = 'sha256';
$serviceKey = 'eAyhFLuHgwl5hu-32GM8QVlCVMWRU0dGjH1c';
$fields = [
'merchantId' => '6yt3gjtm9p1odfgx8491',
'serviceId' => '63f574ed-d90d-4abe-9cs1-39117584a7b',
'amount' => '300',
'currency' => 'PLN',
'orderId' => '123',
'orderDescription' => 'Example transaction',
'customerFirstName' => 'John',
'customerLastName' => 'Doe',
'customerEmail' => 'johndoe@domain.com',
'customerPhone' => '501501501',
'urlSuccess' => 'https://your-shop.com/success',
'urlFailure' => 'https://your-shop.com/failure',
'urlReturn' => 'https://your-shop.com/return',
];
$result = createSignature($fields, $serviceKey, $hashMethod) . ';' . $hashMethod;