paypal payment gateway integration in php

Today, We want to share with you paypal payment gateway integration in php.In this post we will show you payment gateway in php and mysql, hear for paypal integration in php with sandbox we will give you demo and example for implement.In this post, we will learn about PayPal Payment Gateway with PHP MySQL Database with an example.

paypal integration in php Source Code

There are the Following The simple About paypal payment gateway api integration in php Full Information With Example and source code.

As I will cover this Post with live Working example to develop REST API Code Samples – PayPal Demo, so the paypal payment gateway integration in php codeigniter is used for this example is following below.

files structure for integrate PayPal payment gateway API in PHP

β”œβ”€β”€ config.php
β”œβ”€β”€ databaseconnection.php
β”œβ”€β”€ index.php
β”œβ”€β”€ success.php
β”œβ”€β”€ cancel.php
β”œβ”€β”€ ipn.php
β”œβ”€β”€ css/
β”‚   └── style.css
└── images/

Create PayPal Sandbox Account

Go to the

Also Read This πŸ‘‰   MySQL MULTIPLE INNER JOINS Example

Create Database Tables

SQL creates a products table in the MySQL database

CREATE TABLE `products` (
 `name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
 `image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `price` float(10,2) NOT NULL,
 `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

SQL creates a paypalpayments table in the MySQL database

CREATE TABLE `paypalpayments` (
 `paypalpayment_id` int(11) NOT NULL AUTO_INCREMENT,
 `item_number` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `txn_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `paypalpayment_gross` float(10,2) NOT NULL,
 `currency_code` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
 `paypalpayment_status` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`paypalpayment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

PayPal Settings and Database Configuration



define('PAYPAL_ID', 'Insert_PayPal_Business_Email'); 
define('PAYPAL_RETURN_URL', ''); 
define('PAYPAL_CANCEL_URL', ''); 
define('PAYPAL_NOTIFY_URL', ''); 
define('PAYPAL_CURRENCY', 'USD'); 
define('DB_HOST', 'MySQL_Database_Host'); 
define('DB_USERNAME', 'MySQL_Database_Username'); 
define('DB_PASSWORD', 'MySQL_Database_Password'); 
define('DB_NAME', 'MySQL_Database_Name'); 
define('PAYPAL_URL', (PAYPAL_SANDBOX == true)?"":"");

Database Connection


// Connect with the database 
// Display error if failed to connect 
if ($db->connect_errno) { 
    printf("Connect failed: %s\n", $db->connect_error); 

Products Page


// Include configuration file 
include_once 'config.php'; 
// Include database connection file 
include_once 'databaseconnection.php'; 

<div class="container">
        // Fetch products from the database 
        $results = $db->query("SELECT * FROM products WHERE status = 1"); 
        while($row = $results->fetch_assoc()){ 
        <div class="pro-box">
            <img src="images/<?php echo $row['image']; ?>"/>
            <div class="body">
                <h5><?php echo $row['name']; ?></h5>
                <h6>Price: <?php echo '$'.$row['price'].' '.PAYPAL_CURRENCY; ?></h6>
                <form action="<?php echo PAYPAL_URL; ?>" method="post">
                    <input type="hidden" name="business" value="<?php echo PAYPAL_ID; ?>">
                    <input type="hidden" name="cmd" value="_xclick">
                    <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="<?php echo PAYPAL_CURRENCY; ?>">
                    <input type="hidden" name="return" value="<?php echo PAYPAL_RETURN_URL; ?>">
                    <input type="hidden" name="cancel_return" value="<?php echo PAYPAL_CANCEL_URL; ?>">
                    <input type="image" name="submit" border="0" src="">
    <?php } ?>

paypalpayment Success


include_once 'config.php'; 

include_once 'databaseconnection.php'; 
if(!empty($_GET['item_number']) && !empty($_GET['tx']) && !empty($_GET['amt']) && !empty($_GET['cc']) && !empty($_GET['st'])){ 

    $item_number = $_GET['item_number'];  
    $txn_id = $_GET['tx']; 
    $paypalpaypalpayment_gross = $_GET['amt']; 
    $currency_code = $_GET['cc']; 
    $paypalpaypalpayment_status = $_GET['st']; 
    $productResult = $db->query("SELECT * FROM products WHERE id = ".$item_number); 
    $productRow = $productResult->fetch_assoc(); 

    $prevpaypalpaymentResult = $db->query("SELECT * FROM paypalpayments WHERE txn_id = '".$txn_id."'"); 
    if($prevpaypalpaymentResult->num_rows > 0){ 
        $paypalpaymentRow = $prevpaypalpaymentResult->fetch_assoc(); 
        $paypalpayment_id = $paypalpaymentRow['paypalpayment_id']; 
        $paypalpayment_gross = $paypalpaymentRow['paypalpayment_gross']; 
        $paypalpayment_status = $paypalpaymentRow['paypalpayment_status']; 

        $insert = $db->query("INSERT INTO paypalpayments(item_number,txn_id,paypalpayment_gross,currency_code,paypalpayment_status) VALUES('".$item_number."','".$txn_id."','".$paypalpayment_gross."','".$currency_code."','".$paypalpayment_status."')"); 
        $paypalpayment_id = $db->insert_id; 

<div class="container">
    <div class="status">
        <?php if(!empty($paypalpayment_id)){ ?>
            <h1 class="success">Your paypalpayment has been Successful</h1>
            <h4>paypalpayment Information</h4>
            <p><b>Reference Number:</b> <?php echo $paypalpayment_id; ?></p>
            <p><b>Transaction ID:</b> <?php echo $txn_id; ?></p>
            <p><b>Paid Amount:</b> <?php echo $paypalpayment_gross; ?></p>
            <p><b>paypalpayment Status:</b> <?php echo $paypalpayment_status; ?></p>
            <h4>Product Information</h4>
            <p><b>Name:</b> <?php echo $productRow['name']; ?></p>
            <p><b>Price:</b> <?php echo $productRow['price']; ?></p>
        <?php }else{ ?>
            <h1 class="error">Your paypalpayment has Failed</h1>
        <?php } ?>
    <a href="index.php" class="btn-link">Back to Products</a>

paypalpayment Cancellation


<div class="container">
    <div class="status">
        <h1 class="error">Your PayPal Transaction has been Canceled</h1>
    <a href="index.php" class="btn-link">Back to Products</a>


You can also read about AngularJS, ASP.NET, VueJs, PHP.

Also Read This πŸ‘‰   how to disable back button in php

I hope you get an idea about paypal integration in php.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.