Handling Paypal Instant Payment Notification (IPN)
When you reach the payment detail page on paypal site and click the "Pay" button paypal will send a POST message to our server. Our script then send a confirmation back to paypal to verify this POST. Here's that flow diagram again.
Paypal Configuration FileThe script that process the notification is located in include/paypal/ipn.php. But before talking about that file let's see the paypal configuration first. Source code : paypal.inc.php <?php
$paypal = array(); $paypal['business'] = "armanpi@phpwebcommerce.com";
// ... other "not so important" settings down here ?> Here is the description for each configuration :
Submitting The Payment InformationFrom the second step on the flowchart above we post some form values to paypal server so they can process the payment. Below you can see the variables sent from payment.php and their description
Processing The PaymentHere is the code for the IPN script : Source code : include/paypal/ipn.php
<?php
if (strpos($_SERVER['REMOTE_ADDR'], '66.135.197.') === false) { exit; } require_once './paypal.inc.php'; // repost the variables we get to paypal site //check the ipn result received back from paypal $result = dbQuery($sql); // if no invoice with such number
is found, exit
// check that the buyer sent the right amount
of money } else { Right at the top of the script we check if the ip address of the page requester. Since this page is meant only to be accessed by paypal server we disallow any connection that didn't come from paypal. Paypal server's IP address begin with 66.135.197 so if we don't see that in the remote address we just assume someone trying to mess with our script and exit immediately. If the request does come from paypal we send a reply POST to let Paypal know that we have receive their message. Upon receiving our reply Paypal send another reply notifying the payment status whether it's verified or not. If we found the word VERIFIED in the reply that means payment has been made and we move on updating the status for the order Here is the series of checking that the script perform :
That's it. Once the customer made her payment you can check the order on the admin page. When you click on the "Order" menu on the admin page you can see all paid orders. From there it's up to you as the store owner to decide what to do with those orders.
|
Creating Your Paypal AccountIf you want to test the shopping cart script on your own server you should create your own developer account. Here are the steps to setup a developer account with paypal :
Since our script already pass the hidden input called notify_url, which is the url of the script that process IPN notifications, this step is actually not required anymore. I just put this for the sake of completeness. I did try to make checkout process as simple as possible but somehow i feel it gets quite complex now. If you found something weird / not clear please contact me. Howevert if you're question is about paypal it would be a lot better to ask the questions in the paypal forum especially the IPN section. This forum is packed with paypal experts. |
|
|
|
|
|
Online Shop - Using Paypal | PHP MySQL Shopping Cart Tutorial : Online Shop - Handling Paypal IPN | Source Code |
|
At long last i'm finally able to update this site. Now the shopping cart can handle payment through paypal. As always you have any critiques, questions, comments or problems about this tutorial please tell me. Click here to send your feedback. And if you like this tutorial please link to this site. It will really help a lot :-) |
PHP MySQL Shopping Cart Tutorial
Copyright © 2005 - 2008 www.phpwebcommerce.com