|
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
8 |
| -namespace Magento\QuoteGraphQl\Model\Cart\PaymentMethod; |
| 8 | +namespace Magento\QuoteGraphQl\Model\Resolver; |
9 | 9 |
|
10 | 10 | use Magento\Checkout\Api\PaymentInformationManagementInterface;
|
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 13 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 14 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
11 | 15 | use Magento\Quote\Api\Data\CartInterface;
|
12 | 16 |
|
13 | 17 | /**
|
14 |
| - * Get array of available payment methods. |
| 18 | + * Get list of active payment methods resolver. |
15 | 19 | */
|
16 |
| -class AvailablePaymentMethodsDataProvider |
| 20 | +class AvailablePaymentMethods implements ResolverInterface |
17 | 21 | {
|
18 | 22 | /**
|
19 | 23 | * @var PaymentInformationManagementInterface
|
20 | 24 | */
|
21 | 25 | private $informationManagement;
|
22 | 26 |
|
23 | 27 | /**
|
24 |
| - * AvailablePaymentMethodsDataProvider constructor. |
25 | 28 | * @param PaymentInformationManagementInterface $informationManagement
|
26 | 29 | */
|
27 | 30 | public function __construct(PaymentInformationManagementInterface $informationManagement)
|
28 | 31 | {
|
29 | 32 | $this->informationManagement = $informationManagement;
|
30 | 33 | }
|
31 | 34 |
|
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 39 | + { |
| 40 | + if (!isset($value['model'])) { |
| 41 | + throw new LocalizedException(__('"model" value should be specified')); |
| 42 | + } |
| 43 | + |
| 44 | + $cart = $value['model']; |
| 45 | + return $this->getPaymentMethodsData($cart); |
| 46 | + } |
| 47 | + |
32 | 48 | /**
|
33 | 49 | * Collect and return information about available payment methods
|
34 | 50 | *
|
35 | 51 | * @param CartInterface $cart
|
36 | 52 | * @return array
|
37 | 53 | */
|
38 |
| - public function getPaymentMethods(CartInterface $cart): array |
| 54 | + private function getPaymentMethodsData(CartInterface $cart): array |
39 | 55 | {
|
40 | 56 | $paymentInformation = $this->informationManagement->getPaymentInformation($cart->getId());
|
41 | 57 | $paymentMethods = $paymentInformation->getPaymentMethods();
|
42 | 58 |
|
43 |
| - $paymentMethodsNested = []; |
| 59 | + $paymentMethodsData = []; |
44 | 60 | foreach ($paymentMethods as $paymentMethod) {
|
45 |
| - $paymentMethodsNested[] = [ |
| 61 | + $paymentMethodsData[] = [ |
46 | 62 | 'title' => $paymentMethod->getTitle(),
|
47 |
| - 'code' => $paymentMethod->getCode() |
| 63 | + 'code' => $paymentMethod->getCode(), |
48 | 64 | ];
|
49 | 65 | }
|
50 |
| - |
51 |
| - return $paymentMethodsNested; |
| 66 | + return $paymentMethodsData; |
52 | 67 | }
|
53 | 68 | }
|
0 commit comments