PaymentVaultConfigurationProcess.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Plugin;
  7. /**
  8. * Class PaymentVaultConfigurationProcess
  9. *
  10. * Checks if vault group have active vaults.
  11. */
  12. class PaymentVaultConfigurationProcess
  13. {
  14. /**
  15. * @var \Magento\Vault\Api\PaymentMethodListInterface
  16. */
  17. private $vaultPaymentList;
  18. /**
  19. * @var \Magento\Vault\Api\PaymentMethodListInterface
  20. */
  21. private $paymentMethodList;
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface
  24. */
  25. private $storeManager;
  26. /**
  27. * @param \Magento\Vault\Api\PaymentMethodListInterface $vaultPaymentList
  28. * @param \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList
  29. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  30. */
  31. public function __construct(
  32. \Magento\Vault\Api\PaymentMethodListInterface $vaultPaymentList,
  33. \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList,
  34. \Magento\Store\Model\StoreManagerInterface $storeManager
  35. ) {
  36. $this->vaultPaymentList = $vaultPaymentList;
  37. $this->paymentMethodList = $paymentMethodList;
  38. $this->storeManager = $storeManager;
  39. }
  40. /**
  41. * Checkout LayoutProcessor before process plugin.
  42. *
  43. * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $processor
  44. * @param array $jsLayout
  45. * @return array
  46. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  47. */
  48. public function beforeProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)
  49. {
  50. $configuration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
  51. ['children']['payment']['children']['renders']['children'];
  52. if (!isset($configuration)) {
  53. return [$jsLayout];
  54. }
  55. $storeId = $this->storeManager->getStore()->getId();
  56. $activePaymentMethodList = $this->paymentMethodList->getActiveList($storeId);
  57. $activeVaultList = $this->vaultPaymentList->getActiveList($storeId);
  58. $getCodeFunc = function ($method) {
  59. return $method->getCode();
  60. };
  61. $getProviderCodeFunc = function ($method) {
  62. return $method->getProviderCode();
  63. };
  64. $activePaymentMethodCodes = array_map($getCodeFunc, $activePaymentMethodList);
  65. $activeVaultProviderCodes = array_map($getProviderCodeFunc, $activeVaultList);
  66. $activePaymentMethodCodes = array_merge(
  67. $activePaymentMethodCodes,
  68. $activeVaultProviderCodes
  69. );
  70. foreach ($configuration as $paymentGroup => $groupConfig) {
  71. $notActivePaymentMethodCodes = array_diff(array_keys($groupConfig['methods']), $activePaymentMethodCodes);
  72. foreach ($notActivePaymentMethodCodes as $notActivePaymentMethodCode) {
  73. unset($configuration[$paymentGroup]['methods'][$notActivePaymentMethodCode]);
  74. }
  75. if ($paymentGroup === 'vault' && !empty($activeVaultProviderCodes)) {
  76. continue;
  77. }
  78. if (empty($configuration[$paymentGroup]['methods'])) {
  79. unset($configuration[$paymentGroup]);
  80. }
  81. }
  82. return [$jsLayout];
  83. }
  84. }