PayPal IPN Listeners

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
Hi all

I'm confused and tired and need some help....

I have an ecommerce site, which is pretty much done now - providing a CMS for the client and then dynamically generating item lists.

I'm using the PayPal buttons "add to cart" form/hidden variable technique, which works great and has been tested.

My problem is....

In order to update the database that the item has been sold, I need an "IPN listener" which is a piece of code that waits for paypal to send information. When this information is received, the listener then sends back EXACTLY the same info (I believe) at which point PayPal returns a valid or invalid response.

On receiving a valid response, I need to use that (if,then) to update the associated records to say that they are no longer for sale.

There is a bunch of stuff under the PayPal implementation site, but the sample code is a bit over my head.

Does anyone know of a similar listener (php) that I can copy/paste and configure fairly simply?

Either than or a good simple tut I can follow.

Rich
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Checking the payment_status and receiver_email values is very straightforward, but the other stuff you need to check before you return the okey-dokey depends on how your database is structured, what db you're using, whether you have an ORM layer between your application and your database, and so forth. You need to look in your transactions to see whether the current transaction ID (that's what the txn_id variable is) has been processed or not, confirm that the article purchased (item_number) is valid and that the price being paid actually matches the price you were charging (malicious users may want a discount you weren't planning to offer).

If all of that is valid, then you need to process the transaction. If you're selling hard goods, that means removing the purchased goods from the inventory, beginning the fulfillment process, and adding the amount paid into your "income" pile, along with creating a transaction record detailing the purchase. (With downloads there's no inventory to handle, but you may need to generate a license key or enable a download link.) Again, the code will be specific to your site (or to your eCommerce package); there's no generic code you can just paste in beyond what PayPal gave you in the IPN_PHP text file. Those five commented-out lines can represent a complex bunch of code.

We can help, but we need more to work with. (And you may find the PayPal developer community to be more help than we are here. Not that we all suck, but this is the first comment in the thread with any content and I'm two days late to the party -- and I may be of no great help over the long run for reasons beyond my control.)
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Actually, getting the code template from the PayPal developer site is *really* easy -- posting it here isn't all that helpful:

PHP:
// PHP 4.1

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

// 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'];

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>

It's filling in the details specific to a site that gets hard, and it depends (as I said) on the way the site's data (and data object APIs if an ORM is in play) are structured. The top-level code only deals with the message content -- the problem space is integration.
 

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
Thanks Essellar (do I see a clever photography letter-play in your username BTW?? :) )

I didn't see any quick responses so guessed this was probably out of the field of the helpful guys/gals on here. I did post on the paypal forum - with NO response at all.

Yes - the script wasn't too tricky to copy, but as you say, it's integrating it in with the site that proves tricky.

My first problem was to get validation back from Paypal. I was getting the first post data, but when it was sent back, I wasn't getting a reply so couldn't validate the sender. Even with a sandbox account, this got strangely quite confusing.

Anyway - I'm there now with a check for already processed ID's and a check on price by pulling in a recordset based on item_number - or whatever it was.

I had to switch to "buy now" rather than "add to basket" option as it made my life easier with simple single returned id's and, considering the site content, isn't going to be a major headache.

Process registers item as "SOLD" or "RESERVED" and stores some registration info like transaction id, name, e-mail etc.. to cross-reference the paypal account.

Thanks for your efforts.

Rich
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Photography, yes, but they're also my initials. I remember bragging about a monogrammed camera when I was a young lad (my friends -- those that had cameras, at least -- all had Brownies, Instamatics or, in one case, a Rolleiflex TLR).

Sorry I didn't see your question early enough, Rich -- we might have baked a solution.

Stan
 

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
Thanks Stan

To be honest, I probably learned more going through it the hard way rather than a baked solution, but thanks again for the consideration.

I've only recently got back into photography. In my early days, I had a Practica which had such a dodgy shutter release, I always had motion blur, despite the tripod/weights I was using for macro work (on tubes - yuk).

Now moved on to (dare I say it) Nikon DSLR with my new Manfrotto toy! Life is so much easier with VR and without film!!

Rich
 

theone48

New Member
Messages
237
Reaction score
7
Points
0
There is great info on this thread for anyone setting-up PayPal. I'll be sure to keep this one in mind.
 
Top