TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-06-2009, 03:43 AM   #1 (permalink)
The Contributor
 
SaintIsaiah's Avatar
 
Join Date: Jan 2009
Posts: 48
Thanks: 5
SaintIsaiah is on a distinguished road
Default Weird Paypal IPN problem...

I went to https://www.paypaltech.com/SG2/ to get a php IPN script with a database it's supposed to connect to and place transactions into. However, I did a test donation to myself and this is the error I'm getting via email:

Quote:
INVALID
cmd=_notify-validate
I don't really understand what's causing this since I have never worked with IPN before. Can someone take a look at the ipn page below and provide any clues? I have replaced my DB info inside of it with generic info. Thanks!

PHP Code:
<?php


/////////////////////////////////////////////////
/////////////Begin Script below./////////////////
/////////////////////////////////////////////////

// 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";

// If testing on Sandbox use:
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

$fp fsockopen ('ssl://www.paypal.com'443$errno$errstr30);


// assign posted variables to local variables
$item_name $_POST['item_name'];
$business $_POST['business'];
$item_number $_POST['item_number'];
$payment_status $_POST['payment_status'];
$mc_gross $_POST['mc_gross'];
$payment_currency $_POST['mc_currency'];
$txn_id $_POST['txn_id'];
$receiver_email $_POST['receiver_email'];
$receiver_id $_POST['receiver_id'];
$quantity $_POST['quantity'];
$num_cart_items $_POST['num_cart_items'];
$payment_date $_POST['payment_date'];
$first_name $_POST['first_name'];
$last_name $_POST['last_name'];
$payment_type $_POST['payment_type'];
$payment_status $_POST['payment_status'];
$payment_gross $_POST['payment_gross'];
$payment_fee $_POST['payment_fee'];
$settle_amount $_POST['settle_amount'];
$memo $_POST['memo'];
$payer_email $_POST['payer_email'];
$txn_type $_POST['txn_type'];
$payer_status $_POST['payer_status'];
$address_street $_POST['address_street'];
$address_city $_POST['address_city'];
$address_state $_POST['address_state'];
$address_zip $_POST['address_zip'];
$address_country $_POST['address_country'];
$address_status $_POST['address_status'];
$item_number $_POST['item_number'];
$tax $_POST['tax'];
$option_name1 $_POST['option_name1'];
$option_selection1 $_POST['option_selection1'];
$option_name2 $_POST['option_name2'];
$option_selection2 $_POST['option_selection2'];
$for_auction $_POST['for_auction'];
$invoice $_POST['invoice'];
$custom $_POST['custom'];
$notify_version $_POST['notify_version'];
$verify_sign $_POST['verify_sign'];
$payer_business_name $_POST['payer_business_name'];
$payer_id =$_POST['payer_id'];
$mc_currency $_POST['mc_currency'];
$mc_fee $_POST['mc_fee'];
$exchange_rate $_POST['exchange_rate'];
$settle_currency  $_POST['settle_currency'];
$parent_txn_id  $_POST['parent_txn_id'];
$pending_reason $_POST['pending_reason'];
$reason_code $_POST['reason_code'];


// subscription specific vars

$subscr_id $_POST['subscr_id'];
$subscr_date $_POST['subscr_date'];
$subscr_effective  $_POST['subscr_effective'];
$period1 $_POST['period1'];
$period2 $_POST['period2'];
$period3 $_POST['period3'];
$amount1 $_POST['amount1'];
$amount2 $_POST['amount2'];
$amount3 $_POST['amount3'];
$mc_amount1 $_POST['mc_amount1'];
$mc_amount2 $_POST['mc_amount2'];
$mc_amount3 $_POST['mcamount3'];
$recurring $_POST['recurring'];
$reattempt $_POST['reattempt'];
$retry_at $_POST['retry_at'];
$recur_times $_POST['recur_times'];
$username $_POST['username'];
$password $_POST['password'];

//auction specific vars

$for_auction $_POST['for_auction'];
$auction_closing_date  $_POST['auction_closing_date'];
$auction_multi_item  $_POST['auction_multi_item'];
$auction_buyer_id  $_POST['auction_buyer_id'];

//DB connect creds and email
$notify_email =  "you@domain.com";         //email address to which debug emails are sent to
$DB_Server "localhost"//your MySQL Server
$DB_Username "user"//your MySQL User Name
$DB_Password "password"//your MySQL Password
$DB_DBName "DB"//your MySQL Database Name


