|
| 1 | +diff --git a/vendor/magento/module-quote-graph-ql/Model/Resolver/PlaceOrder.php b/vendor/magento/module-quote-graph-ql/Model/Resolver/PlaceOrder.php |
| 2 | +index 48346918b31cf..7cbc64a41d37c 100644 |
| 3 | +--- a/vendor/magento/module-quote-graph-ql/Model/Resolver/PlaceOrder.php |
| 4 | ++++ b/vendor/magento/module-quote-graph-ql/Model/Resolver/PlaceOrder.php |
| 5 | +@@ -7,16 +7,17 @@ |
| 6 | + |
| 7 | + namespace Magento\QuoteGraphQl\Model\Resolver; |
| 8 | + |
| 9 | +-use Magento\Framework\Exception\AuthorizationException; |
| 10 | ++use Magento\Framework\App\ObjectManager; |
| 11 | + use Magento\Framework\Exception\LocalizedException; |
| 12 | +-use Magento\Framework\Exception\NoSuchEntityException; |
| 13 | + use Magento\Framework\GraphQl\Config\Element\Field; |
| 14 | +-use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
| 15 | + use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 16 | + use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 17 | + use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 18 | ++use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter; |
| 19 | + use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout; |
| 20 | ++use Magento\GraphQl\Model\Query\ContextInterface; |
| 21 | + use Magento\QuoteGraphQl\Model\Cart\PlaceOrder as PlaceOrderModel; |
| 22 | ++use Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutexInterface; |
| 23 | + use Magento\Sales\Api\OrderRepositoryInterface; |
| 24 | + |
| 25 | + /** |
| 26 | +@@ -24,49 +25,50 @@ |
| 27 | + */ |
| 28 | + class PlaceOrder implements ResolverInterface |
| 29 | + { |
| 30 | +- /**#@+ |
| 31 | +- * Error message codes |
| 32 | ++ /** |
| 33 | ++ * @var GetCartForCheckout |
| 34 | + */ |
| 35 | +- private const ERROR_CART_NOT_FOUND = 'CART_NOT_FOUND'; |
| 36 | +- private const ERROR_CART_NOT_ACTIVE = 'CART_NOT_ACTIVE'; |
| 37 | +- private const ERROR_GUEST_EMAIL_MISSING = 'GUEST_EMAIL_MISSING'; |
| 38 | +- private const ERROR_UNABLE_TO_PLACE_ORDER = 'UNABLE_TO_PLACE_ORDER'; |
| 39 | +- private const ERROR_UNDEFINED = 'UNDEFINED'; |
| 40 | +- /**#@-*/ |
| 41 | ++ private $getCartForCheckout; |
| 42 | + |
| 43 | + /** |
| 44 | +- * List of error messages and codes. |
| 45 | ++ * @var PlaceOrderModel |
| 46 | + */ |
| 47 | +- private const MESSAGE_CODES = [ |
| 48 | +- 'Could not find a cart with ID' => self::ERROR_CART_NOT_FOUND, |
| 49 | +- 'The cart isn\'t active' => self::ERROR_CART_NOT_ACTIVE, |
| 50 | +- 'Guest email for cart is missing' => self::ERROR_GUEST_EMAIL_MISSING, |
| 51 | +- 'A server error stopped your order from being placed. Please try to place your order again' => |
| 52 | +- self::ERROR_UNABLE_TO_PLACE_ORDER, |
| 53 | +- 'Some addresses can\'t be used due to the configurations for specific countries' => |
| 54 | +- self::ERROR_UNABLE_TO_PLACE_ORDER, |
| 55 | +- 'The shipping method is missing. Select the shipping method and try again' => |
| 56 | +- self::ERROR_UNABLE_TO_PLACE_ORDER, |
| 57 | +- 'Please check the billing address information' => self::ERROR_UNABLE_TO_PLACE_ORDER, |
| 58 | +- 'Enter a valid payment method and try again' => self::ERROR_UNABLE_TO_PLACE_ORDER, |
| 59 | +- 'Some of the products are out of stock' => self::ERROR_UNABLE_TO_PLACE_ORDER, |
| 60 | +- ]; |
| 61 | ++ private $placeOrder; |
| 62 | + |
| 63 | + /** |
| 64 | +- * @var \string[] |
| 65 | ++ * @var OrderRepositoryInterface |
| 66 | + */ |
| 67 | +- private $errors = []; |
| 68 | ++ private $orderRepository; |
| 69 | ++ |
| 70 | ++ /** |
| 71 | ++ * @var AggregateExceptionMessageFormatter |
| 72 | ++ */ |
| 73 | ++ private $errorMessageFormatter; |
| 74 | ++ |
| 75 | ++ /** |
| 76 | ++ * @var PlaceOrderMutexInterface |
| 77 | ++ */ |
| 78 | ++ private $placeOrderMutex; |
| 79 | + |
| 80 | + /** |
| 81 | + * @param GetCartForCheckout $getCartForCheckout |
| 82 | + * @param PlaceOrderModel $placeOrder |
| 83 | + * @param OrderRepositoryInterface $orderRepository |
| 84 | ++ * @param AggregateExceptionMessageFormatter $errorMessageFormatter |
| 85 | ++ * @param PlaceOrderMutexInterface|null $placeOrderMutex |
| 86 | + */ |
| 87 | + public function __construct( |
| 88 | +- private readonly GetCartForCheckout $getCartForCheckout, |
| 89 | +- private readonly PlaceOrderModel $placeOrder, |
| 90 | +- private readonly OrderRepositoryInterface $orderRepository, |
| 91 | ++ GetCartForCheckout $getCartForCheckout, |
| 92 | ++ PlaceOrderModel $placeOrder, |
| 93 | ++ OrderRepositoryInterface $orderRepository, |
| 94 | ++ AggregateExceptionMessageFormatter $errorMessageFormatter, |
| 95 | ++ ?PlaceOrderMutexInterface $placeOrderMutex = null |
| 96 | + ) { |
| 97 | ++ $this->getCartForCheckout = $getCartForCheckout; |
| 98 | ++ $this->placeOrder = $placeOrder; |
| 99 | ++ $this->orderRepository = $orderRepository; |
| 100 | ++ $this->errorMessageFormatter = $errorMessageFormatter; |
| 101 | ++ $this->placeOrderMutex = $placeOrderMutex ?: ObjectManager::getInstance()->get(PlaceOrderMutexInterface::class); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | +@@ -74,12 +76,29 @@ public function __construct( |
| 106 | + */ |
| 107 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 108 | + { |
| 109 | +- $this->errors = []; |
| 110 | +- $order = null; |
| 111 | + if (empty($args['input']['cart_id'])) { |
| 112 | + throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); |
| 113 | + } |
| 114 | + |
| 115 | ++ return $this->placeOrderMutex->execute( |
| 116 | ++ $args['input']['cart_id'], |
| 117 | ++ \Closure::fromCallable([$this, 'run']), |
| 118 | ++ [$field, $context, $info, $args] |
| 119 | ++ ); |
| 120 | ++ } |
| 121 | ++ |
| 122 | ++ /** |
| 123 | ++ * Run the resolver. |
| 124 | ++ * |
| 125 | ++ * @param Field $field |
| 126 | ++ * @param ContextInterface $context |
| 127 | ++ * @param ResolveInfo $info |
| 128 | ++ * @param array|null $args |
| 129 | ++ * @return array[] |
| 130 | ++ * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
| 131 | ++ */ |
| 132 | ++ private function run(Field $field, ContextInterface $context, ResolveInfo $info, ?array $args): array |
| 133 | ++ { |
| 134 | + $maskedCartId = $args['input']['cart_id']; |
| 135 | + $userId = (int)$context->getUserId(); |
| 136 | + $storeId = (int)$context->getExtensionAttributes()->getStore()->getId(); |
| 137 | +@@ -88,69 +107,24 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value |
| 138 | + $cart = $this->getCartForCheckout->execute($maskedCartId, $userId, $storeId); |
| 139 | + $orderId = $this->placeOrder->execute($cart, $maskedCartId, $userId); |
| 140 | + $order = $this->orderRepository->get($orderId); |
| 141 | +- } catch (NoSuchEntityException $exception) { |
| 142 | +- $this->addError($exception->getMessage()); |
| 143 | +- } catch (GraphQlInputException $exception) { |
| 144 | +- $this->addError($exception->getMessage()); |
| 145 | +- } catch (AuthorizationException $exception) { |
| 146 | +- throw new GraphQlAuthorizationException( |
| 147 | +- __($exception->getMessage()) |
| 148 | +- ); |
| 149 | + } catch (LocalizedException $e) { |
| 150 | +- $this->addError($e->getMessage()); |
| 151 | +- } |
| 152 | +- if ($this->errors) { |
| 153 | +- return [ |
| 154 | +- 'errors' => |
| 155 | +- $this->errors |
| 156 | +- ]; |
| 157 | ++ throw $this->errorMessageFormatter->getFormatted( |
| 158 | ++ $e, |
| 159 | ++ __('Unable to place order: A server error stopped your order from being placed. ' . |
| 160 | ++ 'Please try to place your order again'), |
| 161 | ++ 'Unable to place order', |
| 162 | ++ $field, |
| 163 | ++ $context, |
| 164 | ++ $info |
| 165 | ++ ); |
| 166 | + } |
| 167 | ++ |
| 168 | + return [ |
| 169 | + 'order' => [ |
| 170 | + 'order_number' => $order->getIncrementId(), |
| 171 | + // @deprecated The order_id field is deprecated, use order_number instead |
| 172 | + 'order_id' => $order->getIncrementId(), |
| 173 | + ], |
| 174 | +- 'errors' => [] |
| 175 | + ]; |
| 176 | + } |
| 177 | +- |
| 178 | +- /** |
| 179 | +- * Add order line item error |
| 180 | +- * |
| 181 | +- * @param string $message |
| 182 | +- * @return void |
| 183 | +- */ |
| 184 | +- private function addError(string $message): void |
| 185 | +- { |
| 186 | +- $this->errors[] = [ |
| 187 | +- 'message' => $message, |
| 188 | +- 'code' => $this->getErrorCode($message) |
| 189 | +- ]; |
| 190 | +- } |
| 191 | +- |
| 192 | +- /** |
| 193 | +- * Get message error code. Ad-hoc solution based on message parsing. |
| 194 | +- * |
| 195 | +- * @param string $message |
| 196 | +- * @return string |
| 197 | +- */ |
| 198 | +- private function getErrorCode(string $message): string |
| 199 | +- { |
| 200 | +- $code = self::ERROR_UNDEFINED; |
| 201 | +- |
| 202 | +- $matchedCodes = array_filter( |
| 203 | +- self::MESSAGE_CODES, |
| 204 | +- function ($key) use ($message) { |
| 205 | +- return false !== strpos($message, $key); |
| 206 | +- }, |
| 207 | +- ARRAY_FILTER_USE_KEY |
| 208 | +- ); |
| 209 | +- |
| 210 | +- if (!empty($matchedCodes)) { |
| 211 | +- $code = current($matchedCodes); |
| 212 | +- } |
| 213 | +- |
| 214 | +- return $code; |
| 215 | +- } |
| 216 | + } |
| 217 | +diff --git a/vendor/magento/module-quote-graph-ql/etc/schema.graphqls b/vendor/magento/module-quote-graph-ql/etc/schema.graphqls |
| 218 | +index ffe6655e927e1..27433a30f3c92 100644 |
| 219 | +--- a/vendor/magento/module-quote-graph-ql/etc/schema.graphqls |
| 220 | ++++ b/vendor/magento/module-quote-graph-ql/etc/schema.graphqls |
| 221 | +@@ -202,13 +202,7 @@ type ApplyCouponToCartOutput @doc(description: "Contains details about the cart |
| 222 | + } |
| 223 | + |
| 224 | + type PlaceOrderOutput @doc(description: "Contains the results of the request to place an order.") { |
| 225 | +- order: Order @doc(description: "The ID of the order.") |
| 226 | +- errors: [PlaceOrderError!]! @doc(description:"An array of place order errors.") |
| 227 | +-} |
| 228 | +- |
| 229 | +-type PlaceOrderError @doc(description:"An error encountered while placing an order."){ |
| 230 | +- message: String! @doc(description: "A localized error message.") |
| 231 | +- code: PlaceOrderErrorCodes! @doc(description: "An error code that is specific to place order.") |
| 232 | ++ order: Order! @doc(description: "The ID of the order.") |
| 233 | + } |
| 234 | + |
| 235 | + type Cart @doc(description: "Contains the contents and other details about a guest or customer cart.") { |
| 236 | +@@ -423,11 +417,4 @@ enum CartUserInputErrorType { |
| 237 | + INSUFFICIENT_STOCK |
| 238 | + UNDEFINED |
| 239 | + } |
| 240 | +-enum PlaceOrderErrorCodes { |
| 241 | +- CART_NOT_FOUND |
| 242 | +- CART_NOT_ACTIVE |
| 243 | +- GUEST_EMAIL_MISSING |
| 244 | +- UNABLE_TO_PLACE_ORDER |
| 245 | +- UNDEFINED |
| 246 | +-} |
| 247 | + |
0 commit comments