Paypal is a multinational financial technology company proving online international payment services. Paypal is one of the best international payment gateway which is available for freelancers' businesses, smallest businesses to the largest businesses. In this tutorial, we will integrate the standard PayPal payment gateway in PHP with the MYSQL database. Standard means, we will consider the highest secured service to integrate PayPal payment gateway with IPN.
What is PayPal IPN?
IPN stands for instant payment notification. IPN is a message service that automatically notifies merchants of events related to PayPal transactions. Merchants can use it to automate back-office and administrative functions, including automatically fulfilling orders and providing customers with order status. IPN is very secure. In simple words, IPN is used to get payment success, payment refund, pending, completed, or denied status events, Recurring payments, subscription actions, Chargebacks, disputes, reversals, refunds, etc information from Paypal without merchant interaction(Automated). It works automatically. For example- If a customer buys a product then transaction data insert into the database with IPN and if Paypal refunds the amount, then refund data is also inserted into the database without merchant or customer interaction. Merchants can check payment status. It’s completely automated.
How to standard PayPal payment gateway integration in PHP with MYSQL database –
As you know, we use the Paypal payment gateway for eCommerce system and subscription systems. We must need a Paypal PHP kit to integrate the PayPal payment gateway in PHP. Paypal has many PHP kits or SDKs but we will use IPN-based PHP KIT.
We will create a shop. We will create many products with different prices and sell &pay with Paypal. For example – A customer can select any product from the shop page and click on the buy now button. After then, the customer fills details like the customer first name, last name, email, mobile number, email id, address, and a note just like an eCommerce system checkout page.
After that, the customer can pay with PayPal. Here, we will use the Paypal payment form with Paypal sandbox URL for testing purposes and Paypal live URL for production.
When payment is successful, the customer can check the transaction details on the success page and at the same time, the IPN file gets executed and insert data into the data. We receive payment data in the IPN.php file and verify with success.php.
While integrating the PayPal payment form, we set PayPal return URL (success page URL in sandbox account or PayPal production account), cancel URL, and IPN URL. This information goes to the PayPal website and as you know IPN is a message service that automatically notifies merchants of events related to PayPal transactions.
Let’s integrate a standard PayPal payment gateway in PHP with the MYSQL database –
First of all, create a table “products” using the below query –
CREATE TABLE `products` (
`pid` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`title` varchar(355) DEFAULT NULL,
`price` varchar(20) DEFAULT NULL,
`image` varchar(255) DEFAULT NULL
);
We will create a form to create new products with the product title, product price, and product image. We will fetch and display all products on another page.
Now, create a table “payments” using the below query –
CREATE TABLE `payments` (
`payid` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`firstname` varchar(40) DEFAULT NULL,
`lastname` varchar(40) DEFAULT NULL,
`amount` varchar(20) DEFAULT NULL,
`txnid` varchar(255) DEFAULT NULL,
`pid` int(11) DEFAULT NULL,
`payer_email` varchar(40) DEFAULT NULL,
`currency` varchar(10) DEFAULT NULL,
`mobile` varchar(15) NOT NULL,
`address` varchar(455) DEFAULT NULL,
`note` text DEFAULT NULL,
`payment_date` datetime DEFAULT NULL,
`status` varchar(30) DEFAULT NULL
);
We will insert every transaction record into this MYSQL database table with the product id.
Now, we will create a connection file. The connection file will help you to make a connection between PHP and MYSQL. We will use PHP PDO-prepared statements to make secure PayPal payment gateway integration.
config.php
<?php session_start();
define('DBNAME','gateway');
define('DBUSER','root');
define('DBPASS','');
define('DBHOST','localhost');
try {
$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Your page is connected with database successfully..";
} catch(PDOException $e) {
echo "Issue -> Connection failed: " . $e->getMessage();
}
?>
Kindly set all credentials according to your database.
We will now create products with the product title, price, and image. Users can buy these products from your website using the PayPal payment gateway. We will use bootstrap to make responsive forms, tables, product cards, etc.
Let’s create products –
create-product.php
<?php require_once("config.php");?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add Product - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 form-container">
<h1>Create a Product </h1>
<?php
if(isset($_POST['submit_form']))
{
$title=$_POST['title'];
$price=$_POST['price'];
$folder = "uploads/";
$image_file=$_FILES['image']['name'];
$file = $_FILES['image']['tmp_name'];
$path = $folder . $image_file;
$target_file=$folder.basename($image_file);
//move image to the folder
move_uploaded_file($file,$target_file);
$sql="INSERT into products(title,price,image) VALUES(:title,:price,:image)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':price', $price, PDO::PARAM_STR);
$stmt->bindParam(':image', $image_file, PDO::PARAM_STR);
$stmt->execute();
header("location:products.php");
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label class="label">Product Title </label>
<input type="text" class="form-control" name="title" required>
</div>
<div class="mb-3">
<label class="label">Product Price</label>
<input type="number" class="form-control" name="price" required>
</div>
<div class="mb-3">
<label class="label">Product Image</label>
<input type="file" class="form-control" name="image" accept="image/*" required>
</div>
<button type="submit" class="btn btn-primary" name="submit_form">Create</button>
</form>
</div>
<div class="col-sm-3"></div>
</div>
</div>
</body>
</html>
We have created a simple insert query in the above code using PHP PDO-prepared statements.
Note – Kindly create an uploads folder in your project folder. Create a new folder name "uploads"
After that, we display all products on another page.
products.php
<?php require_once("config.php");?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Products - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8 form-container">
<h1>Products</h1>
<hr>
<a href="create-product.php" class="btn btn-primary" style="float:right;">Create New </a>
<table class="table">
<tr>
<th>Image</th>
<th>Title</th>
<th>Price</th>
</tr>
<?php
$sql="SELECT * from products order by pid DESC";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows=$stmt->fetchAll();
foreach ($rows as $row) {
echo '<tr>
<td><img src="uploads/'.$row['image'].'" height="100"></td>
<td>'.$row['title'].'</td>
<td>'.$row['price'].' USD</td>
</tr>';
}
?>
</table>
</div>
<div class="col-sm-2"></div>
</div>
</div>
</body>
</html>
In the above code, we have fetched and displayed all products.
If you want to design these forms, kindly use the below CSS stylesheet.
style.css
body{
background-color: #f1f1f1;
}
.form-container
{
padding: 20px;
border-radius: 20px;
background-color: #fff;
margin-top: 10px ;
}
.label{
font-weight: 500;
}
.paypal_button{
background-color: #00BD68;
border-radius: 5px;
color: #FFFFFF;
cursor: pointer;
height: 44px;
line-height: 44px;
width: 100%;
border: 0;
}
Products are created. Now, we will fetch and display these products on the index page with the buy now button.
index.php
<?php require_once("config.php");?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Shop - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 form-container">
<h1>Shop </h1>
<hr>
<div class="row">
<?php
$sql="SELECT * from products order by pid DESC";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows=$stmt->fetchAll();
foreach ($rows as $row) {
echo '<div class="col-4 text-center"><div class="card" style="width: 18rem;">
<img class="card-img-top" src="uploads/'.$row['image'].'" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">'.$row['title'].'</h5>
<p class="card-text">'.$row['price'].' USD</p>
<a href="checkout.php?product_id='.$row['pid'].'" class="btn btn-primary">Buy Now</a>
</div>
</div></div>';
}
?>
</div>
</div>
</div>
</div>
</body>
</html>
The customer will click on the buy now button and process for checkout. This is a similar process to the eCommerce system in PHP with a PayPal payment gateway.
Now, we will create a checkout page to receive user data. As you know, we create a checkout page in the eCommerce system to receive user data and process payment using the PayPal payment gateway.
Let's create a checkout page
checkout.php
<?php require_once("config.php"); $pid=$_GET['product_id'];
$sql="SELECT count(*) from products WHERE pid=:pid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':pid', $pid, PDO::PARAM_INT);
$stmt->execute();
$count=$stmt->fetchcolumn();
if($count==0)
{
header('location:index.php');
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Checkout - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 form-container">
<h1>Checkout</h1>
<hr>
<?php
if(isset($_POST['submit_form']))
{
$_SESSION['fname']=$_POST['fname'];
$_SESSION['lname']=$_POST['lname'];
$_SESSION['email']=$_POST['email'];
$_SESSION['mobile']=$_POST['mobile'];
$_SESSION['note']=$_POST['note'];
$_SESSION['address']=$_POST['address'];
$_SESSION['pid']=$pid;
if($_POST['email']!='')
{
header("location:pay.php");
}
}
?>
<div class="row">
<div class="col-8">
<form action="" method="POST">
<div class="mb-3">
<label class="label">First Name</label>
<input type="text" class="form-control" name="fname" required>
</div>
<div class="mb-3">
<label class="label">Last Name</label>
<input type="text" class="form-control" name="lname" required>
</div>
<div class="mb-3">
<label class="label">Email </label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="mb-3">
<label class="label">Mobile</label>
<input type="number" class="form-control" name="mobile" required>
</div>
<div class="mb-3">
<label class="label">Address</label>
<textarea name="address" class="form-control" name="address" required></textarea>
</div>
<div class="mb-3">
<label class="label">Note</label>
<textarea name="note" class="form-control" name="note"></textarea>
</div>
</div>
<div class="col-4 text-center">
<?php
$sql="SELECT * from products WHERE pid=:pid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':pid',$pid,PDO::PARAM_INT);
$stmt->execute();
$row=$stmt->fetch();
echo '<div class="card" style="width: 18rem;">
<img class="card-img-top" src="uploads/'.$row['image'].'" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">'.$row['title'].'</h5>
<p class="card-text">'.$row['price'].' USD</p>
</div>
</div>';
?>
<br>
<button type="submit" class="btn btn-primary" name="submit_form">Place Order</button>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
We have created the HTML form with the product image in the above code. We are getting the product id from the URL. As you can see, there is a place order button.
Logic – We will take all details from the buyer and save them in different session variables and we will use these details during payment time.
When you will click the place order button, you will be redirected to the payment page. We will use the PayPal payment gateway PHP kit code in this file.
Create a pay PHP file.
pay.php
<?php require_once("config.php");
if(!isset($_SESSION['email']))
{
header('location:index.php');
exit();
}
else
{
$pid=$_SESSION['pid'];
}
include("gateway-config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Payment - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 form-container">
<h1>Payment</h1>
<hr>
<?php
$firstname=$_SESSION['fname'];
$lastname=$_SESSION['lname'];
$email=$_SESSION['email'];
$mobile=$_SESSION['mobile'];
$address=$_SESSION['address'];
$note=$_SESSION['note'];
$sql="SELECT * from products WHERE pid=:pid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':pid',$pid,PDO::PARAM_INT);
$stmt->execute();
$row=$stmt->fetch();
$price=$row['price'];
$title=$row['title'];
?>
<div class="row">
<div class="col-8">
<h4>(Payer Details)</h4>
<div class="mb-3">
<label class="label">First Name :- </label>
<?php echo $firstname; ?>
</div>
<div class="mb-3">
<label class="label">Last Name:- </label>
<?php echo $lastname; ?>
</div>
<div class="mb-3">
<label class="label">Email:- </label>
<?php echo $email; ?>
</div>
<div class="mb-3">
<label class="label">Mobile:- </label>
<?php echo $mobile; ?>
</div>
<div class="mb-3">
<label class="label">Address:- </label>
<?php echo $address; ?>
</div>
<div class="mb-3">
<label class="label">Note:- </label>
<?php echo $note; ?>
</div>
</div>
<div class="col-4 text-center">
<?php
$sql="SELECT * from products WHERE pid=:pid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':pid',$pid,PDO::PARAM_INT);
$stmt->execute();
$row=$stmt->fetch();
echo '<div class="card" style="width: 18rem;">
<img class="card-img-top" src="uploads/'.$row['image'].'" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">'.$row['title'].'</h5>
<p class="card-text">'.$row['price'].' '.PAYPAL_CURRENCY.'</p>
</div>
</div>';
?>
<form action="<?php echo PAYPAL_URL; ?>" method="post" class="form-container price">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="<?php echo PAYPAL_ID; ?>">
<!-- 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 $title;?> ">
<input type="hidden" name="item_number" value="<?php echo $pid;?> ">
<input type="hidden" name="amount" value="<?php echo $price; ?>">
<input type="hidden" name="currency_code" value="<?php echo PAYPAL_CURRENCY; ?>">
<!-- Specify URLs -->
<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="hidden" name="notify_url" value="<?php echo PAYPAL_NOTIFY_URL; ?>">
<td><input type="hidden" class="form-control" value="<?php echo $firstname;?>" readonly/></td>
<td><input type="hidden" class="form-control" value="<?php echo $lastname;?>" readonly/></td>
<td><input type="hidden" class="form-control" value="<?php echo $email;?>" readonly/></td>
<input type="hidden" name="custom" value="mob=<?php echo $mobile;?>&add=<?php echo $address;?>¬e=<?php echo $note;?>"/>
<center><input type="submit" name="submit" class="paypal_button" value="Pay Now" ></center>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
In the above code, we have used the Paypal payment form with customer data using session. As you know, we are getting customer information from the checkout page. We have used the same session variables values in the Paypal payment form. In the above code, you can see a custom field. The custom field is used to send multiple values to the Paypal website and of course, we can retrieve custom values from the Paypal website.
We can save different values in different variables.
Now you will need a gateway config file. This config file will be used to set all required credentials and URLs.
gateway-config.php
<?php
// PayPal configuration
define('PAYPAL_ID', 'xxxxxxxxxxxxxxxxx'); //Business Email
$mode='test'; // test or live
define('PAYPAL_RETURN_URL', 'https://technosmarter.com/paypal/success.php');
define('PAYPAL_CANCEL_URL', 'https://technosmarter.com/paypal/cancel.php');
define('PAYPAL_NOTIFY_URL', 'https://technosmarter.com/paypal/ipn.php');
define('PAYPAL_CURRENCY', 'USD');
// Change not required
if($mode=='live')
{
define('PAYPAL_URL','https://www.paypal.com/cgi-bin/webscr');
}
else
{
define('PAYPAL_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
}
?>
In the above code, as you can see, we have created many constants for different values. If you want to test payment before live then use the sandbox account seller and buyer account.
Steps –
https://developer.paypal.com/
Login with your Paypal business email. Create two sandbox emails.
I. For the seller ( Merchant Email ) – Use as Paypal id in the setup form
II. For buyer (Buyer email ) - Use at payment time
Note – Check in testing tools ->sandbox account
Click on create account and create a Personal (Buyer Account) and Business (Merchant Account) .
Use business (Merchant Account) account in the gateway config file as PAYPAL_ID and use Personal (Buyer Account) during payment time.
Set the password for both emails. View and edit the account to set password, first name, last name, and email whatever you want the update.
Open PayPal sandbox login using the link below.
https://sandbox.paypal.com/
1. Login with PayPal sandbox account with Business (Merchant Account) email id and password
2. Go to the Account setting after a click on the gear icon.
3. Now click on the website payment option.
4. Now check for Website preferences and click on update.
5. Auto return – On
Paste your success.php file path in auto return and save.
Payment data transfer - On
Block non-encrypted website payment - Off
6. PayPal account optional – On
7. Contact telephone - Off (PayPal recommends this option)
8. support giropay and bank transfer payments- No
Now, create an IPN PHP file.
ipn.php
<?php include("config.php"); include("gateway-config.php");
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if($get_magic_quotes_exists == true) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
$paypalURL = PAYPAL_URL;
$txn_id='ipnfalse';
$ch = curl_init($paypalURL);
if ($ch == FALSE) {
return FALSE;
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: company-name'));
$res = curl_exec($ch);
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp($res, "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) {
$pid = $_POST['item_number'];
$txn_id = $_POST['txn_id'];
$payment_gross = $_POST['mc_gross'];
$currency_code = $_POST['mc_currency'];
$payment_status = $_POST['payment_status'];
$payer_email=$_POST['payer_email'];
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$payer_id=$_POST['payer_id'];
parse_str($_POST['custom'],$_MYVAR);
$phone=$_MYVAR['mob'];
$address=$_MYVAR['add'];
$note=$_MYVAR['note'];
$payer_address_country=$_POST['address_country'];
$date = new DateTime(null, new DateTimezone("Asia/Kolkata"));
$payment_date=$date->format('Y-m-d H:i:s');
$sql="SELECT count(*) from payments WHERE txnid=:txnid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':txnid', $txn_id, PDO::PARAM_STR);
$stmt->execute();
$pay_count=$stmt->fetchcolumn();
if($pay_count > 0){
exit();
}else{
$sql="INSERT INTO payments(firstname,lastname,amount,status,txnid,pid,payer_email,currency,mobile,address,note,payment_date) VALUES(:firstname,:lastname,:amount,:status,:txnid,:pid,:payer_email,:currency,:mobile,:address,:note,:payment_date)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);
$stmt->bindParam(':lastname', $lastname, PDO::PARAM_STR);
$stmt->bindParam(':amount', $payment_gross, PDO::PARAM_STR);
$stmt->bindParam(':status', $payment_status, PDO::PARAM_STR);
$stmt->bindParam(':txnid', $txn_id, PDO::PARAM_STR);
$stmt->bindParam(':pid', $pid, PDO::PARAM_INT);
$stmt->bindParam(':payer_email', $payer_email, PDO::PARAM_STR);
$stmt->bindParam(':currency', $currency_code, PDO::PARAM_STR);
$stmt->bindParam(':mobile', $phone, PDO::PARAM_STR);
$stmt->bindParam(':address', $address, PDO::PARAM_STR);
$stmt->bindParam(':note', $note, PDO::PARAM_STR);
$stmt->bindParam(':payment_date', $payment_date, PDO::PARAM_STR);
$stmt->execute();
}
}
header('HTTP/1.1 200 OK');
As we have already discussed, we get instant payment details using IPN and its automated feature. In the above code, the IPN file received data from Paypal. You can see, we have inserted data into the database. This is very secure to store data.
Now, create a success page to display transaction details to customers.
success.php
<?php require_once("config.php");
include("gateway-config.php");
sleep(2);
if(!empty($_GET['tx']) && !empty($_GET['amt']) && !empty($_GET['cc']) && !empty($_GET['st'])){
// Get transaction information from URL
$item_number = $_GET['item_number'];
$txn_id = $_GET['tx'];
$payment_gross = $_GET['amt'];
$currency_code = $_GET['cc'];
$payment_status = $_GET['st'];
$sql="SELECT count(*) from payments WHERE txnid=:txnid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':txnid', $txn_id, PDO::PARAM_STR);
$stmt->execute();
$pay_count=$stmt->fetchcolumn();
if($pay_count > 0){
$rows= $sql="SELECT * from payments WHERE txnid=:txnid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':txnid',$txn_id,PDO::PARAM_STR);
$stmt->execute();
$rows=$stmt->fetchAll();
foreach($rows as $paymentRow){
$payid= $paymentRow['payid'];
$amount = $paymentRow['amount'];
$status = $paymentRow['status'];
$email = $paymentRow['payer_email'];
$firstname = $paymentRow['firstname'];
$lastname = $paymentRow['lastname'];
//$payer_id = $paymentRow['payer_id'];
$currency = $paymentRow['currency'];
$dbdate = $paymentRow['payment_date'];
$pid= $paymentRow['pid'];
$mobile = $paymentRow['mobile'];
$address = $paymentRow['address'];
$note = $paymentRow['note'];
}
}/*else{
// You can insert transaction data here without IPN
}*/
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Payment Status - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 form-container">
<h1>Payment Status</h1>
<hr>
<div class="row">
<div class="col-8">
<?php
if(!empty($payid))
{
$subject='Your payment has been successful..';
if($txn_id!=''){
echo ' <h2 style="color:#33FF00";>'.$subject.'</h2> <hr>';
echo '<table class="table">';
echo '<tr> ';
echo '<tr>
<th>Transaction ID:</th>
<td>'.$txn_id.'</td>
</tr>
<tr>
<th>Paid Amount:</th>
<td>'.$amount.' '. $currency.'</td>
</tr>
<tr>
<th>Payment Status:</th>
<td>'.$status.'</td>
</tr>
<tr>
<th>Payer Email:</th>
<td>'.$email.'</td>
</tr>
<tr>
<th>Name:</th>
<td>'.$firstname.' '.$lastname.'</td>
</tr>
<tr>
<th>Mobile No:</th>
<td>'.$mobile.'</td>
</tr>
<tr>
<th>Address:</th>
<td>'.$address.'</td>
</tr>
<tr>
<th>Note:</th>
<td>'.$note.'</td>
</tr>
<tr>
<th>Date :</th>
<td>'.$dbdate.'</td>
</tr>
</table>';
}
}
else
{
$html = "<p><div class='errmsg'>Invalid Transaction. Please Try Again</div></p>
";
$error_found=1;
}
if(isset($html)){
echo $html;
}
?>
</div>
<div class="col-4 text-center">
<?php
if(!isset($error_found)){
$sql="SELECT * from products WHERE pid=:pid";
$stmt = $db->prepare($sql);
$stmt->bindParam(':pid',$pid,PDO::PARAM_INT);
$stmt->execute();
$row=$stmt->fetch();
echo '<div class="card" style="width: 18rem;">
<img class="card-img-top" src="uploads/'.$row['image'].'" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">'.$row['title'].'</h5>
</div>
</div>';
}
?>
<br>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You can check, we have already sent our success page URL to PayPal for the return path. At return time, we get payment details in the success page URL and we verify these details with IPN data. First of all, IPN notifies the merchant to store the payment data, and then the success page check that data with the transaction id.
If the payment data is found with txn_id then display payment details otherwise display payment Invalid Transaction. Please Try Again.
Now, create a cancel page.
This is just returning path whenever the user presses the cancel button or link from the PayPal website. We have already created a constant for it.
Now, create a cancel page.
cancel.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Payment Canceled - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 form-container">
<h1>Payment Canceled</h1>
<hr>
<div class="row">
<div class="col-8">
<p><div class='errmsg'>Your payment has been canceled.</div></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
We have already discussed that we are created a simple eCommerce system in PHP with PayPal. We are integrating the PayPal payment gateway using PHP and MYSQL database.
Now, we will display all payments (orders ) on a page.
payments.php
<?php require_once("config.php");?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Payments | Orders - Techno Smarter </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 form-container">
<h1>Payments | Orders</h1>
<hr>
<table class="table">
<tr>
<th>Paid By </th>
<th>Payer Email</th>
<th>Txn Id </th>
<th>Product Image</th>
<th>Title</th>
<th>Paid Amount</th>
<th>Address</th>
<th>Mobile</th>
<th>Note</th>
<th>Order Date</th>
</tr>
<?php
$sql="SELECT * from products,payments WHERE products.pid=payments.pid order by payments.payid DESC ";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows=$stmt->fetchAll();
foreach ($rows as $row) {
echo '<tr>
<td>'.$row['firstname'].' '.$row['lastname'].'</td>
<td>'.$row['payer_email'].'</td>
<td>'.$row['txnid'].'</td>
<td><img src="uploads/'.$row['image'].'" height="100"></td>
<td>'.$row['title'].'</td>
<td>'.$row['amount'].' '.$row['currency'].'</td>
<td>'.$row['address'].'</td>
<td>'.$row['mobile'].'</td>
<td>'.$row['note'].'</td>
<td>'.$row['payment_date'].'</td>
</tr>';
}
?>
</table>
</div>
</div>
</div>
</body>
</html>
You can use it as an orders page or a payments page. Admin can check all payments or orders on this page.
You can execute on test mode or live anytime from the gateway config file.
In this way, you can integrate the standard PayPal payment gateway in PHP with the MYSQL database.
Recommended Posts:-