eZ Publish  [4.0]
ezpaymentgatewaytype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZPaymentGatewayType class
00004 //
00005 // Created on: <18-Jul-2004 14:18:58 dl>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezpaymentgatewaytype.php
00032 */
00033 
00034 /*!
00035   \class eZPaymentGatewayType ezpaymentgatewaytype.php
00036   \brief Interface for different types of payment gateways.
00037 
00038   Allows use multiple payment gateways in workflow.
00039   Allows user to choose necessary gateway type 'on the fly'.
00040 */
00041 
00042 //include_once( 'kernel/classes/ezworkflowtype.php' );
00043 
00044 //include_once( 'kernel/classes/workflowtypes/event/ezpaymentgateway/ezpaymentlogger.php' );
00045 
00046 class eZPaymentGatewayType extends eZWorkflowEventType
00047 {
00048     const WORKFLOW_TYPE_STRING = 'ezpaymentgateway';
00049     const GATEWAY_NOT_SELECTED = 0;
00050     const GATEWAY_SELECTED = 1;
00051 
00052     /*!
00053     Constructor.
00054     */
00055 
00056     function eZPaymentGatewayType()
00057     {
00058         $this->logger   = eZPaymentLogger::CreateForAdd( "var/log/eZPaymentGatewayType.log" );
00059 
00060         $this->eZWorkflowEventType( eZPaymentGatewayType::WORKFLOW_TYPE_STRING, ezi18n( 'kernel/workflow/event', "Payment Gateway" ) );
00061         $this->loadAndRegisterGateways();
00062     }
00063 
00064     /*!
00065     Creates necessary gateway and delegate execution to it.
00066     If there are multiple gateways in eZPaymentGatewayType, fetches
00067     template with list of 'selected'(see. 'attributes' section)
00068     gateways and asks user to choose one.
00069     */
00070 
00071     function execute( $process, $event )
00072     {
00073         $this->logger->writeTimedString( 'execute' );
00074 
00075         if( $process->attribute( 'event_state' ) == eZPaymentGatewayType::GATEWAY_NOT_SELECTED )
00076         {
00077             $this->logger->writeTimedString( 'execute: eZPaymentGatewayType::GATEWAY_NOT_SELECTED' );
00078 
00079             $process->setAttribute( 'event_state', eZPaymentGatewayType::GATEWAY_SELECTED );
00080             if ( !$this->selectGateway( $event ) )
00081             {
00082                 $process->Template = array();
00083                 $process->Template['templateName'] = 'design:workflow/selectgateway.tpl';
00084                 $process->Template['templateVars'] = array ( 'event' => $event );
00085 
00086                 return eZWorkflowType::STATUS_FETCH_TEMPLATE_REPEAT;
00087             }
00088         }
00089 
00090         $theGateway = $this->getCurrentGateway( $event );
00091         if( $theGateway != null )
00092         {
00093             return $theGateway->execute( $process, $event );
00094         }
00095 
00096         $this->logger->writeTimedString( 'execute: something wrong' );
00097         return eZWorkflowType::STATUS_REJECTED;
00098     }
00099 
00100     /*!
00101     Attributes. There are three types of gateways in eZPaymentGatewayType.
00102     'Available' gateways - gateways that were installed in the eZPublish
00103                    (as extensions, build-in);
00104     'Selected' gateways  - gateways that were selected for this instance of
00105                    eZPaymentGatewayType;
00106     'Current' gateway    - through this gateway payment will be made.
00107     */
00108 
00109     function attributeDecoder( $event, $attr )
00110     {
00111         switch ( $attr )
00112         {
00113             case 'selected_gateways_types':
00114             {
00115                 return explode( ',', $event->attribute( 'data_text1' ) );
00116             }
00117             break;
00118 
00119             case 'selected_gateways':
00120             {
00121                 $selectedGatewaysTypes  = explode( ',', $event->attribute( 'data_text1' ) );
00122                 return $this->getGateways( $selectedGatewaysTypes );
00123             }break;
00124 
00125             case 'current_gateway':
00126             {
00127                 return $event->attribute( 'data_text2' );
00128             }
00129             break;
00130         }
00131         return null;
00132     }
00133 
00134     function typeFunctionalAttributes( )
00135     {
00136         return array( 'selected_gateways_types', 'selected_gateways', 'current_gateway' );
00137     }
00138 
00139     function attributes()
00140     {
00141         return array_merge( array( 'available_gateways' ),
00142                             eZWorkflowEventType::attributes() );
00143     }
00144 
00145     function hasAttribute( $attr )
00146     {
00147         return in_array( $attr, $this->attributes() );
00148     }
00149 
00150     function attribute( $attr )
00151     {
00152         switch( $attr )
00153         {
00154             case 'available_gateways':
00155             {
00156                 return $this->getGateways( array( -1 ) );
00157             }break;
00158         }
00159         return eZWorkflowEventType::attribute( $attr );
00160     }
00161 
00162     /*!
00163      \static
00164     Searches 'available' gateways( built-in or as extensions ).
00165     */
00166 
00167     function loadAndRegisterGateways()
00168     {
00169         eZPaymentGatewayType::loadAndRegisterBuiltInGateways();
00170         eZPaymentGatewayType::loadAndRegisterExtensionGateways();
00171     }
00172 
00173     /*!
00174       \static
00175     */
00176     function loadAndRegisterBuiltInGateways()
00177     {
00178         $gatewaysINI        = eZINI::instance( 'paymentgateways.ini' );
00179         $gatewaysTypes      = $gatewaysINI->variable( 'GatewaysSettings', 'AvailableGateways' );
00180         $gatewaysDir        = false;
00181 
00182         // GatewaysDirectories was spelt as GatewaysDerictories, which is
00183         // confusing for people writing ini files - it's a typo.
00184         if ( $gatewaysINI->hasVariable( 'GatewaysSettings', 'GatewaysDerictories' ) )
00185             $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDerictories' );
00186         else
00187             $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDirectories' );
00188 
00189         if ( is_array( $gatewaysDir ) && is_array( $gatewaysTypes ) )
00190         {
00191             foreach( $gatewaysDir as $dir )
00192             {
00193                 foreach( $gatewaysTypes as $gateway )
00194                 {
00195                     $gatewayPath = "$dir/$gateway/classes/" . $gateway . 'gateway.php';
00196                     if( file_exists( $gatewayPath ) )
00197                     {
00198                         include_once( $gatewayPath );
00199                     }
00200                 }
00201             }
00202         }
00203     }
00204 
00205     /*!
00206       \static
00207     */
00208     function loadAndRegisterExtensionGateways()
00209     {
00210         $gatewaysINI        = eZINI::instance( 'paymentgateways.ini' );
00211         $siteINI            = eZINI::instance( 'site.ini' );
00212         $extensionDirectory = $siteINI->variable( 'ExtensionSettings', 'ExtensionDirectory' );
00213         $activeExtensions   = eZExtension::activeExtensions();
00214 
00215         foreach ( $activeExtensions as $extension )
00216         {
00217             $gatewayPath = "$extensionDirectory/$extension/classes/" . $extension . 'gateway.php';
00218             if ( file_exists( $gatewayPath ) )
00219             {
00220                 include_once( $gatewayPath );
00221             }
00222         }
00223     }
00224 
00225     /*!
00226     Each gateway must call this function to become 'available'.
00227     */
00228 
00229     function registerGateway( $gateway, $class_name, $description )
00230     {
00231         $gateways =& $GLOBALS["eZPaymentGateways"];
00232         if ( !is_array( $gateways ) )
00233         {
00234             $gateways = array();
00235         }
00236 
00237         if ( isset( $gateways[$gateway] ) )
00238         {
00239             eZDebug::writeError( "Gateway already registered: $gateway", "eZPaymentGatewayType::registerGateway" );
00240         }
00241         else
00242         {
00243             $gateways[$gateway] = array( "class_name" => $class_name, "description" => $description );
00244         }
00245     }
00246 
00247     /*!
00248     Returns an array of gateways difinitions( class_name, description ) by
00249     'gatewaysTypes'( array of 'gateway' values that were passed to
00250     'registerGateway' function).
00251     */
00252     function getGateways( $gatewaysTypes )
00253     {
00254         $gateways           = array();
00255         $availableGateways  = $GLOBALS[ 'eZPaymentGateways' ];
00256         if ( !is_array( $availableGateways ) ){
00257             return $gateways;
00258         }
00259 
00260         if ( in_array( '-1', $gatewaysTypes ) )
00261         {
00262             $gatewaysTypes  = array_keys( $availableGateways );
00263         }
00264 
00265         foreach ( $gatewaysTypes as $key )
00266         {
00267             $gateway = $availableGateways[$key];
00268 
00269             $gateway['Name']    = $gateway['description'];
00270             $gateway['value']   = $key;
00271             $gateways[] = $gateway;
00272         }
00273 
00274         return $gateways;
00275     }
00276 
00277     /*!
00278     Creates and returns object of eZPaymentGateway subclass.
00279     */
00280 
00281     function createGateway( $inGatewayType )
00282     {
00283         $gateway_difinition = $GLOBALS[ 'eZPaymentGateways' ][ $inGatewayType ];
00284 
00285         $this->logger->writeTimedString( $gateway_difinition, "createGateway. gateway_difinition" );
00286 
00287         if( $gateway_difinition )
00288         {
00289             $class_name = $gateway_difinition[ 'class_name' ];
00290             return new $class_name();
00291         }
00292 
00293         return null;
00294     }
00295 
00296     /*!
00297     Returns 'current' gateway.
00298     */
00299 
00300     function getCurrentGateway( $event )
00301     {
00302         $theGateway  = null;
00303         $gatewayType = $this->getCurrentGatewayType( $event );
00304 
00305         if( $gatewayType != null )
00306         {
00307             $theGateway = $this->createGateway( $gatewayType );
00308         }
00309 
00310         return $theGateway;
00311     }
00312 
00313     /*!
00314     Returns 'current' gatewaytype.
00315     */
00316 
00317     function getCurrentGatewayType( $event )
00318     {
00319         $gateway =  null;
00320         $http    = eZHTTPTool::instance();
00321 
00322         if ( $http->hasPostVariable( 'SelectButton' ) && $http->hasPostVariable( 'SelectedGateway' ) )
00323         {
00324             $gateway = $http->postVariable( 'SelectedGateway' );
00325             $event->setAttribute( 'data_text2', $gateway );
00326             $event->store();
00327         }
00328         else if ( $http->hasPostVariable( 'CancelButton' ) )
00329         {
00330             $gateway = null;
00331         }
00332         else
00333         {
00334             $gateway = $event->attribute( 'current_gateway' );
00335         }
00336 
00337         return $gateway;
00338     }
00339 
00340     /*!
00341     Sets 'current' gateway from 'selected' gateways. If 'selected' is just one,
00342     it becomes 'current'. Else user have to choose some( appropriate template
00343     will be shown).
00344     */
00345 
00346     function selectGateway( $event )
00347     {
00348         $selectedGatewaysTypes  = explode( ',', $event->attribute( 'data_text1' ) );
00349 
00350         if ( count( $selectedGatewaysTypes ) == 1 && $selectedGatewaysTypes[0] != -1 )
00351         {
00352             $event->setAttribute( 'data_text2', $selectedGatewaysTypes[0] );
00353             $event->store();
00354 
00355             $this->logger->writeTimedString( $selectedGatewaysTypes[0], 'selectGateway' );
00356             return true;
00357         }
00358 
00359         $this->logger->writeTimedString( 'selectGateways. multiple gateways, let user choose.' );
00360         return false;
00361     }
00362 
00363     function needCleanup()
00364     {
00365         return true;
00366     }
00367 
00368     /*!
00369     Delegate to eZPaymentGateway subclass.
00370     */
00371 
00372     function cleanup( $process, $event )
00373     {
00374         $theGateway = $this->getCurrentGateway( $event );
00375         if( $theGateway != null and $theGateway->needCleanup() )
00376         {
00377             $theGateway->cleanup( $process, $event );
00378         }
00379     }
00380 
00381     function initializeEvent( $event )
00382     {
00383     }
00384 
00385     /*!
00386     Sets 'selected' gateways. -1 means 'Any' - all 'available' gateways
00387     becomes 'selected'.
00388     */
00389 
00390     function fetchHTTPInput( $http, $base, $event )
00391     {
00392         $gatewaysVar = $base . "_event_ezpaymentgateway_gateways_" . $event->attribute( "id" );
00393         if ( $http->hasPostVariable( $gatewaysVar ) )
00394         {
00395             $gatewaysArray = $http->postVariable( $gatewaysVar );
00396             if ( in_array( '-1', $gatewaysArray ) )
00397             {
00398                 $gatewaysArray = array( -1 );
00399             }
00400 
00401             $gatewaysString = implode( ',', $gatewaysArray );
00402             $event->setAttribute( "data_text1", $gatewaysString );
00403         }
00404     }
00405 
00406     public $logger;
00407 }
00408 
00409 eZWorkflowEventType::registerEventType( eZPaymentGatewayType::WORKFLOW_TYPE_STRING, 'ezpaymentgatewaytype' );
00410 ?>