| 1 |  |  | <?php | 
            
                                                                        
                            
            
                                    
            
            
                | 2 |  |  | class Intraface_Controller_ModulePackage_Process extends k_Component | 
            
                                                                        
                            
            
                                    
            
            
                | 3 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 4 |  |  |     protected $kernel; | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  |     protected $mdb2; | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     function __construct(MDB2_Driver_Common $mdb2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         $this->mdb2 = $mdb2; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     function postForm() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         // When we recieve from Quickpay payment not being logged in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $action = Intraface_modules_modulepackage_ActionStore::restoreFromIdentifier($this->mdb2, $_GET['action_store_identifier']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         if (!$action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             throw new Exception('Unable to restore action from identifier '. $_GET['action_store_identifier']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         // We login to the intranet with the private key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $adapter = new Intraface_Auth_PrivateKeyLogin($this->mdb2, uniqid(), $action->getIntranetPrivateKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $weblogin = $adapter->auth(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if (!$intranet_id = $weblogin->getActiveIntranetId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             throw new Exception("Unable to log in to the intranet with public key: ".$action->getIntranetPrivateKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->getKernel()->weblogin = $weblogin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->getKernel()->intranet = new Intraface_Intranet($intranet_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->getKernel()->setting = new Intraface_Setting($this->getKernel()->intranet->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $module = $this->getKernel()->module('modulepackage'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $module->includeFile('Manager.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $module->includeFile('ShopExtension.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $module->includeFile('ActionStore.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $module->includeFile('AccessUpdate.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $shop = new Intraface_modules_modulepackage_ShopExtension(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $order = $shop->getOrderDetails($action->getOrderIdentifier()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (empty($order)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             throw new Exception('Unable to restore order from identifier '.$action->getOrderIdentifier()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $payment_provider = 'Ilib_Payment_Authorize_Provider_'.INTRAFACE_ONLINEPAYMENT_PROVIDER; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $payment_authorize = new $payment_provider(INTRAFACE_ONLINEPAYMENT_MERCHANT, INTRAFACE_ONLINEPAYMENT_MD5SECRET); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $payment_postprocess = $payment_authorize->getPostProcess($_GET, $_POST, $_SESSION, $order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $amount = $payment_postprocess->getAmount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $shop->addPaymentToOrder($action->getOrderIdentifier(), $payment_postprocess); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($payment_postprocess->getPbsStatus() == '000') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if ($amount >= $action->getTotalPrice()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 if ($action->execute($this->getKernel()->intranet)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     // we delete the action from the store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     $action_store = new Intraface_modules_modulepackage_ActionStore($this->getKernel()->intranet->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     $action_store->restore($_GET['action_store_identifier']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     $action_store->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     // TODO: do we maybe want to send an email to the customer? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     $access_update = new Intraface_modules_modulepackage_AccessUpdate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     $access_update->run($this->getKernel()->intranet->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     return new k_TextResponse('SUCCESS!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     $response = new k_TextResponse('Failure:'.$action->error->view()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     $response->setStatus(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 // TODO: Here we can send an e-mail that says they still need to pay some more OR? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 throw new Exception('Failure: Not sufficient payment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             // @todo should throw a 401 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $response = new k_TextResponse('Payment attempt registered. Not authorized'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $response->setStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $response = new k_TextResponse('Unknown failure'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $response->setStatus(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $response; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     function getKernel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (is_object($this->kernel)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return $this->kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return $this->kernel = new Intraface_Kernel(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: