Integrating Paypal Payment Code in PHP Scripts (Video Tuts)

Paypal is without doubts the most popular online payment system which is used by thousands of website owners and companies for their online businesses in order to receive payments from customers. And it’s very necessary for web developers to learn how they can integrate Paypal Payment System into PHP projects for their clients and their own websites. It’s not a difficult task to add Paypal into PHP scripts, but you have to do things accordingly and a little bit carefully. I’ll teach you the most easiest way to include Paypal checkout system to your web applications, I’ve video tutorials for you which you can watch to understand each and every step practically. Furthermore, I’ll share the source code with you which you can simply use in your applications. I was actually creating a course eCommerce website in PHP & MySQLi” and in that project I needed to include “Paypal Integration”, therefore I thought to share it with you on my blog, and here we go.


Tip: You can still join my that full eCommerce course with 50% discount today:

How to Add Paypal System in PHP?

In order to add Paypal Payment System to your PHP script, you must have a Paypal account which can either be personal or business, if you have not created Paypal account yet then just go and create one, after you have an account there, just follow the simple steps below to integrate Paypal in PHP:

  1. Go to
  2. Find the “Login” button at top right and login with your Paypal email & password
  3. Now find the “Application” tab on the page and click that
  4. Find the “Sandbox Accounts” link at left sidebar
  5. Click “Sandbox Accounts” and create 2 accounts there, one personal and one business

Now you can see the above steps in screenshots below, After login & clicking “Application” you’ll see this screen:


In the above screenshot just click “Sandbox Accounts” appearing at left side, so you’ll see this screen:


In the above screenshot, you can simply see the button “Create Account” which I’ve highlighted, by clicking that button, you can create a new Sandbox test account either personal or business, as you can also see I’ve created two accounts, one is personal and another is business, that’s mean, using the personal account, I’ll buy things to spend money and in business account, I’ll receive the money. However, just click the “Create Account” and you’ll see this screen:


In the simplest new account form above, you need to put simple information such as selecting a country, type of account, email, password, your name and test money as much as you want. Add an email in the email address field which can be anything you like for example “” this email will be to login to your test account along with password you enter in above form. You can also add test money in USD. That’s all, after creating this account just login to sandbox using the link available on the main sandbox site where it says “Enter Sandbox Site“.

After all, you have two Paypal test accounts, one is personal which you’ll use to buy things yourself and spend the test money that you’ve added while creating the account, so you can check it when you buy something, and the second account is business which you’ll put in the Paypal form, from where you’ll receive the money into your business account, so you’ll be the customer as well as the merchant just to test the process before making it live. That’s all for test accounts, now let’s move to the Paypal integration part.

Paypal Code to Use in PHP Application

Now as you have created 2 test accounts (Sandbox), that’s mean you can shop online and can buy things via Paypal as a test, and you can also receive the money into your business account, so it’s time to take Paypal code and integrate it in a PHP script, whatever PHP script you have, but you’ll have to pass few important values to Paypal from your script, so that Paypal can understand what your customer has purchased and how much amount is to be charged, However, Here is the simple Paypal integration code you can use in your PHP application:

<–Paypal Form starts–>

<form action=”” method=”post” >

<!– Identify your business so that you can collect the payments. –>
<input type=”hidden” name=”business” value=”Paypal Business Email (Sandbox)”>

<!– Specify a Buy Now button. –>
<input type=”hidden” name=”cmd” value=”_xclick”>

<!– Specify details about the item that buyers will purchase. –>
<input type=”hidden” name=”item_name” value=”Product Name PHP Variable”>
<input type=”hidden” name=”item_number” value=”The Product ID PHP Variable”>
<input type=”hidden” name=”amount” value=”The Final Amount in PHP Variable”>
<input type=”hidden” name=”quantity” value=”The Quantity PHP Variable”>
<input type=”hidden” name=”currency_code” value=”USD”>

<–Specify the Pages for Successful payment & failed Payment–>

<input type=”hidden” name=”return” value=””/>

<input type=”hidden” name=”cancel_return” value=””/>

<!– Display the payment button. –>
<input type=”image” name=”submit” border=”0″ src=”paypal_button.png” alt=”PayPal – The safer, easier way to pay online”>
<img alt=”” border=”0″ width=”1″ height=”1″ src=”” >


<–Paypal Form ends–>

