Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.61 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PayPal IPN doesn't go through without errors
  2. <?php
  3.  
  4. include('./core/session.class.php');
  5.  
  6. $host = "localhost";
  7. $user = "***";
  8. $pass = "***";
  9. $dbname = "***";
  10.  
  11. $connect = mysql_connect($host, $user, $pass) or die("COULD NOT CONNECT");
  12. $select_db = mysql_select_db($dbname, $connect) or die("INFO NOT FOUND");
  13.  
  14. // PHP 4.1
  15. // read the post from PayPal system and add 'cmd'
  16. $req = 'cmd=_notify-validate';
  17.  
  18. foreach ($_POST as $key => $value) {
  19.     $value = urlencode(stripslashes($value));
  20.     $req .= "&$key=$value";
  21. }
  22.  
  23. // post back to PayPal system to validate
  24. $header .= "POST /cgi-bin/webscr HTTP/1.0rn";
  25. $header .= "Content-Type: application/x-www-form-urlencodedrn";
  26. $header .= "Content-Length: " . strlen($req) . "rnrn";
  27. $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
  28.  
  29. // assign posted variables to local variables
  30. $item_name = $_POST['item_name'];
  31. $item_number = $_POST['item_number'];
  32. $payment_status = $_POST['payment_status'];
  33. $payment_amount = $_POST['mc_gross'];
  34. $payment_currency = $_POST['mc_currency'];
  35. $txn_id = $_POST['txn_id'];
  36. $receiver_email = $_POST['receiver_email'];
  37. $payer_email = $_POST['payer_email'];
  38. $custom = (int) $_POST['custom'];
  39.  
  40. $string = explode("_", $custom);
  41.  
  42. $place_id = $string[0];
  43. $account_id = $string[1];
  44. $member_type = $item_number;
  45.  
  46. if (!$fp) {
  47. // HTTP ERROR
  48. } else {
  49.     fputs($fp, $header . $req);
  50.     while (!feof($fp)) {
  51.         $res = fgets($fp, 1024);
  52.         if (strcmp($res, "VERIFIED") == 0) {
  53.             if ($payment_status == "Completed") {
  54.                 $check_txn_sql = "SELECT `txn_id`,`account_id` FROM `log` WHERE `txn_id`='$txn_id'";
  55.                 $check_txn_query = mysql_query($check_txn_sql) or die("Cannot check transaction.");
  56.  
  57.                 if (mysql_num_rows($check_txn_query) != 1) {
  58.                     if ($receiver_email == "example@email.com") {
  59.                         if ($payment_amount == "0.01" && $payment_currency == "USD") {
  60.  
  61.                             $log_txn_sql = "INSERT INTO `log` VALUES ('','$txn_id','$payer_email')";
  62.                             $log_txn_query = mysql_query($log_txn_sql) or die("Cannot log the transaction.");
  63.  
  64.                             if ($log_txn_query) {
  65.                                 $add_connect_sql = "INSERT INTO `connect` VALUES ('','$account_id','$place_id','$member_type','0','1','" . time() . "','0','0','0')";
  66.                                 $add_connect_query = mysql_query($add_connect_sql) or die("Cannot subscribe.");
  67.                             }
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         } else if (strcmp($res, "INVALID") == 0) {
  73.         }
  74.     }
  75.     fclose($fp);
  76. }
  77. ?>