Integrate PayPal Payment System in php and mysql

Abhigyan Singh 08th Oct 2020

This tutorial will explain to integrate the PayPal payment system in PHP and MySQL. PayPal is the most popular payment method and money transfers method through the Internet. We received requests from our viewer that asked me "how to integrate the PayPal payment system in PHP and MySQL". So, in the bellows tutorial, we’ll explain how to integrate the PayPal payment system in PHP and MySQL.

I have designed a database named "my_paypal" and create a table named "my_products" to integrate the PayPal payment system. My database contains three tables.

There are:

1.    ‘my_users’ table: This table is for user information.

2.    ‘products’ table: This table is for product information.

3.    ‘sales’ table: This table is for sales information.

To create a table named ‘my_users’, I have used this code.

CREATE TABLE IF NOT EXISTS `my_users` (
`my_user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`my_user_id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Fine, my_users table creating is done. Now I’ll create code for ‘my_products’.

CREATE TABLE IF NOT EXISTS `my_products` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`product` varchar(255) NOT NULL,
`product_img` varchar(100) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`currency` varchar(10) DEFAULT 'USD',
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Fine, my_products table creating is done. Now I’ll create code for ‘my_sales’.

CREATE TABLE IF NOT EXISTS `my_sales` (
  `sales_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) DEFAULT NULL,
  `my_user_id` int(11) DEFAULT NULL,
  `saledate` date DEFAULT NULL,
  'transactionid' varchar(125),
  PRIMARY KEY (`sales_id`),
  KEY `product_id` (`product_id`),
  KEY `my_user_id` (`my_user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

Fine, my_sales table creating is done.  Now follow bellows step for create PayPal sandbox account.

Step: 1

Now I’ll create a PayPal sandbox account from this URL https://developer.paypal.com/

Step: 2

Login using your PayPal username & password to create sandbox account.

  1. Go to left side top menu
  2. Go to left side top menu

Step: 3

I have created two type sandbox accounts from test account.

a)    One is Buyer (personal)

b)    Another is Seller (merchant/business)


OK, Fine. Sandbox account is done. Now I’ll create a php file named ‘products.php’. I have use bellows code to create ‘products.php’ file.

<?php
session_start();
$_SESSION['uid'] = '1';
$_SESSION['username'] = 'yourname';
$paypal_url='https://www.sandbox.paypal.com/cgi-bin/webscr';
$paypal_id='your_seller_id'; // [email protected]
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My Demo | Products</title>
<style type="text/css">
body{
width: 80%;
margin: 0 auto;
margin-top: 50px;
font:bold 14px arial;
}
.product{
float: left;
margin-right: 10px;
border: 1px solid #cecece;
padding: 10px;
margin-right: 20px;
}
.price{
text-align: right;
}
.btn{
text-align: center;
}
</style>
</head>
<body>
<h2>Hello, <?php echo $_SESSION['username'];?></h2>
<?php
require 'db_config.php';
$result = mysql_query("SELECT * from my_products");
while($row = mysql_fetch_array($result)) {
?>
<div class="product">
<div class="image">
<img src="images/<?php echo $row['product_img'];?>" alt="" width="197px" height="210px"/>
</div>
<div class="name">
<?php echo $row['product'];?>
</div>
<div class="price">
Price: <?php echo $row['price'];?>$
</div>
<div class="btn">
<form action='<?php echo $paypal_url; ?>' method='post' name='frmPayPal1'>
<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['product'];?>'>
<input type='hidden' name='item_number' value='<?php echo $row['product_id'];?>'>
<input type='hidden' name='amount' value='<?php echo $row['price'];?>'>
<input type='hidden' name='no_shipping' value='1'>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='handling' value='0'>
<input type='hidden' name='cancel_return' value='https://localhost/paypal/cancel.php'>
<input type='hidden' name='return' value='https://localhost/paypal/success.php'>

<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</div>
</div>

<?php
}
?>
</body>
</html>

Step: 4

I have created product page for display my demo product.  Now I’ll create success page for display success message. Follows code for success page.

<?php
require 'db_config.php';

session_start();
$my_user_id = $_SESSION['my_user_id'];
$username=$_SESSION['username'];

$item_no = $_GET['item_number'];
$item_transaction = $_GET['tx'];
$item_price = $_GET['amt'];
$item_currency = $_GET['cc'];

//Getting product details
$sql=mysql_query("select product,price,currency from producst where product_id='$item_no'");
$row=mysql_fetch_array($sql);
$price=$row['price'];
$currency=$row['currency'];

//Rechecking the product details
if($item_price==$price && item_currency==$currency)
{

$result = mysql_query("INSERT INTO my_sales(product_id, my_user_id, saledate,transactionid) VALUES('$item_no', '$my_user_id', NOW(),'$item_transaction')");
if($result){
echo "<h1>Hello, $username</h1>";
echo '<h1>Payment Successful</h1>';
echo "<b>Example Query</b>: INSERT INTO my_sales(product_id, my_user_id, saledate) VALUES('<font color='#f00;'>$item_no</font>', '<font color='#f00;'>$my_user_id</font>', <font color='#f00;'>NOW()</font>)";
}else{
echo "Payment Error";
}
}
else
{
echo "Payment process is failed";
}
?>

Step: 5

Step 5 is done by creating payment success page. Now, I’ll create payment cancel page for display payment cancel message. Given bellow the cancel code.

<?php
session_start();
$username=$_SESSION['username'];
echo "<h1>Hello, $username</h1>";
echo "<h1>Your payment has been canceled</h1>";
?>

Step: 6

When you complete test payment system, you must change your PayPal form action development URL to real URL. Please change as like as bellows step.

$paypal_url='https://www.sandbox.paypal.com/cgi-bin/webscr';

to

$paypal_url='https://www.paypal.com/cgi-bin/webscr';

That’s all to integrate PayPal payment system in php and mysql. If you face problem to integrate PayPal payment system in php and mysql, feel free to comment your suggestions regarding this tutorial without any hesitation.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk