SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    4.1 vs 5.2 PHP version and PayPal IPN

    Hello!

    I implemented a working model for using PayPal's IPN to verify payment with PHP version 4.1. I'm now using PHP version 5.2.17 and all of a sudden, the code doesn't work; while my PayPal payments are going through, my database isn't changing, nor am I receiving an error message. I thought that perhaps it was a magic_quotes issue, and so changed one of the lines below (stripslashes) but to no avail. Might there be some other piece of code that I need to change in order for it to work with my newer version of PHP?

    Thanks for looking at the code,

    Eric

    Code PHP:
    <?php // PHP 4.1
    require_once('../main_scripts/library.php'); /*used for my Zend_mail at the bottom */
    require_once('/databaseconfigstuff.php'); /*Not the real location..but my config stuff for my database */
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
     
    foreach ($_POST as $key => $value) {
    /* I changed the following line because no need for MagicQuotes
    $value = urlencode(stripslashes($value)); */
    $value = urlencode($value);
    $req .= "&$key=$value";
    }
     
    // post back to PayPal system to validate
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    $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'];
    $user_id = $_POST['custom']; //user's id
     
     
     
     
    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') {
    	$txn_id_check = mysql_query ("SELECT txn_id FROM log WHERE txn_id='$txn_id'");
    	if (mysql_num_rows($txn_id_check)!=1) {
    		if ($receiver_email=='support@myalgebrabook.com') {
    			if ($payment_amount=='1'&&$payment_currency=='USD') {
     
    				//add txn_id to database
    				$log_query=mysql_query("INSERT INTO log VALUES ('','".$txn_id."','".$payer_email."','')");
    				//update paid_status to paid
    				$update_paid_status = mysql_query("UPDATE users SET paid_status=1 WHERE user_id='$user_id'");
    				$course_id_query=mysql_query("SELECT course_id FROM users WHERE user_id='$user_id'");
    				$course_id=mysql_result($course_id_query,0,'course_id');
    				$update_course_enrollment = mysql_query("INSERT INTO course_enrollment (user_id, course_id) VALUES ('".$user_id."','".$course_id."')");
     
     
     
    }
    		}
    	}
    }
    }
    else if (strcmp ($res, "INVALID") == 0) {
    $mail = new Zend_Mail('UTF-8');
    	$mail->addTo('support@myalgebrabook.com');
    	$mail->setFrom('support@myalgebrabook.com');
    	$mail->setSubject('Problem with PayPal');
    	$text = 'There was a problem here.';
    	$mail->setBodyHtml($text, 'UTF-8');
    	$success = $mail->send();
    }
    }
    fclose ($fp);
    }
    ?>

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Turn error reporting on...? And why 5.2...why not 5.3? 5.2 is no longer maintained or updated. IT NO LONGER GETS SECURITY UPDATES!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for getting back to me! First, I now use the correct version of PHP.

    Second, I turned error reporting on and nothing seemed to be wrong. After trying a bunch of different things I realized that PayPal is not even making it to my IPN.php script. Ugh....

    After searching a bit online, I see that this could be a hosting issue (I recently changed hosts) --- some hosts don't allow this sort of thing for some reason. I'm not sure where this would fall on SitePoint topics, but if you happen to know anything about this issue, or might be able to point me in the right direction, I'd appreciate it.


    Thanks so much,

    Eric


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •