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
  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 Buynow 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 cancelled.

Sending Payment Parameters to Paypal Sandbox

This form contains item_name, item_number, amount, currency and more parameters with Buynow 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="" 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="" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">

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'];
	$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.


This PHP code tutorial was published on August 21, 2015.

↑ Back to Top