Skip to content

Commit 9091322

Browse files
authored
[ECP-9848] Fix graphql error message of adyenPaymentDetails mutation (#3227)
* [ECP-9848] Fix graphql error message in case of LocalizedException * [ECP-9848] Update PHPDocs * [ECP-9848] Write unit tests
1 parent 3c31c15 commit 9091322

File tree

3 files changed

+338
-46
lines changed

3 files changed

+338
-46
lines changed

Model/Resolver/GetAdyenPaymentDetails.php

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
use Adyen\Payment\Exception\GraphQlAdyenException;
1717
use Adyen\Payment\Logger\AdyenLogger;
18+
use Adyen\Payment\Model\Resolver\DataProvider\GetAdyenPaymentStatus;
1819
use Exception;
20+
use GraphQL\Error\ClientAware;
1921
use Magento\Framework\Exception\LocalizedException;
2022
use Magento\Framework\GraphQl\Config\Element\Field;
2123
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -24,38 +26,37 @@
2426
use Magento\Framework\GraphQl\Query\ResolverInterface;
2527
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2628
use Magento\Framework\Serialize\Serializer\Json;
29+
use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter;
2730
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2831
use Magento\Sales\Model\Order;
2932

3033
class GetAdyenPaymentDetails implements ResolverInterface
3134
{
3235
/**
3336
* @param GetCartForUser $getCartForUser
34-
* @param DataProvider\GetAdyenPaymentStatus $getAdyenPaymentStatusDataProvider
37+
* @param GetAdyenPaymentStatus $getAdyenPaymentStatusDataProvider
3538
* @param Order $order
3639
* @param Json $jsonSerializer
3740
* @param AdyenLogger $adyenLogger
41+
* @param AggregateExceptionMessageFormatter $adyenGraphQlExceptionMessageFormatter
3842
*/
3943
public function __construct(
4044
private readonly GetCartForUser $getCartForUser,
4145
protected readonly DataProvider\GetAdyenPaymentStatus $getAdyenPaymentStatusDataProvider,
4246
protected readonly Order $order,
4347
protected readonly Json $jsonSerializer,
44-
protected readonly AdyenLogger $adyenLogger
48+
protected readonly AdyenLogger $adyenLogger,
49+
protected readonly AggregateExceptionMessageFormatter $adyenGraphQlExceptionMessageFormatter
4550
) { }
4651

4752
/**
48-
* @inheritdoc
49-
*
5053
* @param Field $field
51-
* @param ContextInterface $context
54+
* @param $context
5255
* @param ResolveInfo $info
5356
* @param array|null $value
5457
* @param array|null $args
5558
* @return array
5659
* @throws GraphQlAdyenException
57-
* @throws GraphQlInputException
58-
* @throws GraphQlNoSuchEntityException
5960
*/
6061
public function resolve(
6162
Field $field,
@@ -64,30 +65,30 @@ public function resolve(
6465
?array $value = null,
6566
?array $args = null
6667
): array {
67-
if (empty($args['payload'])) {
68-
throw new GraphQlInputException(__('Required parameter "payload" is missing'));
69-
}
70-
if (empty($args['cart_id'])) {
71-
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
72-
}
73-
$payload = $this->jsonSerializer->unserialize($args['payload']);
74-
if (!array_key_exists('orderId', $payload)) {
75-
throw new GraphQlInputException(__('Missing "orderId" from payload'));
76-
}
68+
try {
69+
if (empty($args['payload'])) {
70+
throw new GraphQlInputException(__('Required parameter "payload" is missing'));
71+
}
72+
if (empty($args['cart_id'])) {
73+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
74+
}
75+
$payload = $this->jsonSerializer->unserialize($args['payload']);
76+
if (!array_key_exists('orderId', $payload)) {
77+
throw new GraphQlInputException(__('Missing "orderId" from payload'));
78+
}
7779

78-
$order = $this->order->loadByIncrementId($payload['orderId']);
79-
$maskedCartId = $args['cart_id'];
80-
$currentUserId = $context->getUserId();
81-
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
82-
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
83-
if (is_null($order->getEntityId()) || $order->getQuoteId() !== $cart->getEntityId()) {
84-
throw new GraphQlNoSuchEntityException(__('Order does not exist'));
85-
}
80+
$order = $this->order->loadByIncrementId($payload['orderId']);
81+
$maskedCartId = $args['cart_id'];
82+
$currentUserId = $context->getUserId();
83+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
84+
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
85+
if (is_null($order->getEntityId()) || $order->getQuoteId() !== $cart->getEntityId()) {
86+
throw new GraphQlNoSuchEntityException(__('Order does not exist'));
87+
}
8688

87-
// Set the orderId in the payload to the entity id, instead of the incrementId
88-
$payload['orderId'] = $order->getId();
89+
// Set the orderId in the payload to the entity id, instead of the incrementId
90+
$payload['orderId'] = $order->getId();
8991

90-
try {
9192
return $this->getAdyenPaymentStatusDataProvider->getGetAdyenPaymentDetails(
9293
$this->jsonSerializer->serialize($payload),
9394
$order,
@@ -111,25 +112,24 @@ public function resolve(
111112
* @param Field $field
112113
* @param ContextInterface $context
113114
* @param ResolveInfo $info
114-
* @return mixed
115+
* @return ClientAware
115116
*/
116-
private function getFormattedException($e, Field $field, ContextInterface $context, ResolveInfo $info)
117+
private function getFormattedException($e, Field $field, ContextInterface $context, ResolveInfo $info): ClientAware
117118
{
118-
if (class_exists(\Magento\QuoteGraphQl\Helper\Error\PlaceOrderMessageFormatter::class)) {
119-
$errorMessageFormatter = \Magento\Framework\App\ObjectManager::getInstance()
120-
->get(\Magento\QuoteGraphQl\Helper\Error\PlaceOrderMessageFormatter::class);
121-
return $errorMessageFormatter->getFormatted(
122-
$e,
123-
__('Unable to place order: A server error stopped your order from being placed. ' .
124-
'Please try to place your order again'),
125-
'Unable to place order',
126-
$field,
127-
$context,
128-
$info
129-
);
130-
} else {
131-
return new GraphQlAdyenException(__('Unable to place order: A server error stopped your order from being placed. ' .
132-
'Please try to place your order again'));
133-
}
119+
/*
120+
* Generic error message will only be shown if the exception is an instance of LocalizedException class.
121+
* Otherwise, the relevant message formatter will be used.
122+
*
123+
* For more details, check AdyenGraphQlExceptionMessageFormatter virtual class in etc/graphql/di.xml
124+
*/
125+
return $this->adyenGraphQlExceptionMessageFormatter->getFormatted(
126+
$e,
127+
__('Unable to place order: A server error stopped your order from being placed. ' .
128+
'Please try to place your order again'),
129+
'Unable to place order',
130+
$field,
131+
$context,
132+
$info
133+
);
134134
}
135135
}

0 commit comments

Comments
 (0)