index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. //including required libs
  3. include ("../../libs/api.openpayz.php");
  4. include ("../../libs/api.ipay.php");
  5. //debug mode with logging
  6. $debug = false;
  7. /**
  8. * Reports some error
  9. *
  10. * @param string $data
  11. *
  12. * @return void
  13. */
  14. function ipay_reportError($data) {
  15. global $debug;
  16. header('HTTP/1.1 400 ' . $data . '"', true, 400);
  17. if ($debug) {
  18. file_put_contents('./debug.log', date("Y-m-d H:i:s") . ': ' . $data . "\n", FILE_APPEND);
  19. file_put_contents('./debug.log', print_r($_POST, true) . "\n", FILE_APPEND);
  20. file_put_contents('./debug.log', '=========================' . "\n", FILE_APPEND);
  21. }
  22. die($data);
  23. }
  24. /**
  25. * Reports some success
  26. *
  27. * @param string $data
  28. *
  29. * @return void
  30. */
  31. function ipay_reportSuccess($data) {
  32. global $debug;
  33. header('HTTP/1.1 200 ' . $data . '"', true, 200);
  34. if ($debug) {
  35. file_put_contents('./debug.log', date("Y-m-d H:i:s") . ': ' . $data . "\n", FILE_APPEND);
  36. }
  37. die($data);
  38. }
  39. /**
  40. * Check is transaction unique?
  41. *
  42. * @param $hash - hash string to check
  43. * @return bool
  44. */
  45. function ipay_CheckTransaction($hash) {
  46. $hash = mysql_real_escape_string($hash);
  47. $query = "SELECT `id` from `op_transactions` WHERE `hash`='" . $hash . "'";
  48. $data = simple_query($query);
  49. if (!empty($data)) {
  50. return (false);
  51. } else {
  52. return (true);
  53. }
  54. }
  55. //catch some xml notification
  56. $xml = $_POST['xml'];
  57. if (!empty($xml)) {
  58. $xml = str_replace('\"', '"', $xml);
  59. $rawXml = xml2array($xml);
  60. if (!empty($rawXml)) {
  61. if (isset($rawXml['payment'])) {
  62. if (isset($rawXml['payment']['status'])) {
  63. if ($rawXml['payment']['status'] == 5) {
  64. $summ = ($rawXml['payment']['amount'] / 100); //в копійках
  65. $timestamp = $rawXml['payment']['timestamp'];
  66. $rawHash = $rawXml['payment']['ident'];
  67. $hash = 'IPAYZ_' . $rawHash;
  68. @$transactionInfoRaw = $rawXml['payment']['transactions']['transaction'][0]['info'];
  69. if (!empty($transactionInfoRaw)) {
  70. $transactionInfo = json_decode($transactionInfoRaw);
  71. $customerId = $transactionInfo->acc;
  72. //очевидно для платежей прилетающих с черджера другой формат данных о транзакции
  73. if (empty($customerId)) {
  74. $customerId = $transactionInfo->step_1->acc;
  75. }
  76. if (!empty($customerId)) {
  77. $allCustomers = op_CustomersGetAll();
  78. if (isset($allCustomers[$customerId])) {
  79. if (ipay_CheckTransaction($hash)) {
  80. op_TransactionAdd($hash, $summ, $customerId, 'IPAY', $transactionInfoRaw);
  81. op_ProcessHandlers();
  82. ipay_reportSuccess('TRANSACTION OK');
  83. } else {
  84. ipay_reportSuccess('TRANSACTION OK');
  85. }
  86. } else {
  87. ipay_reportError('UNKNOWN USER ' . $customerId);
  88. }
  89. } else {
  90. ipay_reportError('CANT PARSE USER');
  91. }
  92. } else {
  93. ipay_reportError('EMPTY TRANSACTION INFO');
  94. }
  95. } else {
  96. ipay_reportError('UNSUCCEFULL STATUS');
  97. }
  98. } else {
  99. ipay_reportError('STATUS SECTION MISSING');
  100. }
  101. } else {
  102. ipay_reportError('PAYMENT SECTION MISSING');
  103. }
  104. } else {
  105. ipay_reportError('XML REQUEST PARSE FAIL');
  106. }
  107. } else {
  108. ipay_reportError('EMPTY REQUEST');
  109. }
  110. ?>