PayPal
is the most popular payment service on the web so you can integrate
your website with PayPal Instant Payment Notification Service (IPN) is
essential if you need to process payments through your website.
There are 3 main parts to the IPN PayPal system:
- A web page that initiates a request to PayPal to make a payment.
- The PHP page on your web server that PayPal contact to inform you that payment has been made.
- A webpage that confirms the payment above and continues into the next phase of your web app, such as the 'Thank You' page.
Parts 1 and 3 are accessible to customers on your website. Part 2 is only visible to PayPal. The diagram below illustrates the interactions between your customers, PayPal, and your website.
The
following steps split each part of the process into easy to follow
snippets, it is assumed that you have knowledge about PHP and MySQL.
Note
If
you do not receive the correct answer from Paypal, make sure that you
are using a primary test account (Verified Business Account) from your
Paypal Sandbox account.
Also make sure
you test Paypal IPN Script on an online web server (not MAMP, Xampp
etc.) Because Paypal requires 'url back', 'undo url' and 'notify url'.
Set up a PayPal Account
Sign up for a PayPal account if you do not already have one. To use IPN, the Paypal account you are selling must be a Business Account.
Once you have a registered PayPal account, your account must be properly set up to use IPN. Choose 'edit profile' from your PayPal account and check the following settings.
- Under 'My Sales Preferences' >> 'Get paid and manage risks' >> 'Instant Payment Notification Preferences'
- Set IPN value to 'On'
- Set the IPN URL to the PHP page containing the IPN code shown in steps 3 & 4 of this tutorial. (http://www.example.com/payment.php)
- Under 'My Sales Preferences' >> 'Get paid and manage risk' >> 'Block of payment'
- Block payments from users who pay with eCheque. (This is because this is not an instant payment)
- Under 'account information' >> 'email'
- Record your primary email address. This email will be visible to the user so as to make it professional. Users may feel worried about sending money to an e-mail address with a domain 'hotmail.com' or 'Yahoo.com' etc
Paypal Example Source Code
In this example I have created four files for paypal payment integration process. Files are
- index.php
- connectiochar(255) COLLATE utf8_unicode_ci NOT NULL, `payment_amount` 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, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `payments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`txn_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`payment_amount` 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,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Now create index.php file and copy the below code and paste it in index.php file.
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
Next step to create database connection file connection.php and copy the below code and paste in just created connection file.
Please change the connection parameter like username,password and database name in connection file.
When paypal transaction is success then return full details of transaction in array format. I have explained some variable like item_no,transaction_id,amt,currency_code,payment_status etc.
Next Step we need to create success file and display transaction details which received from paypal .
include 'connection.php';
//save Trasaction information form PayPal
$item_number = $_GET['item_number'];
$txn_id = $_GET['Trasaction_ID'];
$payment_gross = $_GET['AMT'];
$currency_code = $_GET['CURRENCY_CODE'];
$payment_status = $_GET['PAYMENT_STATUS'];
if(!empty($txn_id)){
//Insert tansaction data into the database
$insert = $db->query("INSERT INTO payments(item_number,txn_id,payment_amount,currency_code,payment_status) VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$payment_status."')");
$last_insert_id = $db->insert_id;
?>
Your payment has been successful.
Your Payment ID - .
}else{
?>
Your payment has failed.
}
?>