The above one is a simple form from Paypal itself, I’ve got it from this page available on

in the above form, I’ve highlighted the important locations where you have to put your own information, so that Paypal can understand what’s coming in the form and where to send the money, the first “” is the URL where Paypal actually process the payment, so after testing the system just remove “sandbox” from the URL and it will become the real system.

In the next hidden input tag where it says value=”Paypal Business email” just put your paypal business email which you created using “Sandbox Accounts”, so you’ll receive the payments from customers in that.

In the next five hidden input fields just put your PHP variables about the product that’s being purchased by the customer, you can add the product name, product ID, product amount, product quantity and the currency which is by default “USD”.

In the next two lines, you can see there are two hidden input tags which have URLs in the value attribute, these are basically for returning the customers to your website and showing them a successful page where you can write something like a “Welcome Note”, and there is a page “paypal_cancel.php” which will be used by paypal to redirect the customer if the Payment is failed.

If I myself want to add the above Paypal code into my PHP application, and I want to pass values to Paypal then I’ll be doing something with the form like this:

<–Paypal Form starts–>

<form action=”” method=”post” >

<!– Identify your business so that you can collect the payments. –>
<input type=”hidden” name=”business” value=””>

<!– Specify a Buy Now button. –>
<input type=”hidden” name=”cmd” value=”_xclick”>

<!– Specify details about the item that buyers will purchase. –>
<input type=”hidden” name=”item_name” value=”<?php echo $product_name;?>”>
<input type=”hidden” name=”item_number” value=”<?php echo $product_id”>
<input type=”hidden” name=”amount” value=”<?php echo $product_price;?>”>
<input type=”hidden” name=”quantity” value=”<?php echo $product_qty;?>”>
<input type=”hidden” name=”currency_code” value=”USD”>

<–Specify the Pages for Successful payment & failed Payment–>

<input type=”hidden” name=”return” value=””/>

<input type=”hidden” name=”cancel_return” value=””/>

<!– Display the payment button. –>
<input type=”image” name=”submit” border=”0″ src=”paypal_button.png” alt=”PayPal – The safer, easier way to pay online”>
<img alt=”” border=”0″ width=”1″ height=”1″ src=”” >


<–Paypal Form ends–>

You can see I changed the above Paypal form according to my application, I just added my PHP variables which are of course coming from my final page and I’m passing them to Paypal, so whatever quantity it will have, whatever total amount it will have and all other information will be passed to Paypal and Paypal will process the Payment.

How to Get the Payment Details Back from Paypal

Now after the customer’s payment is successful, the customer will be automatically redirected to paypal_success.php, and there, Paypal will return the details in the URL variables which will be something like this:


So on paypal_success.php page, you can get and save the details which paypal has returned to you, and you can simply use the PHP $_GET method to get each detail you want from the dynamic URL, and you can store that in the MySQLi table, I’ve created a simple paypal_success.php page which is something like this:

<!DOCTYPE html><html>
<title>Payment Successful!</title>



//Getting payment details from paypal

$amount = $_GET[‘amt’];
$currency = $_GET[‘cc’];
$trx_id = $_GET[‘tx’];

$invoice = mt_rand();

//inserting the payment to table

$insert_payment = “insert into payments (amount,customer_id,product_id,trx_id,currency,payment_date) values (‘$amount’,’$c_id’,’$pro_id’,’$trx_id’,’$currency’,NOW())”;

$run_payment = mysqli_query($con,$insert_payment);


echo “<h2>Welcome:” . $_SESSION[‘customer_email’]. “<br>” . “Your Payment was successful!</h2>”;
echo “<a href=’’>Go to your Account</a>”;

