Blog Post

How to Integrate PayPal Payment Gateway in PHP

How to Integrate PayPal Payment Gateway in PHP?

Mihir Mistry,

As modern commerce has become increasingly digital, the way we pay for the things we buy has changed dramatically. Today, we can pay using a variety of methods, from cash to credit cards to digital payment systems, such as PayPal. No matter which method we use to pay, we expect the transaction to be quick and easy. This is where PayPal comes in.

Just like PHP for web development, PayPal is one of the most trusted online payment methods in the world. Hence, when it comes to multiple payment gateway integrations in a PHP-based platform, PayPal comes first to mind.

Paypal is synonymous with online payments. Millions of businesses and entrepreneurs use it for everything from accepting basic online payments to complex online selling platforms. It offers an all-in-one solution, helping connect merchants and consumers in over 200 markets worldwide. There are many reasons why Paypal integration has become an integral element for every online business.

Why businesses prefer PayPal payment gateway:

  • There are 426 million Active PayPal consumer and merchant accounts;
  • PayPal proceeds an average of 40,006 Payment transactions per minute;
  • Its support for nearly every country and 25 different currencies helps online businesses grow beyond borders and languages;
  • It has cutting-edge features, including pay with QR Codes, Credit/debit card, and even crypto. Users can also pay with one touch through their devices.
  • PayPal also uses fraud detection technology, Device fingerprinting, and more to offer merchants a secure platform to grow their business globally.

If you are an online merchant or eCommerce platform looking for a secured payment gateway to expand globally, you know Paypal is the best. But how to use it in your PHP website or web app? We have got you here and have a reward for you.

In this post, we will see how to do PayPal gateway integration in PHP. But before that, let us know how PayPal works.

How does PayPal work?

How does PayPal work?

When you buy something online, you probably use PayPal without even realizing it. That little payment button on websites is a gateway to the world’s largest online payment platform. It’s become the internet’s universal platform for moving and exchanging money. Millions of people use PayPal to send and receive money to/from friends and family, make purchases, and manage their finances.

Starting with PayPal is no rocket science. You can use the PayPal web app or download a dedicated app for Android or iOS for free. 

How to use PayPal?

When you sign up on the app, it asks you if you want a personal or business account, as both differ in terms of functionality. PayPal also offers premium accounts that a user can upgrade in the future.

Next, you have to enter your personal information: full name, address, phone number, and email. Click all the boxes of policy and hit create your account. You will receive an email with instructions to verify your account.

To receive and send money to your bank account via PayPal, you need to link your bank account, debit, or credit card. You can link it from the app dashboard you will see after logging in. Find the Card button and click on it.

Enter your card details, including type, number, CVV, and expiry date. Save details, and the app takes you to the bank’s website to authenticate your card. You will receive an OTP to confirm your card on PayPal, once you do that, it is done. 

Similarly, you can add multiple cards and manage payments from your bank accounts. You can also set a card as the default payment method unless you choose otherwise.

Now when you pay online, look for a PayPal symbol and check out with it. You can also get a list of the vendors nearby accepting payment on PayPal. 

PayPal offers unique functionality for vendors for secure payment. Suppose you have sent an item, but the buyer hasn’t received it yet. If you can prove that you shipped it, you get to keep the full payment.

integrate the PayPal payment gateway into your PHP website

How to integrate the PayPal payment gateway into your PHP website? 

As promised, now is the time to see how to integrate payment gateway in PHP, specifically PayPal. To integrate PayPal into your PHP website, you must have a merchant account that is linked to a PayPal business account. You can integrate your PayPal account to your PHP website by following a set of steps. 

Before we start, let us see what functionality you need for a payment gateway on your site.

  1. Collect credit card information.
  2. Securely transmit card information.
  3. Payment.php page to handle outgoing and incoming requests to/from PayPal 
  4. Verify the card and process charges.
  5. Add payment details to the database.

Steps To integrate PayPal Payment Gateway in PHP.

1. Create a Sandbox Account

After creating a business account on PayPal with this link Get Sandbox credentials from your PayPal developer account

PayPal Sandbox is a testing environment to test, accept and send payment processes. You can use testing email to test transactions without affecting live PayPal accounts.

2. Create Index.php file

Step 2 is to create a file that will fetch the products record from the shopping cart/database and display them onto the page.

//Set variables for paypal form
$paypal_url = ''; 
//Test PayPal API URL
$paypal_email = '[email protected]';
<title> Paypal Integration in PHP</title>
<div class="container">
	<div class="col-lg-12">
	<div class="row">
		$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>			
			<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="" alt="PayPal - The safer, easier way to pay online">
			<img alt="" border="0" width="1" height="1" src="" >    
		<?php } ?>

3. Create a success.php file

The success file is to identify the payment transaction.

<h1>Your payment has been successful.</h1>
//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 transaction 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>
	<h1>Your payment has failed.</h1>

When the transaction is successful, the patent gateway will return the following details. 

  1. Item_number
  2. Transaction_ID

4. Create a Cancel.php file

Create a file to redirect the transaction in case it is cancelled.

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

5. Test for submission

You can use fake accounts and test the PayPal integration to see if it works correctly. If everything works well, you can go live with your PHP code. You can change the sandbox mode to live.

Final Words

Payment gateway integration in PHP sites is easier said than done, specifically if you are not a developer. The payment gateway must work efficiently on your website. Therefore, you should hire experienced PHP developers.

When it comes to hiring developers, we would recommend connecting with us. We have a full-fledged team of PHP developers with expertise in PayPal and multiple payment gateway integrations. Hence, we assure seamless payment gateway integration into your PHP-based website.

Mihir Mistry

Mihir Mistry is a highly experienced CTO at Kody Technolab, with over 16 years of expertise in software architecture and modern technologies such as Big Data, AI, and ML. He is passionate about sharing his knowledge with others to help them benefit.

Let's Grow and Get Famous Together.

    Contact Information

    +91 93167 56367

    +91 93772 29944



    2nd floor, J block, Mondeal Retail park, Besides Iscon mall, Iscon cross-road, SG Highway, Ahmedabad, Gujarat 380015



    60 Capulet Ln, London, ON N6H OB2, Canada



    Datamac Analytics LLC, One Financial Plaza, FL 1000, Fort Lauderdale FL, 33394



    14 East Bay Lane, The Press Centre, Here East, Queen Elizabeth Olympic Park, London, E20 3BS


    Explore how Kody Technolab is different from other software development companies.


    Download 50+ proven templates and editable frameworks which guide you to build remarkable product