00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 include_once( 'kernel/classes/ezworkflowtype.php' );
00043
00044 define( 'EZ_WORKFLOW_TYPE_PAYMENTGATEWAY_ID', 'ezpaymentgateway' );
00045 define( 'EZ_PAYMENT_GATEWAY_GATEWAY_NOT_SELECTED', 0 );
00046 define( 'EZ_PAYMENT_GATEWAY_GATEWAY_SELECTED', 1 );
00047
00048 include_once( 'kernel/classes/workflowtypes/event/ezpaymentgateway/ezpaymentlogger.php' );
00049
00050 class eZPaymentGatewayType extends eZWorkflowEventType
00051 {
00052
00053
00054
00055
00056 function eZPaymentGatewayType()
00057 {
00058 $this->logger = eZPaymentLogger::CreateForAdd( "var/log/eZPaymentGatewayType.log" );
00059
00060 $this->eZWorkflowEventType( EZ_WORKFLOW_TYPE_PAYMENTGATEWAY_ID, ezi18n( 'kernel/workflow/event', "Payment Gateway" ) );
00061 $this->loadAndRegisterGateways();
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071 function execute( &$process, &$event )
00072 {
00073 $this->logger->writeTimedString( 'execute' );
00074
00075 if( $process->attribute( 'event_state' ) == EZ_PAYMENT_GATEWAY_GATEWAY_NOT_SELECTED )
00076 {
00077 $this->logger->writeTimedString( 'execute: EZ_PAYMENT_GATEWAY_GATEWAY_NOT_SELECTED' );
00078
00079 $process->setAttribute( 'event_state', EZ_PAYMENT_GATEWAY_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 EZ_WORKFLOW_TYPE_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 EZ_WORKFLOW_TYPE_STATUS_REJECTED;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 function &attributeDecoder( &$event, $attr )
00110 {
00111 switch ( $attr )
00112 {
00113 case 'selected_gateways_types':
00114 {
00115 $selectedGatewaysTypes = explode( ',', $event->attribute( 'data_text1' ) );
00116 return $selectedGatewaysTypes;
00117 }
00118 break;
00119
00120 case 'selected_gateways':
00121 {
00122 $selectedGatewaysTypes = explode( ',', $event->attribute( 'data_text1' ) );
00123 return $this->getGateways( $selectedGatewaysTypes );
00124 }break;
00125
00126 case 'current_gateway':
00127 {
00128 $gateway = $event->attribute( 'data_text2' );
00129 return $gateway;
00130 }
00131 break;
00132 }
00133 $retValue = null;
00134 return $retValue;
00135 }
00136
00137 function typeFunctionalAttributes( )
00138 {
00139 return array( 'selected_gateways_types', 'selected_gateways', 'current_gateway' );
00140 }
00141
00142 function attributes()
00143 {
00144 return array_merge( array( 'available_gateways' ),
00145 eZWorkflowEventType::attributes() );
00146 }
00147
00148 function hasAttribute( $attr )
00149 {
00150 return in_array( $attr, $this->attributes() );
00151 }
00152
00153 function &attribute( $attr )
00154 {
00155 switch( $attr )
00156 {
00157 case 'available_gateways':
00158 {
00159 $gateways =& $this->getGateways( array( -1 ) );
00160 return $gateways;
00161 }break;
00162 }
00163 return eZWorkflowEventType::attribute( $attr );
00164 }
00165
00166
00167
00168
00169
00170
00171 function loadAndRegisterGateways()
00172 {
00173 eZPaymentGatewayType::loadAndRegisterBuiltInGateways();
00174 eZPaymentGatewayType::loadAndRegisterExtensionGateways();
00175 }
00176
00177
00178
00179
00180 function loadAndRegisterBuiltInGateways()
00181 {
00182 $gatewaysINI =& eZINI::instance( 'paymentgateways.ini' );
00183 $gatewaysTypes = $gatewaysINI->variable( 'GatewaysSettings', 'AvailableGateways' );
00184 $gatewaysDir = false;
00185
00186
00187
00188 if ( $gatewaysINI->hasVariable( 'GatewaysSettings', 'GatewaysDerictories' ) )
00189 $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDerictories' );
00190 else
00191 $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDirectories' );
00192
00193 if ( is_array( $gatewaysDir ) && is_array( $gatewaysTypes ) )
00194 {
00195 foreach( $gatewaysDir as $dir )
00196 {
00197 foreach( $gatewaysTypes as $gateway )
00198 {
00199 $gatewayPath = "$dir/$gateway/classes/" . $gateway . 'gateway.php';
00200 if( file_exists( $gatewayPath ) )
00201 {
00202 include_once( $gatewayPath );
00203 }
00204 }
00205 }
00206 }
00207 }
00208
00209
00210
00211
00212 function loadAndRegisterExtensionGateways()
00213 {
00214 $gatewaysINI =& eZINI::instance( 'paymentgateways.ini' );
00215 $siteINI =& eZINI::instance( 'site.ini' );
00216 $extensionDirectory = $siteINI->variable( 'ExtensionSettings', 'ExtensionDirectory' );
00217 $activeExtensions = eZExtension::activeExtensions();
00218
00219 foreach ( $activeExtensions as $extension )
00220 {
00221 $gatewayPath = "$extensionDirectory/$extension/classes/" . $extension . 'gateway.php';
00222 if ( file_exists( $gatewayPath ) )
00223 {
00224 include_once( $gatewayPath );
00225 }
00226 }
00227 }
00228
00229
00230
00231
00232
00233 function registerGateway( $gateway, $class_name, $description )
00234 {
00235 $gateways =& $GLOBALS["eZPaymentGateways"];
00236 if ( !is_array( $gateways ) )
00237 $gateways = array();
00238 if ( isset( $gateways[$gateway] ) )
00239 {
00240 eZDebug::writeError( "Gateway already registered: $gateway", "eZPaymentGatewayType::registerGateway" );
00241 }
00242 else
00243 {
00244 $gateways[$gateway] = array( "class_name" => $class_name, "description" => $description );
00245 }
00246 }
00247
00248
00249
00250
00251
00252
00253 function &getGateways( $gatewaysTypes )
00254 {
00255 $gateways = array();
00256 $availableGateways =& $GLOBALS[ 'eZPaymentGateways' ];
00257 if ( !is_array( $availableGateways ) ){
00258 return $gateways;
00259 }
00260
00261 if ( in_array( '-1', $gatewaysTypes ) )
00262 {
00263 $gatewaysTypes = array_keys( $availableGateways );
00264 }
00265
00266 foreach ( $gatewaysTypes as $key )
00267 {
00268 $gateway =& $availableGateways[$key];
00269
00270 $gateway['Name'] = $gateway['description'];
00271 $gateway['value'] = $key;
00272 $gateways[] = $gateway;
00273 }
00274
00275 return $gateways;
00276 }
00277
00278
00279
00280
00281
00282 function &createGateway( &$inGatewayType )
00283 {
00284 $theGateway = null;
00285 $gateway_difinition =& $GLOBALS[ 'eZPaymentGateways' ][ $inGatewayType ];
00286
00287 $this->logger->writeTimedString( $gateway_difinition, "createGateway. gateway_difinition" );
00288
00289 if( $gateway_difinition )
00290 {
00291 $class_name = $gateway_difinition[ 'class_name' ];
00292 $theGateway = new $class_name();
00293 }
00294
00295 return $theGateway;
00296 }
00297
00298
00299
00300
00301
00302 function &getCurrentGateway( &$event )
00303 {
00304 $theGateway = null;
00305 $gatewayType = $this->getCurrentGatewayType( $event );
00306
00307 if( $gatewayType != null )
00308 {
00309 $theGateway = $this->createGateway( $gatewayType );
00310 }
00311
00312 return $theGateway;
00313 }
00314
00315
00316
00317
00318
00319 function &getCurrentGatewayType( &$event )
00320 {
00321 $gateway = null;
00322 $http =& eZHTTPTool::instance();
00323
00324 if ( $http->hasPostVariable( 'SelectButton' ) && $http->hasPostVariable( 'SelectedGateway' ) )
00325 {
00326 $gateway = $http->postVariable( 'SelectedGateway' );
00327 $event->setAttribute( 'data_text2', $gateway );
00328 $event->store();
00329 }
00330 else if ( $http->hasPostVariable( 'CancelButton' ) )
00331 {
00332 $gateway = null;
00333 }
00334 else
00335 {
00336 $gateway = $event->attribute( 'current_gateway' );
00337 }
00338
00339 return $gateway;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348 function selectGateway( &$event )
00349 {
00350 $selectedGatewaysTypes = explode( ',', $event->attribute( 'data_text1' ) );
00351
00352 if ( count( $selectedGatewaysTypes ) == 1 && $selectedGatewaysTypes[0] != -1 )
00353 {
00354 $event->setAttribute( 'data_text2', $selectedGatewaysTypes[0] );
00355 $event->store();
00356
00357 $this->logger->writeTimedString( $selectedGatewaysTypes[0], 'selectGateway' );
00358 return true;
00359 }
00360
00361 $this->logger->writeTimedString( 'selectGateways. multiple gateways, let user choose.' );
00362 return false;
00363 }
00364
00365 function needCleanup()
00366 {
00367 return true;
00368 }
00369
00370
00371
00372
00373
00374 function cleanup( &$process, &$event )
00375 {
00376 $theGateway = $this->getCurrentGateway( $event );
00377 if( $theGateway != null and $theGateway->needCleanup() )
00378 {
00379 $theGateway->cleanup( $process, $event );
00380 }
00381 }
00382
00383 function initializeEvent( &$event )
00384 {
00385 }
00386
00387
00388
00389
00390
00391
00392 function fetchHTTPInput( &$http, $base, &$event )
00393 {
00394 $gatewaysVar = $base . "_event_ezpaymentgateway_gateways_" . $event->attribute( "id" );
00395 if ( $http->hasPostVariable( $gatewaysVar ) )
00396 {
00397 $gatewaysArray = $http->postVariable( $gatewaysVar );
00398 if ( in_array( '-1', $gatewaysArray ) )
00399 {
00400 $gatewaysArray = array( -1 );
00401 }
00402
00403 $gatewaysString = implode( ',', $gatewaysArray );
00404 $event->setAttribute( "data_text1", $gatewaysString );
00405 }
00406 }
00407
00408 var $logger;
00409 }
00410
00411 eZWorkflowEventType::registerType( EZ_WORKFLOW_TYPE_PAYMENTGATEWAY_ID, 'ezpaymentgatewaytype' );
00412 ?>