Jump to content
Larry Ullman's Book Forums

Ipn Script


Recommended Posts

Hello everyone.  I'm following along your book and even resorted to copying the book's version of the ipn.php script.  However, it appears that script is never being executed from Paypal Sandbox.  I'm having issues figuring out what might be going on, even with perusing the forums and have applied the header modification fix that I've seen floating around.  I tried running a test via the Instant Payment Notification (IPN) simulator functionality in the sandbox.  Says "IPN Successfully sent".  Anyone have additional ideas?  For reference, I have included the following code.

 

 

 

<?php

// This page handles the Instant Payment Notification communications with 
PayPal.
// Most of the code comes from PayPal's documentation.
// This script is created in Chapter 6.
// This is a modified version of ipn.php that logs all transactions to a text 
file.

// Require the configuration before any PHP code as the configuration controls error reporting:
require 


('./includes/config.inc.php');
// The config file also starts the session.

// Open the text file:
// Change this path to make it accurate.
// The text file must be writable by PHP!
$file = fopen(
'./ipn.txt', 'a');

// Write the POST data to the file:
fwrite($file,
"Received:\n");
fwrite($file, print_r($_POST,
true));
fwrite($file,
"\n");

// 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:

    trigger_error(
'Could not connect for the IPN!');
    
}
else { // Send the request to PayPal:
    
    $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);

    
// Write the PayPal request to the text file:
    fwrite($file,
"Sent:\n");
    fwrite($file,
"$header\n");
    fwrite($file,
"$req\n");
    
    
// Read in the response:
    
while (!feof($fp)) {

        $res = fgets ($fp, 
1024);

        
// Write the PayPal response to the text file:
        fwrite($file,
"Received:\n");
        fwrite($file,
"$res\n");

        
if (strcmp ($res, "VERIFIED") == 0) {
            
            
// Check for the right values:
            
if ( ($_POST['payment_status'] == 'Completed')
             
&& ($_POST[
'receiver_email'] == 'seller_1361580490_biz@goldmail.etsu.edu')
             
&& ($_POST[
'mc_gross'] == 10.00)
             
&& ($_POST[
'mc_currency'] == 'USD')
             
&& (!
empty($_POST['txn_id']))
            ) 
{
                
                
// Need the database connection now:
                
require(MYSQL);
                
                
// Check for this transaction in the database:
                $txn_id 
= mysqli_real_escape_string($dbc, $_POST[
'txn_id']);                
                $q 
=
"SELECT id FROM orders WHERE transaction_id='$txn_id'";
                $r 
= mysqli_query ($dbc, $q);
                
if (mysqli_num_rows($r) == 0) { // Add this new transaction:
                    
                    $uid 
= (int) $_POST[
'custom'];
                    $status 
= mysqli_real_escape_string($dbc, $_POST[
'payment_status']);
                    $amount 
= (float) $_POST[
'mc_gross'];
                    $q 
=
"INSERT INTO orders (user_id, transaction_id, payment_status, payment_amount) 
VALUES ($uid, '$txn_id', '$status', $amount)";
                    $r 
= mysqli_query ($dbc, $q);
                    
if (mysqli_affected_rows($dbc) == 1) {
                        
                        
// Update the users table:
                        $q 
=
"UPDATE users SET date_expires = IF(date_expires > NOW(), 
ADDDATE(date_expires, INTERVAL 1 YEAR), ADDDATE(NOW(), INTERVAL 1 YEAR)), 
date_modified=NOW() WHERE id=$uid";
                        $r 
= mysqli_query ($dbc, $q);
                        
if (mysqli_affected_rows($dbc) != 1) {
                            trigger_error(
'The user\'s expiration date could not be updated!');
                        }
                        
                    }
else { // Problem inserting the order!
                        trigger_error(
'The transaction could not be stored in the orders 
table!');                        
                    }
                    
                }
// The order has already been stored!
                
            }
// The right values don't exist in $_POST!

        }
elseif (strcmp ($res, "INVALID") == 0) {
            
// log for manual investigation
        }

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

}
// End of $fp IF-ELSE.

// Inidicate the end of this transaction in the text file:
fwrite($file,
"--------------\n");
fclose($file);

?>
 
Link to comment
Share on other sites

I have attempted using PayPal sandbox at three different times and find it frustrating and difficult and it has not worked for me. I don't now what the problem is but have had a similar experience to yours. I have contacted PayPal and they say they do not offer support for the development part of their site. I don't know if it is because I am on a Mac but I have to login MANY times before it will take me to the area where I can choose the fake buyer or seller email and most of the time when I try to log in it takes me to a page saying that there is a server error.

 

