Step 4. Handle callback

Prepare for check callback data

We have a eZPaymentCallbackChecker class that can perform common checks. But we need to additional checks which are specific for paypal payment system. Lets create our checker class eZPaypalChecker. Add these lines to 'ezpaypalchecker.php':


include_once( 'kernel/shop/classes/ezpaymentcallbackchecker.php' );

class eZPaypalChecker extends eZPaymentCallbackChecker
{
    function eZPaypalChecker( $iniFile )
    {
        $this->eZPaymentCallbackChecker( $iniFile );
    }
}

Now we have to add checks specific to paypal.
requestValidation function ensure that callback was received from paypal site.


    function requestValidation()
    {
        $server     = $this->ini->variable( 'ServerSettings', 'ServerName');
        $serverPort = 80;
        $requestURI = $this->ini->variable( 'ServerSettings', 'RequestURI');
        $request    = $this->buildRequestString();
        $response   = $this->sendPOSTRequest( $server, $serverPort, $requestURI, $request);

        if( $response && strcasecmp( $response, 'VERIFIED' ) == 0 )
        {
            return true;
        }
        return false;
    }

buildRequestString prepares request string for requestValidation.


    function &buildRequestString()
    {
        $request = "cmd=_notify-validate";
        foreach( $this->callbackData as $key => $value )
        {
            $request .= "&$key=".urlencode( $value );
        }
        return $request;
    }

handleResponse handles response from server after sendPOSTRequest was called.


    function &handleResponse( &$socket )
    {
        if( $socket )
        {
            while ( !feof( $socket ) )
            {
                $response = fgets ( $socket, 1024 );
            }
      
            fclose( $socket );
            return $response;
        }
        return null;
    }

checkPaymentStatus ensures that payment was complete.


    function checkPaymentStatus()
    {
        if( $this->checkDataField( 'payment_status', 'Completed' ) )
        {
            return true;
        }
        return false;
    }

We are done with eZPaypalChecker.

Handle callback

During creation of eZPaypalGateway class, we specified that our callback handler will be named notify_url (see field notify_url in createRedirectionURL function). Lets make a body for it. Add lines below to notify_url.php:


    ext_activate( 'ezpaypal', 'classes/ezpaypalchecker.php' );
    
    $checker =& new eZPaypalChecker( 'paypal.ini' );
    if( $checker->createDataFromPOST() )
    {
      unset ($_POST);                     
      if( $checker->requestValidation() && $checker->checkPaymentStatus() )
      {
          $orderID = $checker->getFieldValue( 'custom' );
          if( $checker->setupOrderAndPaymentObject( $orderID ) )
          {
              $amount   = $checker->getFieldValue( 'mc_gross' );
              $currency = $checker->getFieldValue( 'mc_currency' );
              if( $checker->checkAmount( $amount ) && $checker->checkCurrency( $currency ) )
              {
                  $checker->approvePayment();
              }
          }
      }
    }

Also we need this in module.php:


    $Module = array( "name" => "eZPaypal" );

    $ViewList = array();
    $ViewList["notify_url"] = array(
        "script" => "notify_url.php" );

And this in site.ini.append (allows anonymous user access to notify_url):


    [RoleSettings]
    PolicyOmitList[]=paypal/notify_url

That is all.