hedera-web/rest/tpv/transaction.php

61 lines
1.8 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
/**
* Starts a new TPV transaction and returns the params.
2017-12-01 14:38:23 +00:00
*/
2016-08-23 13:15:19 +00:00
class Transaction extends Vn\Web\JsonRequest
2016-07-22 20:00:27 +00:00
{
2016-09-06 14:25:02 +00:00
const PARAMS = ['amount'];
2016-07-22 20:00:27 +00:00
2016-09-06 14:25:02 +00:00
function run ($db)
{
2016-07-22 20:00:27 +00:00
$amount = (int) $_REQUEST['amount'];
$companyId = empty ($_REQUEST['company']) ? NULL : $_REQUEST['company'];
2016-08-25 10:47:09 +00:00
2017-12-01 14:38:23 +00:00
$row = $db->getObject ('CALL transactionStart (#, #)',
2016-07-22 20:00:27 +00:00
[$amount, $companyId]);
if (!isset ($row))
throw new Exception ('Transaction error');
2017-12-01 14:38:23 +00:00
$transactionId = str_pad ($row->transactionId, 12, '0', STR_PAD_LEFT);
2016-07-22 20:00:27 +00:00
$urlOk = empty ($_REQUEST['urlOk']) ? '' : sprintf ($_REQUEST['urlOk'], $transactionId);
$urlKo = empty ($_REQUEST['urlKo']) ? '' : sprintf ($_REQUEST['urlKo'], $transactionId);
2017-12-01 14:38:23 +00:00
$merchantUrl = $row->merchantUrl ? $row->merchantUrl : '';
2016-07-22 20:00:27 +00:00
$params = [
'Ds_Merchant_Amount' => $amount
,'Ds_Merchant_Order' => $transactionId
2017-12-01 14:38:23 +00:00
,'Ds_Merchant_MerchantCode' => $row->merchant
,'Ds_Merchant_Currency' => $row->currency
,'Ds_Merchant_TransactionType' => $row->transactionType
,'Ds_Merchant_Terminal' => $row->terminal
2016-07-22 20:00:27 +00:00
,'Ds_Merchant_MerchantURL' => $merchantUrl
,'Ds_Merchant_UrlOK' => $urlOk
,'Ds_Merchant_UrlKO' => $urlKo
];
$encodedParams = base64_encode (json_encode ($params));
2017-12-01 14:38:23 +00:00
$key = base64_decode ($row->secretKey);
2016-07-22 20:00:27 +00:00
$bytes = [0, 0, 0, 0, 0, 0, 0, 0];
$iv = implode (array_map ('chr', $bytes));
$key = mcrypt_encrypt (MCRYPT_3DES, $key, $transactionId, MCRYPT_MODE_CBC, $iv);
$signature = base64_encode (hash_hmac ('sha256', $encodedParams, $key, TRUE));
2017-12-01 14:38:23 +00:00
$url = $row->url;
$postValues = [
'Ds_SignatureVersion' => 'HMAC_SHA256_V1'
,'Ds_MerchantParameters' => $encodedParams
,'Ds_Signature' => $signature
];
2016-07-22 20:00:27 +00:00
2016-08-25 10:47:09 +00:00
return [
2017-12-01 14:38:23 +00:00
'url' => $url
,'postValues' => $postValues
2016-08-25 10:47:09 +00:00
];
2016-07-22 20:00:27 +00:00
}
}