This tutorial aims to introducing how to make registration process work properly with PayPal system.
- Explain how PayPal works (IPN and PDT process). Chapter One
- Give a good example to show how register work with PayPal, containing database, controller and front end form with jQuery functionalities. And explain how to setup a PayPal account (Sandbox and real PayPal business account example). Chapter Two
- Give detailed comments in code. Chapter Three
This tutorial gives a big picture of PayPal process (PDT and IPN), and a real project from starting point to the end. Good explanation and pictures, detailed comments in code line. Hopefully, it will be useful for all readers.
PayPal PDT (Payment Data Transfers)
PDT is enables buyer redirect from merchant’s website to PayPal website, and pay the payment, then redirect back to merchant’s website.
Client clicks on the button and goes to PayPal website, show payment form.
After client filling the payment form, and clicking ‘Pay Now’ button in PayPal, the PayPal will redirect to PayPal Result page, showing :
During this step, PayPal is calling your own PDT handler function, and passing parameters via URL, the most important variable is ‘tx’, which is transaction token.
This step is processed invisibly. After PayPal call your own PDT handler function, your PDT handler function will POST tx and auth_token back to PayPal.
PayPal will identify your merchant account auth_token and tx token. Then, your own PDT handler function, the fsock get data from PayPal.
The payment data from PayPal is a string like:
The last step is simple and easy. Your PDT handler function can render success page or fail page to the buyer’s screen.
So let’s have an overall view about all those four steps.
PayPal IPN (Instance Payment Notification)
IPN is a backup plan for buyers close browser after payment, PDT cannot redirect back to the merchant’s site. If buyers close browser or stop PDT redirect, your PDT handler function cannot $_GET[‘tx’], then you cannot POST back tx and auth_token, then PayPal cannot send payment data to your handler, then result page cannot be shown.
Luckily, PayPal provides a backup plan IPN which is more reliable, because PayPal will POST payment data to your IPN handler function.
PayPal POST payment details to your IPN handler function. POST data is similar like:
POST data should be an array, but I encoded by json_encode, so now it displays in json format.
Your IPN handler function POST payment data back to PayPal via cURL, and wait for PayPal confirmation.
PayPal confirmed payment data from your IPN handler function, and return confirmation result.
So, you can see, the POST data was verified by PayPal.
From the diagram above, you can see, PayPal and your IPN handler function, have a “Handshake Protocol” for the confirmation of payment details. Then, your IPN handler function receives result string from PayPal, then, your function can process different action according to the result.
PayPal IPN & PDT Working Together
Hopefully, you have a clear understanding about PayPal PDT and IPN. The next, we should make the two tools work together, in order to make sure payment processed correctly.
The key thing is to handle duplication handlers. For example, we have a payment record named payment_1. PDT processed payment_1, and IPN also tries to process payment_1, (if user did NOT close the browser).
One method to solve this problem is, in your database, you can mark the payment has been processed, which means, pre-check payment_1 has been processed already or not.
Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.