Paypal Express Checkout Integration with PHP

PayPal is the most popular payment gateway to integrate in your website to get paid all over the world. As we know that the checkout process is very important for any eCommerce website as increase sales. So now PayPal started a new feature PayPal Express Checkout to allow buyers a simple and secure checkout experience that keeps them local to website or mobile app during payment process. The PayPal Express Checkout is a client end solution and need to use the PayPal checkout.js code. So today in this tutorial you will learn how to integrate PayPal Express Checkout in your website with PHP. The tutorial covered in easy steps with live example.

Also, read:

As we will cover this tutorial with live example to integrate PayPal Express Checkout with PHP , so the major files for this example is following.

  • index.php
  • config.php
  • payPalCheckout.php
  • orderDetails.php

Step1: Configure PayPal Sandbox Account and REST API Application
First we need to https://developer.paypal.com and create PayPal Sandbox account to run this example. We also need to create REST API Application by going through REST API apps link in PayPal sandbox dashboard. When REST API Application created, you will get Sandbox and App details with CLIENT id and SECRET KEY to integrate PayPal Express Checkout. We will keep our PayPal account in Sandbox mode to test functionality. When functionality complete and tested then set account as Live.

Step2: Create Configuration
In config.php file, we will define Sandbox and Production configuration to integrate PayPal express checkout. The ProPayPal variable defined with value 0 to test functionality in testing mode. We will change this to 1 when live functionality in production mode.


<?php
define('ProPayPal', 0);
if(ProPayPal){
	define("PayPalClientId", "*********************");
	define("PayPalSecret", "*********************");
	define("PayPalBaseUrl", "https://api.paypal.com/v1/");
	define("PayPalENV", "production");
} else {
	define("PayPalClientId", "*********************");
	define("PayPalSecret", "*********************");
	define("PayPalBaseUrl", "https://api.sandbox.paypal.com/v1/");
	define("PayPalENV", "sandbox");
}
?>

Step3: Display Item Details with PapPal Express Checkout Button
In index.php file, we will display item details with PayPal Express Checkout Button. We will include paypalCheckout.php file to display PayPal Express Checkout Button.

<div class="container">
	<h2>Paypal Express Checkout Demo with PHP</h2>	
	<br>
	<table class="table">
	    <tr>
          <td style="width:150px"><img src="demo_product.png" style="width:50px" /></td>
          <td style="width:150px">$<?php echo $productPrice; ?></td>
          <td style="width:150px">
          <?php include 'paypalCheckout.php'; ?>
          </td>
        </tr>
    </table>	
</div>

Step4: Configure PapPal Express Checkout
In paypalCheckout.php file, we will configure PayPal Express Checkout to display Button and process payment with response. We will call orderDetails.php to return payment process details with required values to check it.

<div id="paypal-button-container"></div>
<div id="paypal-button"></div>
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
paypal.Button.render({
  env: '<?php echo PayPalENV; ?>',
  client: {
	<?php if(ProPayPal) { ?>  
	production: '<?php echo PayPalClientId; ?>'
	<?php } else { ?>
	sandbox: '<?php echo PayPalClientId; ?>'
	<?php } ?>	
  },
  payment: function (data, actions) {
	return actions.payment.create({
	  transactions: [{
		amount: {
		  total: '<?php echo $productPrice; ?>',
		  currency: '<?php echo $currency; ?>'
		}
	  }]
	});
  },
  onAuthorize: function (data, actions) {
	return actions.payment.execute()
	  .then(function () {
		window.location = "<?php echo PayPalBaseUrl ?>orderDetails.php?paymentID="+
data.paymentID+"&payerID="+
data.payerID+"&token="+data.paymentToken+"
&pid=<?php echo $productId; ?>";
	  });
  }
}, '#paypal-button');
</script>

Step5: Check Payment Process Details
In orderDetails.php file, the payment process details returned. So we will check payment process returned values here like token id, payment id, payer id and product id.

<?php 
if(!empty($_GET['paymentID']) && !empty($_GET['payerID']) && !empty($_GET['token']) && !empty($_GET['pid']) ){
	$paymentID = $_GET['paymentID'];
	$payerID = $_GET['payerID'];
	$token = $_GET['token'];
	$pid = $_GET['pid'];   
	?>
	<div class="alert alert-success">
	  <strong>Success!</strong> Your order processed successfully.
	</div>
	<table>       
		<tr>
		  <td>Payment Id:  <?php echo $paymentID; ?></td>
		  <td>Payer Id: <?php echo $payerID; ?></td>
		  <td>product Id: <?php echo $pid; ?></td>
		 </tr>       
	</table>
<?php	
} 
?>

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download


One thought on “Paypal Express Checkout Integration with PHP

Leave a Reply

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