Deploying and Implementing PayPal IPN in PHP

This is the final part of the PayPal IPN PHP script tutorial. This part will discuss the implementation and project deployment procedures for getting the PayPal IPN PHP script project up and running on your own test server.

The server requirements are as follows:

  • Apache web server

  • At least PHP 5

  • At least MySQL 5

  • Linux/Unix operating system

Downloading project files and configuring connect.php

To implement this PayPal IPN PHP project on your own server, follow the procedures below:

1. Download the PHP project files here:

2. Download the MySQL IPN database tables here:

3. Right click on, and then click “Extract here.”

4. Go inside the paypal_ipn_demo folder, and then open “connect.php.” Add your MySQL database login credentials (username, password, hostname, database name) on connect.php. This is the database that you will use for the PayPal IPN project. Save connect.php to implement your changes.

Configure customerdownload.php

Open “customerdownload.php” inside the paypal_ipn_demo folder. This is the download page where the customer inputs the invoice number after successfully paying with PayPal. Make the following changes:

1. Find this line:


Replace it with your own recaptcha public key. If you do not have recaptcha public and private keys, you can download it here:  

2. Find this line: 


Replace it with your own recaptcha private key.

3. Find this line:

include ‘/YOUR/ABSOLUTE/SERVER_PATH_NAME/paypal_ipn_demo/connect.php’;

Replace /YOUR/ABSOLUTE/SERVER_PATH_NAME with your own server path name. Read the following tutorial for details on how to get the absolute server path:

4. Find this line:

header(‘Content-Disposition: attachment; filename="’.$filename.’"’);

Replace with your own website domain name. 

5. Find this line:


Replace it with your own absolute server path. Save customerdownload.php to implement your changes.

Configure index.php

Open the index.php file inside the paypal_ipn_demo folder. This is the shopping page in your website, where the customer shops for digital products. If they are interested in purchasing your products, they will click the PayPal Buy Now buttons. Make the following changes:

1. Find these lines of code:

include ‘/YOUR/ABSOLUTE/SERVER_PATH_NAME/paypal_ipn_demo/connect.php’;
include ‘/YOUR/ABSOLUTE/SERVER_PATH_NAME/paypal_ipn_demo/invoicenumbergenerator.php’;

Replace with your own absolute server path.

2. For the two PayPal BuyNow HTML button codes (for two ebooks: “Codex ebook” and “How to behave like a child”), find this code:

<input type="hidden" name="business" value="YOUR_PAYPAL_SECURE_MERCHANT_ACCOUNT_ID">

Replace YOUR_PAYPAL_SECURE_MERCHANT_ACCOUNT_ID with your own ID. To determine your secure merchant account ID, follow the steps below:

3. Again, for the two PayPal BuyNow HTML button codes, find these lines of code:

<input type="hidden" name="return" value="">

<input type="hidden" name="cancel_return" value="">

Replace with your domain name. You should be using https/SSL secure protocol for your download page to avoid any “unsecure mode” errors experienced by your customers from PayPal to your download page. Example, it will look like this:  

Configure ipnhandler.php and upload the Paypal_IPN_demo folder 

Open ipnhander.php inside the paypal_ipn_demo folder. This is your actual IPN handler script, which will receive and transmit the IPN message between your test server and PayPal.

1. Find this line and replace it with your own absolute server path:
include ‘/YOUR/ABSOLUTE/SERVER_PATH_NAME/paypal_ipn_demo/connect.php’;

2.) Find this code: $receiver_email==’’
Replace with your own PayPal Sandbox business email address. For example:

Your PayPal sandbox business email is easy to spot, because of the phrase “biz” within the email address. 

That’s it. You have completely configured all of the required PHP files in the PayPal IPN project. These files do NOT need to be configured, as they are included PHP files:

  • invoicenumbergenerator.php

  • is_email.php

  • recaptchalib.php

  • PDF files under ebookdownloads, because they are sample/test digital ebook products.

3. Upload the entire paypal_ipn_demo folder (that contains the configured scripts discussed previously as well as included PHP files) to the root directory of your website. For example, if your website is, the paypal_ipn_demo shopping page can be viewed in this URL: . However, do not yet open this page in the browser, as you still need to configure the MySQL database.

{mospagebreak title=Import the MySQL IPN Project Tables}

Right click on the downloaded, and then click “Extract here.” You should then see three database tables which are discussed thoroughly in the third part of this tutorial series:

