Wednesday 29 November 2017

Paypal Payment Gateway Integration in PHP

In this tutorial, we are going to see about how to integrate payment gateway in PHP via Paypal. We are going to see an example code in PHP for payment integration. In this example, we are using Paypal sandbox (test) mode to check payment flow before going live.

Paypal Payment Integration Steps

  1. Login / Sign Up with https://developer.paypal.com/
  2. Go to Dashboard -> Sandbox -> Accounts and create test accounts for seller and buyer by selecting Business and Personal respectively.
  3. Login to Sandbox business account and go to My Account -> Profile -> My selling tools
    • Manage PayPal buttons and copy the button code and paste in your page.
    • Manage Website preferences and turn on Auto Return and enter Return URL.
    • Manage Instant payment notifications and select Receive IPN messages (Enabled) and enter Notification URL.

PHP Paypal Payment Gateway Example

In this example, we are having four files to be used in the payment process. These are
  1. index.php – Containing a form with Paypal payment parameters and Buy now button.
  2. payment.php – IPN listener for receiving payment notification and update payment database.
  3. thanks.php – Shows success message after payment.
  4. cancel.php – Acknowledge user if the payment is canceled.

Sending Payment Parameters to Paypal Sandbox

This form contains item_name, item_number, amount, currency and more parameters with Buy now button. We need to copy the Paypal button code from My Account -> Profile -> My selling tools -> PayPal buttons. After clicking this button, the params are sent to Paypal sandbox as mentioned in the form action.
<img src="camera.jpg" />
<h3>Camera <br> $0.01</h3>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
 <input type='hidden' name='business' value='Paypal_Business_TestAccount_Id'>
 <input type='hidden' name='item_name' value='Camera'>
 <input type='hidden' name='item_number' value='CAM#N1'>
 <input type='hidden' name='amount' value='0.01'>
 <input type='hidden' name='no_shipping' value='1'>
 <input type='hidden' name='currency_code' value='USD'>
 <input type='hidden' name='notify_url' value='http://SITE NAME/payment.php'>
 <input type='hidden' name='cancel_return' value='http://SITE NAME/cancel.php'>
 <input type='hidden' name='return' value='http://SITE NAME/success.php'>
 <!-- COPY and PASTE Your Button Code -->
 <input type="hidden" name="cmd" value="_s-xclick">
 <input type="hidden" name="hosted_button_id" value="### COPY FROM BUTTON CODE ###">
 <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>

Instant Payment Notification Listener

In this example, we are having payment.php as IPN listener to receive payment notification. Paypal provides the IPN listener code samples for various languages. We have used paypal_ipn.php and make changes to update Payment database.
...
if (strcmp ($res, "VERIFIED") == 0) {
 // assign posted variables to local variables
 $item_name = $_POST['item_name'];
 $item_number = $_POST['item_number'];
 $payment_status = $_POST['payment_status'];
 $payment_amount = $_POST['mc_gross'];
 $payment_currency = $_POST['mc_currency'];
 $txn_id = $_POST['txn_id'];
 $receiver_email = $_POST['receiver_email'];
 $payer_email = $_POST['payer_email'];
 
 include("dbcontroller.php");
 $db = new DBController();
 
 // check whether the payment_status is Completed
 $isPaymentCompleted = false;
 if($payment_status == "Completed") {
  $isPaymentCompleted = true;
 }
 // check that txn_id has not been previously processed
 $isUniqueTxnId = false; 
 $result = $db->selectQuery("SELECT * FROM payments WHERE txn_id = '$txn_id'");
 if(empty($result)) {
        $isUniqueTxnId = true;
 } 
 // check that receiver_email is your PayPal email
 // check that payment_amount/payment_currency are correct
 if($isPaymentCompleted && $isUniqueTxnId && $payment_amount == "0.01" && $payment_currency == "USD") {
  $payment_id = $db->insertQuery("INSERT INTO payment(item_number, item_name, payment_status, payment_amount, payment_currency, txn_id) VALUES('$item_number', '$item_name', $payment_status, '$payment_amount', '$payment_currency', '$txn_id')");
 }
 // process payment and mark item as paid.
 
 
 if(DEBUG == true) {
  error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
 }
 
}
...
After To set this IPN listener to Paypal go to My Account -> Profile -> My selling tools and select Receive IPN messages (Enabled) and enter this file’s absolute path as Notification URL.


Download

6 comments:

  1. Thanks for posting this...top likable online payment android app Paylo that gives customers to best payment gateway in India. People can pay using any card like visa, rupay or etc.

    ReplyDelete
  2. Online Payment Gateway Qatar provides the most versatile method for processing sales. From any online computer terminal you have the ability to process.

    ReplyDelete
  3. Great informative post on payment gateway integration. However, Cyber security intelligence is a key factor in the digital world, especially to the payment gateway. So, taking the help of Policy advisor is the best way to get the payments secured.

    ReplyDelete
  4. Magnificent blog with incredible snippet of data. A debt of gratitude is in order for taking as much time as necessary to impart this to us. Anticipating gain more from you.
    Jack just posted: Bluehost Dedicated hosting

    ReplyDelete
  5. If you are a small business owner and looking for high risk merchant account instant approval, you need to carry all documents and take help from 5 star Processing experts who are delivering services in this field for many years.

    ReplyDelete
  6. A Paypal merchant account, otherwise called a Paypal business account, is an online payment receiver for business owners or entrepreneurs. In this article we describe all Paypal merchant account requirements.

    ReplyDelete