if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp$header $req);
while (!
feof($fp)) {
$res fgets ($fp1024);
if (
strcmp ($res"VERIFIED") == 0) {



//create MySQL connection
$Connect = @mysql_connect($DB_Server$DB_Username$DB_Password)
or die(
"Couldn't connect to MySQL:<br>" mysql_error() . "<br>" mysql_errno());


//select database
$Db = @mysql_select_db($DB_DBName$Connect)
or die(
"Couldn't select database:<br>" mysql_error(). "<br>" mysql_errno());


$fecha date("m")."/".date("d")."/".date("Y");
$fecha date("Y").date("m").date("d");

//check if transaction ID has been processed before
$checkquery "select txnid from paypal_payment_info where txnid='".$txn_id."'";
$sihay mysql_query($checkquery) or die("Duplicate txn id check query failed:<br>" mysql_error() . "<br>" mysql_errno());
$nm mysql_num_rows($sihay);
if (
$nm == 0){

//execute query



    
if ($txn_type == "cart"){
    
$strQuery "insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')";

     
$result mysql_query($strQuery) or die("Cart - paypal_payment_info, Query failed:<br>" mysql_error() . "<br>" mysql_errno());
     for (
$i 1$i <= $num_cart_items$i++) {
         
$itemname "item_name".$i;
         
$itemnumber "item_number".$i;
         
$on0 "option_name1_".$i;
         
$os0 "option_selection1_".$i;
         
$on1 "option_name2_".$i;
         
$os1 "option_selection2_".$i;
         
$quantity "quantity".$i;

         
$struery "insert into paypal_cart_info(txnid,itemnumber,itemname,os0,on0,os1,on1,quantity,invoice,custom) values ('".$txn_id."','".$_POST[$itemnumber]."','".$_POST[$itemname]."','".$_POST[$on0]."','".$_POST[$os0]."','".$_POST[$on1]."','".$_POST[$os1]."','".$_POST[$quantity]."','".$invoice."','".$custom."')";
         
$result mysql_query($struery) or die("Cart - paypal_cart_info, Query failed:<br>" mysql_error() . "<br>" mysql_errno());

     }
    }



    else{
     
$strQuery "insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,itemnumber,itemname,os0,on0,os1,on1,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$item_number."','".$item_name."','".$option_name1."','".$option_selection1."','".$option_name2."','".$option_selection2."','".$quantity."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')";
     
$result mysql_query("insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,itemnumber,itemname,os0,on0,os1,on1,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$item_number."','".$item_name."','".$option_name1."','".$option_selection1."','".$option_name2."','".$option_selection2."','".$quantity."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')") or die("Default - paypal_payment_info, Query failed:<br>" mysql_error() . "<br>" mysql_errno());
    }


    
// send an email in any case
 
echo "Verified";
     
mail($notify_email"VERIFIED IPN""$res\n $req\n $strQuery\n $struery\n  $strQuery2");
}
else {
// send an email
mail($notify_email"VERIFIED DUPLICATED TRANSACTION""$res\n $req \n $strQuery\n $struery\n  $strQuery2");
}

    
//subscription handling branch
    
if ( $txn_type == "subscr_signup"  ||  $txn_type == "subscr_payment"  ) {

      
// insert subscriber payment info into paypal_payment_info table
      
$strQuery "insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')";
      
$result mysql_query($strQuery) or die("Subscription - paypal_payment_info, Query failed:<br>" mysql_error() . "<br>" mysql_errno());


         
// insert subscriber info into paypal_subscription_info table
        
$strQuery2 "insert into paypal_subscription_info(subscr_id , sub_event, subscr_date ,subscr_effective,period1,period2, period3, amount1 ,amount2 ,amount3,  mc_amount1,  mc_amount2,  mc_amount3, recurring, reattempt,retry_at, recur_times, username ,password, payment_txn_id, subscriber_emailaddress, datecreation) values ('".$subscr_id."', '".$txn_type."','".$subscr_date."','".$subscr_effective."','".$period1."','".$period2."','".$period3."','".$amount1."','".$amount2."','".$amount3."','".$mc_amount1."','".$mc_amount2."','".$mc_amount3."','".$recurring."','".$reattempt."','".$retry_at."','".$recur_times."','".$username."','".$password."', '".$txn_id."','".$payer_email."','".$fecha."')";
        
$result mysql_query($strQuery2) or die("Subscription - paypal_subscription_info, Query failed:<br>" mysql_error() . "<br>" mysql_errno());


             
mail($notify_email"VERIFIED IPN""$res\n $req\n $strQuery\n $struery\n  $strQuery2");

    }
}


else if (
strcmp ($res"INVALID") == 0) {
// log for manual investigation

mail($notify_email"INVALID IPN""$res\n $req");
}
}
fclose ($fp);
}
?>
SaintIsaiah is offline  
Reply With Quote
Old 12-06-2009, 11:09 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Are you running the script on a local server? Coz paypal wont post back IPN data to localhosts (for numerous reasons).
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-07-2009, 08:13 AM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

My IPN is mildly different from yours in implementation but the post var is the same. I don't see any typo's and ultimately they both perform the exact same task. I've never gotten an error from it though. :(

php Code:
<?php

if ( ! isset( $_POST ) || empty( $_POST ) )
    die ('failure to comply with the kitteh\'s demands will meet with extreme prejudice.');

$verify = 'cmd=_notify-validate';

$reply = new stdClass;