You are to import these tables into the MySQL database you defined in connect.php in the previous section. Let’s use the SSH command line method to import these tables (you can also use phpMyAdmin, although SSH offers better protection in terms of security).

Using this method requires your server to have SSH enabled. You should consult with your hosting support. This also assumes you are using a Linux terminal or Putty in Windows ( ).

Step 1. Upload these three SQL files to a temporary location in your server where you know the absolute path (e.g /home/temporary/). After uploading, log out of your server.

Step 2. Re-connect to your SSH server using either the Linux terminal or PuTTY. Configure it as follows:

Replace with your own domain, as well as the port number (standard SSH port uses 22, but other web hosts may not).

If you are using a Linux terminal:

codex-m@codex-m-desktop:~$ ssh -p 22

You are then required to enter your SSH password.


Step 3. At the bash prompt (after a successful login):

To import customerrecords.sql (customerrecords table) to a MySQL database named databasename:

-bash-2.05b$ mysql -uMySQLUsername -pMySQLPassword -hMySQLhostname  databasename </absolute_path/to_your_SQL_files/customerrecords.sql

Replace with your own MySQL username, password, hostname and database name, as well as the absolute server path to your uploaded SQL files in step 1.

To import ipnlogs.sql (ipnlogs table) to a MySQL database:

-bash-2.05b$ mysql -uMySQLUsername -pMySQLPassword -hMySQLhostname       databasename </absolute_path/to_your_SQL_files/ipnlogs.sql

To import productstable.sql (productstable) to a MySQL database:

-bash-2.05b$ mysql -uMySQLUsername -pMySQLPassword -hMySQLhostname  databasename </absolute_path/to_your_SQL_files/productstable.sql

All of the above commands entered should not produce any error.

Final testing procedures

1. Log in to your PayPal Sandbox account here:
You are required to be logged in to your Sandbox account during this entire testing process. Do not log out while the testing is going on.

2. Go to the shopping page URL:
There should not be any PHP or MySQL-related errors on it. You should see two PayPal buttons for two test ebooks.

See screen shot here:

3. Try to make a test purchase. Suppose you want to purchase the ebook “Codex ebook.” Click its “Buy Now” button.

4. You will then be taken to PayPal for payment processing. Enter your buyer email address in PayPal. Remember that in the first part of this series you created two sandbox email accounts, one for the seller and one for the buyer. Use the buyer login to purchase.

5. You are required to review your payment. Click “Pay Now” to complete the transaction. You will then see the message “Thank you for your payment.” Next, you will see the “Click here to download your ebook.”

See the screen shot here:

6. Click “Click here to Download your ebook.” If you are not using SSL, you will see a security warning; just click “Continue.” To resolve this error, you should be using https for your download URL (refer to step three in the “Configure index.php” section)

7. You are then required to enter your PayPal invoice number. PayPal sandbox emailed this to your test buyer account email. To access the invoice number, go to your PayPal Sandbox page, and then click “Test Email.”

8. Under the subject, click “Notification of Payment received” addressed to your buyer email. See screen shot:

9. The email will then launch in another browser window; scroll down and look for the invoice number. See screen shot:  

10.) Copy and paste this invoice number to the download page:

See screen shot:


11. Enter the recaptcha code, and finally, click “Download my ebook.”

A force download dialog box will then appear, asking you to download and save the purchased ebook to your computer. You can then refer to your MySQL database IPN tables (customerrecords, etc) for detailed records of the transaction. If you are having issues testing the processes above, it can relate to IPN handler script problems, such as your web host imposing limitations to your connections. You can check the logs in the ipnlogs MySQL table for troubleshooting.

Once you have successfully tested everything in your test server, you can then integrate this project into your real website shopping pages, and then use your real PayPal business account instead of the sandbox.

This means that you will replace all references in the script that go to and change them to This can be done by simply removing the word “sandbox” from the PayPal URL. Bear in mind that testing in a real PayPal environment requires real money for the transaction, so take note.

A test purchase in a real PayPal environment (not in the sandbox) is recommended to ensure that there are no technical problems that can affect customer shopping and checkout experiences on your website.

The files that need to be edited to change the references from to for actual implementation of the PayPal IPN include:

1. index.php – the PayPal Button HTML code.

2. ipnhandler.php (find the following lines of code)

a) fsockopen references:

$fp = fsockopen(‘ssl://’, 443, $errno, $errstr, 30);

b) PayPal Business email (change it from the sandbox business email to a real PayPal business account email):

if ($receiver_email==’’) {

Google+ Comments

Google+ Comments