index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // Frontend for accepting payments ConcordPay
  3. // https://pay.concord.ua/docs/docs/ru/dispatcher.html
  4. // Connecting OpenPayz API.
  5. include("../../libs/api.openpayz.php");
  6. // Connecting ConcordPay API.
  7. include("../../backend/concordpay/ConcordPay.php");
  8. // Debug mode with logging.
  9. $debug = false;
  10. // Load config.
  11. $conf_concordpay = parse_ini_file("../../backend/concordpay/config/concordpay.ini");
  12. // Get response JSON.
  13. $rawRequest = file_get_contents("php://input");
  14. $response = json_decode($rawRequest, true);
  15. /**
  16. * Check for POST have needed variables
  17. *
  18. * @param $response
  19. * @return bool
  20. */
  21. function cp_CheckResponse($response)
  22. {
  23. $requiredParameters = array(
  24. 'merchantAccount',
  25. 'orderReference',
  26. 'amount',
  27. 'currency',
  28. 'merchantSignature',
  29. 'transactionStatus',
  30. 'type'
  31. );
  32. $result = true;
  33. if (empty($response)) {
  34. return false;
  35. }
  36. foreach ($requiredParameters as $param) {
  37. if (!isset($response[$param]) || empty($response[$param])) {
  38. $result = false;
  39. break;
  40. }
  41. }
  42. return $result;
  43. }
  44. /**
  45. * @return ConcordPay
  46. */
  47. function cp_GetConcordpayInstance()
  48. {
  49. global $conf_concordpay;
  50. if (isset($conf_concordpay['cp_instance']) && $conf_concordpay['cp_instance'] instanceof ConcordPay) {
  51. return $conf_concordpay['cp_instance'];
  52. }
  53. $conf_concordpay['cp_instance'] = new ConcordPay($conf_concordpay['SECRET_KEY']);
  54. return $conf_concordpay['cp_instance'];
  55. }
  56. /**
  57. * @param array $response
  58. * @return bool
  59. */
  60. function cp_CheckSignature($response)
  61. {
  62. $sign = cp_GetConcordpayInstance()->cp_GenerateResponseSignature($response);
  63. return ($sign === $response['merchantSignature']);
  64. }
  65. /**
  66. * @param $response
  67. * @return bool
  68. */
  69. function cp_CheckOperationType($response)
  70. {
  71. if (!isset($response['type']) || !in_array($response['type'], cp_GetConcordpayInstance()->cp_GetOperationTypes())) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. /**
  77. * @param $response
  78. * @return bool
  79. */
  80. function cp_CheckTransactionStatus($response)
  81. {
  82. if (!isset($response['transactionStatus'])
  83. || !in_array($response['transactionStatus'], array(ConcordPay::TRANSACTION_APPROVED, ConcordPay::TRANSACTION_DECLINED))
  84. ) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. * @return bool
  91. */
  92. function cp_CheckCustomerid()
  93. {
  94. $customerId=trim($_GET['customer_id']);
  95. if (!isset($_GET['customer_id']) || empty($customerId)) {
  96. return false;
  97. }
  98. $allCustomers = op_CustomersGetAll();
  99. if (!array_key_exists(trim($_GET['customer_id']), $allCustomers)) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Check is transaction unique?
  106. *
  107. * @param $hash - transaction hash
  108. *
  109. * @return bool
  110. */
  111. function cp_CheckTransaction($hash)
  112. {
  113. $hash = mysql_real_escape_string($hash);
  114. $query = "SELECT `id` from `op_transactions` WHERE `hash`='" . $hash . "'";
  115. $data = simple_query($query);
  116. if (!empty($data)) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Reports some error
  123. *
  124. * @param string $data
  125. *
  126. * @return void
  127. */
  128. function cp_reportError($data)
  129. {
  130. global $debug;
  131. header('HTTP/1.1 400 ' . $data . '"', true, 400);
  132. if ($debug) {
  133. file_put_contents('./debug.log', date("Y-m-d H:i:s") . ': ' . $data . "\n", FILE_APPEND);
  134. file_put_contents('./debug.log', print_r($_POST, true) . "\n", FILE_APPEND);
  135. file_put_contents('./debug.log', '=========================' . "\n", FILE_APPEND);
  136. }
  137. die($data);
  138. }
  139. /**
  140. * Reports some success
  141. *
  142. * @param string $data
  143. *
  144. * @return void
  145. */
  146. function cp_reportSuccess($data)
  147. {
  148. global $debug;
  149. header('HTTP/1.1 200 ' . $data . '"', true, 200);
  150. if ($debug) {
  151. file_put_contents('./debug.log', date("Y-m-d H:i:s") . ': ' . $data . "\n", FILE_APPEND);
  152. }
  153. die($data);
  154. }
  155. if (cp_CheckResponse($response) !== true) {
  156. cp_reportError($conf_concordpay['ERROR_NO_RESPONSE_DATA']);
  157. }
  158. if (cp_CheckSignature($response) !== true) {
  159. cp_reportError($conf_concordpay['ERROR_WRONG_SIGNATURE']);
  160. }
  161. if (cp_CheckOperationType($response) !== true) {
  162. cp_reportError($conf_concordpay['ERROR_WRONG_OPERATION_TYPE']);
  163. }
  164. if (cp_CheckTransactionStatus($response) !== true) {
  165. cp_reportError($conf_concordpay['ERROR_WRONG_TRANSACTION_STATUS']);
  166. }
  167. if (cp_CheckCustomerid() !== true) {
  168. cp_reportError($conf_concordpay['ERROR_UNKNOWN_CUSTOMER']);
  169. }
  170. $hash = $response['orderReference'];
  171. $customerid = htmlspecialchars(trim($_GET['customer_id']));
  172. $summ = $response['amount'];
  173. $paysys = 'CONCORDPAY';
  174. $note = 'Transaction ID: ' . $response['transactionId'];
  175. if (cp_CheckTransaction($hash) !== true) {
  176. cp_reportError($conf_concordpay['ERROR_DOUBLE_PAYMENT']);
  177. }
  178. if ($response['transactionStatus'] === ConcordPay::TRANSACTION_APPROVED) {
  179. if ($response['type'] === ConcordPay::RESPONSE_TYPE_PAYMENT) {
  180. // Ordinary payment.
  181. // Register a new transaction.
  182. op_TransactionAdd($hash, $summ, $customerid, $paysys, $note);
  183. // Calling the raw transaction handlers.
  184. op_ProcessHandlers();
  185. // Finish the work.
  186. cp_reportSuccess($conf_concordpay['TRANSACTION_SUCCESSFUL']);
  187. }
  188. }