| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For full license information, please view the LICENSE file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace SprykerEco\Zed\CrefoPayApi\Business\Response\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class CrefoPayApiResponseValidator implements CrefoPayApiResponseValidatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected const API_HEADER_MAC = 'X-Payco-HMAC'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected const API_RESPONSE_FIELD_RESULT_CODE = 'resultCode'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected const RESULT_CODE_OK = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected const RESULT_CODE_REDIRECT = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         CrefoPayApiServiceInterface $service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->service = $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array|null $responseData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     public function validateResponse( | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         CrefoPayApiGuzzleResponseInterface $response, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         ?array $responseData | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     ): bool { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         return $responseData !== null | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             && $this->isResultCodeSuccess($responseData) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             && $this->validateMac($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param array $responseData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function isResultCodeSuccess(array $responseData): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $resultCode = $responseData[static::API_RESPONSE_FIELD_RESULT_CODE] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $successResultCodes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             static::RESULT_CODE_OK, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             static::RESULT_CODE_REDIRECT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return in_array($resultCode, $successResultCodes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected function validateMac(CrefoPayApiGuzzleResponseInterface $response): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $mac = $response->getHeader(static::API_HEADER_MAC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($mac === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this->service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ->validateMac([$response->getResponseBody()], $mac); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  |