MethodList.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Framework\App\ObjectManager;
  8. use Magento\Payment\Model\Method\AbstractMethod;
  9. /**
  10. * Methods List service class.
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class MethodList
  16. {
  17. /**
  18. * @var \Magento\Payment\Helper\Data
  19. * @deprecated 100.1.0 Do not use this property in case of inheritance.
  20. */
  21. protected $paymentHelper;
  22. /**
  23. * @var \Magento\Payment\Model\Checks\SpecificationFactory
  24. * @deprecated 100.2.0 Do not use this property in case of inheritance.
  25. */
  26. protected $methodSpecificationFactory;
  27. /**
  28. * @var \Magento\Payment\Api\PaymentMethodListInterface
  29. */
  30. private $paymentMethodList;
  31. /**
  32. * @var \Magento\Payment\Model\Method\InstanceFactory
  33. */
  34. private $paymentMethodInstanceFactory;
  35. /**
  36. * @param \Magento\Payment\Helper\Data $paymentHelper
  37. * @param Checks\SpecificationFactory $specificationFactory
  38. */
  39. public function __construct(
  40. \Magento\Payment\Helper\Data $paymentHelper,
  41. \Magento\Payment\Model\Checks\SpecificationFactory $specificationFactory
  42. ) {
  43. $this->paymentHelper = $paymentHelper;
  44. $this->methodSpecificationFactory = $specificationFactory;
  45. }
  46. /**
  47. * @param \Magento\Quote\Api\Data\CartInterface $quote
  48. * @return \Magento\Payment\Model\MethodInterface[]
  49. */
  50. public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote = null)
  51. {
  52. $store = $quote ? $quote->getStoreId() : null;
  53. $availableMethods = [];
  54. foreach ($this->getPaymentMethodList()->getActiveList($store) as $method) {
  55. $methodInstance = $this->getPaymentMethodInstanceFactory()->create($method);
  56. if ($methodInstance->isAvailable($quote) && $this->_canUseMethod($methodInstance, $quote)) {
  57. $methodInstance->setInfoInstance($quote->getPayment());
  58. $availableMethods[] = $methodInstance;
  59. }
  60. }
  61. return $availableMethods;
  62. }
  63. /**
  64. * Check payment method model
  65. *
  66. * @param \Magento\Payment\Model\MethodInterface $method
  67. * @param \Magento\Quote\Api\Data\CartInterface $quote
  68. * @return bool
  69. */
  70. protected function _canUseMethod($method, \Magento\Quote\Api\Data\CartInterface $quote)
  71. {
  72. return $this->methodSpecificationFactory->create(
  73. [
  74. AbstractMethod::CHECK_USE_CHECKOUT,
  75. AbstractMethod::CHECK_USE_FOR_COUNTRY,
  76. AbstractMethod::CHECK_USE_FOR_CURRENCY,
  77. AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  78. ]
  79. )->isApplicable(
  80. $method,
  81. $quote
  82. );
  83. }
  84. /**
  85. * Get payment method list.
  86. *
  87. * @return \Magento\Payment\Api\PaymentMethodListInterface
  88. */
  89. private function getPaymentMethodList()
  90. {
  91. if ($this->paymentMethodList === null) {
  92. $this->paymentMethodList = ObjectManager::getInstance()->get(
  93. \Magento\Payment\Api\PaymentMethodListInterface::class
  94. );
  95. }
  96. return $this->paymentMethodList;
  97. }
  98. /**
  99. * Get payment method instance factory.
  100. *
  101. * @return \Magento\Payment\Model\Method\InstanceFactory
  102. */
  103. private function getPaymentMethodInstanceFactory()
  104. {
  105. if ($this->paymentMethodInstanceFactory === null) {
  106. $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
  107. \Magento\Payment\Model\Method\InstanceFactory::class
  108. );
  109. }
  110. return $this->paymentMethodInstanceFactory;
  111. }
  112. }