A Guide For PayPal Integration On Your Website In PHP

PayPal is no doubt one of the most popular payment gateways for websites. Most websites are using PayPal to receive payments on their application. In this article, we study PayPal integration on a web application in PHP.

Why Choose PayPal?

Creating an account on PayPal is totally free. You don’t need to pay anything upfront. PayPal will charge you on a transaction basis.

While performing payment transactions, PayPal uses Secure Socket Layers(SSL) encryption. This protects your customer’s personal and confidential data. Because of this, your customers feel safe while doing payments through your web application.

PayPal also allows paying through PayPal balance, credit, or debit cards. It adds a number of options for a customer to pay for your service.

For accepting payment through credit cards you can also use the Authorize.net service. If you want to learn more about Authorize.net follow our tutorial Accept Credit Card Payment Using Authorize.net Payment Gateway In PHP.

PayPal Integration

As we are talking about PHP powered website, we will use the official GiHub library of PayPal. This library is for PHP applications.

We recommend using Composer for the installation of the library.

So, to integrate PayPal into the website first run the below command in the project root directory.

composer require "paypal/rest-api-sdk-php:*"

Get Client Id And Client Secret

When customers pay through PayPal, the amount should be deposited in your PayPal account. To do so, we need to get your client id and client secret. You can create your credentials from here.

On this, My Apps & Credentials page, scroll down to the section ‘Rest API apps’ and click the ‘Create App’ button.

Create App

Give the name to your App and hit the ‘Create App’ button.

Create App Info

After the above steps, you will get credentials for both Sandbox and Live mode.


Now we are ready with our client id and client secret keys. Let’s get a step ahead and write some piece of code.

Actual Code For PayPal Integration

At this stage, we are ready with the library and credentials. Now to integrate PayPal into a website, we create 4 files:

checkout.php: In this file, we have a form that contains the item name and amount.
payment.php: This is the file where we write all PayPal-related code.
success.php: After successful payment, the customer will redirect to this file.
error.php: If the payment is unsuccessful, then the customer redirects to this file.


<form action="payment.php" method="post">
    <input type="text" name="item" placeholder="Enter Item Name">
    <input type="text" name="amount" placeholder="Enter Amount">
    <input type="submit" name="submit" value="Pay">


require_once 'vendor/autoload.php';
use PayPal\Api\Item;
use PayPal\Api\ItemList;
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'YOUR_CLIENT_ID',     // ClientID
        'YOUR_CLIENT_SECRET'  // ClientSecret
        'log.LogEnabled' => true,
        'log.FileName' => 'PayPal.log',
        'log.LogLevel' => 'DEBUG',
        'mode' => 'sandbox', //'live' or 'sandbox'
$payer = new \PayPal\Api\Payer();
$item1 = new Item();
$itemList = new ItemList();
$amount = new \PayPal\Api\Amount();
$transaction = new \PayPal\Api\Transaction();
$transaction->setDescription("Payment For Service")
$redirectUrls = new \PayPal\Api\RedirectUrls();
$payment = new \PayPal\Api\Payment();
// 4. Make a Create Call
try {
    header('Location: '. $payment->getApprovalLink());
catch (\PayPal\Exception\PayPalConnectionException $ex) {
    // This will print the detailed information on the exception.
    echo $ex->getData();

Make sure to replace placeholders with the actual values. In the above code, we set the ‘mode’ value to ‘sandbox’. While moving to production, make this value ‘live’ and add your live client id and client secret.

Once your payment process is completed through PayPal, on the basis of success or failure payment, it will redirect to either the success.php or error.php file.


<h2>Payment Successful.</h2>


<h2>Something went wrong. Try again later.</h2>

We hope you understand PayPal integration on the web application. Please share your thoughts in the comment section below.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *