This tutorial will explain you about Simple PayPal Integration code using PHP in web application for online checkout. Normally PayPal has two environments such as Sandbox, Real Time. Sandbox is for developers which help to do their test transactions (test purchases) before application or project goes live.

paypal integration code using php

To integrate PayPal in your applications first go to https://developer.paypal.com/ and create a developer account in PayPal. Then sign in to the account using created credentials. There you can find ‘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. 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.  Then we will start the coding phase now. Download the simple ready to use PayPal integration script using PHP from below by clicking download icon. And change the line as like instructed below to use this PayPal integration code.

Step 1 Open paypal.php:

Change the below mentioned lines,

define('EMAIL_ADD', 'notifyemail@anydomain.com'); // define any notification email
define('PAYPAL_EMAIL_ADD', 'anyname-facilitator@domain.com'); // facilitator email which used to receive payments.

Constant EMAIL_ADD is for receive notifications about the transactions like is it completed, pending and all. You will get emails by notification during IPN.

Constant PAYPAL_EMAIL_ADD is the mail which will receive funds from the users. Define the facilitator email address here. Define the original PayPal facilitator address here before site goes live.

Step 2 Design the form:

Design the form with mentioned fields and addition custom fields if you need.

NOTE: Remove ‘?sandbox=1’ from thw form action for live transactions.

Step 3 Post to a page:

Post the form to a page where you need to send the form data to PayPal. In this page we are going send the values to the paypal.php.

$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

Save the form data before posted to PayPal like coded above. In this step the transaction will get started.

After the transaction got completed you will get an email to your Notification Email (EMAIL_ADD) regarding the transaction process. First you will get an email. Find ‘payment_status’ on that mail body. It will shows as ‘Pending’. Now the transaction is in pending status. Until PayPal sends ‘payment_status = Completed’ the transaction will be in the pending status.  

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

Step 4 IPN (Instant Payment Notification):

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

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. We had already mentioned the notify_url above. Paste the URL and choose ‘Transaction type’ as cart checkout. You will get number of fields. The field label will represent you the POST parameter name. No need to fill everything, but you should provide the txn_id (which you got from PayPal screen after transaction completion), choose payment_status as completed and fill invoice for further process. Then click on ‘Send IPN’ button. You will get the IPN details to the IPN URL and to the notification email either you got ‘IPN sent successfully’ or ‘sorry couldn’t send IPN’

Wait and check your email. You will get the mail(s) from IPN. Find on the payment_status. Make sure it got completed.

Save the data in your database. Check the payment_status is ‘completed’ then raise other stuffs such as sending invoice email and so on.

Notes (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.

 

I have implemented this simple PayPal integration code and working fine in many of shopping cart projects and my projects contains checkout. Donwload this simple and ready to use script for your projects from below download link. Happy coding!

Post a comment



Mohamed Asif Am a web developer i love web designing, web developing. for any tutorials mail me at asif18[at]asif18.com