- The payments.php code (Line 60-64) has been slightly amended to fix the IPN Invalid Response.
- If you are not receiving the correct response from Paypal ensure that you are using the main test account (Verified Business Account) from your Paypal Sandbox account.
- Also ensure that you are testing the Paypal IPN Script on an online webserver (Not MAMP, Xampp etc..) as Paypal requires a reachable ‘return url’, ‘cancel url’ and ‘notify url’.
- The downloadable source code has been updated with the above changes.
PayPal is the most popular payment service on the web so being able to integrate your website with PayPal’s Instant Payment Notification Service (IPN) is essential if you need to process payments through your website.
There are 3 main parts to the PayPal IPN system.
- A webpage that initiates a request to PayPal to make a payment.
- A PHP page on your webserver that PayPal calls to notify you that payment has been made.
- A webpage that confirms the above payment and continues on to the next phase of your web application, such as a ‘Thank You’ page.
Parts 1 and 3 are accessible by customers on your website. Part 2 is only visible to PayPal. The diagram below illustrates the interaction between your customer, PayPal and your website.
The following steps break down each part of the process into easy to follow chunks, it is assumed that you have knowledge of PHP and MySQL.
Step 1 – Setup PayPal Account
Sign up for a PayPal account if you don’t already have one. Select an appropriate account type, either Personal or Business.
Once you have a registered PayPal account your account must be setup correctly to use IPN.
Select ‘edit profile’ from your PayPal account and check the following settings.
- Under ‘Selling Preferences’ >> ‘Instant Payment Notification Preferences’
- Set the 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 ‘Selling Preferences’ >> ‘payment receiving preferences’
- Block payments from users who pay with echeck. (This is because these will not be instant payments)
- Under ‘account information’ >> ‘email’
- Note down your primary email address. This email will be visible to users so make it a professional one. User’s may feel apprehensive about sending money to an e-mail address with the domain ‘hotmail.com’ or ‘Yahoo.com’ etc…
Step 2 – Simple HTML Form
Your website must now send all the required values to PayPal so that the payment can be processed.
The following code example demonstrates a basic form that we will use to send the values:
1 | < form id = "paypal_form" class = "paypal" action = "payments.php" method = "post" > |
2 | < input name = "cmd" type = "hidden" value = "_xclick" /> |
3 | < input name = "no_note" type = "hidden" value = "1" /> |
4 | < input name = "lc" type = "hidden" value = "UK" /> |
5 | < input name = "currency_code" type = "hidden" value = "GBP" /> |
6 | < input name = "bn" type = "hidden" value = "PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" /> |
7 | < input name = "first_name" type = "hidden" value = "Customer's First Name" /> |
8 | < input name = "last_name" type = "hidden" value = "Customer's Last Name" /> |
9 | < input name = "payer_email" type = "hidden" value = "customer@example.com" /> |
10 | < input name = "item_number" type = "hidden" value = "123456" /> |
11 | < input type = "submit" value = "Submit Payment" /> |
The business name, price, submit type, notify URL and other sensitive values will be sent during the next step.
A full list of the values to send can be found at the PayPal website under the title “A Sample IPN Message and Response”.
Step 3 – Payments.php (The Request)
The payment.php page will be used to handle the outgoing request to PayPal and also to handle the incoming response after the payment has been processed.
The following sample code shows the querystring being constructed before it is posted to PayPal. Here you can specify the following values:
- Business ($paypal_email) – Enter the email address of your PayPal account.
- Item name ($item_name) – The name of the item being purchased.
- Amount ($item_amount) – The price of the item.
- Return ($return_url) – The address to return to after a successful payment.
- Cancel Return ($cancel_url) – the address to return to after a cancelled payment.
- Notify URL ($notify_url) – The address of the payments.php page on your website.
- Custom – Any other data to be sent and returned with the PayPal request.