PaymentMethodList.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Model;
  7. use Magento\Payment\Api\Data\PaymentMethodInterface;
  8. use Magento\Payment\Api\PaymentMethodListInterface;
  9. use Magento\Payment\Model\Method\InstanceFactory;
  10. use Magento\Payment\Model\MethodInterface;
  11. use Magento\Vault\Api\PaymentMethodListInterface as VaultPaymentMethodListInterface;
  12. /**
  13. * Contains methods to retrieve configured vault payments
  14. */
  15. class PaymentMethodList implements VaultPaymentMethodListInterface
  16. {
  17. /**
  18. * @var InstanceFactory
  19. */
  20. private $instanceFactory;
  21. /**
  22. * @var PaymentMethodListInterface
  23. */
  24. private $paymentMethodList;
  25. /**
  26. * PaymentMethodList constructor.
  27. * @param PaymentMethodListInterface $paymentMethodList
  28. * @param InstanceFactory $instanceFactory
  29. */
  30. public function __construct(PaymentMethodListInterface $paymentMethodList, InstanceFactory $instanceFactory)
  31. {
  32. $this->instanceFactory = $instanceFactory;
  33. $this->paymentMethodList = $paymentMethodList;
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function getList($storeId)
  39. {
  40. return $this->filterList($this->paymentMethodList->getList($storeId));
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function getActiveList($storeId)
  46. {
  47. return $this->filterList($this->paymentMethodList->getActiveList($storeId));
  48. }
  49. /**
  50. * Filter vault methods from payments
  51. * @param PaymentMethodInterface[] $list
  52. * @return VaultPaymentInterface[]
  53. */
  54. private function filterList(array $list)
  55. {
  56. $paymentMethods = array_map(
  57. function (PaymentMethodInterface $paymentMethod) {
  58. return $this->instanceFactory->create($paymentMethod);
  59. },
  60. $list
  61. );
  62. $availableMethods = array_filter(
  63. $paymentMethods,
  64. function (MethodInterface $methodInstance) {
  65. return $methodInstance instanceof VaultPaymentInterface;
  66. }
  67. );
  68. return array_values($availableMethods);
  69. }
  70. }