SessionInitiator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Core\Exception as KlarnaException;
  12. use Klarna\Core\Model\Api\Exception as KlarnaApiException;
  13. use Klarna\Kp\Api\Data\ResponseInterface;
  14. use Klarna\Kp\Api\QuoteRepositoryInterface;
  15. use Klarna\Kp\Api\SessionInitiatorInterface;
  16. use Magento\Framework\App\Config\ScopeConfigInterface;
  17. use Magento\Framework\Exception\NoSuchEntityException;
  18. use Magento\Quote\Api\Data\CartInterface;
  19. use Magento\Store\Model\ScopeInterface;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * Class SessionInitiator
  23. *
  24. * @package Klarna\Kp\Model
  25. */
  26. class SessionInitiator implements SessionInitiatorInterface
  27. {
  28. /**
  29. * @var QuoteRepositoryInterface
  30. */
  31. private $quoteRepository;
  32. /**
  33. * @var LoggerInterface
  34. */
  35. private $log;
  36. /**
  37. * @var Session
  38. */
  39. private $session;
  40. /**
  41. * @var ScopeConfigInterface
  42. */
  43. private $config;
  44. /**
  45. * Constructor
  46. *
  47. * @param QuoteRepositoryInterface $quoteRepository
  48. * @param Session $session
  49. * @param LoggerInterface $log
  50. * @param ScopeConfigInterface $config
  51. */
  52. public function __construct(
  53. QuoteRepositoryInterface $quoteRepository,
  54. Session $session,
  55. LoggerInterface $log,
  56. ScopeConfigInterface $config
  57. ) {
  58. $this->quoteRepository = $quoteRepository;
  59. $this->session = $session;
  60. $this->log = $log;
  61. $this->config = $config;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function checkAvailable($quote, $code)
  67. {
  68. if (!$quote) {
  69. $quote = $this->checkQuote();
  70. }
  71. /** @var ResponseInterface $klarnaPayments */
  72. try {
  73. $klarnaPayments = $this->session->getApiResponse();
  74. if ($klarnaPayments === null) {
  75. $sessionId = $this->getSessionId($quote);
  76. $klarnaPayments = $this->session->init($sessionId);
  77. }
  78. return $klarnaPayments->isSuccessfull() && $this->checkMethodAvailable($quote, $code);
  79. } catch (NoSuchEntityException $e) {
  80. $this->log->error($e);
  81. return false;
  82. } catch (KlarnaApiException $e) {
  83. $this->log->error($e);
  84. return false;
  85. } catch (KlarnaException $e) {
  86. $this->log->error($e);
  87. return true;
  88. }
  89. }
  90. /**
  91. * @param \Magento\Quote\Model\Quote $quote
  92. * @return \Magento\Quote\Model\Quote
  93. */
  94. private function checkQuote()
  95. {
  96. $quote = $this->session->getQuote();
  97. if (!$quote) {
  98. return null;
  99. }
  100. $version = $this->config->getValue('klarna/api/api_version', ScopeInterface::SCOPE_STORES, $quote->getStore());
  101. if (!in_array($version, ['kp_na', 'kp_eu'], true)) {
  102. return null;
  103. }
  104. return $quote;
  105. }
  106. /**
  107. * @param CartInterface $quote
  108. * @return string
  109. */
  110. private function getSessionId(CartInterface $quote)
  111. {
  112. try {
  113. return $this->quoteRepository->getActiveByQuote($quote)->getSessionId();
  114. } catch (NoSuchEntityException $e) {
  115. return null;
  116. }
  117. }
  118. /**
  119. * Check to ensure that this payment method is in the list of methods returned by API
  120. *
  121. * @param CartInterface $quote
  122. * @param string $code
  123. * @return bool
  124. * @throws NoSuchEntityException
  125. */
  126. private function checkMethodAvailable(CartInterface $quote, $code)
  127. {
  128. $kQuote = $this->quoteRepository->getActiveByQuote($quote);
  129. return (in_array($code, $kQuote->getPaymentMethods()));
  130. }
  131. }