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