|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2011 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 |
|
7 | 7 | namespace Magento\Sales\Model\AdminOrder;
|
|
12 | 12 | use Magento\Framework\Api\ExtensibleDataObjectConverter;
|
13 | 13 | use Magento\Framework\App\ObjectManager;
|
14 | 14 | use Magento\Framework\Exception\LocalizedException;
|
| 15 | +use Magento\Payment\Model\Checks\SpecificationFactory; |
| 16 | +use Magento\Payment\Model\MethodInterface; |
15 | 17 | use Magento\Quote\Model\Quote\Address;
|
16 | 18 | use Magento\Quote\Model\Quote\Address\CustomAttributeListInterface;
|
17 | 19 | use Magento\Quote\Model\Quote\Item;
|
@@ -271,6 +273,26 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
|
271 | 273 | */
|
272 | 274 | private $request;
|
273 | 275 |
|
| 276 | + /** |
| 277 | + * @var SpecificationFactory |
| 278 | + */ |
| 279 | + private $paymentMethodSpecificationFactory; |
| 280 | + |
| 281 | + /** |
| 282 | + * List of payment method specifications |
| 283 | + * |
| 284 | + * array( |
| 285 | + * array( |
| 286 | + * 'type' => 'zero_total', |
| 287 | + * 'sortOrder' => 100, |
| 288 | + * 'disabled' => false |
| 289 | + * ) |
| 290 | + * ) |
| 291 | + * |
| 292 | + * @var array |
| 293 | + */ |
| 294 | + private $paymentMethodSpecifications; |
| 295 | + |
274 | 296 | /**
|
275 | 297 | * @param \Magento\Framework\ObjectManagerInterface $objectManager
|
276 | 298 | * @param \Magento\Framework\Event\ManagerInterface $eventManager
|
@@ -306,6 +328,8 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
|
306 | 328 | * @param CustomAttributeListInterface|null $customAttributeList
|
307 | 329 | * @param OrderRepositoryInterface|null $orderRepositoryInterface
|
308 | 330 | * @param HttpRequest|null $request
|
| 331 | + * @param SpecificationFactory|null $paymentMethodSpecificationFactory |
| 332 | + * @param array $paymentMethodSpecifications |
309 | 333 | * @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
310 | 334 | */
|
311 | 335 | public function __construct(
|
@@ -342,7 +366,9 @@ public function __construct(
|
342 | 366 | StoreManagerInterface $storeManager = null,
|
343 | 367 | CustomAttributeListInterface $customAttributeList = null,
|
344 | 368 | OrderRepositoryInterface $orderRepositoryInterface = null,
|
345 |
| - HttpRequest $request = null |
| 369 | + HttpRequest $request = null, |
| 370 | + ?SpecificationFactory $paymentMethodSpecificationFactory = null, |
| 371 | + array $paymentMethodSpecifications = [] |
346 | 372 | ) {
|
347 | 373 | $this->_objectManager = $objectManager;
|
348 | 374 | $this->_eventManager = $eventManager;
|
@@ -383,6 +409,9 @@ public function __construct(
|
383 | 409 | ->get(OrderRepositoryInterface::class);
|
384 | 410 | $this->request = $request ?: ObjectManager::getInstance()
|
385 | 411 | ->get(HttpRequest::class);
|
| 412 | + $this->paymentMethodSpecificationFactory = $paymentMethodSpecificationFactory ?: ObjectManager::getInstance() |
| 413 | + ->get(SpecificationFactory::class); |
| 414 | + $this->paymentMethodSpecifications = $paymentMethodSpecifications; |
386 | 415 | }
|
387 | 416 |
|
388 | 417 | /**
|
@@ -2185,7 +2214,7 @@ protected function _validate()
|
2185 | 2214 | $this->_errors[] = __("The payment method isn't selected. Enter the payment method and try again.");
|
2186 | 2215 | } else {
|
2187 | 2216 | $method = $this->getQuote()->getPayment()->getMethodInstance();
|
2188 |
| - if (!$method->isAvailable($this->getQuote())) { |
| 2217 | + if (!$this->isPaymentMethodApplicable($method, $this->getQuote())) { |
2189 | 2218 | $this->_errors[] = __('This payment method is not available.');
|
2190 | 2219 | } else {
|
2191 | 2220 | try {
|
@@ -2351,4 +2380,49 @@ private function removeCartTransferredItemsAndUpdateQty(int|null|Item $cartItem,
|
2351 | 2380 | $this->getSession()->setTransferredItems($removeCartTransferredItems);
|
2352 | 2381 | }
|
2353 | 2382 | }
|
| 2383 | + |
| 2384 | + /** |
| 2385 | + * Check if payment method is applicable to quote |
| 2386 | + * |
| 2387 | + * @param MethodInterface $method |
| 2388 | + * @param Quote $quote |
| 2389 | + * @return bool |
| 2390 | + * @throws LocalizedException |
| 2391 | + */ |
| 2392 | + private function isPaymentMethodApplicable(MethodInterface $method, Quote $quote): bool |
| 2393 | + { |
| 2394 | + if (!$method->isAvailable($this->getQuote())) { |
| 2395 | + return false; |
| 2396 | + } |
| 2397 | + |
| 2398 | + $specifications = $this->getPaymentMethodSpecifications(); |
| 2399 | + |
| 2400 | + if (!empty($specifications)) { |
| 2401 | + return $this->paymentMethodSpecificationFactory->create($specifications)->isApplicable($method, $quote); |
| 2402 | + } |
| 2403 | + |
| 2404 | + return true; |
| 2405 | + } |
| 2406 | + |
| 2407 | + /** |
| 2408 | + * Get payment method specifications |
| 2409 | + * |
| 2410 | + * @return array |
| 2411 | + */ |
| 2412 | + private function getPaymentMethodSpecifications(): array |
| 2413 | + { |
| 2414 | + $specifications = array_filter( |
| 2415 | + $this->paymentMethodSpecifications, |
| 2416 | + fn (array $spec) => !isset($spec['disabled']) || $spec['disabled'] === false |
| 2417 | + ); |
| 2418 | + |
| 2419 | + usort( |
| 2420 | + $specifications, |
| 2421 | + fn (array $a, array $b) => ($a['sortOrder'] ?? 0) <=> ($b['sortOrder'] ?? 0) |
| 2422 | + ); |
| 2423 | + |
| 2424 | + $specifications = array_column($specifications, 'type', 'type'); |
| 2425 | + |
| 2426 | + return array_values($specifications); |
| 2427 | + } |
2354 | 2428 | }
|
0 commit comments