Kristian Kraljić You may compare programming with poetry,
a harmonic script is a piece of creativity which will last forever.
- Kristian Kraljić

PayPal – Generic PHP Payment (PDT/IPN) Script

by Kristian Kraljic, October 7, 2011

To quote the refrain of one of my favorite songs: I’m “Still Alive”. My fourth publication took again a little longer than expected. I had to finish a lot of things at work, unfortunately nothing which I can publish here (yet).

Today I want to introduce my generic PayPal PDT (Payment Data Transfer) and IPN (Instant Payment Notification) script. I have been using it for quite a while now personally, so I thought: “Why not put some more effort into it and release it for everybody”. Using the merchant services of PayPal has been kind of tricky in the past. If you handle your transactions manually you are good to go, but as soon as you start to process your payments automatically, you have no other chance as to integrate either your own PDT and/or IPN script to handle the transactions.

There are a lot of examples of implementing a proper IPN or PDT script, but this script is a more general solution for all PayPal szenarios using PDT and/or IPN. The script automatically:

  • Records & validates the transactions
  • Extracts necesarry information and writes it to the database
  • Notifies you via e-mail if a transaction has been processed
  • Provides possibilites to enhance the script to write your own “transaction handler” functions

The script works out of the box and is able to deal with PDT and IPN requests. Simply follow the instructions in the readme file to customize your database connection and contact e-mail. Also a explanation is given how to customize your PayPal account for using the PDT and/or IPN szenarios.

Download Source: (Distributed under the Simplified BSD License)

If you have anything to say, suggestions or questions feel free to drop a comment below.

Update (10.17.11): Version 1.1 released. Please update your scripts. A bug was fixed if a ‘pending’ transaction comes in.

Update (03.18.11): Version 1.2 released. Please update your scripts. A bug was fixed for transactions not in sandbox.

8 Comments

  1. rajesh says:

    Just gone over your code in php. I am an asp.net developer trying to port your code to c#, asp.net. I am having trouble in understanding the following part of the code located in file payment.php:

    for($index=0;;$index++)
    if(array_key_exists(!$index?’item_name’:”item_name$index”,$transaction))
    {
    database_query(‘INSERT INTO `%sitems` (`txn_id`,`item_sno`,`item_name`,`item_number`,`quantity`,`mc_gross`,`mc_handling`,`mc_shipping`,`tax`)
    VALUES (“%s” ,”%s” ,”%s” ,%s ,”%s” ,”%s” ,%s ,%s ,%s );’,
    PAYMENT_DATABASE_TABLE_PREFIX,mysql_escape($transaction[‘txn_id’]),$index,mysql_escape($transaction[!$index?’item_name’:”item_name$index”]),mysql_quote($transaction[!$index?’item_number’:”item_number$index”]),mysql_escape($transaction[!$index?’quantity’:”quantity$index”]),mysql_escape($transaction[!$index?’mc_gross’:”mc_gross_$index”]),mysql_quote($transaction[!$index?’mc_handling’:”mc_handling$index”]),mysql_quote($transaction[!$index?’mc_shipping’:”mc_shipping$index”]),mysql_quote($transaction[!$index?’tax’:”tax$index”]));
    }
    else if($index!=0) break;

    Can you please let me know in pseudocode what this peice of code does?

  2. Kristian Kraljic says:

    @rajesh, generally a transaction may include several “items”, for example if you process a shopping cart using PayPal. You will get the information for all of the items in the query string. Unfortionately they havn’t numbered the names consistently. Therefore the first item will be labled with “item_name”, the second one with “item_name1” and so on. This piece of code takes care of the index handling. You can rewrite it to:

    if(array_key_exists(’item_name’)) [... INSERT and so on]
    for($index=1;;$index++)
    if(array_key_exists(’item_name’+$index)) [... INSERT]

    Hope this helps. Regards, Kristian

  3. chirag says:

    I downloaded your script and also read readme file, but unfortunately could not find a way to integrate the script with my requirement. Here is my requirement.

    I have a php script with user accounts. I want a user to buy points by paying some dollars using paypal. For example, user can buy 40 points by paying 40$ through paypal. I am sure i can initiate a payment using your script but dont know how.

    For example, i have a page where there is button called “buy now” which refferes to buy 40 points at 40$. Clicking on the button should initiate the paypal payment transaction using your script (if i am not wrong). and then using your payment_process() call back function I can handle the recharge of their user accounts with 40 points. So the question is how do I initiate paypal transaction with your script??? Can you please explain in a little more details.

    Thanks,
    Chirag.

    • Kristian Kraljic says:

      Hello Chirag,

      Thanks for commening on this article. Please see this link to PayPal first. You will get an introduction how IPN works. IPN notifies you, as soon as any PayPal transaction using your business account was completed / processed. This means the whole payment process is done at PayPal side (and has nothing to do with this script). As soon as the payment was processed PayPal will do a callback (for example to this script), to process the payment.

      This callback should handle all values returned by PayPal, validate the transaction and so on. This script handles this part for you. The rest (how to set up your shop, etc.) needs to be handled by you.

      Hope this helps. If you have any further questions feel free to contact me. Regards, Kristian

  4. Alberto says:

    Sir all i can say to you is, THANK YOU from the bottom of my heart this script is just perfect for my needs, i’m a veteran programmer that recently got a gig for a website and paypal was never my forte.

  5. DK says:

    Greeting,
    I am having problem with my current paypal integration. Suppose from my site user click on pay via paypal button . Site redirect to paypal site ..after payment ..before redirecting browser will down and closed.. how to handle / verify payment is received or not? how the user will know the order is place or not.

    Thanks in advance.
    Please suggest.

  6. Get the hang of these exercises thoսgh and you ѡill have billboard worthy buns іn no timе at all.
    But don’t expect it іs easy, if you want goоd resսlts
    with it, you are gоing tօ have to work harⅾ.
    This is ԝhere you will turn into a fat-burning calorie-destroying machine.