Home » paypal payment gateway integration in php

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

paypal_integration_php/
├── config.php
├── databaseconnection.php
├── index.php
├── success.php
├── cancel.php
├── ipn.php
├── css/
│   └── style.css
└── images/

Create PayPal Sandbox Account

Go to the https://developer.paypal.com/

Create Database Tables

SQL creates a products table in the MySQL database

CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `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',
 PRIMARY KEY (`id`)
) 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

config.php

<?php 

define('PAYPAL_ID', 'Insert_PayPal_Business_Email'); 
define('PAYPAL_SANDBOX', TRUE); //TRUE or FALSE 
 
define('PAYPAL_RETURN_URL', 'http://www.domain_name.com/success.php'); 
define('PAYPAL_CANCEL_URL', 'http://www.domain_name.com/cancel.php'); 
define('PAYPAL_NOTIFY_URL', 'http://www.domain_name.com/ipn.php'); 
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)?"https://www.sandbox.paypal.com/cgi-bin/webscr":"https://www.paypal.com/cgi-bin/webscr");

Database Connection

databaseconnection.php

<?php 
// Connect with the database 
$db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); 
 
// Display error if failed to connect 
if ($db->connect_errno) { 
    printf("Connect failed: %s\n", $db->connect_error); 
    exit(); 
}

Products Page

index.php

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

<div class="container">
    <?php 
        // 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="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif">
                </form>
            </div>
        </div>
    <?php } ?>
</div>

paypalpayment Success

success.php

<?php 
 
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']; 
    }else{ 

        $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 } ?>
    </div>
    <a href="index.php" class="btn-link">Back to Products</a>
</div>

paypalpayment Cancellation

cancel.php

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

Summary

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

Read Also:  PHP Laravel 6 Multiple Database Connections Tutorial

I hope you get an idea about paypal integration in php.
I would like to have feedback on my infinityknow.com 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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  install PHP Accelerator in Xampp - prestashop
  2. Read Also:  JQuery Remove combobox items(options) from DropDownList
  3. Read Also:  Vue 2.0 Hello World Step By Step
  4. Read Also:  PayPal Payment Gateway with PHP MySQL Database
  5. Read Also:  what are the main components of a database system?
  6. Read Also:  How to add custom jquery image validation?
  7. Read Also:  Simple PHP Shopping Cart
  8. Read Also:  Laravel JSON Encode Decode Handling
  9. Read Also:  Laravel 5/6/7 Joins Example Tutorial - Query Builder
  10. Read Also:  jQuery Hide DIV After 5 Seconds Dynamic

Leave a Comment

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