index.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Draft implementation of https://platon.atlassian.net/wiki/spaces/docs/pages/1315733632/Client+-+Server#Callback
  4. */
  5. error_reporting(E_ALL);
  6. //external service payment percent: (float for external payment, 0 - disabled)
  7. const SERVICE_PAYMENT_PERCENT = 1.7;
  8. //including required libs
  9. include("../../libs/api.openpayz.php");
  10. // Send main headers
  11. header('Last-Modified: ' . gmdate('r'));
  12. header('Content-Type: text/html; charset=utf-8');
  13. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  14. header("Pragma: no-cache");
  15. /**
  16. * Reports some error
  17. *
  18. * @param string $data
  19. *
  20. * @return void
  21. */
  22. function platon_reportError($data) {
  23. header('HTTP/1.1 400 ' . $data . '"', true, 400);
  24. die($data);
  25. }
  26. /**
  27. * Reports some success
  28. *
  29. * @param string $data
  30. *
  31. * @return void
  32. */
  33. function platon_reportSuccess($data) {
  34. header('HTTP/1.1 200 ' . $data . '"', true, 200);
  35. die($data);
  36. }
  37. /**
  38. * Returns request data
  39. *
  40. * @return array
  41. */
  42. function platon_RequestGet() {
  43. $result = array();
  44. if (!empty($_POST)) {
  45. $result = $_POST;
  46. }
  47. return ($result);
  48. }
  49. // __
  50. // .,-;-;-,. /'_\
  51. // _/_/_/_|_\_\) /
  52. // '-<_><_><_><_>=/\
  53. // `/_/====/_/-'\_\
  54. // "" "" ""
  55. // ^^^ CE CHEREPASHKA ^^^
  56. /**
  57. * Check is transaction unique?
  58. *
  59. * @param $hash - hash string to check
  60. * @return bool
  61. */
  62. function platon_CheckTransaction($hash) {
  63. $hash = mysql_real_escape_string($hash);
  64. $query = "SELECT `id` from `op_transactions` WHERE `hash`='" . $hash . "'";
  65. $data = simple_query($query);
  66. if (!empty($data)) {
  67. return (false);
  68. } else {
  69. return (true);
  70. }
  71. }
  72. //processing callback
  73. $requestData = platon_RequestGet();
  74. if (!empty($requestData)) {
  75. if (is_array($requestData)) {
  76. if (isset($requestData['id']) and isset($requestData['order']) and isset($requestData['description'])) {
  77. $allCustomers = op_CustomersGetAll();
  78. $customerId = $requestData['description'];
  79. if (isset($allCustomers[$customerId])) {
  80. $summRaw = $requestData['amount'];
  81. $summ = $summRaw;
  82. if (SERVICE_PAYMENT_PERCENT) {
  83. $summ = round($summ / (1 + (SERVICE_PAYMENT_PERCENT / 100)));
  84. }
  85. $paysys = 'PLATON';
  86. $hash = $paysys . '_' . $requestData['id'];
  87. $note = $requestData['ip'] . ' (' . $requestData['date'] . ') [rawsumm: ' . $summRaw . ' | paysumm:' . $summ . ' ] ' . $requestData['description'];
  88. if (platon_CheckTransaction($hash)) {
  89. if ($requestData['status'] == 'SALE') {
  90. op_TransactionAdd($hash, $summ, $customerId, $paysys, $note);
  91. op_ProcessHandlers();
  92. platon_reportSuccess('Transaction processed');
  93. } else {
  94. platon_reportError('Unknown callback status');
  95. }
  96. } else {
  97. platon_reportSuccess('Transaction processed');
  98. }
  99. } else {
  100. platon_reportError('User not found');
  101. }
  102. } else {
  103. platon_reportError('Required fields not found');
  104. }
  105. } else {
  106. platon_reportError('Callback proceesing error');
  107. }
  108. } else {
  109. platon_reportError('Empty callback request');
  110. }