Unsupported scripts and control panel web app for a hosting company
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipn.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?PHP
  2. require_once('lib/common.php');
  3. require_once('lib/database.php');
  4. // Log the transaction
  5. $count = count(glob('/home/utd/public_html/ipn/*.html'));
  6. $count++; $id = str_pad($count,5,'0',STR_PAD_LEFT); define('ID', $id);
  7. $data = '<html><head><title>IPN Transaction details</title></head><body>';
  8. $data .= '<h2>Post details</h2><table>';
  9. foreach ($_POST as $k => $v) {
  10. $data .= '<tr><td>'.htmlentities($k).'</td>';
  11. $data .= '<td>'.htmlentities($v).'</td></tr>';
  12. }
  13. $data .= '</table><h2>Server details</h2><table>';
  14. foreach ($_SERVER as $k => $v) {
  15. if (is_array($v)) { continue; }
  16. $data .= '<tr><td>'.htmlentities($k).'</td>';
  17. $data .= '<td>'.htmlentities($v).'</td></tr>';
  18. }
  19. $data .= '</table></html>';
  20. file_put_contents('/home/utd/public_html/ipn/'.ID.'.html', $data);
  21. // Read the post from PayPal system and add 'cmd'
  22. $req = 'cmd=_notify-validate';
  23. foreach ($_POST as $key => $value) {
  24. $value = urlencode(stripslashes($value));
  25. $req .= "&$key=$value";
  26. }
  27. // Post back to PayPal system to validate
  28. $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  29. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  30. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  31. $sb = '';
  32. $fp = fsockopen ('www.'.$sb.'paypal.com', 80, $errno, $errstr, 30);
  33. if (!$fp) { fail('Unable to connect to paypal'); }
  34. // assign posted variables to local variables
  35. $item_name = $_POST['item_name'];
  36. $item_number = $_POST['item_number'];
  37. $payment_status = $_POST['payment_status'];
  38. $payment_amount = $_POST['mc_gross'];
  39. $payment_currency = $_POST['mc_currency'];
  40. $txn_id = $_POST['txn_id'];
  41. $receiver_email = strtolower($_POST['receiver_email']);
  42. $payer_email = $_POST['payer_email'];
  43. function fail($m) {
  44. logger::log(chr(2).'IPN'.chr(2).': Transaction '.ID.': Failure: '.$m, logger::important);
  45. exit;
  46. }
  47. if (!$fp) {
  48. fail('HTTP error when posting back: '.$errstr);
  49. } else {
  50. fputs ($fp, $header . $req);
  51. while (!feof($fp)) {
  52. $res = fgets ($fp, 1024);
  53. if (strcmp ($res, "VERIFIED") == 0) {
  54. // check the payment_status is Completed
  55. if ($payment_status != 'Completed') {
  56. fail('Payment status is '.$payment_status.' (expected "Completed")');
  57. }
  58. // check that txn_id has not been previously processed
  59. // check that receiver_email is your Primary PayPal email
  60. if ($receiver_email != 'chris87@gmail.com'
  61. && $receiver_email != 'accounts@utd-hosting.com') {
  62. fail('Receiver is '.$receiver_email);
  63. }
  64. // check that payment_amount/payment_currency are correct
  65. if ($payment_currency != 'GBP') {
  66. fail('Invalid currency: '.$payment_currency);
  67. }
  68. $id = preg_replace('~^.*#([0-9]+)$~', '\1', $item_name);
  69. if (!is_numeric($id)) {
  70. fail('Unable to parse item_name: '.$item_name);
  71. }
  72. $sql = 'SELECT user_id, user_name, bill_total, bill_paid';
  73. $sql .= ' FROM bills NATURAL JOIN users WHERE bill_id = '.$id;
  74. $res = mysql_query($sql) or mf(__FILE__, __LINE__, $sql);
  75. if (mysql_num_rows($res) == 1) {
  76. $row = mysql_fetch_array($res);
  77. $amount = $payment_amount * 100;
  78. if ($amount != $row['bill_total'] || $row['bill_paid'] == 1) {
  79. fail('bill_total is incorrect, or bill already paid');
  80. }
  81. $sql = 'UPDATE bills SET bill_paid = 1 WHERE bill_id = '.$id;
  82. $res = mysql_query($sql) or fail('SQL error: '.mysql_error());
  83. $sql = 'UPDATE userpackages, billitems, packages SET up_cost = package_cost, up_expires = up_expires + package_duration WHERE bill_id = '.$id.' AND userpackages.up_id = billitems.up_id AND packages.package_id = userpackages.package_id';
  84. $res = mysql_query($sql) or fail('SQL error: '.mysql_error());
  85. $sql = 'SELECT finance_balance FROM finances ORDER BY finance_time DESC';
  86. $res = mysql_query($sql) or fail('SQL error: '.mysql_error());
  87. $ro2 = mysql_fetch_array($res); $balance = $ro2[0];
  88. $sql = 'INSERT INTO finances (finance_time, finance_desc, user_id,';
  89. $sql .= ' finance_receipts, finance_payments, finance_balance) VALUES (';
  90. $sql .= time().', \'Bill payment\', '.$row['user_id'].', ';
  91. $sql .= $row['bill_amount'].', '.($_POST['mc_fee']*100).', ';
  92. $sql .= ($balance+$row['bill_amount']-($_POST['mc_fee']*100)).')';
  93. $res = mysql_query($sql) or fail('SQL error: '.mysql_error());
  94. logger::log('User '.chr(2).$row['user_name'].chr(2).': Bill '.$id.' paid.', logger::normal);
  95. } else {
  96. fail('Bill not found: '.$id);
  97. }
  98. } else if (strcmp ($res, "INVALID") == 0) {
  99. fail('INVALID REQUEST -- INVESTIGATE -- http://admin.utd-hosting.com/ipn/'.ID.'.html');
  100. }
  101. }
  102. fclose ($fp);
  103. }
  104. ?>