vendor/aspello/payum-paybynet/src/Action/API/AssertNotifyRequestDataAction.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Aspello\Payum\PayByNet\Action\API;
  4. use Aspello\Payum\PayByNet\Exception\InvalidNotifyHttpRequestException;
  5. use Aspello\Payum\PayByNet\Exception\WrongNotifyHttpRequestException;
  6. use Aspello\Payum\PayByNet\Request\AssertNotifyRequestData;
  7. use Aspello\Payum\PayByNet\Api;
  8. use Payum\Core\Action\ActionInterface;
  9. use Payum\Core\ApiAwareInterface;
  10. use Payum\Core\ApiAwareTrait;
  11. use Payum\Core\Request\GetHttpRequest;
  12. final class AssertNotifyRequestDataAction implements ActionInterfaceApiAwareInterface
  13. {
  14.     use ApiAwareTrait;
  15.     /** @var Api */
  16.     protected $api;
  17.     public function __construct()
  18.     {
  19.         $this->apiClass Api::class;
  20.     }
  21.     /**
  22.      * @param AssertNotifyRequestData $request
  23.      * @return void
  24.      */
  25.     public function execute($request): void
  26.     {
  27.         if (!$this->isNotifyHttpRequest($request->notifyRequest)) {
  28.             throw new WrongNotifyHttpRequestException('Invalid notify request.');
  29.         }
  30.         $requestData $request->notifyRequest->request;
  31.         $hashToCompareWith $this->api->prepareNotifyHash($requestData);
  32.         if ($requestData['hash'] !== $hashToCompareWith) {
  33.             throw new InvalidNotifyHttpRequestException('Invalid hash value.');
  34.         }
  35.         if ($request->details instanceof \ArrayObject && (float)$requestData['transAmount'] !== (float)$request->details['amount']) {
  36.             throw new InvalidNotifyHttpRequestException('Invalid transAmount value.');
  37.         }
  38.     }
  39.     public function supports($request): bool
  40.     {
  41.         return $request instanceof AssertNotifyRequestData;
  42.     }
  43.     private function isNotifyHttpRequest(GetHttpRequest $httpRequest): bool
  44.     {
  45.         if ('POST' !== strtoupper($httpRequest->method)) {
  46.             return false;
  47.         }
  48.         $mandatoryFields = ['newStatus''transAmount''paymentId''hash'];
  49.         foreach ($mandatoryFields as $mandatoryField) {
  50.             if (!array_key_exists($mandatoryField$httpRequest->request)) {
  51.                 return false;
  52.             }
  53.         }
  54.         return true;
  55.     }
  56. }