robokassa-ipn.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. if(!isset($_REQUEST["OutSum"]) or !isset($_REQUEST["InvId"]) or !isset($_REQUEST["SignatureValue"]))
  3. exit;
  4. ini_set('log_errors', true);
  5. ini_set('error_log', dirname(__FILE__).'/robokassa-ipn_errors.log');
  6. chdir("../../"); /* It just makes life easier */
  7. set_include_path(get_include_path() . PATH_SEPARATOR . "includes/");
  8. /* Includes */
  9. require_once("helpers.php");
  10. require_once("config.inc.php");
  11. require_once("functions.php");
  12. require_once("lib_remote.php");
  13. require_once("lang.php");
  14. require_once("modules/config_games/server_config_parser.php");
  15. ogpLang();
  16. /* Query DB */
  17. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  18. $panel_settings = $db->getSettings();
  19. // HTTP parameters:
  20. $out_summ = $_REQUEST["OutSum"];
  21. $inv_id = $_REQUEST["InvId"];
  22. $crc = $_REQUEST["SignatureValue"];
  23. $cart_price_info = $db->resultQuery( "SELECT price,tax_amount,currency
  24. FROM OGP_DB_PREFIXbilling_carts AS cart
  25. JOIN
  26. OGP_DB_PREFIXbilling_orders AS orders
  27. ON
  28. orders.cart_id=cart.cart_id
  29. WHERE cart.cart_id=".$inv_id);
  30. $cart_price = number_format( $cart_price_info[0]['price'] + (($cart_price_info[0]['price']/100)*$cart_price_info[0]['tax_amount']) , 2 );
  31. $paid_price = $out_summ;
  32. if($cart_price > $paid_price)
  33. {
  34. // If for some reason someone achieves to hack the price then we will just change the order price.
  35. // By a rule of Three:
  36. // new price without tax = ( new price with tax * old price without tax ) / old price with tax
  37. $new_price = ( ($cart_price - $paid_price) * $cart_price_info[0]['price'] ) / $cart_price;
  38. // we don't want to loose money in this fraudulent transaction,
  39. // so if the rounded new price is less than the new price then we sum one cent to the rounded value.
  40. if($new_price > number_format( $new_price, 2 ))
  41. $new_price = number_format( $new_price, 2 ) + 0.01;
  42. $subject = "Error: Incorrect payment amount";
  43. $body = 'CART ID = '.$inv_id."<br>".
  44. 'Price = '.$cart_price."<br>".
  45. '<b style="color:red;">Amount paid</b>: '.$paid_price."<br>".
  46. '<b style="color:red;">Amount owed</b>: '.$new_price."<br>";
  47. mymail($panel_settings['panel_email_address'], $subject, $body, $panel_settings);
  48. die("Error: Incorrect payment amount");
  49. }
  50. // your registration data
  51. $mrh_pass2 = $panel_settings['robokassa_securepass2']; // merchant pass2 here
  52. // HTTP parameters: $out_summ, $inv_id, $crc
  53. $crc = strtoupper($crc); // force uppercase
  54. // build own CRC
  55. $my_crc = strtoupper(md5("$out_summ:$inv_id:$mrh_pass2"));
  56. if (strtoupper($my_crc) != strtoupper($crc))
  57. {
  58. echo "bad sign\n";
  59. exit();
  60. }
  61. // perform some action (change order state to paid)
  62. else
  63. {
  64. $body = 'Amount paid : '.$out_summ."<br>".
  65. 'CART ID : '.$inv_id."<br>";
  66. // Here you can do whatever you want with the variables, for instance inserting or updating data into your Database
  67. $user_homes = $db->resultQuery( "SELECT *
  68. FROM OGP_DB_PREFIXbilling_carts AS cart
  69. JOIN
  70. OGP_DB_PREFIXbilling_orders AS orders
  71. ON
  72. orders.cart_id=cart.cart_id
  73. WHERE cart.cart_id=".$cart_id);
  74. $query = "UPDATE " . $table_prefix . "billing_carts
  75. SET paid=1
  76. WHERE cart_id=".$cart_id;
  77. foreach($user_homes as $user_home)
  78. {
  79. if($user_home['home_id'] != 0)
  80. {
  81. $home_id = $user_home['home_id'];
  82. $home_info = $db->getGameHomeWithoutMods($home_id);
  83. $server_info = $db->getRemoteServerById($home_info['remote_server_id']);
  84. $remote = new OGPRemoteLibrary($server_info['agent_ip'], $server_info['agent_port'], $server_info['encryption_key'], $server_info['timeout']);
  85. if ( isset( $home_info['ftp_password'] ) AND !empty( $home_info['ftp_password'] ) )
  86. {
  87. $remote->ftp_mgr("useradd", $home_info['home_id'], $home_info['ftp_password'], $home_info['home_path']);
  88. $db->changeFtpStatus('enabled',$home_info['home_id']);
  89. }
  90. if ($user_home['end_date'] == "0")
  91. {
  92. if ($user_home['invoice_duration'] == "hour")
  93. {
  94. $add_time = time() + ($user_home['qty'] * 60 * 60);
  95. $end_date = date('YmdHi',$add_time);
  96. }
  97. elseif ($user_home['invoice_duration'] == "month")
  98. {
  99. $end_date = date('YmdHi', strtotime('+'.$user_home['qty'].' month'));
  100. }
  101. elseif ($user_home['invoice_duration'] == "year")
  102. {
  103. $end_date = date('YmdHi', strtotime('+'.$user_home['qty'].' year'));
  104. }
  105. //Set the expiration date to the new order
  106. $db->query( "UPDATE " . $table_prefix . "billing_orders
  107. SET end_date='$end_date'
  108. WHERE order_id=".$user_home['order_id']);
  109. // Set payment/creation date
  110. $date = date('d/m/Y H:i');
  111. $db->query( "UPDATE OGP_DB_PREFIXbilling_carts
  112. SET date='$date'
  113. WHERE cart_id=".$cart_id);
  114. }
  115. $services = $db->resultQuery( "SELECT *
  116. FROM OGP_DB_PREFIXbilling_services
  117. WHERE service_id=".$user_home['service_id']);
  118. $service = $services[0];
  119. $user_id = $user_home['user_id'];
  120. $db->assignHomeTo("user", $user_id, $home_id, $service['access_rights']);
  121. $query = "UPDATE " . $table_prefix . "billing_carts
  122. SET paid=3
  123. WHERE cart_id=".$cart_id;
  124. }
  125. }
  126. $db->query($query);
  127. $subject = "Payment done.";
  128. mymail($panel_settings['panel_email_address'], $subject, $body, $panel_settings);
  129. }
  130. ?>