How to use PayPal Instant Payment Notification for e-commerce Website?

In this part, we are going to learn about PayPal Instant Payment Notification (IPN).

IPN is a message service that notifies you of events related to PayPal transactions. You can use IPN to automate back-office and administrative functions. As the name says Instant Payment Notification, it really is instant. The moment a customer buys a product from your website using PayPal, IPN reaches your server within few seconds.

The process is shown in the following image (Copied from PayPal)

When a customer buys your product, PayPal sends the transaction detail to the IPN URL provided by you.

Lets write a code to handle IPN

        $req = 'cmd=_notify-validate';
        //Collect the variables sent by PayPal
        foreach ($_POST as $key => $value) {
            $value = urlencode(stripslashes($value));
            $req .= "&$key=$value";
        }

        //Send them back to PayPal to verify whether they are authentic or not
        $paypal_action = 'https://www.paypal.com/cgi-bin/webscr';
        //For PayPal Sandbox: $paypal_action = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
        $ch = curl_init($paypal_action);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
        $result = curl_exec($ch);
        curl_close($ch);

        //If PayPal verified the posted data
        if (strcmp ($result, "VERIFIED") == 0) {
            $transaction_id = $_POST['txn_id'];
            $payment_status = $_POST['payment_status'];
            $item_name = $_POST['item_name'];
            $amount = $_POST['mc_gross'];
            $quantity = $_POST['quantity'];
            $fee_deducted = $_POST['mc_fee'];
            $payer_email = $_POST['payer_email'];
            $payment_date = $_POST['payment_date'];

            //Check the payment status
            switch($payment_status) {
                case 'Processed':
                    //Processed and Completed are almost same. Perform the action for successful payment.
                    break;

                case 'Completed':
                    //Perform the action for successful payment.
                    break;

                case 'Pending':
                     //Perform the action for pending payment status.
                    break;

                default:
                    break;

            }
        }
        else {
            //Perform the action for invalid IPN.
        }

Lets see what we have done so far.

PayPal sends the transaction details to our server. To check whether, PayPal sent this data, we need to send the data back to PayPal. If the data was sent by PayPal, it will display “VERIFIED”.

  • Line # 1-6: To send data back to PayPal, we will first collect the variables posted to our server. We need to add ‘cmd=_notify-validate’ to these variables to tell PayPal to validate the data provided.
  • Line # 8-17: We will use CURL to send data back to PayPal. We will collect the information displayed by the PayPal in $result.
  • Line # 20: We will check whether the posted data was valid or not.
  • Line # 21-28: If the data is valid, then we will stored the sent data in variables. Few examples of the variables are:
    • txn_id : This is the transaction id of the transaction made. It is unique for every transaction.
    • item_name : This is the product name.
    • mc_gross: Full amount of the customer’s payment, before transaction fee is subtracted.
    • mc_fee: Transaction fee which is deducted for the transaction.
    • payment_status: The payment status such as Processed, Completed, Pending, Failed, etc.
    • payer_email: Email address of the payer.
    • payment_date: Date on which payment was made.

    For more information on variables, refer PayPal IPN and PDT Variables

  • Line # 31-47: Depending on the payment status, perform the necessary action.

By using IPN, you can maintain inventory, transaction logs and automate so many other things.

For more information on PayPal IPN, you can refer the following link:

One Comment

Hafis December 17, 2010

Paypal has become more complex nw a days.