Example – PayPal Standard Payment Gateway Integration in PHP


PayPal is an American international e-commerce business allowing payments and money transfers to be made through the Internet. PayPal is the most popular payment gateway to send and receive payment worldwide. PayPal is a widely used payment gateway to accept payment in the web application. PayPal payment gateway is the easiest option for the web developer to implement payment system on the website.

This tutorial will explain how to integrate PayPal standard payment gateway in PHP and store the transaction data using PayPal IPN. Our step-by-step guide on PayPal payment gateway integration in PHP helps you to easily integrate the payment option in your web project.

In our example script, we’ll display some products with buy now option. Once the user clicks on the Buy Now button, they will be redirected to the PayPal where the payment will complete. After payment completion, the user will be redirected back to the website and the transaction details to be shown to the user. Also, the transaction information would be stored in the MySQL database.

PayPal has two environments such as Sandbox and Real Time. PayPal Sandbox allows developers to do their test transaction before the project go live. Real Time environment is used after project live. Once PayPal payment process is working fine on Sandbox environment, you can set PayPal to Real-Time environment.

Creating a PayPal Sandbox Account

Before start accepting payment from buyers via PayPal, payment gateway needs to be tested. To test transaction process you need to create PayPal sandbox account.

  • Go to the PayPal Developer page and log in to your PayPal account. If you don’t have any PayPal account, sign up for a PayPal account first.
  • After logged in you would be redirected to the developer homepage. Now click on the Dashboard link from the top navigation menu.
  • Click on the Accounts link under the Sandbox label from the left menu panel.
  • Create buyer account and merchant account from the Create Account link. For buyer account, you need to select Personal radio button under the Account type section or select Business radio button for a merchant account.

You may follow the detailed guide on creating a PayPal Sandbox account from the below link.

  • Creating PayPal Sandbox Test Account and Website Payments Pro Account

Database Tables Creation

To store products details and payment transaction information two tables need to be created in MySQL database. The products data is stored into the products table and payments table is used for storing the transaction information provided by PayPal.

Database Configuration (dbConfig.php)

This file is used to connect and select database using PHP and MySQL. Change $dbHost, $dbUsername, $dbPassword, and $dbNamevariable’s value with your database credentials.

Products (products.php)

All the products will be fetched from the products table and listed with PayPal Buy Now button. To use PayPal standard payment gateway, you need to submit a form with some predefined PayPal HTML form field variable. Follow the comment tags (<!– –>) to know about the form hidden fields.

Note: Don’t forget to change the cancel_return and return input field value with your desired URL in PayPal HTML form.

Payment Success (success.php)

Once the PayPal payment is successful, the buyer is redirected to this page. The transaction information is received using $_GET method and inserts payment data into the database.

Payment Cancel (cancel.php)

If the buyer wishes to cancel payment at the PayPal payment page, the buyer is redirected to this page.

Configure PayPal Auto Return and Payment Data Transfer

Make sure you had configured Auto Return for Website Payments on your PayPal business account. Otherwise, you’ll not get the transaction information from PayPal in success.php file. Read the following guide to enable Auto Return, Payment Data Transfer and set Return URL on your PayPal account.

  • How to Configure PayPal Auto Return and Payment Data Transfer

Setup PayPal Instant Payment Notification (IPN)

To make the PayPal Standard Payment more secure, Instant Payment Notification (IPN) should be used to validate the transaction. Follow the below steps to setup IPN in PayPal payment gateway integration.

Enable IPN
To Use this feature, IPN must be enabled in PayPal account. We’ve already published a step-by-step guide to enable IPN in PayPal.

  • How to enable PayPal Instant Payment Notification

Add Notify URL in PayPal Form
Add the following input field (notify_url) HTML along with the other PayPal HTML Variables.

Validate Transaction
Once IPN is enabled PayPal will send the transaction data to the Notify URL (http://www.tutspointer.com/ipn.php). Place the following code in ipn.php file to validate and insert payment information into the database.

Note that: Once PayPal IPN setup is completed, database insertion code is not required in success.php file.

Make PayPal Payment Gateway Live

When your application payment flow testing is completed, you need to modify two files to make PayPal payment gateway live.

Open the products.php file, change the $paypalURL and $paypalID value with live PayPal URL and business email.

Open the ipn.php file, change the $paypalURL value with live PayPal URL.


Leave A Reply

Your email address will not be published.