| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Pagantis\OrdersApiClient\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Pagantis\ModuleUtils\Exception\ConcurrencyException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Pagantis\ModuleUtils\Exception\AlreadyProcessedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Pagantis\ModuleUtils\Exception\AmountMismatchException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Pagantis\ModuleUtils\Exception\MerchantOrderNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Pagantis\ModuleUtils\Exception\NoIdentificationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Pagantis\ModuleUtils\Exception\OrderNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Pagantis\ModuleUtils\Exception\QuoteNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Pagantis\ModuleUtils\Exception\UnknownException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Pagantis\ModuleUtils\Exception\WrongStatusException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Pagantis\ModuleUtils\Model\Response\JsonSuccessResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Pagantis\ModuleUtils\Model\Response\JsonExceptionResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Pagantis\ModuleUtils\Model\Log\LogEntry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Pagantis\OrdersApiClient\Model\Order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | if (!defined('ABSPATH')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class WcPagantisNotify extends WcPagantisGateway | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** Concurrency tablename  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const CONCURRENCY_TABLE = 'pagantis_concurrency'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** Seconds to expire a locked request */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const CONCURRENCY_TIMEOUT = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var mixed $pagantisOrder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $pagantisOrder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @var $string $origin */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public $origin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** @var $string */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public $order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** @var mixed $woocommerceOrderId */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $woocommerceOrderId = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** @var mixed $cfg */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected $cfg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** @var Client $orderClient */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $orderClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** @var  WC_Order $woocommerceOrder */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected $woocommerceOrder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** @var mixed $pagantisOrderId */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected $pagantisOrderId = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** @var $string */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected $product; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** @var $string */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected $urlTokenVerification = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Validation vs PagantisClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return JsonExceptionResponse|JsonSuccessResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws ConcurrencyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function processInformation() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             require_once(__ROOT__.'/vendor/autoload.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 if ($_SERVER['REQUEST_METHOD'] == 'GET' && $_GET['origin'] == 'notification') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     return $this->buildResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $this->checkConcurrency(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $this->getProductType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->getMerchantOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $this->getPagantisOrderId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $this->getPagantisOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $checkAlreadyProcessed = $this->checkOrderStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 if ($checkAlreadyProcessed) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     return $this->buildResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $this->validateAmount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 if ($this->checkMerchantOrderStatus()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     $this->processMerchantOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $this->insertLog($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 return $this->buildResponse($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $this->confirmPagantisOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 return $this->buildResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $this->rollbackMerchantOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $this->insertLog($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 return $this->buildResponse($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $this->insertLog($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             return $this->buildResponse($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * COMMON FUNCTIONS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @throws ConcurrencyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @throws QuoteNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     private function checkConcurrency() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->woocommerceOrderId = $_GET['order-received']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if ($this->woocommerceOrderId == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             throw new QuoteNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->unblockConcurrency(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $this->blockConcurrency($this->woocommerceOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * getProductType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     private function getProductType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if ($_GET['product'] == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $this->setProduct(WcPagantisGateway::METHOD_ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->setProduct($_GET['product']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @throws MerchantOrderNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     private function getMerchantOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $this->woocommerceOrder = new WC_Order($this->woocommerceOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $this->woocommerceOrder->set_payment_method_title($this->getProduct()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             throw new MerchantOrderNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @throws NoIdentificationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     private function getPagantisOrderId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $this->setUrlToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->checkDbTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $order_id = $wpdb->get_var("SELECT order_id FROM $tableName WHERE token='{$this->getUrlToken()}' "); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $this->pagantisOrderId = $order_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if ($this->pagantisOrderId == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             throw new NoIdentificationException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @throws OrderNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     private function getPagantisOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $this->cfg = get_option('woocommerce_pagantis_settings'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $this->cfg = get_option('woocommerce_pagantis_settings'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             if ($this->isProduct4x()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 $publicKey = $this->cfg['pagantis_public_key_4x']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 $secretKey = $this->cfg['pagantis_private_key_4x']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 $publicKey = $this->cfg['pagantis_public_key']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                 $secretKey = $this->cfg['pagantis_private_key']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             $this->orderClient = new Client($publicKey, $secretKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             throw new OrderNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @throws WrongStatusException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     private function checkOrderStatus() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             $this->checkPagantisStatus(array('AUTHORIZED')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |             if ($this->pagantisOrder instanceof Order) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |                 $status = $this->pagantisOrder->getStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |                 $status = '-'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             if ($status === Order::STATUS_CONFIRMED) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             throw new WrongStatusException($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     private function checkMerchantOrderStatus() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         //Order status reference => https://docs.woocommerce.com/document/managing-orders/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         $validStatus   = array('on-hold', 'pending', 'failed', 'processing', 'completed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         $isValidStatus = apply_filters( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             'woocommerce_valid_order_statuses_for_payment_complete', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             $validStatus, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |             $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         if (!$this->woocommerceOrder->has_status($isValidStatus)) { // TO CONFIRM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |             $logMessage = "WARNING checkMerchantOrderStatus." . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |                           " Merchant order id:".$this->woocommerceOrder->get_id(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |                           " Merchant order status:".$this->woocommerceOrder->get_status(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |                           " Pagantis order id:".$this->pagantisOrder->getStatus(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                           " Pagantis order status:".$this->pagantisOrder->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |             $this->insertLog(null, $logMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             $this->woocommerceOrder->add_order_note($logMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             $this->woocommerceOrder->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         return true; //TO SAVE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |      * @throws AmountMismatchException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |     private function validateAmount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |         $wcAmount = intval(strval(100 * $this->woocommerceOrder->get_total())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         if ($pagantisAmount != $wcAmount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             throw new AmountMismatchException($pagantisAmount, $wcAmount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |     private function processMerchantOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |         $this->saveOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         $this->updateBdInfo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |      * @return false|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * @throws UnknownException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |     private function confirmPagantisOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |             $this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |             $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |             if ($this->pagantisOrder->getStatus() !== Order::STATUS_CONFIRMED) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                 throw new UnknownException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |                 $logMessage = 'Concurrency issue: Order_id '.$this->pagantisOrderId.' was confirmed by other process'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |                 $this->insertLog(null, $logMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         $jsonResponse = new JsonSuccessResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |         return $jsonResponse->toJson(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |      * UTILS FUNCTIONS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |     /** STEP 1 CC - Check concurrency */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * Check if orders table exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |     private function checkDbTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |         global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         $tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |             $charset_collate = $wpdb->get_charset_collate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |             $sql= "CREATE TABLE IF NOT EXISTS $tableName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |                 (id INT,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |                 order_id varchar(60), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |                 wc_order_id varchar(60), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |                 token varchar(32) NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |                 ADD PRIMARY KEY (id,order_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |                 )$charset_collate"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |             require_once(ABSPATH.'wp-admin/includes/upgrade.php'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |             dbDelta($sql); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |      * Check if logs table exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |     private function checkDbLogTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |         $tableName = $wpdb->prefix.PG_LOGS_TABLE_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |         if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |             $charset_collate = $wpdb->get_charset_collate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |             $sql = "CREATE TABLE $tableName ( id int NOT NULL AUTO_INCREMENT, log text NOT NULL,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |                     createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (id)) $charset_collate"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |             require_once(ABSPATH.'wp-admin/includes/upgrade.php'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |             dbDelta($sql); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |     /** STEP 2 GMO - Get Merchant Order */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |     /** STEP 3 GPOI - Get Pagantis OrderId */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     /** STEP 4 GPO - Get Pagantis Order */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |     /** STEP 5 COS - Check Order Status */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |      * @param $statusArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |     private function checkPagantisStatus($statusArray) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |         $pagantisStatus = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |         foreach ($statusArray as $status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |             $pagantisStatus[] = constant("\Pagantis\OrdersApiClient\Model\Order::STATUS_$status"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |         if ($this->pagantisOrder instanceof Order) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |             $payed = in_array($this->pagantisOrder->getStatus(), $pagantisStatus); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |             if (!$payed) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |                 if ($this->pagantisOrder instanceof Order) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |                     $status = $this->pagantisOrder->getStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |                     $status = '-'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |                 throw new WrongStatusException($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |             throw new OrderNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |     /** STEP 6 CMOS - Check Merchant Order Status */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |     /** STEP 7 VA - Validate Amount */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |     /** STEP 8 PMO - Process Merchant Order */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |     private function saveOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |         global $woocommerce; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         $paymentResult = $this->woocommerceOrder->payment_complete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |         if ($paymentResult) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |             $metadataOrder = $this->pagantisOrder->getMetadata(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |             $metadataInfo = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |             foreach ($metadataOrder as $metadataKey => $metadataValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |                 if ($metadataKey == 'promotedProduct') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |                     $metadataInfo.= "/Producto promocionado = $metadataValue"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |             if ($metadataInfo != null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |                 $this->woocommerceOrder->add_order_note($metadataInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |             $this->woocommerceOrder->add_order_note("Notification received via $this->origin"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |             $this->woocommerceOrder->reduce_order_stock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |             $this->woocommerceOrder->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |             $woocommerce->cart->empty_cart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |             sleep(3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |             throw new UnknownException('Order can not be saved'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |      * Save the merchant order_id with the related identification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |     private function updateBdInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |         global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |         $this->checkDbTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |         $tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |         $wpdb->update( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |             $tableName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |             array('wc_order_id'=>$this->woocommerceOrderId), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |             array('token' => $this->getUrlToken(),'order_id' => $this->pagantisOrderId), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |             array( '%s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |             array( '%s', '%s' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |     /** STEP 9 CPO - Confirmation Pagantis Order */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |     private function rollbackMerchantOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |         $this->woocommerceOrder->update_status('pending', __('Pending payment', 'woocommerce')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |      * @param null $exception | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |      * @param null $message | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 432 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 433 |  |  |     private function insertLog($exception = null, $message = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 434 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 435 |  |  |         global $wpdb; | 
            
                                                                        
                            
            
                                    
            
            
                | 436 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 437 |  |  |         $this->checkDbLogTable(); | 
            
                                                                        
                            
            
                                    
            
            
                | 438 |  |  |         $logEntry     = new LogEntry(); | 
            
                                                                        
                            
            
                                    
            
            
                | 439 |  |  |         if ($exception instanceof \Exception) { | 
            
                                                                        
                            
            
                                    
            
            
                | 440 |  |  |             $logEntry = $logEntry->error($exception); | 
            
                                                                        
                            
            
                                    
            
            
                | 441 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 442 |  |  |             $logEntry = $logEntry->info($message); | 
            
                                                                        
                            
            
                                    
            
            
                | 443 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 444 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 445 |  |  |         $tableName = $wpdb->prefix.PG_LOGS_TABLE_NAME; | 
            
                                                                        
                            
            
                                    
            
            
                | 446 |  |  |         $wpdb->insert($tableName, array('log' => $logEntry->toJson())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  |      * @param null $orderId | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |      * @throws ConcurrencyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |     private function unblockConcurrency($orderId = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |         global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |         $tableName = $wpdb->prefix.PG_CONCURRENCY_TABLE_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |         if ($orderId == null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |             $query = "DELETE FROM $tableName WHERE createdAt<(NOW()- INTERVAL ".self::CONCURRENCY_TIMEOUT." SECOND)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |             $query = "DELETE FROM $tableName WHERE order_id = $orderId"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |         $resultDelete = $wpdb->query($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |         if ($resultDelete === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |             throw new ConcurrencyException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  |      * @param $orderId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |      * @throws ConcurrencyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |     private function blockConcurrency($orderId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |         global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |         $tableName = $wpdb->prefix.PG_CONCURRENCY_TABLE_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |         $insertResult = $wpdb->insert($tableName, array('order_id' => $orderId)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |         if ($insertResult === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |             if ($this->getOrigin() == 'Notify') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |                 throw new ConcurrencyException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |                 $query = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |                     "SELECT TIMESTAMPDIFF(SECOND,NOW()-INTERVAL %s SECOND, createdAt) as rest FROM %s WHERE %s", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |                     self::CONCURRENCY_TIMEOUT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |                     $tableName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |                     "order_id=$orderId" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |                 $resultSeconds = $wpdb->get_row($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |                 $restSeconds = isset($resultSeconds) ? ($resultSeconds->rest) : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |                 $secondsToExpire = ($restSeconds>self::CONCURRENCY_TIMEOUT) ? self::CONCURRENCY_TIMEOUT : $restSeconds; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |                 sleep($secondsToExpire+1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |                 $logMessage = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |                     "User waiting %s seconds, default seconds %s, bd time to expire %s seconds", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |                     $secondsToExpire, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |                     self::CONCURRENCY_TIMEOUT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |                     $restSeconds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |                 $this->insertLog(null, $logMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |      * @param null $exception | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |      * @return JsonExceptionResponse|JsonSuccessResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |      * @throws ConcurrencyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |     private function buildResponse($exception = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |         $this->unblockConcurrency($this->woocommerceOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |         if ($exception == null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |             $jsonResponse = new JsonSuccessResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |             $jsonResponse = new JsonExceptionResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |             $jsonResponse->setException($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  |         $jsonResponse->setMerchantOrderId($this->woocommerceOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |         $jsonResponse->setPagantisOrderId($this->pagantisOrderId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |         if ($_SERVER['REQUEST_METHOD'] == 'POST') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |             $jsonResponse->printResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |             return $jsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |      * GETTERS & SETTERS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |     public function getOrigin() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |         return $this->origin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  |      * @param mixed $origin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |     public function setOrigin($origin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |         $this->origin = $origin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |     private function isProduct4x() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  |         return ($this->product === Ucfirst(WcPagantis4xGateway::METHOD_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 |  |  |     public function getProduct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 |  |  |         return $this->product; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  |      * @param mixed $product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |     public function setProduct($product) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |         $this->product = Ucfirst($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 |  |  |     public function getWoocommerceOrderId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 |  |  |         return $this->woocommerceOrderId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |     private function getUrlToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 |  |  |         return $this->urlTokenVerification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |     private function setUrlToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 597 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 598 |  |  |         if (!isset($_GET['token'])){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 599 |  |  |             $logEntry = '$_GET[\'token\'] is empty' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 600 |  |  |                 " cart hash: ".WC()->cart->get_cart_hash(). | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 601 |  |  |                 " Merchant order id: ".$this->woocommerceOrderId. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 602 |  |  |                 " Pagantis order id: ".$this->pagantisOrderId. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 603 |  |  |                 " Pagantis urlToken: ".$this->getUrlToken(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 604 |  |  |                 " Function: " . __FUNCTION__; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 605 |  |  |             insertLogEntry(null, $logEntry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 606 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 607 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 608 |  |  |         $this->urlTokenVerification = $_GET['token']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 609 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 610 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 611 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 612 |  |  |  |