Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions Model/Resolver/GetAdyenPaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

use Adyen\Payment\Exception\GraphQlAdyenException;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Resolver\DataProvider\GetAdyenPaymentStatus;
use Exception;
use GraphQL\Error\ClientAware;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -24,38 +26,37 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Sales\Model\Order;

class GetAdyenPaymentDetails implements ResolverInterface
{
/**
* @param GetCartForUser $getCartForUser
* @param DataProvider\GetAdyenPaymentStatus $getAdyenPaymentStatusDataProvider
* @param GetAdyenPaymentStatus $getAdyenPaymentStatusDataProvider
* @param Order $order
* @param Json $jsonSerializer
* @param AdyenLogger $adyenLogger
* @param AggregateExceptionMessageFormatter $adyenGraphQlExceptionMessageFormatter
*/
public function __construct(
private readonly GetCartForUser $getCartForUser,
protected readonly DataProvider\GetAdyenPaymentStatus $getAdyenPaymentStatusDataProvider,
protected readonly Order $order,
protected readonly Json $jsonSerializer,
protected readonly AdyenLogger $adyenLogger
protected readonly AdyenLogger $adyenLogger,
protected readonly AggregateExceptionMessageFormatter $adyenGraphQlExceptionMessageFormatter
) { }

/**
* @inheritdoc
*
* @param Field $field
* @param ContextInterface $context
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
* @throws GraphQlAdyenException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function resolve(
Field $field,
Expand All @@ -64,30 +65,30 @@ public function resolve(
?array $value = null,
?array $args = null
): array {
if (empty($args['payload'])) {
throw new GraphQlInputException(__('Required parameter "payload" is missing'));
}
if (empty($args['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
$payload = $this->jsonSerializer->unserialize($args['payload']);
if (!array_key_exists('orderId', $payload)) {
throw new GraphQlInputException(__('Missing "orderId" from payload'));
}
try {
if (empty($args['payload'])) {
throw new GraphQlInputException(__('Required parameter "payload" is missing'));
}
if (empty($args['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
$payload = $this->jsonSerializer->unserialize($args['payload']);
if (!array_key_exists('orderId', $payload)) {
throw new GraphQlInputException(__('Missing "orderId" from payload'));
}

$order = $this->order->loadByIncrementId($payload['orderId']);
$maskedCartId = $args['cart_id'];
$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
if (is_null($order->getEntityId()) || $order->getQuoteId() !== $cart->getEntityId()) {
throw new GraphQlNoSuchEntityException(__('Order does not exist'));
}
$order = $this->order->loadByIncrementId($payload['orderId']);
$maskedCartId = $args['cart_id'];
$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
if (is_null($order->getEntityId()) || $order->getQuoteId() !== $cart->getEntityId()) {
throw new GraphQlNoSuchEntityException(__('Order does not exist'));
}

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

try {
return $this->getAdyenPaymentStatusDataProvider->getGetAdyenPaymentDetails(
$this->jsonSerializer->serialize($payload),
$order,
Expand All @@ -111,25 +112,24 @@ public function resolve(
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @return mixed
* @return ClientAware
*/
private function getFormattedException($e, Field $field, ContextInterface $context, ResolveInfo $info)
private function getFormattedException($e, Field $field, ContextInterface $context, ResolveInfo $info): ClientAware
{
if (class_exists(\Magento\QuoteGraphQl\Helper\Error\PlaceOrderMessageFormatter::class)) {
$errorMessageFormatter = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\QuoteGraphQl\Helper\Error\PlaceOrderMessageFormatter::class);
return $errorMessageFormatter->getFormatted(
$e,
__('Unable to place order: A server error stopped your order from being placed. ' .
'Please try to place your order again'),
'Unable to place order',
$field,
$context,
$info
);
} else {
return new GraphQlAdyenException(__('Unable to place order: A server error stopped your order from being placed. ' .
'Please try to place your order again'));
}
/*
* Generic error message will only be shown if the exception is an instance of LocalizedException class.
* Otherwise, the relevant message formatter will be used.
*
* For more details, check AdyenGraphQlExceptionMessageFormatter virtual class in etc/graphql/di.xml
*/
return $this->adyenGraphQlExceptionMessageFormatter->getFormatted(
$e,
__('Unable to place order: A server error stopped your order from being placed. ' .
'Please try to place your order again'),
'Unable to place order',
$field,
$context,
$info
);
}
}
Loading
Loading