paygol-ipn.php 5.6 KB

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