Categories: PHP

paypal integration in php

paypal integration in php

This simple tutorial will explin you how to integrate paypal for your application. Normally paypal having two modes 1. Live, 2. Sandbox. Sandbox mode is used for testiong purpose which is helpful for developers before the application goes into live. To integarate paypal we need paypal sandbox credetials.

How to get Paypal Sandbox Credetials?

Step1 :

Go to https://developer.paypal.com/ and create a developer account in PayPal.

Step2 :

Login into paypal developer account.

Step3 :

Click ‘Applications’ menu click on that and click ‘Sandbox Accounts’ from the left menu. There you can find your email with ‘– facilitator’ that is you PayPal facilitator email address which will receive funds from users

Step 4 :

Then click ‘Create Account’ at the right of the page. Create test accounts for make transactions. Make sure that ‘Account Type’ – ‘Personal’ has been checked. Now you have the both facilitator email address and test account email address.

Now we are ready to start integrate paypal to your site. Download below code by clicking Download button and follow my instructions.



Step1 :

Open paypal.php and change below lines

define('EMAIL_ADD', 'notifyemail@anydomain.com'); // define any notification email
define('PAYPAL_EMAIL_ADD', 'youname@yourdomain.com'); // facilitator email which will receive payments change this email to a live paypal account

 EMAIL_ADD => The Email id for status about the tranasctions. The notifications will be send during IPN time.

PAYPAL_EMAIL_ADD => Your paypal facilitator id to receive payments.

Step 2 :

Desing Your HTML form.

<form action="paypal.php?sandbox=1" method="post"> <?php // remove sandbox=1 for live transactions ?>
    <input type="hidden" name="action" >
" ?sandbox=1 "  => Your are running under live or developer mode.
You can add your custom fields also like <input type="text" name="custom" >Step 3:

Add your item detials to Paypal
$p 				= new paypal_class(); // paypal class
$p->admin_mail 	= EMAIL_ADD; // set notification email

$this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
		$p->add_field('business', PAYPAL_EMAIL_ADD); // Call the facilitator eaccount
		$p->add_field('cmd', $_POST["cmd"]); // cmd should be _cart for cart checkout
		$p->add_field('upload', '1');
		$p->add_field('return', $this_script.'?action=success'); // return URL after the transaction got over
		$p->add_field('cancel_return', $this_script.'?action=cancel'); // cancel URL if the trasaction was cancelled during half of the transaction
		$p->add_field('notify_url', $this_script.'?action=ipn'); // Notify URL which received IPN (Instant Payment Notification)
		$p->add_field('currency_code', $_POST["currency_code"]);
		$p->add_field('invoice', $_POST["invoice"]);
		$p->add_field('item_name_1', $_POST["product_name"]);
		$p->add_field('item_number_1', $_POST["product_id"]);
		$p->add_field('quantity_1', $_POST["product_quantity"]);
		$p->add_field('amount_1', $_POST["product_amount"]);
		$p->add_field('first_name', $_POST["payer_fname"]);
		$p->add_field('last_name', $_POST["payer_lname"]);
		$p->add_field('address1', $_POST["payer_address"]);
		$p->add_field('city', $_POST["payer_city"]);
		$p->add_field('state', $_POST["payer_state"]);
		$p->add_field('country', $_POST["payer_country"]);
		$p->add_field('zip', $_POST["payer_zip"]);
		$p->add_field('email', $_POST["payer_email"]);
		$p->submit_paypal_post(); // POST it to paypal

From this step your paypal transaction starts. It will redirect you to paypal payment page once the payment done you will get the mail with tranasction detials. Your mail contains Transaction id, Payment Staus, Invoice. Initially you will get the Payment status as " Pending" once the IPN processed you will get the notification with status "Payment Success". Till you get Success status your tranasctions are under processed.

Note: Make sure about the transaction id, payment status and invoice number for further steps

Step 4:

What is IPN?.

PayPal has a module called IPN which means Instant Payment Notification(IPN). This will send you the transaction details from PayPal Server to your Server directly through posting data values in POST method.

How to send IPN when in developer mode for testing.

Step1 : Go to your PayPal sandbox account and go to 'Application -> IPN Simulator' paste the URL in IPN handler URL which want to receive the IPN Notifications.

Step2 : You can see the Forms with number of fields, Don't care about that just fill txn_id field with user transaction id and press Send IPN button. Then paypal will send the trnasction detials to the given URL or else will give error message.

Note : You cannot view the IPN details directly. You need to write it in "txt" file or else have to save it into database or else you can send all $_POST details to uour mail by writting PHP code in the given URL page.

Before Live:

  • Remove 'sandbox=1' from the form action before your app goes live in index.php file.
  • Change the Notify email to other log email address or you will get hundreds of emails.
  • Change the facilitator account to a live account.


Thanks!. If you like this article don't forget to share and comment.



View Comments