|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Vault\Plugin; |
| 9 | + |
| 10 | +use Magento\Checkout\Api\PaymentInformationManagementInterface; |
| 11 | +use Magento\Quote\Api\Data\AddressInterface; |
| 12 | +use Magento\Quote\Api\Data\PaymentInterface; |
| 13 | +use Magento\Vault\Model\PaymentMethodList; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Payment vault information management process |
| 18 | + */ |
| 19 | +class PaymentVaultInformationManagement |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var PaymentMethodList |
| 23 | + */ |
| 24 | + private $vaultPaymentMethodList; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var StoreManagerInterface |
| 28 | + */ |
| 29 | + private $storeManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * PaymentVaultInformationManagement constructor. |
| 33 | + * |
| 34 | + * @param PaymentMethodList $vaultPaymentMethodList |
| 35 | + * @param StoreManagerInterface $storeManager |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + PaymentMethodList $vaultPaymentMethodList, |
| 39 | + StoreManagerInterface $storeManager |
| 40 | + ) { |
| 41 | + $this->vaultPaymentMethodList = $vaultPaymentMethodList; |
| 42 | + $this->storeManager = $storeManager; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Set available vault method code without index to payment |
| 47 | + * |
| 48 | + * @param PaymentInformationManagementInterface $subject |
| 49 | + * @param string $cartId |
| 50 | + * @param PaymentInterface $paymentMethod |
| 51 | + * @param AddressInterface|null $billingAddress |
| 52 | + * @return void |
| 53 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 54 | + */ |
| 55 | + public function beforeSavePaymentInformation( |
| 56 | + PaymentInformationManagementInterface $subject, |
| 57 | + string $cartId, |
| 58 | + PaymentInterface $paymentMethod, |
| 59 | + AddressInterface $billingAddress = null |
| 60 | + ): void { |
| 61 | + $availableMethods = $this->vaultPaymentMethodList->getActiveList($this->storeManager->getStore()->getId()); |
| 62 | + foreach ($availableMethods as $availableMethod) { |
| 63 | + if (strpos($paymentMethod->getMethod(), $availableMethod->getCode()) !== false) { |
| 64 | + $paymentMethod->setMethod($availableMethod->getCode()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments