How to Set Up Paypal Payment Gateway Integration in PHP

How to Set Up Paypal Payment Gateway Integration in PHP
2.9 (57.7%) 165 votes

Paypal payment gateway is an American international eCommerce business that allows people to make money transfer through internet. And most of the eCommerce stores integrate Paypal payment gateway to provide online payment option. This Paypal tutorial will explain about the complete process of Paypal payment gateway integration in PHP.

Paypal, being the top payment gateway has two environments: Real-time and Sandbox. So while integrating, developers can check/test transactions in the Sandbox environment.

Let’s Get Started

To integrate Paypal payment gateway, the functionalities will be divided into following three categories.

  1. Database
  2. CODE
  3. Paypal detail

First, we’ll start with the database.
Login into your phpmyadmin through here: http://localhost/phpmyadmin

Click on database. Enter database name and click on create button.

Now select the new database created by you and click on Import button. Select Paypal.sql file and click on Go button.

Here, we’ve successfully created database tables. Next step would be to put CSS and JS file into your project directory.

Payment Gateway Integration

Start Code Integration

Here, we’ll need following files to integrate Paypal Gateway.

  1. Index.php
  2. Connection.php
  3. Paypal_success.php
  4. Paypal_cancel.php
  5. images/

Paypal Sandbox Link:

Paypal ID: (Your Paypal ID)

Now create a file with Index.php to display output.

Create a form in this file using following code.

<!DOCTYPE html> 

<html lang="en"> 


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>Paypal :: SPACE-O </title> 




//fetch products from the database 

$results = $db->query("SELECT * FROM products"); 

while($row = $results->fetch_assoc()) 



    <img src="images/<?php echo $row['image']; ?>"/> 

    <br/>Prodcut Name: <?php echo $row['name']; ?> 

    <br/>Product Price: <?php echo $row['price']; ?> 

    <form action="<?php echo $paypal_link; ?>" method="post"> 


        <input type="hidden" name="business" value="<?php echo $paypal_username; ?>"> 


        <!-- Specify a Buy Now button. --> 

        <input type="hidden" name="cmd" value="_xclick"> 


        <!-- Specify details about the item that buyers will purchase. --> 

        <input type="hidden" name="item_name" value="<?php echo $row['name']; ?>"> 

        <input type="hidden" name="item_number" value="<?php echo $row['id']; ?>"> 

        <input type="hidden" name="amount" value="<?php echo $row['price']; ?>"> 

        <input type="hidden" name="currency_code" value="USD"> 


        <!-- Specify URLs --> 

        <input type='hidden' name='cancel_return' value='http://localhost/paypal_integration_php/paypal_cancel.php'> 

<input type='hidden' name='return' value='http://localhost/paypal_integration_php/paypal_success.php'> 


        <!-- Display the payment button. --> 

        <input type="image" name="submit" border="0" 

        src="" alt="PayPal - The safer, easier way to pay online"> 

        <img alt="" border="0" width="1" height="1" src="" > 



    <?php } ?> 



The above code will fetch product record from database and display it on the page.

Next, we’ll specify if the transaction is failed, then we’ll define the callback URL.

<!-- Specify  Call Back URLs --> 

        <input type='hidden' name='cancel_return' value='http://{Server name / Domain Name}/paypal_integration_php/paypal_cancel.php'> 

<input type='hidden' name='return' value='http://{server name/ Donmain Name}/paypal_integration_php/paypal_success.php'> 

After defining both callback URLs, we’ll define the hidden parameter for the buyer information.

<!-- Identify your business so that you can collect the payments. --> 

        <input type="hidden" name="business" value="<?php echo $paypal_username; ?>"> 


<!-- Specify a Buy Now button for Product. --> 

<input type="hidden" name="cmd" value="_xclick"> 


<!-- Specify product detail that product will buyer purchase. --> 

        <input type="hidden" name="item_name" value="<?php echo $row['name']; ?>"> 

        <input type="hidden" name="item_number" value="<?php echo $row['id']; ?>"> 

        <input type="hidden" name="amount" value="<?php echo $row['price']; ?>"> 

        <input type="hidden" name="currency_code" value="USD"> 

With this, the Index.php file code is complete. Next step is to create a connection.

Create a new file with Connection.php file name.

define('HOST', 'localhost'); 

define('USERNAME', '{USERNAME}'); 

define('PASSWORD', '{PASSWORD}'); 

define('DATABASE_NAME', 'paypal'); 

//Connect and select the database 


if ($db->connect_error) { 

    die("Connection failed: " . $db->connect_error); 


NOTE: change the value of HOST , USERNAME , PASSWORD , DATABASE_NAME when you run your connection.php file in browser. If you get any error, then you might have put the values wrong.

Now if you run index.php file, then you’ll see product list there with a buy button.

Click on that buy button, it’ll redirect you to Paypal page. Paypal requires user to fill email and password. Once this process is complete, and when you make the transaction and fails, it’ll redirect you to

http://{Server name / DomainName}/paypal_integration_php/paypal_cancel.php

And if your transaction is successful, then it’ll redirect you on following link:

http://{server name/ Donmain Name}/paypal_integration_php/paypal_success.php

NOTE :- When your transaction is successful, then at that time Paypal will return you following transaction details.

  1. Item_number
  2. Transaction_ID
  3. AMT

Now to display success message, write the following code.


include 'connection.php'; 

//save Trasaction information form PayPal 

$item_number = $_GET['item_number']; 

$txn_id = $_GET['Trasaction_ID']; 

$payment_gross = $_GET['AMT']; 

$currency_code = $_GET['CURRENCY_CODE']; 

$payment_status = $_GET['PAYMENT_STATUS']; 

//Get product price 

$productResult = $db->query("SELECT price FROM products WHERE id = ".$item_number); 

$productRow = $productResult->fetch_assoc(); 

$productPrice = $productRow['price']; 

if(!empty($txn_id) && $payment_gross == $productPrice){ 

//Check if payment data exists with the same TXN ID. 

    $prevPaymentResult = $db->query("SELECT payment_id FROM payments WHERE txn_id = '".$txn_id."'"); 

    if($prevPaymentResult->num_rows > 0){ 

        $paymentRow = $prevPaymentResult->fetch_assoc(); 

        $last_insert_id = $paymentRow['payment_id']; 


        //Insert tansaction data into the database 

        $insert = $db->query("INSERT INTO payments(item_number,txn_id,payment_gross,currency_code,payment_status) VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$payment_status."')"); 

        $last_insert_id = $db->insert_id; 



<h1>Your payment has been successful.</h1> 

    <h1>Your Payment ID - <?php echo $last_insert_id; ?>.</h1> 




<h1>Your payment has failed.</h1> 




And Done!

Before you make this feature live, make sure to check it multiple times in sandbox. The Paypal Sandbox offers all of the functionalities of Paypal to test. And when you’re doing business online, it is must to provide online payment options. And not just the one, the user should have all the online payment options available in the market. However, the Paypal payment gateway is used by majority of them. Therefore, if you’re developing an online store, then Paypal payment gateway integration in PHP should be your top-most priority. Even if you don’t know how exactly to integrate it, hire PHP developer to do the job for you.

Grab a free copy of Paypal API Integration Demo from Github.

This page was last edited on August 30th, 2018, at 1:22.

Want to Develop User-Friendly Online eCommerce Store? Contact Us Now

Get your free consultation now