Easy PayPal IPN

IMPORTANT UPDATE: I’ve since updated the code substantially, please read my new blog post.

[divider type=”dashed” spacing=”10″]

Many of you should be familiar with PayPal and the shopping cart services they offer, namely PDT and IPN. IPN is the behind the scenes system that most shopping cart systems use, and if you are developing your own shopping cart system (like me), the IPN code may be a bit complicated. To allow you to head straight to the actual coding, I created an IPN class for your use!

Simply create a directory, I recommend using 8 random characters. For instance, http://cart.mysite.com/5a6jafg3/. Then copy the following ipn.class.php and ipn.interface.php files into that folder.


Now, all you need to do is create an ipn.php file (or whatever you want to call it) and place code similar to this in it:


include( 'ipn.class.php' );
include( 'ipn.interface.php' );    

class ipn_class extends paypal_ipn_class implements paypal_ipn_interface
	public function _validate_success( $paypal_post )
		print_r( $paypal_post );
		$post = ob_get_contents();

		$fh = fopen( 'ipns/' . time() . '.txt' , 'w' ) or die( 'can\'t open file' );
		fwrite( $fh , $post );
		fclose( $fh );


$ipn = new ipn_class();
$ipn->set_sandbox_mode( true );


Obviously substituting your own code in. This simply writes all IPN transactions to a text file in the current directory, which isn’t all that useful. Instead, you could check if the payment is complete, and send them an e-product or email your site owner letting them know to ship some merchandise, insert the data into a database, etc.

Transaction data will be stored in $paypal_post and will look like this:

    [test_ipn] => 1
    [payment_type] => instant
    [payment_date] => 19:21:43 Apr 30, 2010 PDT
    [payment_status] => Completed
    [payer_status] => verified
    [first_name] => John
    [last_name] => Smith
    [payer_email] => [email protected]
    [payer_id] => TESTBUYERID01
    [business] => [email protected]
    [receiver_email] => [email protected]
    [receiver_id] => TESTSELLERID1
    [residence_country] => US
    [item_name1] => something
    [item_number1] => AK-1234
    [quantity1] => 1
    [tax] => 2.02
    [mc_currency] => USD
    [mc_fee] => 0.44
    [mc_gross] => 15.34
    [mc_gross_1] => 12.34
    [mc_handling] => 2.06
    [mc_handling1] => 1.67
    [mc_shipping] => 3.02
    [mc_shipping1] => 1.02
    [txn_type] => cart
    [txn_id] => 4351221
    [notify_version] => 2.4
    [custom] => xyz123
    [invoice] => abc1234
    [charset] => windows-1252
    [verify_sign] => AvBLd0otomBysn1PCGFhV3R-Fyu9A2cxK6AwN3rrtYd.JagqsnR3.KC1

Use PayPal Sandbox for testing

3 Replies to “Easy PayPal IPN”

  1. […] long time ago, I wrote a few PHP scripts to help me work with PayPal IPN & PDT requests. I’ve been using them in my projects ever […]

  2. […] isn’t my best example, but you could always take a look at my PayPal IPN class or similar project. I do go on the heavy side of commenting, but only because I want novice […]

  3. […] and the shopping cart services they offer, namely PDT and IPN. I’ve already dealt with IPN in this post, so now it’s time for PDT. PDT allows visitors to return to your site after a transaction and […]

Leave a Reply