Paypal Payment Gateway Integration with PHP

Paypal is the most popular payment gateway to send and receive money using web application and mobile. PayPal provides functionality to receive and send fund using internet.

This php payment gateway tutorial help to add PayPal gateway with web application using PHP. Paypal have two environment , one is sandbox account for developer testing and other is prod , for live environment.This paypal payment gateway tutorial will use paypal sandbox account to test product transaction.

How to Create a New PayPal Sandbox Account

  • Log in with your PayPal account. If you do not have created your PayPal account yet, first sign up at PayPal and create paypal account.
  • Now logged-in with PayPal account using credentials(which was given at the time of signup), Now you can see developer home page and click the Dashboard.
  • Then click on the Accounts link under the Sandbox.
  • Now create test accounts for seller and buyer by selecting Business and Personal from the create account link.


You can check other recommended tutorial of Payment Gateway,

There are following files will participate into this tutorial:

  • product_listing.php : This file will use to display product listing.
  • connection.php : This file will use to established connection with php and mysql.
  • success.php : This file will use to display success payment information.
  • cancel.php : This file will use to show cancel payment information.

You will have two configuration parameters which are required for paypal gateway integration.The $paypalURL and $paypalID value with live PayPal URL and business email.

MySQL Database Tables Creation

We will create two tables products and payments_info into MySQL 'test' database.The products table will be used to store product details information and payments_info table will be used for storing the transaction details from PayPal.

Create MySQL database connection

We will create connection.php file. This file is used to connect and select database using PHP and MySQL.We will add $dbhost, $username, $password, and $dbname variable’s value with your database credentials.

We have created connection with MySQL database, so now we will fetch product information from product table and display into a page.We will add some information with each product listing record.The product details will have product name, product number, amount, currency using html input hidden fields.
We will add paypal buy now button with each listed record and user will click on that to purchase product.The hidden parameters will send to paypal account.
We will add product_listing.php file and add below code to display product listing.

When the PayPal payment is successful, buyer would be redirected to provide success page or on failed message on transaction failed. We will receive the transaction information with $_GET variable and insert transaction data into the payments information database table.We will add below code into success.php file.

Please make sure you have enabled Auto Return for Website Payments on your PayPal business account to get transaction information from PayPal in success.php file.

Cancel Paypal Payment

User will ge below message when he cancelled transaction.We will redirect user to can page and show message.We will add below ode into cancel.php file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.