REGISTER WITH PAYPAL TUTORIAL (1/3): PDT and IPN Process

Abstract

This tutorial aims to introducing how to make registration process work properly with PayPal system.

  1. Explain how PayPal works (IPN and PDT process). Chapter One
  2. 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
  3. 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.

Chapter One

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.

Step 1:

Client clicks on the button and goes to PayPal website, show payment form.

Step 2:

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.

e.g.: http://domain.com/register/paypalPDT?tx=4XV95919FA406935A&st=Completed&amt=400.00&cc=AUD&cm=&item_number=38

Step 3:

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:

step-3-2

Step 4:

The last step is simple and easy. Your PDT handler function can render success page or fail page to the buyer’s screen.

Conclusion Overview:

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.

Step 1:

PayPal POST payment details to your IPN handler function. POST data is similar like:

IPN-POST

POST data should be an array, but I encoded by json_encode, so now it displays in json format.

Step 2:

Your IPN handler function POST payment data back to PayPal via cURL, and wait for PayPal confirmation.

Step 3:

PayPal confirmed payment data from your IPN handler function, and return confirmation result.

Something like:

So, you can see, the POST data was verified by PayPal.

Conclusion Overview:

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.