PaymentMethodList.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Kp\Model;
  11. use Klarna\Kp\Api\QuoteRepositoryInterface;
  12. use Klarna\Kp\Model\Payment\Kp;
  13. use Magento\Framework\Exception\NoSuchEntityException;
  14. use Magento\Payment\Model\Method\Factory;
  15. use Magento\Quote\Api\Data\CartInterface;
  16. use Psr\Log\LoggerInterface;
  17. /**
  18. * Class PaymentMethodList
  19. *
  20. * @package Klarna\Kp\Model
  21. */
  22. class PaymentMethodList implements \Klarna\Kp\Api\PaymentMethodListInterface
  23. {
  24. /**
  25. * Factory for payment method models
  26. *
  27. * @var Factory
  28. */
  29. private $methodFactory;
  30. /**
  31. * @var QuoteRepositoryInterface
  32. */
  33. private $quoteRepository;
  34. /**
  35. * @var LoggerInterface
  36. */
  37. private $log;
  38. /**
  39. * @var \Klarna\Kp\Model\Payment\Kp[]
  40. */
  41. private $paymentMethods = [];
  42. /**
  43. * PaymentMethodList constructor.
  44. *
  45. * @param Factory $methodFactory
  46. * @param QuoteRepositoryInterface $quoteRepository
  47. * @param LoggerInterface $log
  48. */
  49. public function __construct(
  50. Factory $methodFactory,
  51. QuoteRepositoryInterface $quoteRepository,
  52. LoggerInterface $log
  53. ) {
  54. $this->methodFactory = $methodFactory;
  55. $this->quoteRepository = $quoteRepository;
  56. $this->log = $log;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getKlarnaMethodCodes(CartInterface $quote = null)
  62. {
  63. if (!$quote) {
  64. return [];
  65. }
  66. try {
  67. return $this->quoteRepository->getActiveByQuote($quote)->getPaymentMethods();
  68. } catch (NoSuchEntityException $e) {
  69. return [];
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getPaymentMethod($method)
  76. {
  77. if (!isset($this->paymentMethods[$method])) {
  78. $this->paymentMethods[$method] = $this->methodFactory->create(\Klarna\Kp\Model\Payment\Kp::class)
  79. ->setCode($method);
  80. }
  81. return $this->paymentMethods[$method];
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getKlarnaMethodInfo(CartInterface $quote)
  87. {
  88. try {
  89. return $this->quoteRepository->getActiveByQuote($quote)->getPaymentMethodInfo();
  90. } catch (NoSuchEntityException $e) {
  91. return null;
  92. }
  93. }
  94. }