Paypal is the most popular payment gateway to send and receive money using web application and mobile. PayPal provides functionality to receive and send fund using internet.
This php payment gateway tutorial help to add PayPal gateway with web application using PHP. Paypal have two environment , one is sandbox account for developer testing and other is prod , for live environment.This paypal payment gateway tutorial will use paypal sandbox account to test product transaction.
Table of Contents
How to Create a New PayPal Sandbox Account
- Log in with your PayPal account. If you do not have created your PayPal account yet, first sign up at PayPal and create paypal account.
- Now logged-in with PayPal account using credentials(which was given at the time of signup), Now you can see developer home page and click the Dashboard.
- Then click on the Accounts link under the Sandbox.
- Now create test accounts for seller and buyer by selecting Business and Personal from the create account link.
You can check other recommended tutorial of Payment Gateway,
- How to Integrate Authorize.net Payment Gateway in PHP
- Paypal Payment Gateway Integration with PHP
- Paytm Payment Gateway Integration Using PHP
- CCAvenue Payment Gateway Integration Using PHP
- EBS Payment Gateway Integration Using PHP
There are following files will participate into this tutorial:
- product_listing.php : This file will use to display product listing.
- connection.php : This file will use to established connection with php and mysql.
- success.php : This file will use to display success payment information.
- cancel.php : This file will use to show cancel payment information.
You will have two configuration parameters which are required for paypal gateway integration.The $paypalURL
and $paypalID
value with live PayPal URL and business email.
1 2 |
$paypalURL = 'https://www.paypal.com/cgi-bin/webscr'; $paypalID = 'PayPalBusinessEmailID'; |
MySQL Database Tables Creation
We will create two tables products and payments_info into MySQL 'test'
database.The products table will be used to store product details information and payments_info table will be used for storing the transaction details from PayPal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CREATE TABLE `products` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `image` varchar(255) 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=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE `payments_info` ( `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; |
Create MySQL database connection
We will create connection.php
file. This file is used to connect and select database using PHP and MySQL.We will add $dbhost
, $username
, $password
, and $dbname
variable’s value with your database credentials.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php Class dbObj{ /* Database connection start */ var $dbhost = "localhost"; var $username = "root"; var $password = ""; var $dbname = "test"; var $conn; function getConnstring() { $con = mysqli_connect($this->dbhost, $this->username, $this->password, $this->dbname) or die("Connection failed: " . mysqli_connect_error()); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $this->conn = $con; } return $this->conn; } } ?> |
We have created connection with MySQL database, so now we will fetch product information from product table and display into a page.We will add some information with each product listing record.The product details will have product name, product number, amount, currency using html input hidden fields.
We will add paypal buy now button with each listed record and user will click on that to purchase product.The hidden parameters will send to paypal account.
We will add product_listing.php
file and add below code to display product listing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php include_once("connection.php"); $db = new dbObj(); $conn = $db->getConnstring(); $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['image']; ?>"/> <div class="caption"> <h4 class="pull-right">Price: <?php echo $row['price']; ?></h4> <h4>Name: <?php echo $row['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['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 or on failed message on transaction failed. We will receive the transaction information with $_GET
variable and insert transaction data into the payments information database table.We will add below code into success.php
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php include_once("connection.php"); $db = new dbObj(); $conn = $db->getConnstring(); $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_info(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 } ?> |
Please make sure you have enabled Auto Return for Website Payments on your PayPal business account to get transaction information from PayPal in success.php
file.
Cancel Paypal Payment
User will ge below message when he cancelled transaction.We will redirect user to can page and show message.We will add below ode into cancel.php
file.
1 |
Your PayPal transaction has been cancelled. |