Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

10/18/2017 Paypal Payment Gateway Integration in PHP

Paypal Payment Gateway Integration in PHP


 Last Updated: 4 June, 2017 (http://www.phpzag.com/paypal-payment-gateway-integration-in-php/)  PHPZAG Team (http://www.phpzag.com/author/admin/) 
PHP Tutorial (http://www.phpzag.com/category/php-tutorial-2/)

Are you looking for tutorial for Paypal payment gateway integration in PHP? Yes, you are here at right place.
Find Top 10 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.
MySQL Queries
You will also like related tutorial to Integrate Payment Gateways
Monitor MySQL Database
CCAvenue Payment Gateway Integration in PHP (http://www.phpzag.com/ccavenue-payment-gateway-
Metrics,Track Server Con g
integration-in-php/)
Changes & Get Instant Alerts.
EBS Payment Gateway Integration in PHP (http://www.phpzag.com/ebs-payment-gateway-integration-in-php/)
Monyog - MySQL Monitor Paytm Payment Gateway Integration in PHP (http://www.phpzag.com/paytm-payment-gateway-integration-in-
php/)

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

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.

Find Top 10 MySQL Queries


Monitor MySQL Database Metrics,Track Server Con g Changes & Get Instant Alerts. webyog.com

Steps to create a new Sandbox test account:

Step 1 – First go to the https://developer.paypal.com/ (https://developer.paypal.com/). Log in with your PayPal account. If you have not created your
PayPal account yet, rst 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) ) {

http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 1/5
10/18/2017 Paypal Payment Gateway Integration in PHP
?>
<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 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.

http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 2/5
10/18/2017 Paypal Payment Gateway Integration in PHP

Demo
(http://phpzag.com
/demo/paypal_inte
gration_php/)  

Download Code is Locked


Please support us, use one of the buttons below to unlock the content.

Like 58like Tweet tweet +1 us


error

.NET Tools for NET Paytm Payment Find Top 10 MySQL Image Upload and
Ninjas Gateway Integration Queries Crop in Modal with
in PHP PHP and jQuery
Ad Progress Telerik phpzag.com Ad Monyog - MySQL Monitor phpzag.com

Start Selling Online Show Popup Multi Step Form The Best PHP
Now Message Few using jQuery, Frameworks in 2017
Seconds Using... Bootstrap and PHP
Ad Shopify phpzag.com phpzag.com phpzag.com

Payment Gateway Integration (http://www.phpzag.com/tag/payment-gateway-integration/)

LEAVE A REPLY

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

Comment

Name *

Email *

Enter Your Email ID Here..

Website

POST COMMENT

Notify me of follow-up comments by email.

Notify me of new posts by email.

http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 3/5
10/18/2017 Paypal Payment Gateway Integration in PHP

 Creating Dynamic Graphs and Charts with PHP (http://www.phpzag.com/creating-dynamic-graphs-and-charts-with-php/)

CCAvenue Payment Gateway Integration in PHP  (http://www.phpzag.com/ccavenue-payment-gateway-integration-in-php/)

Get started now


All you need to do your best work, in
one package that works seamlessly
from any device.

ABOUT ME

Laeeq, A Programmer and Blogger, who love the web. Live in Delhi, India.

(https://www.facebook.com/Phpzag-203097989797749/) (http://feeds.feedburner.com/phpzag/)
h l l 24 0 8 64494696 938 h i h

POPULAR POSTS

Image Upload and Crop in Modal with PHP and jQuery (http://www.phpzag.com/image-upload-and-crop-in-modal-with-php-and-jquery/)
Last Updated: 04 Jun , 2017

YouTube video downloader script in PHP (http://www.phpzag.com/php-youtube-video-downloader-script/)


Last Updated: 07 Oct , 2016

Currency conversion in PHP Using Google API (http://www.phpzag.com/convert-currency-using-google-api/)


Last Updated: 12 Oct , 2017

Image Upload without Page Refresh with PHP and jQuery with Demo (http://www.phpzag.com/ajax-image-upload-without-refreshing-page-
using-jquery/)
Last Updated: 04 Jun , 2017

Inline Editing using PHP MySQL and jQuery Ajax (http://www.phpzag.com/inline-editing-using-php-mysql-and-jquery-ajax/)


Last Updated: 04 Jun , 2017

Paytm Payment Gateway Integration in PHP (http://www.phpzag.com/paytm-payment-gateway-integration-in-php/)


Last Updated: 18 Mar , 2017

POPULAR SEARCH TAGS

AngularJS (http://www.phpzag.com/category/angularjs/) Articles (http://www.phpzag.com/category/articles/) Code Snippets (http://www.phpzag.com/category/snippets/)

HTML5 (http://www.phpzag.com/category/html5-tutorial/) Interview Questions (http://www.phpzag.com/category/interview-questions/)

Javascript (http://www.phpzag.com/category/javascript-2/) JQuery (http://www.phpzag.com/category/jquery/) Magento (http://www.phpzag.com/category/magento/)

MYSQL (http://www.phpzag.com/category/mysql/) PHP Tutorial (http://www.phpzag.com/category/php-tutorial-2/) Wordpress (http://www.phpzag.com/category/wordpress/)

http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 4/5
10/18/2017 Paypal Payment Gateway Integration in PHP

LATEST POSTS

Build Shopping Cart with Ajax, PHP and MySQL (http://www.phpzag.com/build-shopping-cart-with-ajax-php-and-mysql/) 15 October, 2017

AngularJS Multiselect Dropdown List using PHP & MySQL (http://www.phpzag.com/angularjs-multiselect-dropdown-list-using-php-mysql/) 7 October, 2017

Bootstrap Contact Form with Captcha using Ajax and PHP (http://www.phpzag.com/bootstrap-contact-form-with-captcha-using-ajax-and-php/) 23 September, 2017

Export Data to Excel with PHP and MySQL (http://www.phpzag.com/export-data-to-excel-with-php-and-mysql/) 16 September, 2017

Angular Multiple File Upload with PHP and MySQL (http://www.phpzag.com/angular-multiple- le-upload-with-php-and-mysql/) 9 September, 2017

POPULAR TAGS

AngularJS (http://www.phpzag.com/category/angularjs/) Articles (http://www.phpzag.com/category/articles/) Code Snippets (http://www.phpzag.com/category/snippets/)

HTML5 (http://www.phpzag.com/category/html5-tutorial/) Interview Questions (http://www.phpzag.com/category/interview-questions/)

Javascript (http://www.phpzag.com/category/javascript-2/) JQuery (http://www.phpzag.com/category/jquery/) Magento (http://www.phpzag.com/category/magento/)

MYSQL (http://www.phpzag.com/category/mysql/) PHP Tutorial (http://www.phpzag.com/category/php-tutorial-2/) Wordpress (http://www.phpzag.com/category/wordpress/)

 

(htt (htt

ps:// ps://

ww plus.

w.fa goo

ceb gle.c 

ook. om/ (htt

com 112 p://f

/Php  450 eeds

zag- (htt 781 .fee

203 ps:// 644 dbu

097 twitt 946 rner

989 er.c 967 .co

797 om/ 938/ m/p

749/ php post hpz

) zag) s) ag/)

http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 5/5

You might also like