HOWEVER, things are working fine when I have moved my scripts to the LIVE mode. The ipn script is updating my users and orders table and then going to the Thanks page. The only problem is that one cannot do too much testing because PaylPal starts blocking the credit cards to avoid fraud.

Link to comment
Share on other sites

PayPal is clearly difficult to work with, from a developer perspective. I forget whether I tested IPN with the sandbox the most recent time I used PayPal. Are you using a live URL for the IPN script? Are you logged into the sandbox when you're testing it?

Link to comment
Share on other sites

I am logged into the sandbox while I am testing it and using a live URL for the IPN script. So I really don't know what is going on there. I get fed up and then go back to it and try different things. At one point it was doing something with the orders table but never updated the users table.

Link to comment
Share on other sites

  • 3 weeks later...

Just an update. I STILL have given up on the PayPal Sandbox and am doing my testing on the live PayPal. BUT I have discovered that if one sets up a bank account with PayPal you can use that over an over. At least so far I have not been blocked. As far as I know one can have one business account and one personal account. So I am using the personal account with my bank account info and I am able to test out my fake users. This is working fine. It is updating my users and orders table. So I really don't know what is going on with PayPal Sandbox. Live PayPal is working.


Thanks for all the helpful suggestions and comments.

Link to comment
Share on other sites

Hello Marie,


Thanks for your update. I'm glad you got a workable solution. I just did a PayPal connection in December (I think), but I still don't remember how the PayPal Sandbox worked for me, if at all. Just for the record, are you using the main PayPal (the same as in the US) or a Canadian version? I know that some people in the UK have experienced problems with the PayPal Sandbox that I never saw in the US.

Link to comment
Share on other sites

As far as I know it is Canadian. Paypal seems to reroute me that way no matter what. Also, as far as I know they are located not far from where I live so maybe I can drop in their office sometime and ask some questions.

 

Marie

Link to comment
Share on other sites

I suggest you should try posting at their forum. I heard that PayPal has 'Evangelists'. That's what they call them. Their job is to attract people to PayPal. If you could get one of them interested in your issue, maybe she/he would do something for you. You might sublty suggest that you might have to use Stripe instead of PayPal because you can't even test PayPal.

Link to comment
Share on other sites

Yes it would be amazing if there was someone there that could actually help. The forum idea is good as well. I am going to get my list together and try approaching them when I am back in Toronto. Right now I am vacationing in Florida.

Link to comment
Share on other sites

I spent a week straight figuring all this out and got it to work successfully. I made several posts on this site with information that could help but, because it was more than a year ago, I could not locate them.

 

In reading your initial post, the following caught my eye:

 

Says "IPN Successfully sent" (while in the simulation mode)

 

I recall spending the most (and most frustrating) time working on the fact that it was supposed to have been sent but nothing was happening in my "myIPN" script.

 

I did speak with tech support and he gave me the answer right away. I tried it and it worked.  Once again, I can't find the post with how it was solved but it could be the answer you seek.

 

Here is what I do remember about it:

The solutions was in the sandbox settings in or about the same area where you set the simulator. You'd never know because the setting seems innocuous, like it wouldn't have anything to do with it. But that was it! I clicked it just to see what it was for but nothing seemed to happen and I forgot about it. I didn't figure it out for days.

 

This might not be much help but if I can get back into my sandbox account I'll snoop around and see if it jogs my memory.

Link to comment
Share on other sites

Wow. I went into the sandbox and found everything has changed completely since I last used it. I became lost again. It reminds me of being in a hall of mirrors. I used to have two separate PP accounts for sandbox and a real merchant setup. They changed all that so now the two are both under a single email address.

 

After finding nothing but old "how to " information, I contacted one of the "evangelists" for updated versions. This is what he set to me. You might already have this but here is anyway:

 

You can learn how to use the Sandbox in the Sandbox guide (in the Applications tab on developer.paypal.com) at https://www.x.com/developers/paypal/documentation-tools/ug_sandbox.

 

You can find the latest IPN info at https://www.x.com/developers/paypal/documentation-tools/ipn/integration-guide/IPNIntro

Regards,
Autum

Link to comment
Share on other sites

  • 2 months later...

Okay I actually DID find their Canadian offices in Toronto. Apparently, eBay Canada, PayPal Canada and Kijiji are all located in the same place and have a great front entrance with their logos displayed large and loud, however, one is not allowed to enter through their front door without the secret code. I am not giving up though and will attempt to speak to someone soon.

 

Marie

Link to comment
Share on other sites

 Share

×
×
  • Create New...