Showing posts with label Payment Gateway Integration. Show all posts
Showing posts with label Payment Gateway Integration. Show all posts

How to integrate the PayPal payment gateway in PHP

Hi, Today I am going to explain the major payment gateway used by all websites. i.e PayPal
This is a very simple mechanism to integrate the Paypal payment gateway in web applications.

First, we need the business email for the Paypal Account for this you have to sign up in the Paypal Website.

Before going to integrate the Paypal in web applications you need know about these Payment Modes

1) Test Mode
2) Live Mode

In test mode, you can set the test URL i.e https://www.sandbox.paypal.com/cgi-bin/webscr
Here you can test the payment mechanism before going to live mode. This is very helpful for the developers

In live mode, you can set the live URL i.e https://www.paypal.com/cgi-bin/webscr
Here you can't test the payment mechanism in live mode. All the transactions are done in real accounts (Means all transactions are made live)

You need to pass the some of the hidden input parameters through the POST request to the Paypal.
item_name, item_number, amount, currency_code. These are the Product details.

And one more thing finally you need to pass the two input parameters through the POST request
cancel_return -- Here you need to specify the cancel return URL if a transaction fails.
return -- Here you need to specify the return URL after the successful transaction completes

Payments db
CREATE TABLE `payments` (
  `payment_id` int(11) NOT NULL,
  `item_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `txn_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `payment_gross` float(10,2) NOT NULL,
  `currency_code` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
  `payment_status` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `payments`
  ADD PRIMARY KEY (`payment_id`);
index.php
 <?php  
 $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; //Test PayPal API URL  
 $paypal_id = 'techiesbadi@gmail.com'; //Business Email  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 <title>Paypal Payments --TechiesBadi</title>  
 </head>  
 <body>  
 <center>  
 <h1>Paypal Payments</h1>  
   <img src="images/redmi.png"/>  
   <br/>Redmi 3s prime  
   <br/> $ 10.00  
   <form action="<?php echo $paypal_url; ?>" method="post">  
     <!-- Here Specify the Paypal Bussiness Email-->  
     <input type="hidden" name="business" value="<?php echo $paypal_id; ?>">  
     <!-- Specify a Buy Now button. -->  
     <input type="hidden" name="cmd" value="_xclick">  
     <!-- Here Specify the buyer product details -->  
     <input type="hidden" name="item_name" value="Product1">  
     <input type="hidden" name="item_number" value="1">  
     <input type="hidden" name="amount" value="10">  
     <input type="hidden" name="currency_code" value="USD">  
     <!-- Here Specify the Return and Cancel URLs -->  
     <input type='hidden' name='cancel_return' value='http://demos.techiesbadi.in/paypal/cancel.php'>  
           <input type='hidden' name='return' value='http://demos.techiesbadi.in/paypal/success.php'>  
     <!-- Display the payment button. -->  
     <input type="image" name="submit" border="0"  
     src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">  
     <img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >  
   </form>  
   </center>  
 </body>  
 </html>
success.php
<?php  
 include 'config.php';  
 extract($_REQUEST);  
 //Store transaction information from PayPal  
 $item_number = $item_number;   
 $txn_id = $tx;  
 $payment_gross = $amt;  
 $currency_code = $cc;  
 $payment_status = $st;  
 if(!empty($txn_id)){  
      //Check if payment data exists with the same TXN ID.  
   $prevPaymentResult = $db->query("SELECT payment_id FROM payments WHERE txn_id = '".$txn_id."'");  
   if($prevPaymentResult->num_rows > 0){  
     $paymentRow = $prevPaymentResult->fetch_assoc();  
     $last_insert_id = $paymentRow['payment_id'];  
   }else{  
     //Insert tansaction data into the database  
     $insert = $db->query("INSERT INTO payments(item_number,txn_id,payment_gross,currency_code,payment_status) VALUES('$item_number','$txn_id','$payment_gross','$currency_code','$payment_status')");  
     $last_insert_id = $db->insert_id;  
   }  
 ?>  
      <h1>Your payment has been successful.</h1>  
   <h1>Your Payment ID - <?php echo $last_insert_id; ?>.</h1>  
 <?php  
 }else{  
 ?>  
      <h1>Your payment has failed.</h1>  
 <?php  
 }  
 ?> 
 
cancel.php
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 <title>PayPal Transaction Cancel - TechiesBadi</title>  
 </head>  
 <body>  
      <h1>Your PayPal transaction has been canceled.</h1>  
 </body>  
 </html>  
config.php
 <?php  
 //Database credentials  
 $dbHost = 'localhost';  
 $dbUsername = 'root';  
 $dbPassword = '';  
 $dbName = 'techiesbadi';  
 //Connect with the database  
 $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);  
 ?>  
 
Here you can download the Full Source code and check the demo.

Download Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff