PaymentConfigurationProcess.php 2.4 KB

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