Paypal Payment Gateway Integration In PHP Step By Step

By : PHPErrorCode July 14, 2017 1587 Views php

In this post i am going to give you example of how to integrate Paypal payment gateway in php. It is very simple to use paypal payment method in your native php website because you don't need to use any api for paypal payment gateway integration. Paypal provide very simple way to do this and paypal also provide developer console i mean Sandbox account that way you can check it better. In this example i give you step by step payment integration of paypal.

A paypal payment gateway links your website to your processing network and merchant account. Like most gateways, Payflow Payment Gateway handles all major credit and debit cards. What makes our gateway different is the low rates and incremental sales boost from offering PayPal and PayPal Credit payment options on your site.

Step 1 Create a Paypal Sandbox account :

Create a Paypal Sandbox account at   https://developer.paypal.com/

Now create test accounts for payment system. Take a look at Sandbox menu left-side top  Sandbox -> Test Accounts

Step 2: Create Index File (index.php)

Now in our demo we have index.php file with a demo plan 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:

In this file you have to change

  1. $paypalId
  2. $cancel_url
  3. $success_url
<html lang="en">
<head>
    <title>Paypal Payment Gateway Integration in PHP</title>
</head>
<body style="background: #ffc7c7">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<?php
    $paypalUrl='https://www.sandbox.paypal.com/cgi-bin/webscr';
    $paypalId='vikraminphp@gmail.com';
    $cancel_url='http://demo.phperrorcode.com/paypal/cancel.php';
    $success_url='http://demo.phperrorcode.com.com/paypal/success.php';
?>
<div class="container text-center" style="width: 40%;">
    <br/>
    <h2><strong>Paypal Payment Gateway Integration in PHP</strong></h2>
    <br/>
    <form action="<?php echo $paypalUrl; ?>" method="post" name="frmPayPal1">
	 <div class="panel price panel-red">
	    <input type="hidden" name="business" value="<?php echo $paypalId; ?>">
	    <input type="hidden" name="cmd" value="_xclick">
	    <input type="hidden" name="item_name" value="It Solution Stuff">
	    <input type="hidden" name="item_number" value="2">
	    <input type="hidden" name="amount" value="20">
	    <input type="hidden" name="no_shipping" value="1">
	    <input type="hidden" name="currency_code" value="USD">
	    <input type="hidden" name="cancel_return" value="<?php echo $cancel_url; ?>">
	    <input type="hidden" name="return" value="<?php echo $success_url; ?>">  
			
	    <div class="pay-heading text-center">
		 <p class="btn btn-lg btn-block btn-primary" href="#">Ultimate PLAN</p>
	    </div>
	    <div class="panel_body btn-danger text-center">
		 <p class="lead" style="font-size:40px"><strong>$100 / month</strong></p>
	    </div>
	    <ul class="list-group list-group-flush text-center">
		 <li class="list-group-item"><i class="icon-ok text-danger"></i> 27/7 support</li>
		 <li class="list-group-item"><i class="icon-ok text-danger"></i> Unlimited projects</li>
	    </ul>
	    <div class="pay-heading text-center">
		 <button class="btn btn-lg btn-block btn-success" href="#">BUY NOW!</button>
	    </div>
	 </div>
    </form>
</div>
</body>
</html>
note : change $paypalId and $paypalUrl.

It is complete for our sendbox with paypal integration but when you live this then you have to change $paypalId and $paypalUrl.

$paypalUrl='https://www.sandbox.paypal.com/cgi-bin/webscr';
INTO
$paypalUrl='https://www.paypal.com/cgi-bin/webscr';
Step 3: Create Payment Success (success.php)

Once the PayPal payment is successful, the buyer is redirected to this page. The transaction information is received using $__REQUEST($_GET)method and inserts payment data into the database.

$itemNo            = $_REQUEST['item_number'];
$itemTransaction   = $_REQUEST['tx']; // Paypal transaction ID
$itemPrice         = $_REQUEST['amt']; // Paypal received amount
$itemCurrency      = $_REQUEST['cc']; // Paypal received currency type
 
$price = '100.00';
$currency='USD';
 
if($itemPrice==$price && $itemCurrency==$currency)
{
  echo "Payment Successful";
}
else
{
  echo "Payment Failed";
}
Step 4: Create Cancel File (cancel.php)

We also need to create cancel.php file because when paypal transaction will be cancel then it will redirect on this file.

<h1>Your PayPal transaction has been canceled.</h1>