All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Integrate Paypal Payment System Using PHP And MySQL

Last Updated : Jul 1, 2023

IN - PHP MySQL | Written & Updated By - Riya

In this tutorial we will show you how to integrate paypal payment system using PHP and MySQL, Payment system is must have for those website who have something to sell on their website and integrate payment system in your website makes the whole process so easy.

You have to create account on Paypal Developer

Integrate Paypal Payment System Using PHP And MySQL

To Create Paypal Payment System It Takes Only two Steps:-

  1. Make a PHP file and define markup and scripting
  2. Make a PHP file to store transaction

Step 1. Make a PHP file and define markup and scripting

We make a PHP file and save it with a name payment.php

<?php
$paypal_url='https://www.paypal.com/cgi-bin/webscr';
$paypal_id='your_seller_id'; // Business email ID
?>

<html>
<body>

<div class="product_div">
 <p class="image"><img src="product1.jpg"/></p>
 <p class="name">Sample Product 1</p>
 <p class="price">Price:$10</p>
    
 <div class="paypal_button">
  <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="Sample Product 1">
  <input type="hidden" name="item_number" value="1">
  <input type="hidden" name="credits" value="300">
  <input type="hidden" name="userid" value="1">
  <input type="hidden" name="amount" value="10">
  <input type="hidden" name="cpp_header_image" value="http://sample_site.com/images/product1.jpg">
  <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="http://sample_site.com/cancel.php">
  <input type="hidden" name="return" value="http://sample_site.com/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>
</body>
</html>

In this step we create a sample product for selling and insert paypal button then insert all the required fields in paypal payment form.

In this form there are two main fields one is 'return' and another is 'cancel_return'.'return' is the url where paypal send data like transaction id after payment done successfully then in 'cancel_return' we give cancel payment url to cancel payment.

Step 2. Make a PHP file to store transaction

We make a PHP file and save it with a name success.php

// Database Structure 
CREATE TABLE `payment_transaction` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `transaction_id` text NOT NULL,
 `amount` int(11) NOT NULL,
 `currency_type` text NOT NULL,
 `item_number` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php
$host="localhost";
$username="root";
$password="";
$databasename="sample";

$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

if($_GET['st']=="Completed")
{
 $item_transaction = $_GET['tx'];
 $item_price = $_GET['amt']; 
 $item_currency = $_GET['cc'];
 $item_no = $_GET['item_number'];
 
 mysql_query("insert into payment_transaction values('','$item_transaction','$item_price','$item_currency','$item_no')");
 echo "Payment Done Successfully";
}
?>

In this step we first create a sample table to store transaction detail after every successful payment done.

This kind of url 'success.php?tx=734HJ28JKDF32&st=Completed&amt=10.00&cc=USD&cm=&item_number=1' is return after completion of payment successfully.Then we get all the values and store them in our database.

Thats all, this is how to integrate paypal payment system using PHP and MySQL. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on integrate paypal using php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