Location: PHPKode > scripts > Paypal Instant Payment Notification > paypal-instant-payment-notification/example.php
<?php

//
// Edit History:
//
//  Last $Author: munroe $
//  Last Modified: $Date: 2006/03/25 11:43:02 $
//
//  Dick Munroe (hide@address.com) 28-Feb-2006
//      Initial version created
//
//  Dick Munroe (hide@address.com) 14-Mar-2006
//      Add alternatePaymentStatus method.
//
//	Dick Munroe (hide@address.com) 25-Mar-2006
//		Put in a notify_url field into the button so that
//		we get back to this script from the sandbox.
//

/**
 * @author Dick Munroe <hide@address.com>
 * @copyright copyright @ 2006 by Dick Munroe, Cottage Software Works, Inc.
 * @license http://www.csworks.com/publications/ModifiedNetBSD.html
 * @version 1.0.2
 * @package dm.paypal
 * @example ./example.php
 *
 * Is an example of how to write Paypal IPN processors using the classes provided
 * by dm.paypal from phpclasses.org.  The overall process is pretty simple.
 *
 * 1. Create a class that extends paypalIPNBase.
 * 2. Override the following methods:
 *
 * 		A. preprocess - called before any $_POST processing has occurred.
 *      B. [optional] sourceNotEmpty - called if there is any data left in $_POST after extracting Paypal IPN data.
 *      C. alternatePaymentStatus - called if the IPN is not a completed payment.
 *      D. checkTransactionId - called to determine if the transaction Id is unique in your application.
 *      E. processPayment - called when the payment has been fully verified.
 *      F. postprocess - called after any/all processing has occurred.
 *
 * 3. Create your Purchasing environment.
 * 4. Configure Paypal to invoke your IPN page.
 * 5. Test, test, test.
 */

include_once("class.paypalIPNBase.php") ;
include_once("SDD/class.SDD.php") ;

/*
 * An example class to process IPNs in the context of the virtual_payment.php
 * invocation.
 */

class examplePaypalIPN extends paypalIPNBase
{
    var $m_localEmail = "" ;
    var $m_message = "" ;

    function examplePaypalIPN($theLocalEmail, $theReceiverEmail, $thePaypalURL = NULL, $theSandboxURL = NULL)
    {
	$this->m_localEmail = $theLocalEmail ;

	$this->paypalIPNBase($theReceiverEmail, $thePaypalURL, $theSandboxURL) ;
    }

    function preprocess(&$theSource)
    {
	$this->m_message .= "<pre>\n" ;
	$this->m_message .= "preprocess: \n\n" ;
	$this->m_message .= "The Source: \n" ;
	$this->m_message .= SDD::dump($theSource, FALSE) ;
	$this->m_message .= "\n" ;
    }

    function postprocess(&$theIPN, $theStatus)
    {
	$this->m_message .= "postprocess: \n\n" ;
	$this->m_message .= "The IPN: \n" ;
	$this->m_message .= SDD::dump($theIPN, FALSE) ;
	$this->m_message .= "\nThe Status: \n" ;
	$this->m_message .= SDD::dump($theStatus, FALSE) ;
	$this->m_message .= "</pre>\n" ;

	if ($this->m_localEmail != '')
	{
	    mail($this->m_localEmail, "dm.paypal debugging", $this->m_message) ;
	}
	else
	{
	    $f = fopen("./log/message.html", "w") ;
	    fwrite($f, $this->m_message) ;
	    fclose($f) ;
	}
    }

    function sourceNotEmpty(&$theIPN, &$theSource)
    {
	$this->m_message .= "sourceNotEmpty: \n\n" ;
	$this->m_message .= "The IPN: \n" ;
	$this->m_message .= SDD::dump($theIPN, FALSE) ;
	$this->m_message .= "\nThe Source: \n\n" ;
	$this->m_message .= SDD::dump($theSource, FALSE) ;
	$this->m_message .= "\n" ;
	$theReturnStatus = parent::sourceNotEmpty($theIPN, $theSource) ;
	$this->m_message .= "\nThe Status: \n\n" ;
	$this->m_message .= SDD::dump($theReturnStatus, FALSE) ;
	$this->m_message .= "\n" ;
	return $theReturnStatus ;
    }
    
    function alternatePaymentStatus(&$theIPN)
    {
    	$this->m_message .= "alternatePaymentStatus: \n\n" ;
    	$this->m_message .= "The IPN: \n" ;
    	$this->m_message .= SDD::dump($theIPN, FALSE) ;
    	$this->m_message .= "\n" ;

        /*
         * Indicate that the alternate payment status has been successfully
         * processed.
         */
         
        return true ;
    }

    /**
     * This is not normally overridden.  I do so here to provide additional information
     * in the context of the example.
     */

    function httpPost ($url, &$theIPN)
    {
	$this->m_message .= "httpPost: \n\n" ;
	$theReturnStatus = parent::httpPost($url, $theIPN) ;
	$this->m_message .= "The Curl: \n\n" ;
	$this->m_message .= SDD::dump($this->m_curl, FALSE) ;
	$this->m_message .= "\n" ;
	$this->m_message .= "The Status: \n\n" ;
	$this->m_message .= SDD::dump($theReturnStatus, FALSE) ;
	$this->m_message .= "\n" ;
	return $theReturnStatus ;
    }

    function checkTransactionId($theTransactionId)
    {
	$this->m_message .= "checkTransactionId: \n\n" ;
	$this->m_message .= "The Transaction Id: \n" ;
	$this->m_message .= SDD::dump($theTransactionId, FALSE) ;
	$this->m_message .= "\n" ;
	return true ;
    }

    function processPayment(&$theIPN)
    {
	$this->m_message .= "processPayment: \n\n" ;
	$this->m_message .= "The IPN: \n" ;
	$this->m_message .= SDD::dump($theIPN, FALSE) ;
	$this->m_message .= "\n" ;
	return true ;
    }
}

if(count($_POST)>0)
{

    /*
     * Since this is a test environment, always use the receiver email provided from Paypal, and
     * write output to a log file (./log/message.html).  If you wish to have email sent to an
     * account instead, provide your email address as the first argument when creating the
     * example Paypal IPN object.
     */

    $ipn = new examplePaypalIPN("", $_POST['receiver_email']) ;
    $theStatus = $ipn->processNotification($_POST) ;
}
else
{
?>
<pre>
<!--
    This button was generated by the paypal sandbox and is specific to your particular site.  Generate a new one
    using the sandbox tools for your site and insert the html here.
-->

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="hide@address.com">
<input type="hidden" name="item_name" value="Test Purchase">
<input type="hidden" name="item_number" value="000001">
<input type="hidden" name="amount" value="10.00">
<input type="hidden" name="notify_url" value="<? echo $_SERVER['SCRIPT_URI'] ;?>">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
<pre>
	<?php
}

?>
Return current item: Paypal Instant Payment Notification