|
7 | 7 |
|
8 | 8 | namespace Magento\PaypalGraphQl\Model;
|
9 | 9 |
|
| 10 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 11 | +use Magento\Framework\Url\Validator as UrlValidator; |
10 | 12 | use Magento\Paypal\Model\Config;
|
11 | 13 | use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
|
12 | 14 |
|
|
15 | 17 | */
|
16 | 18 | class PayflowLinkAdditionalDataProvider implements AdditionalDataProviderInterface
|
17 | 19 | {
|
| 20 | + /** |
| 21 | + * @var UrlValidator |
| 22 | + */ |
| 23 | + private $urlValidator; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param UrlValidator $urlValidator |
| 27 | + */ |
| 28 | + public function __construct(UrlValidator $urlValidator) |
| 29 | + { |
| 30 | + $this->urlValidator = $urlValidator; |
| 31 | + } |
| 32 | + |
18 | 33 | /**
|
19 | 34 | * Returns additional data
|
20 | 35 | *
|
21 | 36 | * @param array $data
|
22 | 37 | * @return array
|
| 38 | + * @throws GraphQlInputException |
23 | 39 | */
|
24 | 40 | public function getData(array $data): array
|
25 | 41 | {
|
26 |
| - return $data[Config::METHOD_PAYFLOWLINK] ?? []; |
| 42 | + $additionalData = $data[Config::METHOD_PAYFLOWLINK] ?? []; |
| 43 | + $this->validateUrls($additionalData); |
| 44 | + |
| 45 | + return $additionalData; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Validate redirect urls |
| 50 | + * |
| 51 | + * @param array $data |
| 52 | + * @throws GraphQlInputException |
| 53 | + */ |
| 54 | + private function validateUrls(array $data): void |
| 55 | + { |
| 56 | + $urlKeys = ['cancel_url', 'return_url', 'error_url']; |
| 57 | + |
| 58 | + foreach ($urlKeys as $urlKey) { |
| 59 | + if (isset($data[$urlKey])) { |
| 60 | + if (!$this->urlValidator->isValid($data[$urlKey])) { |
| 61 | + $errorMessage = $this->urlValidator->getMessages()['invalidUrl'] ?? "Invalid Url."; |
| 62 | + throw new GraphQlInputException(__($errorMessage)); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
27 | 66 | }
|
28 | 67 | }
|
0 commit comments