- PayPal IPN doesn't go through without errors
- <?php
- include('./core/session.class.php');
- $host = "localhost";
- $user = "***";
- $pass = "***";
- $dbname = "***";
- $connect = mysql_connect($host, $user, $pass) or die("COULD NOT CONNECT");
- $select_db = mysql_select_db($dbname, $connect) or die("INFO NOT FOUND");
- // PHP 4.1
- // read the post from PayPal system and add 'cmd'
- $req = 'cmd=_notify-validate';
- foreach ($_POST as $key => $value) {
- $value = urlencode(stripslashes($value));
- $req .= "&$key=$value";
- }
- // post back to PayPal system to validate
- $header .= "POST /cgi-bin/webscr HTTP/1.0rn";
- $header .= "Content-Type: application/x-www-form-urlencodedrn";
- $header .= "Content-Length: " . strlen($req) . "rnrn";
- $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
- // assign posted variables to local variables
- $item_name = $_POST['item_name'];
- $item_number = $_POST['item_number'];
- $payment_status = $_POST['payment_status'];
- $payment_amount = $_POST['mc_gross'];
- $payment_currency = $_POST['mc_currency'];
- $txn_id = $_POST['txn_id'];
- $receiver_email = $_POST['receiver_email'];
- $payer_email = $_POST['payer_email'];
- $custom = (int) $_POST['custom'];
- $string = explode("_", $custom);
- $place_id = $string[0];
- $account_id = $string[1];
- $member_type = $item_number;
- if (!$fp) {
- // HTTP ERROR
- } else {
- fputs($fp, $header . $req);
- while (!feof($fp)) {
- $res = fgets($fp, 1024);
- if (strcmp($res, "VERIFIED") == 0) {
- if ($payment_status == "Completed") {
- $check_txn_sql = "SELECT `txn_id`,`account_id` FROM `log` WHERE `txn_id`='$txn_id'";
- $check_txn_query = mysql_query($check_txn_sql) or die("Cannot check transaction.");
- if (mysql_num_rows($check_txn_query) != 1) {
- if ($receiver_email == "example@email.com") {
- if ($payment_amount == "0.01" && $payment_currency == "USD") {
- $log_txn_sql = "INSERT INTO `log` VALUES ('','$txn_id','$payer_email')";
- $log_txn_query = mysql_query($log_txn_sql) or die("Cannot log the transaction.");
- if ($log_txn_query) {
- $add_connect_sql = "INSERT INTO `connect` VALUES ('','$account_id','$place_id','$member_type','0','1','" . time() . "','0','0','0')";
- $add_connect_query = mysql_query($add_connect_sql) or die("Cannot subscribe.");
- }
- }
- }
- }
- }
- } else if (strcmp($res, "INVALID") == 0) {
- }
- }
- fclose($fp);
- }
- ?>