[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/workflowtypes/event/ezpaymentgateway/ -> ezpaymentgatewaytype.php (source)

   1  <?php
   2  //
   3  // Definition of eZPaymentGatewayType class
   4  //
   5  // Created on: <18-Jul-2004 14:18:58 dl>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezpaymentgatewaytype.php
  30  */
  31  
  32  /*!
  33    \class eZPaymentGatewayType ezpaymentgatewaytype.php
  34    \brief Interface for different types of payment gateways.
  35  
  36    Allows use multiple payment gateways in workflow.
  37    Allows user to choose necessary gateway type 'on the fly'.
  38  */
  39  
  40  include_once ( 'kernel/classes/ezworkflowtype.php' );
  41  
  42  define( 'EZ_WORKFLOW_TYPE_PAYMENTGATEWAY_ID', 'ezpaymentgateway' );
  43  define( 'EZ_PAYMENT_GATEWAY_GATEWAY_NOT_SELECTED', 0 );
  44  define( 'EZ_PAYMENT_GATEWAY_GATEWAY_SELECTED', 1 );
  45  
  46  include_once ( 'kernel/classes/workflowtypes/event/ezpaymentgateway/ezpaymentlogger.php' );
  47  
  48  class eZPaymentGatewayType extends eZWorkflowEventType
  49  {
  50      /*!
  51      Constructor.
  52      */
  53  
  54      function eZPaymentGatewayType()
  55      {
  56          $this->logger   = eZPaymentLogger::CreateForAdd( "var/log/eZPaymentGatewayType.log" );
  57  
  58          $this->eZWorkflowEventType( EZ_WORKFLOW_TYPE_PAYMENTGATEWAY_ID, ezi18n( 'kernel/workflow/event', "Payment Gateway" ) );
  59          $this->loadAndRegisterGateways();
  60      }
  61  
  62      /*!
  63      Creates necessary gateway and delegate execution to it.
  64      If there are multiple gateways in eZPaymentGatewayType, fetches
  65      template with list of 'selected'(see. 'attributes' section)
  66      gateways and asks user to choose one.
  67      */
  68  
  69      function execute( &$process, &$event )
  70      {
  71          $this->logger->writeTimedString( 'execute' );
  72  
  73          if( $process->attribute( 'event_state' ) == EZ_PAYMENT_GATEWAY_GATEWAY_NOT_SELECTED )
  74          {
  75              $this->logger->writeTimedString( 'execute: EZ_PAYMENT_GATEWAY_GATEWAY_NOT_SELECTED' );
  76  
  77              $process->setAttribute( 'event_state', EZ_PAYMENT_GATEWAY_GATEWAY_SELECTED );
  78              if ( !$this->selectGateway( $event ) )
  79              {
  80                  $process->Template = array();
  81                  $process->Template['templateName'] = 'design:workflow/selectgateway.tpl';
  82                  $process->Template['templateVars'] = array ( 'event' => $event );
  83  
  84                  return EZ_WORKFLOW_TYPE_STATUS_FETCH_TEMPLATE_REPEAT;
  85              }
  86          }
  87  
  88          $theGateway = $this->getCurrentGateway( $event );
  89          if( $theGateway != null )
  90          {
  91              return $theGateway->execute( $process, $event );
  92          }
  93  
  94          $this->logger->writeTimedString( 'execute: something wrong' );
  95          return EZ_WORKFLOW_TYPE_STATUS_REJECTED;
  96      }
  97  
  98      /*!
  99      Attributes. There are three types of gateways in eZPaymentGatewayType.
 100      'Available' gateways - gateways that were installed in the eZPublish
 101                     (as extensions, build-in);
 102      'Selected' gateways  - gateways that were selected for this instance of
 103                     eZPaymentGatewayType;
 104      'Current' gateway    - through this gateway payment will be made.
 105      */
 106  
 107      function &attributeDecoder( &$event, $attr )
 108      {
 109          switch ( $attr )
 110          {
 111              case 'selected_gateways_types':
 112              {
 113                  $selectedGatewaysTypes      = explode( ',', $event->attribute( 'data_text1' ) );
 114                  return $selectedGatewaysTypes;
 115              }
 116              break;
 117  
 118              case 'selected_gateways':
 119              {
 120                  $selectedGatewaysTypes  = explode( ',', $event->attribute( 'data_text1' ) );
 121                  return $this->getGateways( $selectedGatewaysTypes );
 122              }break;
 123  
 124              case 'current_gateway':
 125              {
 126                  $gateway = $event->attribute( 'data_text2' );
 127                  return $gateway;
 128              }
 129              break;
 130          }
 131          $retValue = null;
 132          return $retValue;
 133      }
 134  
 135      function typeFunctionalAttributes( )
 136      {
 137          return array( 'selected_gateways_types', 'selected_gateways', 'current_gateway' );
 138      }
 139  
 140      function attributes()
 141      {
 142          return array_merge( array( 'available_gateways' ),
 143                              eZWorkflowEventType::attributes() );
 144      }
 145  
 146      function hasAttribute( $attr )
 147      {
 148          return in_array( $attr, $this->attributes() );
 149      }
 150  
 151      function &attribute( $attr )
 152      {
 153          switch( $attr )
 154          {
 155              case 'available_gateways':
 156              {
 157                  $gateways =& $this->getGateways( array( -1 ) );
 158                  return $gateways;
 159              }break;
 160          }
 161          return eZWorkflowEventType::attribute( $attr );
 162      }
 163  
 164      /*!
 165       \static
 166      Searches 'available' gateways( built-in or as extensions ).
 167      */
 168  
 169      function loadAndRegisterGateways()
 170      {
 171          eZPaymentGatewayType::loadAndRegisterBuiltInGateways();
 172          eZPaymentGatewayType::loadAndRegisterExtensionGateways();
 173      }
 174  
 175      /*!
 176        \static
 177      */
 178      function loadAndRegisterBuiltInGateways()
 179      {
 180          $gatewaysINI        =& eZINI::instance( 'paymentgateways.ini' );
 181          $gatewaysTypes      = $gatewaysINI->variable( 'GatewaysSettings', 'AvailableGateways' );
 182          $gatewaysDir        = false;
 183  
 184          // GatewaysDirectories was spelt as GatewaysDerictories, which is
 185          // confusing for people writing ini files - it's a typo.
 186          if ( $gatewaysINI->hasVariable( 'GatewaysSettings', 'GatewaysDerictories' ) )
 187              $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDerictories' );
 188          else
 189              $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDirectories' );
 190  
 191          if ( is_array( $gatewaysDir ) && is_array( $gatewaysTypes ) )
 192          {
 193              foreach( $gatewaysDir as $dir )
 194              {
 195                  foreach( $gatewaysTypes as $gateway )
 196                  {
 197                      $gatewayPath = "$dir/$gateway/classes/" . $gateway . 'gateway.php';
 198                      if( file_exists( $gatewayPath ) )
 199                      {
 200                          include_once( $gatewayPath );
 201                      }
 202                  }
 203              }
 204          }
 205      }
 206  
 207      /*!
 208        \static
 209      */
 210      function loadAndRegisterExtensionGateways()
 211      {
 212          $gatewaysINI        =& eZINI::instance( 'paymentgateways.ini' );
 213          $siteINI            =& eZINI::instance( 'site.ini' );
 214          $extensionDirectory = $siteINI->variable( 'ExtensionSettings', 'ExtensionDirectory' );
 215          $activeExtensions   = eZExtension::activeExtensions();
 216  
 217          foreach ( $activeExtensions as $extension )
 218          {
 219              $gatewayPath = "$extensionDirectory/$extension/classes/" . $extension . 'gateway.php';
 220              if ( file_exists( $gatewayPath ) )
 221              {
 222                  include_once( $gatewayPath );
 223              }
 224          }
 225      }
 226  
 227      /*!
 228      Each gateway must call this function to become 'available'.
 229      */
 230  
 231      function registerGateway( $gateway, $class_name, $description )
 232      {
 233          $gateways =& $GLOBALS["eZPaymentGateways"];
 234          if ( !is_array( $gateways ) )
 235              $gateways = array();
 236          if ( isset( $gateways[$gateway] ) )
 237          {
 238              eZDebug::writeError( "Gateway already registered: $gateway", "eZPaymentGatewayType::registerGateway" );
 239          }
 240          else
 241          {
 242              $gateways[$gateway] = array( "class_name" => $class_name, "description" => $description );
 243          }
 244      }
 245  
 246      /*!
 247      Returns an array of gateways difinitions( class_name, description ) by
 248      'gatewaysTypes'( array of 'gateway' values that were passed to
 249      'registerGateway' function).
 250      */
 251      function &getGateways( $gatewaysTypes )
 252      {
 253          $gateways           = array();
 254          $availableGateways  =& $GLOBALS[ 'eZPaymentGateways' ];
 255  
 256          if ( in_array( '-1', $gatewaysTypes ) )
 257          {
 258              $gatewaysTypes  = array_keys( $availableGateways );
 259          }
 260  
 261          foreach ( $gatewaysTypes as $key )
 262          {
 263              $gateway =& $availableGateways[$key];
 264  
 265              $gateway['Name']    = $gateway['description'];
 266              $gateway['value']   = $key;
 267              $gateways[] = $gateway;
 268          }
 269  
 270          return $gateways;
 271      }
 272  
 273      /*!
 274      Creates and returns object of eZPaymentGateway subclass.
 275      */
 276  
 277      function &createGateway( &$inGatewayType )
 278      {
 279          $theGateway         = null;
 280          $gateway_difinition =& $GLOBALS[ 'eZPaymentGateways' ][ $inGatewayType ];
 281  
 282          $this->logger->writeTimedString( $gateway_difinition, "createGateway. gateway_difinition" );
 283  
 284          if( $gateway_difinition )
 285          {
 286              $class_name = $gateway_difinition[ 'class_name' ];
 287              $theGateway = new $class_name();
 288          }
 289  
 290          return $theGateway;
 291      }
 292  
 293      /*!
 294      Returns 'current' gateway.
 295      */
 296  
 297      function &getCurrentGateway( &$event )
 298      {
 299          $theGateway  = null;
 300          $gatewayType = $this->getCurrentGatewayType( $event );
 301  
 302          if( $gatewayType != null )
 303          {
 304              $theGateway = $this->createGateway( $gatewayType );
 305          }
 306  
 307          return $theGateway;
 308      }
 309  
 310      /*!
 311      Returns 'current' gatewaytype.
 312      */
 313  
 314      function &getCurrentGatewayType( &$event )
 315      {
 316          $gateway =  null;
 317          $http    =& eZHTTPTool::instance();
 318  
 319          if ( $http->hasPostVariable( 'SelectButton' ) && $http->hasPostVariable( 'SelectedGateway' ) )
 320          {
 321              $gateway = $http->postVariable( 'SelectedGateway' );
 322              $event->setAttribute( 'data_text2', $gateway );
 323              $event->store();
 324          }
 325          else if ( $http->hasPostVariable( 'CancelButton' ) )
 326          {
 327              $gateway = null;
 328          }
 329          else
 330          {
 331              $gateway = $event->attribute( 'current_gateway' );
 332          }
 333  
 334          return $gateway;
 335      }
 336  
 337      /*!
 338      Sets 'current' gateway from 'selected' gateways. If 'selected' is just one,
 339      it becomes 'current'. Else user have to choose some( appropriate template
 340      will be shown).
 341      */
 342  
 343      function selectGateway( &$event )
 344      {
 345          $selectedGatewaysTypes  = explode( ',', $event->attribute( 'data_text1' ) );
 346  
 347          if ( count( $selectedGatewaysTypes ) == 1 && $selectedGatewaysTypes[0] != -1 )
 348          {
 349              $event->setAttribute( 'data_text2', $selectedGatewaysTypes[0] );
 350              $event->store();
 351  
 352              $this->logger->writeTimedString( $selectedGatewaysTypes[0], 'selectGateway' );
 353              return true;
 354          }
 355  
 356          $this->logger->writeTimedString( 'selectGateways. multiple gateways, let user choose.' );
 357          return false;
 358      }
 359  
 360      function needCleanup()
 361      {
 362          return true;
 363      }
 364  
 365      /*!
 366      Delegate to eZPaymentGateway subclass.
 367      */
 368  
 369      function cleanup( &$process, &$event )
 370      {
 371          $theGateway = $this->getCurrentGateway( $event );
 372          if( $theGateway != null and $theGateway->needCleanup() )
 373          {
 374              $theGateway->cleanup( $process, $event );
 375          }
 376      }
 377  
 378      function initializeEvent( &$event )
 379      {
 380      }
 381  
 382      /*!
 383      Sets 'selected' gateways. -1 means 'Any' - all 'available' gateways
 384      becomes 'selected'.
 385      */
 386  
 387      function fetchHTTPInput( &$http, $base, &$event )
 388      {
 389          $gatewaysVar = $base . "_event_ezpaymentgateway_gateways_" . $event->attribute( "id" );
 390          if ( $http->hasPostVariable( $gatewaysVar ) )
 391          {
 392              $gatewaysArray = $http->postVariable( $gatewaysVar );
 393              if ( in_array( '-1', $gatewaysArray ) )
 394              {
 395                  $gatewaysArray = array( -1 );
 396              }
 397  
 398              $gatewaysString = implode( ',', $gatewaysArray );
 399              $event->setAttribute( "data_text1", $gatewaysString );
 400          }
 401      }
 402  
 403      var $logger;
 404  }
 405  
 406  eZWorkflowEventType::registerType( EZ_WORKFLOW_TYPE_PAYMENTGATEWAY_ID, 'ezpaymentgatewaytype' );
 407  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7