paid-ipn.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. ini_set('log_errors', true);
  3. ini_set('error_log', dirname(__FILE__).'/ipn_errors.log');
  4. // instantiate the IpnListener class
  5. include('ipnlistener.php');
  6. $listener = new IpnListener();
  7. // Enable sandbox for developers (https://developer.paypal.com)
  8. //$listener->use_sandbox = true;
  9. try {
  10. $listener->requirePostMethod();
  11. $verified = $listener->processIpn();
  12. } catch (Exception $e) {
  13. error_log($e->getMessage());
  14. }
  15. chdir("../../"); /* It just makes life easier */
  16. set_include_path(get_include_path() . PATH_SEPARATOR . "includes/");
  17. /* Includes */
  18. require_once("helpers.php");
  19. require_once("config.inc.php");
  20. require_once("functions.php");
  21. require_once("lib_remote.php");
  22. require_once("lang.php");
  23. require_once("modules/config_games/server_config_parser.php");
  24. ogpLang();
  25. /* Query DB */
  26. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  27. $panel_settings = $db->getSettings();
  28. $s = ( isset($_SERVER['HTTPS']) and get_true_boolean($_SERVER['HTTPS']) ) ? "s" : "";
  29. $p = isset($_SERVER['SERVER_PORT']) & $_SERVER['SERVER_PORT'] != "80" ? ":".$_SERVER['SERVER_PORT'] : NULL ;
  30. $this_script = 'http'.$s.'://'.$_SERVER['SERVER_NAME'].$p.$_SERVER['SCRIPT_NAME'];
  31. function curPageName()
  32. {
  33. return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
  34. }
  35. $current_folder_url = str_replace( curPageName(), "", $this_script);
  36. if( empty( $panel_settings['panel_name'] ) )
  37. $panel_name = "Open Game Panel";
  38. else
  39. $panel_name = $panel_settings['panel_name'];
  40. $ipn = $_POST;
  41. if(empty($ipn))
  42. {
  43. exit(0);
  44. }
  45. $to = $ipn['receiver_email'] . ', ' . $ipn['payer_email'];
  46. $body = "<b>PayPal Payment For <a href='".
  47. $current_folder_url.
  48. "../../index.php?m=simple-billing&p=shop_guest' >".
  49. $panel_name."</a></b><br><br>".
  50. "<h2>Order</h2>".
  51. "- Item: ".$ipn['item_name']."<br>".
  52. "- Item number: ".$ipn['item_number']."<br>".
  53. "- Quantity: ".$ipn['quantity']."<br>".
  54. "- Shipping: ".$ipn['shipping']."<br>".
  55. "- Tax: ".$ipn['tax']."<br>".
  56. "- Currency: ".$ipn['mc_currency']."<br>".
  57. "- Currency fee: ".$ipn['mc_fee']."<br>".
  58. "- Currency gross: ".$ipn['mc_gross']."<br>".
  59. "- Transaction type: ".$ipn['txn_type']."<br>".
  60. "- Transaction ID: ".$ipn['txn_id']."<br>".
  61. "- Notify version: ".$ipn['notify_version']."<br><br>".
  62. "<h2>Payer Info</h2>".
  63. "- ID: ".$ipn['payer_id']."<br>".
  64. "- First name: ".$ipn['first_name']."<br>".
  65. "- Last name: ".$ipn['last_name']."<br>".
  66. "- Email: ".$ipn['payer_email']."<br>".
  67. "- Email status: ".$ipn['payer_status']."<br><br>".
  68. "<h2>Address</h2>".
  69. "- Name: ".$ipn['address_name']."<br>".
  70. "- Street: ".$ipn['address_street']."<br>".
  71. "- City: ".$ipn['address_city']."<br>".
  72. "- State: ".$ipn['address_state']."<br>".
  73. "- Zip: ".$ipn['address_zip']."<br>".
  74. "- Country code: ".$ipn['address_country_code']."<br>".
  75. "- Country: ".$ipn['address_country']."<br>".
  76. "- Residence country code: ".$ipn['residence_country']."<br>".
  77. "- Address status: ".$ipn['address_status']."<br><br>".
  78. "<h2>Payment Receiver Info</h2>".
  79. "- Email: ".$ipn['receiver_email']."<br>".
  80. "- ID: ".$ipn['receiver_id']."<br><br>".
  81. "<h2>Payment</h2>".
  82. "- Type: ".$ipn['payment_type']."<br>".
  83. "- Date: ".$ipn['payment_date']."<br>".
  84. "- Status: ".$ipn['payment_status']."<br>";
  85. /*
  86. The processIpn() method returned true if the IPN was "VERIFIED" and false if it
  87. was "INVALID".
  88. */
  89. if ($verified AND isset( $ipn['payment_status'] ) )
  90. {
  91. $user_homes = $db->resultQuery( "SELECT *
  92. FROM OGP_DB_PREFIXbilling_carts AS cart
  93. JOIN
  94. OGP_DB_PREFIXbilling_orders AS orders
  95. ON
  96. orders.cart_id=cart.cart_id
  97. WHERE cart.cart_id=".$db->realEscapeSingle($ipn['item_number']));
  98. if( $ipn['payment_status']=="Completed" OR $ipn['payment_status']=="Canceled_Reversal" )
  99. {
  100. $cart_id = $ipn['item_number'];
  101. $cart_price_info = $db->resultQuery( "SELECT price,tax_amount
  102. FROM OGP_DB_PREFIXbilling_carts AS cart
  103. JOIN
  104. OGP_DB_PREFIXbilling_orders AS orders
  105. ON
  106. orders.cart_id=cart.cart_id
  107. WHERE cart.cart_id=".$db->realEscapeSingle($cart_id));
  108. $cart_price = number_format( $cart_price_info[0]['price'] + (($cart_price_info[0]['price']/100)*$cart_price_info[0]['tax_amount']) , 2 );
  109. $paid_price = $ipn['mc_gross'];
  110. if($cart_price > $paid_price)
  111. {
  112. // If for some reason someone achieves to hack the price then we will just change the order price.
  113. // By a rule of Three:
  114. // new price without tax = ( new price with tax * old price without tax ) / old price with tax
  115. $new_price = ( ($cart_price - $paid_price) * $cart_price_info[0]['price'] ) / $cart_price;
  116. // we don't want to loose money in this fraudulent transaction,
  117. // so if the rounded new price is less than the new price then we sum one cent to the rounded value.
  118. if($new_price > number_format( $new_price, 2 ))
  119. $new_price = number_format( $new_price, 2 ) + 0.01;
  120. $subject = "Error: Incorrect payment amount";
  121. $body = "<b>PayPal Payment For <a href='".
  122. $current_folder_url.
  123. "../../index.php?m=simple-billing&p=shop_guest' >".
  124. $panel_name."</a></b><br><br>".
  125. "<h2>Order</h2>".
  126. "- Item: ".$ipn['item_name']."<br>".
  127. "- Item number: ".$ipn['item_number']."<br>".
  128. "- Quantity: ".$ipn['quantity']."<br>".
  129. "- Shipping: ".$ipn['shipping']."<br>".
  130. "- Tax: ".$ipn['tax']."<br>".
  131. "- Currency: ".$ipn['mc_currency']."<br>".
  132. "- Currency fee: ".$ipn['mc_fee']."<br>".
  133. "- Currency gross: ".$ipn['mc_gross']."<br>".
  134. "- Transaction type: ".$ipn['txn_type']."<br>".
  135. "- Transaction ID: ".$ipn['txn_id']."<br>".
  136. "- Notify version: ".$ipn['notify_version']."<br><br>".
  137. "<h2>Payer Info</h2>".
  138. "- ID: ".$ipn['payer_id']."<br>".
  139. "- First name: ".$ipn['first_name']."<br>".
  140. "- Last name: ".$ipn['last_name']."<br>".
  141. "- Email: ".$ipn['payer_email']."<br>".
  142. "- Email status: ".$ipn['payer_status']."<br><br>".
  143. "<h2>Address</h2>".
  144. "- Name: ".$ipn['address_name']."<br>".
  145. "- Street: ".$ipn['address_street']."<br>".
  146. "- City: ".$ipn['address_city']."<br>".
  147. "- State: ".$ipn['address_state']."<br>".
  148. "- Zip: ".$ipn['address_zip']."<br>".
  149. "- Country code: ".$ipn['address_country_code']."<br>".
  150. "- Country: ".$ipn['address_country']."<br>".
  151. "- Residence country code: ".$ipn['residence_country']."<br>".
  152. "- Address status: ".$ipn['address_status']."<br><br>".
  153. "<h2>Payment Receiver Info</h2>".
  154. "- Email: ".$ipn['receiver_email']."<br>".
  155. "- ID: ".$ipn['receiver_id']."<br><br>".
  156. "<h2>Payment</h2>".
  157. "- Type: ".$ipn['payment_type']."<br>".
  158. "- Date: ".$ipn['payment_date']."<br>".
  159. "- Status: ".$ipn['payment_status']."<br>".
  160. '<b style="color:red;">amount paid</b>: '.$paid_price."<br>".
  161. '<b style="color:red;">amount owed</b>: '.$new_price."<br>";
  162. mymail($panel_settings['panel_email_address'], $subject, $body, $panel_settings);
  163. die("Error: Incorrect payment amount");
  164. }
  165. $query = "UPDATE OGP_DB_PREFIXbilling_carts
  166. SET paid=1
  167. WHERE cart_id=".$db->realEscapeSingle($ipn['item_number']);
  168. foreach($user_homes as $user_home)
  169. {
  170. if($user_home['home_id'] != 0)
  171. {
  172. $home_id = $user_home['home_id'];
  173. $home_info = $db->getGameHomeWithoutMods($home_id);
  174. $server_info = $db->getRemoteServerById($home_info['remote_server_id']);
  175. $remote = new OGPRemoteLibrary($server_info['agent_ip'], $server_info['agent_port'], $server_info['encryption_key'], $server_info['timeout']);
  176. if ( isset( $home_info['ftp_password'] ) AND !empty( $home_info['ftp_password'] ) )
  177. {
  178. $remote->ftp_mgr("useradd", $home_info['home_id'], $home_info['ftp_password'], $home_info['home_path']);
  179. $db->changeFtpStatus('enabled',$home_info['home_id']);
  180. }
  181. if ($user_home['end_date'] == "0")
  182. {
  183. if ($user_home['invoice_duration'] == "hour")
  184. {
  185. $add_time = time() + ($user_home['qty'] * 60 * 60);
  186. $end_date = date('YmdHi',$add_time);
  187. $period_to_extend = time() + ( ( $user_home['qty'] * 60 * 60 ) + 900 ); // Fifteen minutes to extend or finish the server.
  188. $finish_date = date('YmdHi',$period_to_extend);
  189. }
  190. elseif ($user_home['invoice_duration'] == "month")
  191. {
  192. $end_date = date('YmdHi', strtotime('+'.$user_home['qty'].' month'));
  193. $finish_date = date('YmdHi', strtotime('+'.$user_home['qty'].' month 5 day'));
  194. }
  195. elseif ($user_home['invoice_duration'] == "year")
  196. {
  197. $end_date = date('YmdHi', strtotime('+'.$user_home['qty'].' year'));
  198. $finish_date = date('YmdHi', strtotime('+'.$user_home['qty'].' year 15 day'));
  199. }
  200. //Set the expiration date to the new order
  201. $db->query("UPDATE OGP_DB_PREFIXbilling_orders
  202. SET end_date='" . $db->realEscapeSingle($end_date) . "'
  203. WHERE order_id=". $db->realEscapeSingle($user_home['order_id']));
  204. $db->query("UPDATE OGP_DB_PREFIXbilling_orders
  205. SET finish_date='" . $db->realEscapeSingle($finish_date) . "'
  206. WHERE order_id=".$db->realEscapeSingle($user_home['order_id']));
  207. // Set payment/creation date
  208. $date = date('d/m/Y H:i');
  209. $db->query("UPDATE OGP_DB_PREFIXbilling_carts
  210. SET date='$date'
  211. WHERE cart_id=".$db->realEscapeSingle($ipn['item_number']));
  212. }
  213. $services = $db->resultQuery( "SELECT *
  214. FROM OGP_DB_PREFIXbilling_services
  215. WHERE service_id=".$db->realEscapeSingle($user_home['service_id']));
  216. $service = $services[0];
  217. $user_id = $user_home['user_id'];
  218. $db->assignHomeTo("user", $user_id, $home_id, $service['access_rights']);
  219. $query = "UPDATE OGP_DB_PREFIXbilling_carts
  220. SET paid=3
  221. WHERE cart_id=".$db->realEscapeSingle($ipn['item_number']);
  222. }
  223. }
  224. }
  225. elseif( $ipn['payment_status']=="Pending" OR $ipn['payment_status']=="In-Progress" )
  226. {
  227. $query = "UPDATE OGP_DB_PREFIXbilling_carts
  228. SET paid=2
  229. WHERE cart_id=".$db->realEscapeSingle($ipn['item_number']);
  230. }
  231. elseif( $ipn['payment_status']=="Reversed" OR $ipn['payment_status']=="Refunded" OR $ipn['payment_status']=="Denied" OR $ipn['payment_status']=="Expired" OR $ipn['payment_status']=="Failed" OR $ipn['payment_status']=="Voided" OR $ipn['payment_status']=="Partially_Refunded" )
  232. {
  233. $body .= "- Reason code: ".$ipn['reason_code'];
  234. $query = "UPDATE OGP_DB_PREFIXbilling_carts
  235. SET paid=0
  236. WHERE cart_id=".$db->realEscapeSingle($ipn['item_number']);
  237. foreach($user_homes as $user_home)
  238. {
  239. $user_id = $user_home['user_id'];
  240. if($user_home['home_id'] != 0)
  241. {
  242. $home_id = $user_home['home_id'];
  243. $home_info = $db->getGameHomeWithoutMods($home_id);
  244. $server_info = $db->getRemoteServerById($home_info['remote_server_id']);
  245. $remote = new OGPRemoteLibrary($server_info['agent_ip'], $server_info['agent_port'], $server_info['encryption_key'], $server_info['timeout']);
  246. $update_ftp_users = "pure-pw userdel ".$home_id." && pure-pw mkdb";
  247. $remote->sudo_exec( $update_ftp_users );
  248. $addresses = $db->getHomeIpPorts($home_id);
  249. foreach($addresses as $address)
  250. {
  251. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  252. if(isset($server_xml->control_protocol_type))$control_type = $server_xml->control_protocol_type; else $control_type = "";
  253. $remote->remote_stop_server($home_id,$address['ip'],$address['port'],$server_xml->control_protocol,$home_info['control_password'],$control_type);
  254. }
  255. $db->unassignHomeFrom("user", $user_id, $home_id);
  256. /*
  257. // Remove the game home from db
  258. $db->deleteGameHome($home_id);
  259. // Remove the game home files from remote server
  260. $remote->remove_home($home_info['home_path']);
  261. // Set order as not installed
  262. $db->resultQuery( "UPDATE OGP_DB_PREFIXbilling_orders
  263. SET home_id=0
  264. WHERE home_id=".$home_id);
  265. */
  266. }
  267. }
  268. }
  269. $db->query($query);
  270. $subject = "Payment ".$ipn['payment_status'];
  271. mymail($to, $subject, $body, $panel_settings);
  272. }
  273. ?>