index.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * Фронтенд для получения уведомлений от PORTMONE в виде POST XML
  4. * http://store.nightfly.biz/st/1421855512/XML.Portmone.Req.009.doc
  5. */
  6. DEFINE('PAYEE_ID', 11111);
  7. // подключаем API OpenPayz
  8. include ("../../libs/api.openpayz.php");
  9. // ловим ответ о транзакции в виде POST XML
  10. if (!empty($_REQUEST['data'])) {
  11. $xml = $_REQUEST['data'];
  12. } else {
  13. die("Get POST xml: FAIL");
  14. }
  15. function po_CheckTransaction($hash) {
  16. $hash = 'PORT_' . mysql_real_escape_string($hash);
  17. $query = "SELECT `id` from `op_transactions` WHERE `hash`='" . $hash . "'";
  18. $data = simple_query($query);
  19. if (!empty($data)) {
  20. return (false);
  21. } else {
  22. return (true);
  23. }
  24. }
  25. function po_TariffGetPricesAll() {
  26. $query = "SELECT `name`,`Fee` from `tariffs`";
  27. $allprices = simple_queryall($query);
  28. $result = array();
  29. if (!empty($allprices)) {
  30. foreach ($allprices as $io => $eachtariff) {
  31. $result[$eachtariff['name']] = $eachtariff['Fee'];
  32. }
  33. }
  34. return ($result);
  35. }
  36. function po_UserGetStargazerData($login) {
  37. $login = mysql_real_escape_string($login);
  38. $query = "SELECT * from `users` WHERE `login`='" . $login . "';";
  39. $result = simple_query($query);
  40. return ($result);
  41. }
  42. //дебаг данные
  43. if (!empty($xml)) {
  44. //разбираем на куски пойманный XML
  45. $xml_arr = xml2array($xml);
  46. if (isset($xml_arr['REQUESTS'])) {
  47. $customerid = $xml_arr['REQUESTS']['PAYER']['CONTRACT_NUMBER'];
  48. $allcustomers = op_CustomersGetAll();
  49. if (isset($allcustomers[$customerid])) {
  50. $customerLogin = $allcustomers[$customerid];
  51. $userdata = po_UserGetStargazerData($customerLogin);
  52. $allTariffs = po_TariffGetPricesAll();
  53. $amount = $allTariffs[$userdata['Tariff']];
  54. $userBalance = $userdata['Cash'] * -1;
  55. $reply = '<?xml version="1.0" encoding="UTF-8"?>
  56. <RESPONSE>
  57. <BILLS>
  58. <PAYEE>' . PAYEE_ID . '</PAYEE>
  59. <BILL_PERIOD>' . date("my") . '</BILL_PERIOD>
  60. <BILL>
  61. <PAYER>
  62. <CONTRACT_NUMBER>' . $customerid . '</CONTRACT_NUMBER>
  63. </PAYER>
  64. <BILL_DATE>' . date("Y-m-d") . '</BILL_DATE>
  65. <BILL_NUMBER>' . microtime(true) . rand(100000000, 999999999) . '</BILL_NUMBER>
  66. <AMOUNT>' . $amount . '</AMOUNT>
  67. <DEBT>' . $userBalance . '</DEBT>
  68. </BILL>
  69. </BILLS>
  70. </RESPONSE>';
  71. die($reply);
  72. }
  73. } elseif (isset($xml_arr['BILLS'])) {
  74. $customerid = $xml_arr['BILLS']['BILL']['PAYER']['CONTRACT_NUMBER'];
  75. $summ = $xml_arr['BILLS']['BILL']['PAYED_AMOUNT'];
  76. $bill_id = $xml_arr['BILLS']['BILL']['BILL_ID'];
  77. $paysys = 'PORTMONE';
  78. $note = '';
  79. $hash = md5('PORT_' . $bill_id);
  80. if (po_CheckTransaction($hash)) {
  81. $allcustomers = op_CustomersGetAll();
  82. if (isset($allcustomers[$customerid])) {
  83. //регистрируем новую транзакцию
  84. op_TransactionAdd($hash, $summ, $customerid, $paysys, $note);
  85. //вызываем обработчики необработанных транзакций
  86. op_ProcessHandlers();
  87. $reply = '<?xml version="1.0" encoding="UTF-8"?>
  88. <RESULT>
  89. <ERROR_CODE>0</ERROR_CODE>
  90. <REASON>OK</REASON>
  91. </RESULT>
  92. ';
  93. die($reply);
  94. } else {
  95. $reply = '<?xml version="1.0" encoding="UTF-8"?>
  96. <RESULT>
  97. <ERROR_CODE>15</ERROR_CODE>
  98. <REASON>User_Not_Found</REASON>
  99. </RESULT>
  100. ';
  101. die($reply);
  102. }
  103. } else {
  104. $reply = '<?xml version="1.0" encoding="UTF-8"?>
  105. <RESULT>
  106. <ERROR_CODE>0</ERROR_CODE>
  107. <REASON>success</REASON>
  108. </RESULT>
  109. ';
  110. die($reply);
  111. }
  112. } else {
  113. die('Input XML: FAIL | WRONG');
  114. }
  115. } else {
  116. die('Input XML: FAIL | EMPTY');
  117. }
  118. ?>