0
1
Fork 0
hedera-web-mindshore/rest/tpv/transaction.php

68 lines
2.1 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
*/
2018-05-23 10:14:20 +00:00
class Transaction extends Vn\Web\JsonRequest {
2016-09-06 14:25:02 +00:00
const PARAMS = ['amount'];
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
function run($db) {
$amount = (int) $_REQUEST['amount'];
2018-05-23 10:14:20 +00:00
$companyId = empty($_REQUEST['company']) ? NULL : $_REQUEST['company'];
2016-08-25 10:47:09 +00:00
2019-05-21 14:16:27 +00:00
$row = $db->getObject('CALL myTpvTransaction_start(#, #)',
2016-07-22 20:00:27 +00:00
[$amount, $companyId]);
2018-05-23 10:14:20 +00:00
if (!isset($row))
throw new Exception('Transaction error');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$transactionId = str_pad($row->transactionId, 12, '0', STR_PAD_LEFT);
2017-12-01 14:38:23 +00:00
$merchantUrl = $row->merchantUrl ? $row->merchantUrl : '';
$urlOk = empty($_REQUEST['urlOk']) ? '' :
str_replace('_transactionId_', $transactionId, $_REQUEST['urlOk']);
$urlKo = empty($_REQUEST['urlKo']) ? '' :
str_replace('_transactionId_', $transactionId, $_REQUEST['urlKo']);
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
];
2018-05-23 10:14:20 +00:00
$encodedParams = base64_encode(json_encode($params));
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$key = base64_decode($row->secretKey);
2016-07-22 20:00:27 +00:00
$bytes = [0, 0, 0, 0, 0, 0, 0, 0];
2018-05-23 10:14:20 +00:00
$iv = implode(array_map('chr', $bytes));
$paddedData = $transactionId;
if (strlen($paddedData) % 8) {
$paddedData = str_pad($paddedData,
strlen($paddedData) + 8 - strlen($paddedData) % 8, "\0");
}
$encryptedData = openssl_encrypt($paddedData,
'des-ede3-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING , $iv);
$signature = base64_encode(hash_hmac('sha256', $encodedParams, $encryptedData, 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
}
}