else {

echo “<h2>Welcome Guest, Payment was failed</h2><br>”;
echo “<a href=’’>Go to Back to shop</a>”;



In the above code, I’ve validated the Paypal variable “amount” with my own “total” so that I can know whether the customer has paid the exact amount which I had passed to paypal or not, in case the customer has tried to send a fake payment, he/she will not be able to get the success message, and the payment will be considered as failed attempt.

If you have got all the above steps to include Paypal Payment System in your PHP scripts then it’s great, but if you still find difficulties then don’t worry, here is the playlist of video tutorials for this whole process, step by step with practical examples.

Video Playlist to Learn Paypal Integration!

The business email you create in Paypal Sandbox & add that to the Paypal form, it will not return the values now in the URL which you need to insert into database/table, this is because Paypal Sandbox has a bug for test accounts after 2013, but at the same time if you want to use this feature then use this email “” this test business email was created in 2011 so it will work perfectly and will return the values in the URL which you can insert to the database.
After watching the video tutorials which is basically a playlist of 8 video tutorials, I surely can say that you’ve understood the process very easily, if you have any question then please ask in comments, and if you like this tutorial then kindly share it on social media and subscribe to my YouTube channel.

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also


Best Websites to Buy PHP Scripts & Themes

PHP is a powerful web development language and many popular platforms are built with PHP ...

  • Did’t test but seems really great 🙂 Abdul wali use a code highlighter if it can be, it’ll help a lot to read codes. 😉 Anyways nice job!

    • Hi Javed,
      Thanks for the suggestion, I’m working on that, will make it more clear and beautiful soon!.

  • Agam Sahu

    Nice Article Sir !!

  • shahid ahmad

    //Getting payment details from paypal

    $amount = $_GET[‘amt’];
    $currency = $_GET[‘cc’];
    $trx_id = $_GET[‘tx’];
    I don’t know where $_GET[‘amt’] [‘cc’] [‘tx’] is from, please explain for this!

    • Hi,

      You’ll get these values when Paypal returns the visitor to the successful page. But if you have created a sandbox account recently then Paypal will not return the values, because now a days this option is not working, so if you want it to work for you properly then kindly use this test email ( in your Paypal form (As a Seller), so it will work fine. This email was basically created 2 years ago, therefore it works.

      • shahid ahmad

        Now it works! thanks very much for your reply

        • great to hear it worked!

        • abid khan

          assalam o alikum

      • Emaan Fatima

        : Undefined index: amt in F:plz dont touch thishtdocsprojectpaypal_sucess.php on line 90

        Notice: Undefined index: cc in F:plz dont touch thishtdocsprojectpaypal_sucess.php on line 92

        Notice: Undefined index: tx in F:plz dont touch thishtdocsprojectpaypal_sucess.php on line 94

        Even i addded this thing…

      • webdhd171

        now how i can use to getting payment detailes if this way not working

  • Farah khan

    Abdul wali I really appreciate how you demonstrate each and everything in your tutorial . Helps me a lot just going to try it out now . Hope it works well 🙂 Thanks once again

  • Jack

    How do i download the source?

  • mior farhan

    sir what is your password for this test email

  • mior farhan

    how can i return to the page that not yet publish after payment is complete?

  • Jerahmeel Acebuche

    Hello Sir, may i know the password for your sandbox account

    • Hello,

      The password is not available for this email.

  • Raul Bustamante

    Hellow Abdul, I cannot thank you enough for your tutorials. Now, I need to complete the circle of a cashflow. Im trying to incorporate the Adaptive Payments to my Web Site, altouhght Im a non-US developer. I need to send money back to my customers (not refunds) in a very automatic way without me involved in any kind of approval. Please let me know if you can help me out on that.

    • Hi Raul,

      You can use services for that.

      • Shoaib Khan

        abdul wali bhai mje php pe admin aur user ka login page banana hai admin k pass rights hun edit delete karnay k baki users srf dekh sakain kuch kar na sakain… help me sir jee

      • Raul Bustamante

        Hi Abdul,
        Unfortunately 2Checkout has declined my account. I would hire you, or any one you recomend, to help me out on the final step of my work flow, which is “Send money to my customers automatically from my web site”

        Please, send contact details to
        Thnx in advz

  • ianhaneyit

    Hi, is this still active as trying to add this into my website and it does work but it is not adding the correct data to the database, in amount it adds 0.00, in customer_id it is adding a 0 on each record and is the same for product_id and trx_id, can anyone help please?

  • Vivek Swansi

    Hi Abdul,
    I am new in Paypal Integration, So can to tell me how could i get the parameter from return URL.
    Note: I have created the sandbox account recently.


    Everything that i need to know i got it from here. Thanks bro

  • Eleanore Rovanne

    Hello, how to store more than one product into my database? This codes only store for single product right? Even tho Im purchased more than 1 product but it only store for 1 product name in my database. Please. I need to do my order and sales function. Huhuhu Its pending because of this.

  • Консильери

    Here’s the mistake

    <input type=”hidden” name=”item_number” value=”