Easily Integrate PayPal Payment Gateway in PHP

Hi guys, in this post we will see how easily we can integrate PayPal payment gateway system in our PHP applications. If you were searching for this tutorial then you are already familiar with PayPal. But those who are not, don’t worry I will tell you everything you need to know in this post. PayPal is one of most used payment gateway by individuals and businesses around the world.   You can transfer/ receive payments to/from people around the world (depending upon PayPal policy for your country). Now let’s start and see how you can setup PayPal payment gateway in your website. In our next tutorial we will add more features to this tutorial like connecting it with MySQL and saving all the transaction details at your end etc.



  Live Demo

Note: For development and testing purpose PayPal offers sandbox mode to test payments.In our demo I am not using sandbox. So if you would like to buy me a beer you can actually transfer some amount via PayPal to me 😉 😀

Following things you will need in order to get started.

  • PayPal account. You can create one from https://www.paypal.com.
  • You don’t need to connect your PayPal account to your bank account if you want to test using sandbox mode.
  • To actually receive payments you need to connect PayPal with your bank account so that you can actually receive payments.

To create sandbox buyer and seller account you can login to your PayPal account and then go to PayPal developers i.e. to https://developer.paypal.com/ and go to dashboard > sandbox > create account.


Here you can create accounts for both buyer and seller. You can also add some credits/balance to your account to do some transactions and they will get listed in live transaction section. See below image for reference.


Now in our demo we have index.php file with a demo product listed and this file is responsible for redirecting user to PayPal website and let him pay the desired amount listed for product. Below are few configurations which you must do:

'product_name'=>'Demo Product',
<p style="text-align: justify;">

  • merchant_email: Change this to your business PayPal email id.
  • product_name: Product you want to sell.
  • amount:  Amount of product to be sold.
  • currency_code:   Currency if Payment.
  • thanks_page: Page where you want to redirect user after successful payment.
  • notify_url: This is for instant payment notification URL. We will use this in next tutorial to set notifications
  • cancel_url: In case if payment is cancelled, redirect users to this URL.
  • paypal_mode: A boolean variable to set if this is a test transaction (sandbox) or a real one. value can be set either true or false. “true” means its a sandbox test payment.

Now you can easily configure our script and get it to work. Remember if you are testing payments then both seller and buyer email id must be created in sandbox and variable “paypal_mode” must be set to true. 

Below is our code for index.php:

'product_name'=>'Demo Product',
<form id='paypal-info' method='post' action='#'>
<label>Product Name : <?php echo $data['product_name']; ?></label></br>
<label>Product Price : <?php echo $data['amount'].''.$data['currency_code']; ?></label>

<input type='submit' name='pay_now' id='pay_now' value='Pay' />

echo infotutsPaypal($data);


function infotutsPaypal( $data) {

define( 'SSL_URL', 'https://www.paypal.com/cgi-bin/webscr' );
 define( 'SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr' );

$action = '';
 //Is this a test transaction?
 $action = ($data['paypal_mode']) ? SSL_SAND_URL : SSL_URL;

$form = '';

$form .= '<form name="frm_payment_method" action="' . $action . '" method="post">';
 $form .= '<input type="hidden" name="business" value="' . $data['merchant_email'] . '" />';
 // Instant Payment Notification & Return Page Details /
 $form .= '<input type="hidden" name="notify_url" value="' . $data['notify_url'] . '" />';
 $form .= '<input type="hidden" name="cancel_return" value="' . $data['cancel_url'] . '" />';
 $form .= '<input type="hidden" name="return" value="' . $data['thanks_page'] . '" />';
 $form .= '<input type="hidden" name="rm" value="2" />';
 // Configures Basic Checkout Fields -->
 $form .= '<input type="hidden" name="lc" value="" />';
 $form .= '<input type="hidden" name="no_shipping" value="1" />';
 $form .= '<input type="hidden" name="no_note" value="1" />';
 // <input type="hidden" name="custom" value="localhost" />-->
 $form .= '<input type="hidden" name="currency_code" value="' . $data['currency_code'] . '" />';
 $form .= '<input type="hidden" name="page_style" value="paypal" />';
 $form .= '<input type="hidden" name="charset" value="utf-8" />';
 $form .= '<input type="hidden" name="item_name" value="' . $data['product_name'] . '" />';
 $form .= '<input type="hidden" value="_xclick" name="cmd"/>';
 $form .= '<input type="hidden" name="amount" value="' . $data['amount'] . '" />';
 $form .= '<script>';
 $form .= 'setTimeout("document.frm_payment_method.submit()", 2);';
 $form .= '</script>';
 $form .= '</form>';
 return $form;

Note: To use this script download it and deploy it in your web root directory. If you wish to deploy it in any sub directory inside root then you must change the paths accordingly in thanks_page, notify_url and cancel_url variables.

In this tutorial we have covered onetime payment via PayPal but we have a lot for you guys so we would be publishing about recurring payment system using PayPal very soon. You can also add a separate cancel_url to display message if payment is canceled. Connect this script with MySQL or any other database to store all the transaction details in your DB. If you want anything more in this script please comment below and share the tutorial with your friends. I would really appreciate your love if you guys like out FB page and support us.

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

20 Responses so far.

  1. Paypal still offers to pay only by a CB interface without registration ?
    And how ?
    This is not the case on the demo, registration is required.

  2. hello sir this code is working only for 1 product how it work for multiple product please help me as soon as possible

  3. how to use the _xclick for multiple items, can you please upload a example tutorial for that ? THANKS
    I cant figure out what params to change as adding multiple items in url gives me a message of incomplete information .

  4. hello sir this code is working only for one profile how it work for multiple profile please help me as soon as possible

  5. Thank You for your code. I have a question how to make my payment live? not sandbox payment.

    How to set paypal_mode: true or false for live payment mode? Please tell me. Thank you.

    1. What to set value of paypal_mode: true or false for live payment mode? Please tell me. Thank you.

Comments are closed.