Home >>PHP API >How to Integrate PayPal Payment Gateway in PHP

How to Integrate PayPal Payment Gateway in PHP

What is Payment Gateway ?

Payment Gateway is a bridge between the credit card holder and Credit card Company to establish a secure connection between them to interchange the data.

This system securely sends your credit card information from the website to the credit card company for processing and then returns with transaction details and response from the bank.

Some of the most Popular Payment gateway examples are: PayPal, Payumoney, Paytm, ccavenue, EBS, Fonepaisa,Atom Paynetz, Citrus, DirecPay etc.

In this tutorial, You will learn

PayPal Payment Gateway integration in PHP

PayPal is an American international e-commerce company which provide a virtual platform for secure and safety money transaction on Internet.

PayPal payment Gateway is a widely used payment gateway used by several web developers for their web pages to establish secure money transfer.

It is the easiest way for web developers to implement a payment system to their Web Applications.

PayPal gateway:-

It uses its own gateway for the transaction known as Pay flow. It is available for both free and paid based on merchant.

Language used in PayPal Payment gateway

Its Front end is created on HTML, CSS, JavaScript and application layer is coded on Java and Node J.S.

How to Integrate ?

Here is a step to step guide for you to how to integrate this to your web applications.

You need to add the following functionalities for a payment gateway.

  1. Prepare the HTML form to collect credit card information.
  2. Create Stripe token to securely transmit card information.
  3. Submit the form with card details.
  4. Verify the card and process charges.
  5. Insert payment details in the database for status to be shown to the user.

It uses two environments i.e. Sandbox and Real-Time.

Sandbox is a self-contained virtual environment which creates a mimic of live environment which allow developers to test their transactions without affecting any live PayPal accounts.

Real-time means when your application will go online. After coding and debugging all you PayPal API in the sandbox move your application to PayPal production environment to go live.

Steps To integrate PayPal Payment Gateway using PHP.

1. Create Sandbox Account

You need to create a Sandbox Account for testing your code. For create your account please visit to this Link.

" https://www.sandbox.paypal.com/cgi-bin/webscr"

2. Create Index.php file

Now you have to create a display output or index file, this file will fetch all the product records from the database and display it onto page.

3. Create a success.php file

Now we have to create a success file for identify your payment when your transaction will be succeeded it will return here. Remember that in case of success transaction the gateway will return you following details.

  1. Item_number
  2. Transaction_ID
  3. AMOUNT
  4. CURRENCY_CODE
  5. PAYMENT_STATUS

So by keeping this in your mind you have to write your code.

4. Create a Cancel.php file

We also need to create a cancel file when the transaction will be cancel then it will redirect here.

5. Going Live

Once everything is working good including all the payment transactions processing flow then you can go live using your PHP code. Just change the sandbox mode to live mode.

Connection File

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "shop";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
?>

Index.php(To fetch Products from database)

<?php 
include_once("db_connect.php");
//Set variables for paypal form
$paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
//Test PayPal API URL
$paypal_email = 'info@phpzag.com';
?>
<title> Paypal Integration in PHP</title>
<div class="container">
	<div class="col-lg-12">
	<div class="row">
		<?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" style="width:300px;height:300px;border:2px solid red;float:left;margin-left:20px">
			<div class="thumbnail"> 
			<img src="images/<?php echo $row['p_image']; ?>"/>
			<div class="caption">
			<h4 class="pull-right">$: <?php echo $row['price']; ?></h4>
			<h4>Name: <?php echo $row['p_name']; ?></h4>			
			</div>					


			<form action="<?php echo $paypal_url; ?>" 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 } ?>
		</div>		
	</div>	
		
</div>

success.php

<h1>Your payment has been successful.</h1>
<?php
/*
error_reporting(1);
include_once("db_connect.php");
//Store transaction information into database from PayPal
$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
}
?>

cancel.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PayPal Transaction Cancel </title>
</head>
<body>
	<h1>Your PayPal transaction has been canceled.</h1>
</body>
</html>

Total Downloads : 1118

Login / Register To Download

No Sidebar ads