Jump to content
Larry Ullman's Book Forums

Verified/Invalid Section Not Working In Ipn Page


Recommended Posts

I set up my sandbox account and built my ipn based on book. I am having trouble passing through "VERIFIED" section.

 

If code is like this below:

 

        if(strcmp($res, "VERIFIED") == 0){

 

it's not going through.

 

But if I put less than 0 like below:

 

        if(strcmp($res, "VERIFIED") < 0){

 

it works perfectly.

 

I assume that the string in $res is shorter or lower characters than the string "VERIFIED". Since I don't know the exact string that I get from $res, I don't seem to find any solution about verification process.

 

I am putting my code below in case you can find errors outside of this section. Please see my entire ipn page code below.

 

Thanks in advance.

 

=========================================================================

<?php
require('../codes/common.php');
require('../function/function.php');
global $dbc;
global $g_db_info;
$tcm_advertise_info = $g_db_info['tcm_advertise_info']['table'];
$tcm_ad_orders = $g_db_info['tcm_ad_orders']['table'];
$error_log = $g_db_info['error_log']['table'];
date_default_timezone_set('America/Toronto');
$today = date("Y-m-d H:i:s", time());
// Start by creating a request variable:
$req = 'cmd=_notify-validate';

// Add each received key=value pair to the request:
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}
        
// Open a socket connection to PayPal:
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live

if (!$fp) { // If we couldn't connect, send an email:

        //append record to error log
        $obj_value['dt_create'] = $today;
        $obj_value['dt_last_update'] = $today;
        $obj_value['error_msg'] = 'It could not connect to my IPN in paypal.';
        update_query($results, "error_log", $obj_value);
    
} else { // Send the request to PayPal:

    $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";
    fputs ($fp, $header . $req);

    // Read in the response:
    while (!feof($fp)) {

        $res = fgets ($fp, 1024);
        
        if(strcmp($res, "VERIFIED") == 0){

            // Check for the right values:
            //receiver email is a merchant email address (thechurchmap email)
            if ( isset($_POST['payment_status'])
             && ($_POST['payment_status'] == 'Completed')
             && ($_POST['receiver_email'] == 'forkhyun@gmail.com')
             && ($_POST['mc_gross'] == 200.00)
             && ($_POST['mc_currency']  == 'CAD')
             && (!empty($_POST['txn_id']))
            ) {
                                
                // Check for this transaction in the database:
                $txn_id = $_POST['txn_id'];                
                //select tcm_advertise_info table
                $obj_carriers="";
                $obj_carriers['transaction_id'] = $txn_id;
                select_data($results, "tcm_ad_orders", $obj_carriers, "", "", "", "");
                if (mysqli_num_rows($results) == 0) { // Add this new transaction:
                    
                    $uid = (isset($_POST['custom'])) ? (int) $_POST['custom'] : 0;
                    $status = mysqli_real_escape_string($dbc, $_POST['payment_status']);
                    $amount = (float) $_POST['mc_gross'];

                    //insert values to tcm ad orders
                    $obj_values="";
                    $obj_values['dt_create'] = $today;
                    $obj_values['dt_last_update'] = $today;                    
                    $obj_values['tcm_ad_id'] = $uid;
                    $obj_values['transaction_id'] = $txn_id;
                    $obj_values['payment_status'] = $status;
                    $obj_values['payment_amount'] = $amount;
                    $obj_values['payment_date_time'] = $today;
                    update_query($results_ins, "tcm_ad_orders", $obj_values);
                    if ($results_ins == 1) {
                        
                        if ($uid > 0) {
                            
                            // Update tcm_advertise_info table:
                            $sql = "UPDATE $tcm_advertise_info SET date_expires = IF(date_expires > NOW(), ADDDATE(date_expires, INTERVAL 1 MONTH), ADDDATE(NOW(), INTERVAL 1 MONTH)), dt_last_update='$today' WHERE id=$uid";
                            run_query($results_upd, $sql);
                            if ($results_upd != 1) {
                                //append record to error log
                                $obj_value['dt_create'] = $today;
                                $obj_value['dt_last_update'] = $today;
                                $obj_value['error_msg'] = "date_expires table could not be updated!";
                                update_query($results, "error_log", $obj_value);
                            }
                            
                        } // Invalid user ID.
                        
                    } else { // Problem inserting the order!
                        
                        //append record to error log
                        $obj_value['dt_create'] = $today;
                        $obj_value['dt_last_update'] = $today;
                        $obj_value['error_msg'] = 'The transaction could not be stored in tcm_ad_orders table!';
                        update_query($results, "error_log", $obj_value);    
                        
                    }
                    
                } // The order has already been stored!
                
            } // The right values don't exist in $_POST!
            
            
        }elseif(strcmp($res, "INVALID") == 0){
        
            //append record to error log
            $obj_value['dt_create'] = $today;
            $obj_value['dt_last_update'] = $today;
            $obj_value['error_msg'] = 'The transaction was Invalid.';
            update_query($results, "error_log", $obj_value);            
        
        }    
            

    } // End of the WHILE loop.
    
    // Close the connection:
    fclose ($fp);

} // End of $fp IF-ELSE.
?>

 

 

Link to comment
Share on other sites

This code:
if(strcmp($res, "VERIFIED") < 0){

is working for you because it's TRUE if "VERIFIED" is NOT in $res. In other words, you've just made the logic all backwards and wrong. 

 

Have you confirmed what the value of $res is?

Link to comment
Share on other sites

I know that "if(strcmp($res, "VERIFIED") < 0){" means it's not verifying at all.

 

I've appended the value of $res to my database right before "if(strcmp($res, "VERIFIED") < 0){".

 

The value is below:

 

Invalid Host header

Content-Length: 19

Connection: close

Server: BigIP

HTTP/1.0 400 Bad Request

 

I've got this message 2 times.

Link to comment
Share on other sites

Well, to be clear, you said that if you made that change, "It works perfectly." So I need to make it clear to everyone that that won't work.

 

The problem is indicated by your $res value, specifically the 400 Bad Request. This means that PayPal is denying the request. Try changing your request code to this:

 

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
fputs ($fp, $header . $req);
Link to comment
Share on other sites

Thanks, Larry. It works perfectly after changing my header to what you suggested. I guess I should change host url to www.paypal.com later when I use it for live site. Or should I use www.paypal.ca as I am in Canada?

Link to comment
Share on other sites

 Share

×
×
  • Create New...