|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Adyen Payment Module |
| 5 | + * |
| 6 | + * Copyright (c) 2026 Adyen N.V. |
| 7 | + * This file is open source and available under the MIT license. |
| 8 | + * See the LICENSE file for more info. |
| 9 | + * |
| 10 | + * Author: Adyen <magento@adyen.com> |
| 11 | + */ |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Adyen\Payment\Model\Resolver; |
| 15 | + |
| 16 | +use Adyen\Payment\Exception\GraphQlAdyenException; |
| 17 | +use Adyen\Payment\Logger\AdyenLogger; |
| 18 | +use Adyen\Payment\Model\GraphqlInputArgumentValidator; |
| 19 | +use Adyen\Payment\Model\Sales\OrderRepository; |
| 20 | +use Exception; |
| 21 | +use Magento\Framework\Exception\LocalizedException; |
| 22 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 23 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 24 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 25 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 26 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 27 | +use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter; |
| 28 | +use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; |
| 29 | +use Magento\Sales\Api\Data\OrderInterface; |
| 30 | + |
| 31 | +abstract class AbstractDonationResolver implements ResolverInterface |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId |
| 35 | + * @param OrderRepository $orderRepository |
| 36 | + * @param GraphqlInputArgumentValidator $graphqlInputArgumentValidator |
| 37 | + * @param AdyenLogger $adyenLogger |
| 38 | + * @param AggregateExceptionMessageFormatter $adyenGraphQlExceptionMessageFormatter |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + protected readonly MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, |
| 42 | + protected readonly OrderRepository $orderRepository, |
| 43 | + protected readonly GraphqlInputArgumentValidator $graphqlInputArgumentValidator, |
| 44 | + protected readonly AdyenLogger $adyenLogger, |
| 45 | + protected readonly AggregateExceptionMessageFormatter $adyenGraphQlExceptionMessageFormatter |
| 46 | + ) { } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return array |
| 50 | + */ |
| 51 | + abstract protected function getRequiredFields(): array; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param OrderInterface $order |
| 55 | + * @param array $args |
| 56 | + * @param Field $field |
| 57 | + * @param $context |
| 58 | + * @param ResolveInfo $info |
| 59 | + * @return array |
| 60 | + * @throws GraphQlAdyenException |
| 61 | + */ |
| 62 | + abstract protected function performOperation( |
| 63 | + OrderInterface $order, |
| 64 | + array $args, |
| 65 | + Field $field, |
| 66 | + $context, |
| 67 | + ResolveInfo $info |
| 68 | + ): array; |
| 69 | + |
| 70 | + /** |
| 71 | + * @return string |
| 72 | + */ |
| 73 | + protected function getGenericErrorMessage(): string |
| 74 | + { |
| 75 | + return 'An error occurred while processing the donation.'; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param Field $field |
| 80 | + * @param $context |
| 81 | + * @param ResolveInfo $info |
| 82 | + * @param array|null $value |
| 83 | + * @param array|null $args |
| 84 | + * @return array |
| 85 | + * @throws GraphQlAdyenException |
| 86 | + * @throws GraphQlInputException |
| 87 | + */ |
| 88 | + public function resolve( |
| 89 | + Field $field, |
| 90 | + $context, |
| 91 | + ResolveInfo $info, |
| 92 | + ?array $value = null, |
| 93 | + ?array $args = null |
| 94 | + ): array { |
| 95 | + $this->graphqlInputArgumentValidator->execute($args, $this->getRequiredFields()); |
| 96 | + |
| 97 | + try { |
| 98 | + $quoteId = $this->maskedQuoteIdToQuoteId->execute($args['cartId']); |
| 99 | + } catch (NoSuchEntityException $e) { |
| 100 | + $this->adyenLogger->error(sprintf("Quote with masked ID %s not found!", $args['cartId'])); |
| 101 | + throw new GraphQlAdyenException(__($this->getGenericErrorMessage())); |
| 102 | + } |
| 103 | + |
| 104 | + $order = $this->orderRepository->getOrderByQuoteId($quoteId); |
| 105 | + |
| 106 | + if (!$order) { |
| 107 | + $this->adyenLogger->error(sprintf("Order for quote ID %s not found!", $quoteId)); |
| 108 | + throw new GraphQlAdyenException(__($this->getGenericErrorMessage())); |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + return $this->performOperation($order, $args, $field, $context, $info); |
| 113 | + } catch (LocalizedException $e) { |
| 114 | + throw $this->adyenGraphQlExceptionMessageFormatter->getFormatted( |
| 115 | + $e, |
| 116 | + __($this->getGenericErrorMessage()), |
| 117 | + $this->getGenericErrorMessage(), |
| 118 | + $field, |
| 119 | + $context, |
| 120 | + $info |
| 121 | + ); |
| 122 | + } catch (Exception $e) { |
| 123 | + $this->adyenLogger->error(sprintf( |
| 124 | + '%s: %s', |
| 125 | + $this->getGenericErrorMessage(), |
| 126 | + $e->getMessage() |
| 127 | + )); |
| 128 | + throw new GraphQlAdyenException(__($this->getGenericErrorMessage())); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments