index.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Draft implementation of https://platon.atlassian.net/wiki/spaces/docs/pages/1315733632/Client+-+Server#Callback
  4. * Модуль написаний для логіки модуля "Опір Гусака".
  5. * Вам необхіжно в вашому білінгу прописати в "Додаткова інформації" "Господорюючого суб'єкта" "Ім`я платіжної системи" у вигляді префіксу "PLATONM_".
  6. * для кожного "Господорюючого суб'єкта" "Ім`я платіжної системи" повинно бути унікальним, наприклад: "PLATONM_1" для іншого "Господорюючого суб'єкта" "PLATONM_UBILL" і т.д.
  7. * Всі дані для бекенду також беруться з білінгу, і далі фронтенд оброблює їх зі своєї сторони.
  8. */
  9. error_reporting(E_ALL);
  10. //external service payment percent: (float for external payment, 0 - disabled)
  11. const SERVICE_PAYMENT_PERCENT = 0;
  12. //URL вашего работающего Ubilling
  13. define('API_URL', 'http://localhost/billing/');
  14. //И его серийный номер
  15. define('API_KEY', 'UBxxxxxxxxxx');
  16. // Префікс платіжної системи в білінгу для роширеної інформації по агентам
  17. define('PAYSYS_PREFIX', 'PLATONM' . '_');
  18. //including required libs
  19. include("../../libs/api.openpayz.php");
  20. // Send main headers
  21. header('Last-Modified: ' . gmdate('r'));
  22. header('Content-Type: text/html; charset=utf-8');
  23. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  24. header("Pragma: no-cache");
  25. /**
  26. * Returns user's assigned agent extended data, if available
  27. *
  28. * @param $gentID
  29. *
  30. * @return array|empty
  31. */
  32. function getGoosData($customerId, $amountRaw = '') {
  33. $baseUrl = API_URL . '?module=remoteapi&key=' . API_KEY . '&action=goose';
  34. $callbackUrl = $baseUrl . '&amount=' . $amountRaw . '&paymentid=' . $customerId;
  35. $gooseResult = @file_get_contents($callbackUrl);
  36. return ($gooseResult);
  37. }
  38. /**
  39. * Reports some error
  40. *
  41. * @param string $data
  42. *
  43. * @return void
  44. */
  45. function platon_reportError($data) {
  46. header('HTTP/1.1 400 ' . $data . '"', true, 400);
  47. die($data);
  48. }
  49. /**
  50. * Reports some success
  51. *
  52. * @param string $data
  53. *
  54. * @return void
  55. */
  56. function platon_reportSuccess($data) {
  57. header('HTTP/1.1 200 ' . $data . '"', true, 200);
  58. die($data);
  59. }
  60. /**
  61. * Returns request data
  62. *
  63. * @return array
  64. */
  65. function platon_RequestGet() {
  66. $result = array();
  67. if (!empty($_POST)) {
  68. $result = $_POST;
  69. }
  70. return ($result);
  71. }
  72. /**
  73. * Check is transaction unique?
  74. *
  75. * @param $hash - hash string to check
  76. * @return bool
  77. */
  78. function platon_CheckTransaction($hash) {
  79. $hash = mysql_real_escape_string($hash);
  80. $query = "SELECT `id` from `op_transactions` WHERE `hash`='" . $hash . "'";
  81. $data = simple_query($query);
  82. if (!empty($data)) {
  83. return (false);
  84. } else {
  85. return (true);
  86. }
  87. }
  88. //processing callback
  89. $requestData = platon_RequestGet();
  90. if (!empty($requestData)) {
  91. if (is_array($requestData)) {
  92. if (isset($requestData['id']) and isset($requestData['order']) and isset($requestData['description'])) {
  93. $allCustomers = op_CustomersGetAll();
  94. $customerId = $requestData['description'];
  95. if (isset($allCustomers[$customerId])) {
  96. $summRaw = $requestData['amount'];
  97. $summ = $summRaw;
  98. if (SERVICE_PAYMENT_PERCENT) {
  99. $summ = $summ / (1 + (SERVICE_PAYMENT_PERCENT / 100));
  100. }
  101. $paysys = 'PLATON';
  102. $hash = $paysys . '_' . $requestData['id'];
  103. $note = $requestData['ip'] . ' (' . $requestData['date'] . ') [rawsumm: ' . $summRaw . ' | paysumm:' . $summ . ' ] ' . $requestData['description'];
  104. if (platon_CheckTransaction($hash)) {
  105. if ($requestData['status'] == 'SALE') {
  106. if (isset($requestData['ext1']) and $requestData['ext1']) {
  107. $sRulesArr = json_decode($requestData['ext1'], true);
  108. if (!empty($sRulesArr)) {
  109. if ($summRaw == array_sum($sRulesArr)) {
  110. $gooseResult = getGoosData($customerId);
  111. if (!empty($gooseResult)) {
  112. $gooseResult = json_decode($gooseResult);
  113. if (!empty($gooseResult)) {
  114. $agentsExtInfo = preg_grep("/^" . PAYSYS_PREFIX . ".+/", array_column((array)$gooseResult->agentsextinfo, 'internal_paysys_name', 'id'));
  115. if (!empty($agentsExtInfo)) {
  116. $billPayStatus = FALSE;
  117. foreach ($agentsExtInfo as $id => $paysysPrefix) {
  118. $agentId = $gooseResult->agentsextinfo->{$id}->agentid;
  119. $edrpo = $gooseResult->agents->{$agentId}->edrpo;
  120. $ipn = $gooseResult->agents->{$agentId}->ipn;
  121. if (isset($sRulesArr[$edrpo]) or isset($sRulesArr[$ipn])) {
  122. $summ = isset($sRulesArr[$edrpo]) ? $sRulesArr[$edrpo] : $sRulesArr[$ipn];
  123. if (SERVICE_PAYMENT_PERCENT) {
  124. $summ = $summ / (1 + (SERVICE_PAYMENT_PERCENT / 100));
  125. }
  126. $paysys = $paysysPrefix;
  127. $hash = $paysys . '_' . $requestData['id'];
  128. if (platon_CheckTransaction($hash)) {
  129. op_TransactionAdd($hash, $summ, $customerId, $paysys, $note);
  130. op_ProcessHandlers();
  131. $billPayStatus = TRUE;
  132. }
  133. }
  134. }
  135. if ($billPayStatus) {
  136. platon_reportSuccess('Transaction processed');
  137. } else {
  138. platon_reportError('Billing procces fail');
  139. }
  140. } else {
  141. platon_reportError('Critical error. No advanced information found for agents');
  142. }
  143. } else {
  144. platon_reportError('Goose resistance could not find information about the subscriber');
  145. }
  146. } else {
  147. platon_reportError('Goose resistance could not find information about the subscriber');
  148. }
  149. } else {
  150. platon_reportError('The sum of the splits does not equal the total');
  151. }
  152. } else {
  153. platon_reportError('Incorrect format of additional field for split identification');
  154. }
  155. } else {
  156. // Do payments without additional information
  157. op_TransactionAdd($hash, $summ, $customerId, $paysys, $note);
  158. op_ProcessHandlers();
  159. platon_reportSuccess('Transaction processed');
  160. }
  161. } else {
  162. platon_reportError('Unknown callback status');
  163. }
  164. } else {
  165. platon_reportSuccess('Transaction processed');
  166. }
  167. } else {
  168. platon_reportError('User not found');
  169. }
  170. } else {
  171. platon_reportError('Required fields not found');
  172. }
  173. } else {
  174. platon_reportError('Callback proceesing error');
  175. }
  176. } else {
  177. platon_reportError('Empty callback request');
  178. }