PaymentMethodList.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Model;
  7. use Magento\Payment\Api\Data\PaymentMethodInterface;
  8. /**
  9. * Payment method list class.
  10. */
  11. class PaymentMethodList implements \Magento\Payment\Api\PaymentMethodListInterface
  12. {
  13. /**
  14. * @var \Magento\Payment\Api\Data\PaymentMethodInterfaceFactory
  15. */
  16. private $methodFactory;
  17. /**
  18. * @var \Magento\Payment\Helper\Data
  19. */
  20. private $helper;
  21. /**
  22. * @param \Magento\Payment\Api\Data\PaymentMethodInterfaceFactory $methodFactory
  23. * @param \Magento\Payment\Helper\Data $helper
  24. */
  25. public function __construct(
  26. \Magento\Payment\Api\Data\PaymentMethodInterfaceFactory $methodFactory,
  27. \Magento\Payment\Helper\Data $helper
  28. ) {
  29. $this->methodFactory = $methodFactory;
  30. $this->helper = $helper;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getList($storeId)
  36. {
  37. $methodsCodes = array_keys($this->helper->getPaymentMethods());
  38. $methodsInstances = array_map(
  39. function ($code) {
  40. return $this->helper->getMethodInstance($code);
  41. },
  42. $methodsCodes
  43. );
  44. $methodsInstances = array_filter($methodsInstances, function (MethodInterface $method) {
  45. return !($method instanceof \Magento\Payment\Model\Method\Substitution);
  46. });
  47. @uasort(
  48. $methodsInstances,
  49. function (MethodInterface $a, MethodInterface $b) use ($storeId) {
  50. return (int)$a->getConfigData('sort_order', $storeId) - (int)$b->getConfigData('sort_order', $storeId);
  51. }
  52. );
  53. $methodList = array_map(
  54. function (MethodInterface $methodInstance) use ($storeId) {
  55. return $this->methodFactory->create([
  56. 'code' => (string)$methodInstance->getCode(),
  57. 'title' => (string)$methodInstance->getTitle(),
  58. 'storeId' => (int)$storeId,
  59. 'isActive' => (bool)$methodInstance->isActive($storeId)
  60. ]);
  61. },
  62. $methodsInstances
  63. );
  64. return array_values($methodList);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getActiveList($storeId)
  70. {
  71. $methodList = array_filter(
  72. $this->getList($storeId),
  73. function (PaymentMethodInterface $method) {
  74. return $method->getIsActive();
  75. }
  76. );
  77. return array_values($methodList);
  78. }
  79. }