DataPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * This file is part of the Klarna Kp module
  4. *
  5. * (c) Klarna AB
  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\Plugin\Payment\Helper;
  11. use Klarna\Kp\Api\PaymentMethodListInterface;
  12. use Klarna\Kp\Model\Payment\Kp;
  13. use Magento\Checkout\Model\Session;
  14. use Magento\Framework\App\Config\ScopeConfigInterface;
  15. use Magento\Framework\App\RequestInterface;
  16. use Magento\Framework\Exception\LocalizedException;
  17. use Magento\Framework\Exception\NoSuchEntityException;
  18. use Magento\Payment\Model\MethodInterface;
  19. use Magento\Quote\Api\CartRepositoryInterface;
  20. use Magento\Quote\Api\Data\CartInterface;
  21. use Magento\Sales\Api\OrderRepositoryInterface;
  22. use Magento\Store\Model\StoreManagerInterface;
  23. use Magento\Store\Model\ScopeInterface;
  24. /**
  25. * Class DataPlugin
  26. *
  27. * @package Klarna\Kp\Plugin\Payment\Helper
  28. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  29. */
  30. class DataPlugin
  31. {
  32. /**
  33. * @var CartInterface
  34. */
  35. private $quote;
  36. /**
  37. * @var OrderRepositoryInterface
  38. */
  39. private $orderRepository;
  40. /**
  41. * @var RequestInterface
  42. */
  43. private $request;
  44. /**
  45. * @var CartRepositoryInterface
  46. */
  47. private $mageQuoteRepository;
  48. /**
  49. * @var Session
  50. */
  51. private $session;
  52. /**
  53. * @var PaymentMethodListInterface
  54. */
  55. private $paymentMethodList;
  56. /**
  57. * @var ScopeConfigInterface
  58. */
  59. private $config;
  60. /**
  61. * @var StoreManagerInterface $storeManager
  62. */
  63. private $storeManager;
  64. /**
  65. * DataPlugin constructor.
  66. *
  67. * @param RequestInterface $request
  68. * @param OrderRepositoryInterface $orderRepository
  69. * @param CartRepositoryInterface $mageQuoteRepository
  70. * @param Session $session
  71. * @param ScopeConfigInterface $config
  72. * @param PaymentMethodListInterface $paymentMethodList
  73. */
  74. public function __construct(
  75. RequestInterface $request,
  76. OrderRepositoryInterface $orderRepository,
  77. CartRepositoryInterface $mageQuoteRepository,
  78. Session $session,
  79. ScopeConfigInterface $config,
  80. PaymentMethodListInterface $paymentMethodList,
  81. StoreManagerInterface $storeManager
  82. ) {
  83. $this->request = $request;
  84. $this->orderRepository = $orderRepository;
  85. $this->mageQuoteRepository = $mageQuoteRepository;
  86. $this->session = $session;
  87. $this->config = $config;
  88. $this->paymentMethodList = $paymentMethodList;
  89. $this->storeManager = $storeManager;
  90. }
  91. /**
  92. * Modify results of getPaymentMethods() call to add in Klarna methods returned by API
  93. *
  94. * @param \Magento\Payment\Helper\Data $subject
  95. * @param $result
  96. * @return array
  97. * @SuppressWarnings(PMD.UnusedFormalParameter)
  98. * @throws NoSuchEntityException
  99. */
  100. public function afterGetPaymentMethods(\Magento\Payment\Helper\Data $subject, $result)
  101. {
  102. $store = $this->storeManager->getStore();
  103. $scope = ($store === null ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORES);
  104. if (!$this->config->isSetFlag('payment/' . Kp::METHOD_CODE . '/active', $scope, $store)) {
  105. return $result;
  106. }
  107. $quote = $this->getQuote();
  108. if (!$quote) {
  109. return $result;
  110. }
  111. $methods = $this->paymentMethodList->getKlarnaMethodInfo($quote);
  112. if (empty($methods)) {
  113. return $result;
  114. }
  115. foreach ($methods as $method) {
  116. $code = 'klarna_' . $method->identifier;
  117. $result[$code] = $result['klarna_kp'];
  118. $result[$code]['title'] = $method->name;
  119. }
  120. return $result;
  121. }
  122. /**
  123. * @return CartInterface|\Magento\Quote\Model\Quote|null
  124. */
  125. private function getQuote()
  126. {
  127. if ($this->quote) {
  128. return $this->quote;
  129. }
  130. try {
  131. if ($order = $this->getOrder()) {
  132. $this->quote = $this->mageQuoteRepository->get($order->getQuoteId());
  133. return $this->quote;
  134. }
  135. $this->quote = $this->session->getQuote();
  136. } catch (NoSuchEntityException $e) {
  137. return null;
  138. }
  139. return $this->quote;
  140. }
  141. /**
  142. * @return \Magento\Sales\Api\Data\OrderInterface|bool
  143. */
  144. private function getOrder()
  145. {
  146. $id = $this->request->getParam('order_id');
  147. if (!$id) {
  148. return false;
  149. }
  150. try {
  151. return $this->orderRepository->get($id);
  152. } catch (LocalizedException $e) {
  153. return false;
  154. }
  155. }
  156. /**
  157. * Modify results of getMethodInstance() call to add in details about Klarna payment methods
  158. *
  159. * @param \Magento\Payment\Helper\Data $subject
  160. * @param callable $proceed
  161. * @param string $code
  162. * @return MethodInterface
  163. * @throws \Magento\Framework\Exception\LocalizedException
  164. * @SuppressWarnings(PMD.UnusedFormalParameter)
  165. */
  166. public function aroundGetMethodInstance(\Magento\Payment\Helper\Data $subject, callable $proceed, $code)
  167. {
  168. if (false === strpos($code, 'klarna_')) {
  169. return $proceed($code);
  170. }
  171. if ($code === 'klarna_kco') {
  172. return $proceed($code);
  173. }
  174. return $this->paymentMethodList->getPaymentMethod($code);
  175. }
  176. }