Paypal Payment Gateway Integration in PHP

Are you looking for tutorial for Paypal payment gateway integration in PHP? Yes, you are here at the right place. In this tutorial, we are going to cover PayPal payment gateway integration using PHP. By going through this short tutorial, you can easily integrate Paypal payment system in your web project.

In this tutorial, we will use Paypal sandbox developer mode to check payment flow. First we will receive payment from the buyers end and then store the payment transaction information into the database.

Also, read:

As the PayPal have two payment environments such as Sandbox and Real Time. The Sandbox environment help developers to test transaction before the project go live. Real Time environment is used after project live. So here you will see example with Paypal sandbox test account.

Steps to create a new Sandbox test account:


  • Step 1 – First go to the https://developer.paypal.com/. Log in with your PayPal account. If you have not created your PayPal account yet, first sign up at PayPal. Once the sign up is completed, login with this account.
  • Step 2 – After logged with Paypal account, you would be redirected to the developer home page. Now go to the Dashboard.
  • Step 3 – Then click on the Accounts link under the Sandbox.
  • Step 4 – Now create test accounts for seller and buyer by selecting Business and Personal respectively from the Create Account link.

Here we have created two tables products and payments. The products table will be used to store product details and payments table will be used for storing the transaction details from PayPal.

products table:

CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`p_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`p_image` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`price` float(10,2) NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

payments table:

CREATE TABLE IF NOT EXISTS `payments` (
`payment_id` int(11) NOT NULL AUTO_INCREMENT,
`item_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`txn_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`payment_gross` float(10,2) NOT NULL,
`currency_code` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`payment_status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`payment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Now all products would be displayed from the products table. The product details are used in a FORM that contains item_name, item_number, amount, currency and more parameters with Buy Now button. After clicking But Now button, the params are sent to Paypal sandbox as mentioned in the form action.

<?php
$sql = "SELECT * FROM products";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while( $row = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="images/<?php echo $row['p_image']; ?>"/>
<div class="caption">
<h4 class="pull-right">Price: <?php echo $row['price']; ?></h4>
<h4>Name: <?php echo $row['p_name']; ?></h4>
</div>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<!-- Paypal business test account email id so that you can collect the payments. -->
<input type="hidden" name="business" value="<?php echo $paypal_email; ?>">
<!-- Buy Now button. -->
<input type="hidden" name="cmd" value="_xclick">
<!-- Details about the item that buyers will purchase. -->
<input type="hidden" name="item_name" value="<?php echo $row['p_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="USD">
<!-- URLs -->
<input type='hidden' name='cancel_return' 
value='http://localhost/paypal_integration_php/cancel.php'>
<input type='hidden' name='return' 
value='http://localhost/paypal_integration_php/success.php'>
<!-- payment button. -->
<input type="image" name="submit" border="0"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" 
alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" 
src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>
</div>
</div>
<?php } ?>

When the PayPal payment is successful, buyer would be redirected to provide success page. On success page, we will receive the transaction information with $_GET variable and insert transaction data into the database. If the payment is successful, then we will show success message to buyer otherwise displayed failed message.


<?php
$item_number = $_GET['item_number'];
$txn_id = $_GET['tx'];
$payment_gross = $_GET['amt'];
$currency_code = $_GET['cc'];
$payment_status = $_GET['st'];
//Get product price to store into database
$sql = "SELECT * FROM products WHERE id = ".$item_number;
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
$row = mysqli_fetch_assoc($resultset);
if(!empty($txn_id) && $payment_gross == $row['price']){
//Insert tansaction data into the database
mysqli_query($conn, "INSERT INTO payments
(item_number,txn_id,payment_gross,currency_code,payment_status) 
VALUES('".$item_number."','".$txn_id."',
'".$payment_gross."',
'".$currency_code."','".$payment_status."')");
$last_insert_id = mysqli_insert_id($conn);
?>
<h1>Your payment has been successful.</h1>
<h1>Your Payment ID - <?php echo $last_insert_id; ?>.</h1>
<?php
}else{
?>
<h1>Your payment has failed.</h1>
<?php
}
?>

If buyer click Buy Now button but and wish to cancel payment at the PayPal payment page, then buyer would be redirected cancel page with following message.

Your PayPal transaction has been cancelled.

You may also like:

You can view the live demo from the Live Demo link. If you want to download full script, you can download the script from the Download link below.

Demo Download


One thought on “Paypal Payment Gateway Integration in PHP

Leave a Reply

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