views:

162

answers:

0

Hi all Im having a really hard time getting my head around this. I have a simple PayPal IPN script;

// 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.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);


// HTTP ERROR
if( $fp ) {
    fputs ($fp, $header . $req);
    while ( !feof($fp) ) {
        $res = fgets ($fp, 1024);   
        if (strcmp ($res, "VERIFIED") == 0) {
            // PAYMENT VALIDATED & VERIFIED!

            mail( '[email protected]', 'thing', "yes" );

            $CON = mysql_connect( DB_SERVER, DB_USERNAME, DB_PASSWORD ) or die ( mysql_error() );
            $DB = mysql_select_db( DB_NAME, $CON );

            $TEMP_REF = $post['custom'];
            $query = "SELECT * FROM `nnn_temp_accounts` WHERE TEMP_REF = '$TEMP_REF'";

            $q = mysql_query( $query, $CON ) or die( mysql_error() );
            if( mysql_num_rows( $q ) != 0 ) {

                $rows = mysql_fetch_array( $q );
                $id = $rows['ID'];

                $vars = "( ";
                $vals = "( ";
                $x=0;
                foreach( $rows as $key => $val ) {
                    if( $x !== 0 ) {
                        $vars .= $key . ", ";
                        $vals .= "'$val', ";
                    }
                    $x++;
                }
                $vars = substr( $vars, 0, -2 ) . " )";
                $vals = substr( $vals, 0, -2 ) . " )";
                $insert_query = "INSERT INTO `nnn_accounts` $vars VALUES $vals";
                mysql_query( $insert_query, $CON ) or die( mysql_error() );
                $delete_query = "DELETE FROM `nnn_temp_accounts` WHERE ID = '$id'";
                mysql_query( $delete_query, $CON ) or die( mysql_error() );

            }else{

            }           
        }elseif (strcmp ($res, "INVALID") == 0) {
            // PAYMENT INVALID & INVESTIGATE MANUALY!
            mail( '[email protected]', 'thing', "no" );
        }
    }
    fclose ($fp);
}else{
    mail( '[email protected]', 'thing', 'no 2' ); 
}

When this if finished the IPN is sent and both emails ( one to say new subscription created, second to say payment accepted ) that I have rigged both say "no".

yet if I log into paypal, go to the IPN history and resend the most rencent IPN, i then get an email saying "yes".

Can anyone explain how and why the above is not working?

Regards,

Phil