| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace DigitalOrigin\Pmt\Controller\Payment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Magento\Framework\App\Action\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Magento\Framework\App\ResourceConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Magento\Framework\DB\Ddl\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Magento\Framework\App\CsrfAwareActionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Magento\Framework\App\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Magento\Framework\App\Request\InvalidRequestException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class LogV2 extends Action implements CsrfAwareActionInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     /** Concurrency tablename */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     const LOGS_TABLE = 'pmt_logs'; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** @var mixed $config */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     protected $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** @var ResourceConnection $dbObject */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     protected $dbObject; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * Log constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @param \Magento\Framework\App\Action\Context $context | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @param \DigitalOrigin\Pmt\Helper\Config      $pmtConfig | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @param ResourceConnection                    $dbObject | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         \Magento\Framework\App\Action\Context $context, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         \DigitalOrigin\Pmt\Helper\Config $pmtConfig, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         ResourceConnection $dbObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->config = $pmtConfig->getConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->dbObject = $dbObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // CsrfAwareAction Magento2.3 compatibility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $request = $this->getRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             if ($request instanceof HttpRequest && $request->isPost() && empty($request->getParam('form_key'))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $request->setParam('form_key', $formKey->getFormKey()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |         return parent::__construct($context); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * Main function | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function execute() | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             $response = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $secretKey = $this->getRequest()->getParam('secret'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $privateKey = isset($this->config['pmt_private_key']) ? $this->config['pmt_private_key'] : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             if ($secretKey!='' && $privateKey!='') { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 $this->checkDbLogTable(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 $dbConnection = $this->dbObject->getConnection(); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 $sql          = $dbConnection | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     ->select() | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                     ->from($tableName, array('log', 'createdAt')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 if ($dateFrom = $this->getRequest()->getParam('from')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                     $sql->where('createdAt > ?', $dateFrom); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 if ($dateTo = $this->getRequest()->getParam('to')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                     $sql->where('createdAt < ?', $dateTo); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 50; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 $sql->limit($limit); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 $sql->order('createdAt', 'desc'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 $results = $dbConnection->fetchAll($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 if (isset($results) && $privateKey == $secretKey) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                     foreach ($results as $key => $result) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                         $response[$key]['timestamp'] = $result['createdAt']; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                         $response[$key]['log']       = json_decode($result['log']); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                     $response['result'] = 'Error'; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 $response = json_encode($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 header("HTTP/1.1 200", true, 200); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 header('Content-Type: application/json', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 header('Content-Length: '.strlen($response)); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 echo($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 exit(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } catch (\Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             die($e->getMessage()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return void|\Zend_Db_Statement_Interface | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @throws \Zend_Db_Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     private function checkDbLogTable() | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         $dbConnection = $this->dbObject->getConnection(); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $tableName = $this->dbObject->getTableName(self::LOGS_TABLE); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         if (!$dbConnection->isTableExists($tableName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             $table = $dbConnection | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 ->newTable($tableName) | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 ->addColumn( | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                     'id', | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                     Table::TYPE_SMALLINT, | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                     null, | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                     array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true) | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                 ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false)) | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |                 ->addColumn( | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |                     'createdAt', | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |                     Table::TYPE_TIMESTAMP, | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |                     null, | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |                     array('nullable'=>false, | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                           'default'=>Table::TIMESTAMP_INIT) | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             return $dbConnection->createTable($table); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         return; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * @param RequestInterface $request | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * @return InvalidRequestException|null | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * @param RequestInterface $request | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |      * @return bool|null | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public function validateForCsrf(RequestInterface $request): ?bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  |         return true; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 154 |  |  |  |