foreach ( $_POST as $key => $value ) {
    $reply->$key = $value;
    $verify .= '&' . $key . '=' . urlencode( $value );
}

$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, 'http://www.sandbox.paypal.com/us/cgi-bin/webscr');
curl_setopt( $ch, CURLOPT_FAILONERROR, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_TIMEOUT, 3);
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $verify );

$result = curl_exec( $ch );
curl_close( $ch );

if ( strcmp( $result, 'VERIFIED' ) == 0 ) {
 ...
delayedinsanity is offline  
Reply With Quote
Old 12-07-2009, 08:27 AM   #4 (permalink)
The Contributor
 
SaintIsaiah's Avatar
 
Join Date: Jan 2009
Posts: 48
Thanks: 5
SaintIsaiah is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
Are you running the script on a local server? Coz paypal wont post back IPN data to localhosts (for numerous reasons).
There is an actual ip address for my database which I use, not "localhost" - I just replaced the SQL info with generic stuff so my DB info was anonymous.

Quote:
Originally Posted by delayedinsanity View Post
My IPN is mildly different from yours in implementation but the post var is the same. I don't see any typo's and ultimately they both perform the exact same task. I've never gotten an error from it though. :(
I see what you mean. Different structure, yes but the same post. I'm not sure what's up with this...
SaintIsaiah is offline  
Reply With Quote
Old 12-07-2009, 09:48 AM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Originally Posted by SaintIsaiah View Post
There is an actual ip address for my database which I use, not "localhost" - I just replaced the SQL info with generic stuff so my DB info was anonymous.
You seem to have missed my point, when i used the term 'localhost' I wasnt mearly referring to the host name of your DB server. Paypal will only post back IPN validation data to servers it can actually connect to! thus if paypal can't see your server it cant give you POST variables, that was my point, you need to either fudge your router's firewall to allow traffic on port 80 or use a test web server externally hosted.

This may or may not be your problem, depending if your getting variables or not.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-07-2009, 11:27 AM   #6 (permalink)
The Contributor
 
SaintIsaiah's Avatar
 
Join Date: Jan 2009
Posts: 48
Thanks: 5
SaintIsaiah is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
You seem to have missed my point, when i used the term 'localhost' I wasnt mearly referring to the host name of your DB server. Paypal will only post back IPN validation data to servers it can actually connect to! thus if paypal can't see your server it cant give you POST variables, that was my point, you need to either fudge your router's firewall to allow traffic on port 80 or use a test web server externally hosted.

This may or may not be your problem, depending if your getting variables or not.
My apologies, I misinterpreted what you meant. I have an external server host through Dallas, TX and I can connect to the DB just fine myself, but you're saying that this error is the result of a failed connection to the SQL? Or the entire server? Because the weird thing is that under the IPN history it said it sent the data to the page with a 200 response code and it states 200 means it was successful. So I'm kind of at a loss.
SaintIsaiah is offline  
Reply With Quote
Old 12-07-2009, 12:31 PM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Originally Posted by SaintIsaiah View Post
My apologies, I misinterpreted what you meant. I have an external server host through Dallas, TX and I can connect to the DB just fine myself, but you're saying that this error is the result of a failed connection to the SQL? Or the entire server? Because the weird thing is that under the IPN history it said it sent the data to the page with a 200 response code and it states 200 means it was successful. So I'm kind of at a loss.
No problems m8, i misinterpreted people on a regular basis, you can almost set your watch by it!

If its saying it sent it fine, then it seems like paypal IS sending data back. The reason I suggested it is because its a well documented and frequent problem with IPN, people trying to test a script on a server that isn't connect-able to paypal. As-long as your server is capible of accepting traffic on port 80 then it *should* be fine!
It doesn't look to me like the DB is at fault as we would have SQL errors, all we seem to have is INVALID being sent back from paypal (is that correct, or am i making this up i haven't had my morning coffee yet you see.)

Just to be on the safe side, do some checks to see if paypal is posting the data.

I did find this, it may or may not help:
http://www.pdncommunity.com/pdn/boar...thread.id=8230
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-08-2009, 09:30 AM   #8 (permalink)
The Contributor
 
SaintIsaiah's Avatar
 
Join Date: Jan 2009
Posts: 48
Thanks: 5
SaintIsaiah is on a distinguished road
Default

Well that topic certainly wins the vague award heh. It does tell me why it could fail, but not the necessary things to check and verify. Does paypal offer any ipn page download link?
SaintIsaiah is offline  
Reply With Quote
Old 12-08-2009, 09:36 AM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

https://www.paypal.com/ipn

has some sample code, looks awfully similar to the one you posted above though
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird UTF-8 problem Tanax Absolute Beginners 17 06-23-2009 09:36 AM
Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM
Strange string comparison problem Jelmer General 0 05-23-2008 04:00 PM
A bug or a cache problem?! yazid Advanced PHP Programming 0 05-22-2008 08:40 AM


All times are GMT. The time now is 08:35 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design