Tag Archive for: ipn

Easy PayPal IPN

30 Apr
April 30, 2010

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.

ipn.class.php
ipn.interface.php

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:

<?php

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 )
	{
		ob_start();
		print_r( $paypal_post );
		$post = ob_get_contents();
		ob_end_clean();

		$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 );
$ipn->start();

?>

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:

Array
(
    [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] => buyer@paypalsandbox.com
    [payer_id] => TESTBUYERID01
    [business] => seller@paypalsandbox.com
    [receiver_email] => seller@paypalsandbox.com
    [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