Below is the complete script for the IPN handler (with validation, error logging and insertion of validated information into the database). The script is a modification of the original IPN PHP script by PayPal here: https://cms.paypal.com/cms_content/US/en_US/files/developer/IPN_PHP_41.txt <?php //Receive and read the post from PayPal //After successful customer transaction in PayPal, its servers will send an IPN message to the IPN URL you provide in your PayPal business account. $req = 'cmd=_notify-validate'; //To ensure that the message is coming from PayPal, you are required by PayPal to post the entire IPN message back to PayPal for verification. //This means that the same set of IPN messages are sent back to PayPal immediately after you receive them. //It is recommended that you use port 443 for connecting to PayPal servers for security reasons because it will be an encrypted communication. //Make sure your web host allows communication in this port number, also connecting using ssl in fsockopen requires your server to have OpenSSL installed. //If you see errors, you need to contact and ask support to your web host. $header .= "POST /cgi-bin/webscr HTTP/1.0rn"; //Then you need to assign posted variables from PayPal to PHP variables //However, for PayPal IPN implementation for digital downloads such as ebooks, mp3, etc, the following are the important variables that you need to receive, process, validate and insert to your database. //Take note that the invoice and customer ip address variables are coming from the shopping page you set up in the second part of this tutorial, and are passed from PayPal to your script using IPN communication. $payment_status = $_POST['payment_status']; //Connect to MySQL database //This is discussed in the second part of this tutorial series include '/home/www/php-developer.org/paypal_ipn_demo/connect.php'; //HTTP ERROR, this is most likely caused by the issue relating to fsockopen transaction. For example, your web host prevents access to the secure port 443, or there is a syntax error in your fsockopen. //It is important to log the error to the MySQL database for easy troubleshooting. The ipnlogs table was c created in the third part of this tutorial series. The error numbers of the fsockopen are also logged to your database. You can see more about error numbers here: http://php.net/manual/en/function.fsockopen.php //Remember that if there are errors relating to fsockopen, you will not be able to send back the IPN message to PayPal or get the reply from PayPal, which is an important requirement for IPN verification. $log='http error='.$errno; //Now that the IPN transaction is "VERIFIED" according to PayPal, you can log this successful transaction to ipnlogs table for tracking purposes. $log='Verified IPN Transaction'; //For every verified IPN transaction, it is required by PayPal to check that the txn_id has not been previously processed. This will prevent duplicate transactions. So query the database to see if the $txn_id is or is not new. $txn_id = mysql_real_escape_string($txn_id); //No records found in the customerrecords table, transaction ID is new //check that receiver_email is your Primary PayPal email. This is very important to prevent spoofing the transaction and ensures that this payment belongs to you and not to other accounts. if ($receiver_email=='codex__1293512831_biz@yahoo.com') { if ($payment_currency=='USD') { //Check if the payment amount is correct //By comparing the amount paid by the customer in PayPal to the exact price stored in the products table (created in the third part of this tutorial), you can ensure that the amount paid is correct and not spoofed. //First, retrieve the product price in the productstable for the purchased product. $productname = mysql_real_escape_string($productname); //Finally, compare the amount paid by the customer in PayPal to the original product price stored in productstable. if ($payment_amount==$productprice) { //check to see if the payment_status is "Completed" if ($payment_status=='Completed') { //Validate Payer email address require_once('is_email.php'); //Validate invoice number if (ctype_alnum($invoice)){ //Validate IP address if(filter_var($customeripaddress, FILTER_VALIDATE_IP)){ //Set download status to "incomplete" because the user still needs to download the purchased digital products from your website. $downloadstatus='incomplete'; //Now that everything has been verified and validated, you can insert all validated records into the customerrecords database. //Bear in mind that all of these variables are sanitized with mysql_real_escape_string before insertion into the database. mysql_query("INSERT INTO customerrecords (PaymentStatus,PaymentAmount,PaymentCurrency,PayerEmail,ReceiverEmail,TransactionID, //close MySQL database connection mysql_close($dbhandle); //transaction ID already exists in the database, could not process request die('Could not process request-transaction ID already exist'); //Invalid IPN transaction $log='Invalid IPN transaction'; fclose ($fp); Implementing the ipnhandler.php script Now that the script is done, you can save this script inside your paypal_ipn_demo folder. All of the required files and folders inside the paypal_ipn_demo that we've covered so far in this series are shown in the screen shot below:
1. ebookdownloads - This is the secure folder where you store the digital products. This is protected with .htaccess. The details of how this folder is created were discussed in the third part of the tutorial series. 2. connect.php - This is the PHP script that will be used to connect to your MySQL database for storing the IPN records and information about your products. This was discussed in the second part of this series. 3. index.php - This the shopping page where visitors can see your product as well as your PayPal Buy now buttons. This was discussed in part two. 4. invoicenumbergenerator.php - This is the script that will produce the invoice numbers. This was also discussed in part two. 5. is_email.php - This is the RFC compliant email validator script coming from this source: http://isemail.googlecode.com/files/is_email-2.10.zip . This script is used in this tutorial (part four). 6. ipnhandler.php - This is the IPN script which is discussed thoroughly in this tutorial (part four). You can download the complete ipnhandler script as discussed without comments here: http://www.php-developer.org/wp-content/uploads/scripts/ipnhandler.txt All database tables were created in the third part of this tutorial. However, the project is still not complete. The customer will still need to download the ebook and use the invoice number to authenticate. In this case, you need to create a new PHP script that will execute these tasks. This will be discussed in the fifth part of this tutorial.
blog comments powered by Disqus |
|
|
|
|
|
|
|