How to Integrate PayPal Payment Gateway in PHP?

Now a days there are lots of the service provider of payment gateway for online website like as a ccavenue payment gateway integration in php, razorpay payment gateway integration in php, stripe payment gateway integration in php and paytm payment gateway integration in php.

Payment gateway integration in PHP With Database Connection

Payment integration in PHP is very simple. Please follow the below step by step to integrate Payment gateway in your PHP source code online website with I provided paypal payment gateway integration in php source code download.

  • EBS
  • Paytm
  • PayPal
  • Citrus
  • DirecPay
  • ccavenue
  • Payumoney
  • Fonepaisa
  • Atom Paynetz

What is Payment Gateway ?

Payment Gateway is a one type of the bridge between the credit card merchant holder and Credit card any types of the Company to establish a secure and trustable connection between them to interchange the userful data.

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

Some of the most Popular secure and trustable Payment gateway examples are: Atom Paynetz, Citrus, Fonepaisa, Paytm, ccavenue, PayPal, Payumoney, EBS, DirecPay with debit card payment gateway integration in php etc.

paypal.com, pay pal, discover credit card, facebook sign in, hotmail.com loign, www.hotmail.com, pay
how-paypal-works

In this Article, You will Know

PayPal Payment Gateway integration in PHP

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

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

It is the best and easiest methods for web developers to implement a payment system to their online Web Applications.

Read Also:  remove class jquery

PayPal gateway:-

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

Language used in PayPal Payment gateway

PayPal Payment gateway is Front end is created on HTML source code, CSS source code, JavaScript source code and web application layer is full coded on Java as well as Node J.S.

How to Integrate ?

Here is a phase to phase full information for you to how to integrate this to your online web applications.

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

PHP - Paypal Payment Gateway Integration
PHP – Paypal Payment Gateway Integration

  1. Create the HTML form to get a credit card details.
  2. Generate Stripe token to securely as well as transmit card details.
  3. and then Submit the form and save with card informations.
  4. Now, Verify and check the card as well as step by step process charges.
  5. last all the data Insert payment informations in the database mysql for status(Cancel, Success) to be shown to the user.

It uses two environments Like as a

  • Sandbox
  • Real-Time.

Sandbox (for test purpose) is a self-contained virtual environment all the platform testing which makes a mimic of live environment which allow developers to test purpose their transactions without any types of the affecting any live PayPal accounts.

Real-time (Live) means when your application will go online. And then 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.

step : 1. Make Sandbox Account

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

Read Also:  Sorting MultiDimensional Arrays using PHP

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

step : 2. Make Index.php file

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

step : 3. Make a success.php file

Now we have to make 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 source code.

step : 4. Make a Cancel.php file

I also required to make a cancel php file when the transaction will be cancel then it will redirect here.

step : 5. Going Live

here last step to Once everything is working good including step by step the payment transactions Sandbox mode processing flow after that you can go here simple live URL using your PHP source code. Just update the sandbox(testing) mode to live mode.

paypal express checkout
paypal express checkout

Also Read: Paypal Recurring Payments PHP REST API Example

payment gateway in php and mysql Example

Connection File

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

fetch Products from database

Index.php

<?php 
include_once("db_connect.php");
//Here Live or Sendbox Set variables for paypal form
$paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
//Test Your PayPal API URL
$paypal_email = '[email protected]';
?>
<title> Paypal Integration in PHP - www.pakainfo.com</title>
<div class="container">
	<div class="col-lg-12">
	<div class="row">
		<?php
		$sql = "SELECT * FROM goods";
		$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 paypal_payments_gateway. -->
			<input type="hidden" name="business" value="<?php echo $paypal_email; ?>">			
			<!-- Paypal Buy Now button. -->
			<input type="hidden" name="cmd" value="_xclick">			
			<!-- Full Information about the product that buyers will purchase. -->
			<input type="hidden" name="product_name" value="<?php echo $row['p_name']; ?>">
			<input type="hidden" name="product_number" value="<?php echo $row['id']; ?>">
			<input type="hidden" name="amount" value="<?php echo $row['price']; ?>">
			<input type="hidden" name="currency_code" value="USD">			
			<!-- Paypal URLs -->
			<input type='hidden' name='cancel_return' value='https://your_domain_path/paypal_integration_php/cancel.php'>
			<input type='hidden' name='return' value='https://your_domain_path/paypal_integration_php/success.php'>						
			<!-- paypal_gateway 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>

Also Read: Laravel 5.4 Paypal integration – Paypal Payment Gateway

Read Also:  Creating Laravel Web Installer Setup Wizard

success.php

<h2>Your payment has been successful.</h2>
<?php
/*
error_reporting(1);
include_once("db_connect.php");
//Store or save transaction details into database from PayPal
$product_number = $_GET['product_number']; 
$txn_id = $_GET['tx'];
$paypal_gateway_gross = $_GET['amt'];
$currency_code = $_GET['cc'];
$paypal_gateway_status = $_GET['st'];
//Get product price to store into database
$sql = "SELECT * FROM goods WHERE id = ".$product_number;
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
$row = mysqli_fetch_assoc($resultset);
if(!empty($txn_id) && $paypal_gateway_gross == $row['price']){
    //Insert tansaction data into the database
    mysqli_query($conn, "INSERT INTO paypal_payments_gateway(product_number,txn_id,paypal_gateway_gross,currency_code,paypal_gateway_status) VALUES('".$product_number."','".$txn_id."','".$paypal_gateway_gross."','".$currency_code."','".$paypal_gateway_status."')");
	$payment_transaction_id = mysqli_insert_id($conn);  
	
?>
	<h2>Your payment has been successful.</h2>
    <h2>Your Payment ID - <?php echo $payment_transaction_id; ?>.</h2>
<?php
}else{
?>
	<h2>Your payment has failed.</h2>
<?php
}
?>

cancel.php

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

Also Read: paypal payment Gateway Integration with laravel 5.5 -Paypal checkout Laravel

I hope you get an idea about payment gateway integration in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Laravel Sorting Sort By Collection
  2. Read Also:  Selectize clear selection on Dropdown Click
  3. Read Also:  PHP Convert XML to JSON with Attributes
  4. Read Also:  VueJS Force download GET request using axios
  5. Read Also:  Multiselect dropdown with checkboxes demo code in php
  6. Read Also:  PHP Stripe api Send Accept Payment Integration
  7. Read Also:  PHP for Dummies | how php works? | What is PHP?
  8. Read Also:  Selectize clear selection on Dropdown Click
  9. Read Also:  remove class jquery
  10. Read Also:  Multiselect dropdown with checkboxes demo code in php
CLOSEX