Professional Documents
Culture Documents
Paypal Payment Gateway Integration in PHP PDF
Paypal Payment Gateway Integration in PHP PDF
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.
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:
payments table:
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.
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/)
.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
LEAVE A REPLY
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
Website
POST COMMENT
http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 3/5
10/18/2017 Paypal Payment Gateway Integration in PHP
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
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
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
(htt (htt
ps:// ps://
ww plus.
w.fa goo
ceb gle.c
) zag) s) ag/)
http://www.phpzag.com/paypal-payment-gateway-integration-in-php/ 5/5