How to integrate paypal payment gateway using PHP

This post belongs to newbie PHP developer who wants to integrate paypal payment gateway in his website using PHP. This post will show you the very basic method to integrate paypal using core PHP.

First of all need to create paypal developer account.
https://developer.paypal.com/developer/accounts
paypal-php



After that you’ll see two test email ids one for business type and other one for personal type. Where You’ll receive your paypal payment in your business account and you can purchase any item from your personal account. Both the account just for testing purpose you can use them for buying and selling items.

Time to start coding part.

create your project config file where you can set all configuration of your project like paypal url, paypal id etc.

config.php

<?php
// Your test paypal sandbox url, Replace it with live url after successful testing.
DEFINE('PAYPAL_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr'); 
 
// Set paypal marchent id.
DEFINE('PAYPAL_ID', 'YOUR_PAYPAL_ID');  // you can find thuis in your developer account it look like "[email protected]"
 
// Define your base currency
DEFINE('CURRENCY', 'USD')
?>

Create your payment page this is just for demonstration purpose you can change it according to your need.

index.php

<?php
 require_once('config.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>INTEGRATE PAYPAL PAYMENT GATEWAY IN PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="panel panel-primary" style="width:50%;margin:0 auto; margin-top:2%">
<div class="panel-heading"><h3>Paypal Payment Gateway in PHP</h3></div>
<div class="panel-body" style="height:40%; text-align:center;" >
<p class="bg-info" id="msg"></p>
 <form class="form-horizontal" role="form" id="paypalForm" method="post" action="<?php echo PAYPAL_URL; ?>">
    <input type="hidden" name="business" value="<?php echo PAYPAL_ID; ?>">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="credits" value="510">
    <input type="hidden" name="userid" value="1">
    <input type="hidden" name="cpp_header_image" value="">
    <input type="hidden" name="no_shipping" value="1">
    <input type="hidden" name="handling" value="0">
    <input type="hidden" name="cancel_return" value="http://lab.iamrohit.in/paypal-php/request.php?type=cancel">
    <input type="hidden" name="return" value="http://lab.iamrohit.in/paypal-php/request.php?type=success">
  <div class="form-group">
    <label class="control-label col-sm-2" for="amount">Amount:</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="amount" placeholder="Enter Amount" required="required" value="10">
    </div>
  </div>
     <div class="form-group">
    <label class="control-label col-sm-2" for="currency">Quantity:</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="quantity" placeholder="Enter Quantity" value="1" required="required">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="currency">Currency:</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="currency" placeholder="Enter Currency Type" value="<?php echo CURRENCY; ?>" required="required">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="description">Description:</label>
    <div class="col-sm-10">
      <textarea class="form-control" name="item_name" placeholder="Enter Description">My First Payment</textarea>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </div>
  </div>
</form>
</div>
</div>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>

Create request.php file which will handle all your paypal requests like success or cancel.

request.php

<?php
$type = $_GET['type'];
if($type == 'success') {
  echo "<pre>";
  print_r($_REQUEST);
  echo "<h1>Payment Successful</h1>";
} 
 
if($type == 'cancel') {
  echo "<h1>Payment Canceled</h1>";
}
?>

In payment success case you can see all your sent data by $_REQUEST variable and cross check in your side before showing success message.
paypal-php-1

See the working demo and you can also download source code form my github repository.

Hope this simple paypal integration tutorial will help you to integrate paypal payment gateway in your website.

20+ PHP Paypal Integration Script

Are you looking for PHP Paypal Integration Script for your web based application. If yes then in this post I am going to share hand picked top rated PHP Paypal Integration Script. You can use these popular PHP Paypal Integration Script to make your web application more awesome.


PHP Paypal Integration Script

Following are the list of popular top rated hand picked PHP Paypal Integration Script.

I Hope you liked Hand-picked list of PHP Paypal Integration Script, Don’t forget to Subscribe My Public Notebook for more useful Hand-picked PHP and MySql code examples, tutorials and articles.

2 Replies to “How to integrate paypal payment gateway using PHP”

  1. Trying Alot but unable to pay with sanbox account. can u tell me why my test account is not working and when i click on pay button it goes to paypal and show return to mercant why it show this..is paypal change testing mode ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.