index.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. require_once('../../libs/api.compat.php');
  3. require_once('../../libs/api.astral.php');
  4. require_once('../../libs/api.ubrouting.php');
  5. require_once('../../libs/api.mysql.php');
  6. require_once('../../libs/api.omaeurl.php');
  7. require_once('../../libs/api.paysysproto.php');
  8. $cfgPrvdx = parse_ini_file('config/providex.ini');
  9. $merchant_name = $cfgPrvdx['MERCHANT_NAME'];
  10. $merchant_url = $cfgPrvdx['MERCHANT_URL'];
  11. $merchant_service = $cfgPrvdx['MERCHANT_SERVICE'];
  12. $merchant_logo = $cfgPrvdx['MERCHANT_LOGO'];
  13. $merchant_currency = $cfgPrvdx['MERCHANT_CURRENCY'];
  14. $avail_prices = $cfgPrvdx['AVAIL_PRICES'];
  15. const REDIRECT_MESSAGE = 'Перенаправлення...';
  16. const FAIL_NO_CUSTOMER_ID = 'ПОМИЛКА: не вказано платіжний ID';
  17. const FAIL_SOMETHING_WENT_WRONG = 'ПОМИЛКА: щось пішло не так - будь ласка, спробуйте ще раз';
  18. function providexSumm($customerID, $avail_prices, $merchant_currency) {
  19. global $cfgPrvdx;
  20. $inputs = '';
  21. $result = '';
  22. if (!empty($avail_prices)) {
  23. $avail_prices = explode(',', $avail_prices);
  24. $i = 0;
  25. foreach ($avail_prices as $eachprice) {
  26. $selected = false;
  27. if ($i == 0) {
  28. $selected = true;
  29. }
  30. $inputs .= wf_RadioInput('amount', $eachprice . ' ' . $merchant_currency, $eachprice, true, $selected);
  31. $i++;
  32. }
  33. if (isset($cfgPrvdx['CUSTOM_PRICE']) and ! empty($cfgPrvdx['CUSTOM_PRICE'])) {
  34. $jsCode = 'function change_custom_amount(){
  35. var custom_amount = document.getElementById("radio_custom_amount");
  36. custom_amount.value = document.getElementById("input_custom_amount").value;
  37. custom_amount.value = parseFloat(custom_amount.value).toFixed(2);
  38. }
  39. window.addEventListener(\'pageshow\',
  40. function() {
  41. change_custom_amount();
  42. },
  43. false);';
  44. $inputs .= wf_tag('script') . $jsCode . wf_tag('script', true);
  45. $inputs .= wf_delimiter(0);
  46. $inputs .= wf_tag('input', false, '', 'type="radio" name="amount" value="' . $cfgPrvdx['CUSTOM_PRICE'] . '" id="radio_custom_amount" onClick="change_custom_amount()"');
  47. $inputs .= wf_tag('input', false, '', 'onchange="change_custom_amount()" id="input_custom_amount" type="number" style="width: 4em;" value="' . $cfgPrvdx['CUSTOM_PRICE'] . '" min="' . $cfgPrvdx['CUSTOM_PRICE'] . '" step="any"') . ' ';
  48. $inputs .= wf_tag('label', false, '', 'for="radio_custom_amount"') . $cfgPrvdx['MERCHANT_CURRENCY'] . wf_tag('label', true) . wf_delimiter(0);
  49. }
  50. } else {
  51. $inputs .= wf_TextInput('amount', $merchant_currency, '', true, 5, 'finance');
  52. }
  53. $inputs .= wf_HiddenInput('paymentid', $customerID);
  54. $inputs .= wf_delimiter(0);
  55. $inputs .= wf_Submit('Оплатити');
  56. $result .= wf_Form('', 'POST', $inputs, '');
  57. return ($result);
  58. }
  59. $payment_form = '';
  60. $jsCode = '';
  61. if (!ubRouting::checkPost('amount') and !ubRouting::checkPost('paymentid')) {
  62. if (ubRouting::checkGet('customer_id')) {
  63. $customerID = ubRouting::get('customer_id', 'vf');
  64. $payment_form = providexSumm($customerID, $avail_prices, $merchant_currency);
  65. } else {
  66. $payment_form = wf_tag('h2', false, '', 'style="color: #FF4411;"') . FAIL_NO_CUSTOMER_ID . wf_tag('h2', true);
  67. }
  68. } else {
  69. //push form
  70. $customerID = ubRouting::post('paymentid', 'vf');
  71. $amountRaw = ubRouting::post('amount', 'float');
  72. //optional external service payment
  73. if (isset($cfgPrvdx['SERVICE_PAYMENT_PERCENT'])) {
  74. if ($cfgPrvdx['SERVICE_PAYMENT_PERCENT']) {
  75. $externalPercent = ubRouting::filters($cfgPrvdx['SERVICE_PAYMENT_PERCENT'], 'float');
  76. $amountRaw = $amountRaw + ($amountRaw * ($externalPercent / 100));
  77. }
  78. }
  79. if (!empty($customerID) and !empty($amountRaw)) {
  80. $amount = floatval(number_format($amountRaw, 2)); //required with two finishing zeroes
  81. $userLogin = PaySysProto::getUserLoginByPaymentID($customerID);
  82. $stgData = PaySysProto::getUserStargazerData($userLogin);
  83. $userPasswd = empty($stgData) ? '' : $stgData['Password'];
  84. $actionURL = $cfgPrvdx['UBAPI_URL'] . '?module=remoteapi&key=' . $cfgPrvdx['UBAPI_KEY'] . '&action=getagentdata&param=' . $userLogin;
  85. $agentData = PaySysProto::getUBAgentDataByUBAPIURL($actionURL);
  86. if (empty($agentData['id'])) {
  87. die('EMPTY AGENT ID RETURNED');
  88. }
  89. $agentID = $agentData['id'];
  90. $agentDataExten = PaySysProto::getUBAgentDataExten($agentID, $cfgPrvdx['PAYSYS_EXTINFO_NAME']);
  91. if (empty($agentDataExten[0])) {
  92. die('EMPTY AGENT DATA EXTEN RETURNED');
  93. }
  94. $agentDataExten = $agentDataExten[0];
  95. $prvdxPosID = $agentDataExten['internal_paysys_id'];
  96. $prvdxEndpointKey = $agentDataExten['internal_paysys_srv_id'];
  97. $prvdxAPIKEy = $agentDataExten['paysys_token'];
  98. $prvdxAPISecret = $agentDataExten['paysys_secret_key'];
  99. $prvdxCallbackURL = $agentDataExten['paysys_callback_url'];
  100. $orderID = crc32($userLogin . PaySysProto::genRandNumString()) . crc32(microtime(true));
  101. $customPayload = json_encode(array('L' => $userLogin,
  102. 'P' => md5($userPasswd),
  103. 'OPID' => $customerID,
  104. 'source' => 'BACKEND'
  105. ));
  106. $jsonArr = array(
  107. 'pos_id' => $prvdxPosID,
  108. 'mode' => 'hosted',
  109. 'method' => 'purchase',
  110. 'amount' => $amount,
  111. 'currency' => $cfgPrvdx['PAYSYS_API_CURRENCY'],
  112. 'order_3ds_bypass' => 'supported',
  113. 'products' => [],
  114. 'customer_email' => '',
  115. 'description' => $cfgPrvdx['PAYSYS_API_PAYMENT_PURPOSE'],
  116. 'order_id' => $orderID,
  117. 'server_url' => $prvdxCallbackURL,
  118. 'result_url' => $cfgPrvdx['URL_OK'],
  119. 'payload' => $customPayload
  120. );
  121. $jsonData = json_encode($jsonArr);
  122. /*
  123. // fucking making 'amount' field to be digit in terms of JSON
  124. // and to have a possibility to contain zeroed decimals, like .00 - e.g: 2.00, 4.00, 25.00
  125. preg_match('/(?<=",)"amount":.*?(?=,")/i', $jsonData, $matches);
  126. $tmpArr = explode(':', $matches[0]);
  127. $tmpStr = str_ireplace('"', '', $tmpArr[1]);
  128. $tmpStr = $tmpArr[0] . ':' . $tmpStr;
  129. $jsonData = preg_replace('/(?<=",)"amount":.*?(?=,")/i', $tmpStr, $jsonData);
  130. */
  131. PaySysProto::writeDebugLog('sending JSON:' . "\n" . $jsonData, $cfgPrvdx['DEBUG_MODE_ON'], 4);
  132. $omaeURL = new OmaeUrl($cfgPrvdx['API_URL']);
  133. $omaeURL->setVerboseLog($cfgPrvdx['DEBUG_MODE_ON'], 'curl_debug');
  134. $omaeURL->setOpt(CURLOPT_POST, true);
  135. $omaeURL->setOpt(CURLOPT_FOLLOWLOCATION, true);
  136. $omaeURL->setOpt(CURLOPT_MAXREDIRS, 0);
  137. $omaeURL->dataHeader('Content-type', 'application/json;charset=utf-8');
  138. $omaeURL->dataHeader('X-API-AUTH', 'CPAY ' . $prvdxAPIKEy . ':' . $prvdxAPISecret);
  139. $omaeURL->dataHeader('X-API-KEY', $prvdxEndpointKey);
  140. $omaeURL->dataHeader('Cache-control', 'no-cache');
  141. $omaeURL->dataPostRaw($jsonData);
  142. $sendResult = $omaeURL->response();
  143. $lastResult = $omaeURL->lastRequestInfo();
  144. $redirectURL = empty($lastResult['redirect_url']) ? 'empty_redir_url' : $lastResult['redirect_url'];
  145. PaySysProto::writeDebugLog('CURL resonse:' . "\n" . print_r($sendResult, true), $cfgPrvdx['DEBUG_MODE_ON']);
  146. PaySysProto::writeDebugLog('CURL last request info:' . "\n" . print_r($lastResult, true), $cfgPrvdx['DEBUG_MODE_ON']);
  147. if (empty($redirectURL) or $redirectURL == 'empty_redir_url') {
  148. $payment_form = wf_tag('h2', false, '', 'style="color: #FF4411;"') . FAIL_SOMETHING_WENT_WRONG . wf_tag('h2', true);
  149. } else {
  150. $payment_form = wf_tag('h2', false, '', 'style="color: #0EB400;"') . REDIRECT_MESSAGE . wf_tag('h2', true);
  151. $jsCode = wf_tag('script', false, '', 'type="text/javascript"');
  152. $jsCode .= 'window.location.replace("'. $redirectURL . '");';
  153. $jsCode .= wf_tag('script', true);
  154. }
  155. }
  156. }
  157. include('template.html');
  158. print($jsCode);