Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentVaultConfigurationProcess.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Vault\Plugin;
7 
14 {
18  private $vaultPaymentList;
19 
23  private $paymentMethodList;
24 
28  private $storeManager;
29 
35  public function __construct(
36  \Magento\Vault\Api\PaymentMethodListInterface $vaultPaymentList,
37  \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList,
38  \Magento\Store\Model\StoreManagerInterface $storeManager
39  ) {
40  $this->vaultPaymentList = $vaultPaymentList;
41  $this->paymentMethodList = $paymentMethodList;
42  $this->storeManager = $storeManager;
43  }
44 
53  public function beforeProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)
54  {
55  $configuration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
56  ['children']['payment']['children']['renders']['children'];
57 
58  if (!isset($configuration)) {
59  return [$jsLayout];
60  }
61 
62  $storeId = $this->storeManager->getStore()->getId();
63  $activePaymentMethodList = $this->paymentMethodList->getActiveList($storeId);
64  $activeVaultList = $this->vaultPaymentList->getActiveList($storeId);
65  $getCodeFunc = function ($method) {
66  return $method->getCode();
67  };
68  $getProviderCodeFunc = function ($method) {
69  return $method->getProviderCode();
70  };
71  $activePaymentMethodCodes = array_map($getCodeFunc, $activePaymentMethodList);
72  $activeVaultProviderCodes = array_map($getProviderCodeFunc, $activeVaultList);
73  $activePaymentMethodCodes = array_merge(
74  $activePaymentMethodCodes,
75  $activeVaultProviderCodes
76  );
77 
78  foreach ($configuration as $paymentGroup => $groupConfig) {
79  $notActivePaymentMethodCodes = array_diff(array_keys($groupConfig['methods']), $activePaymentMethodCodes);
80  foreach ($notActivePaymentMethodCodes as $notActivePaymentMethodCode) {
81  unset($configuration[$paymentGroup]['methods'][$notActivePaymentMethodCode]);
82  }
83  if ($paymentGroup === 'vault' && !empty($activeVaultProviderCodes)) {
84  continue;
85  }
86  if (empty($configuration[$paymentGroup]['methods'])) {
87  unset($configuration[$paymentGroup]);
88  }
89  }
90 
91  return [$jsLayout];
92  }
93 }
__construct(\Magento\Vault\Api\PaymentMethodListInterface $vaultPaymentList, \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList, \Magento\Store\Model\StoreManagerInterface $storeManager)
$configuration
Definition: index.php:33
$processor
Definition: 404.php:10
$storeManager
$method
Definition: info.phtml:13
beforeProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)