request = $request; $this->orderRepository = $orderRepository; $this->mageQuoteRepository = $mageQuoteRepository; $this->session = $session; $this->config = $config; $this->paymentMethodList = $paymentMethodList; $this->storeManager = $storeManager; } /** * Modify results of getPaymentMethods() call to add in Klarna methods returned by API * * @param \Magento\Payment\Helper\Data $subject * @param $result * @return array * @SuppressWarnings(PMD.UnusedFormalParameter) * @throws NoSuchEntityException */ public function afterGetPaymentMethods(\Magento\Payment\Helper\Data $subject, $result) { $store = $this->storeManager->getStore(); $scope = ($store === null ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORES); if (!$this->config->isSetFlag('payment/' . Kp::METHOD_CODE . '/active', $scope, $store)) { return $result; } $quote = $this->getQuote(); if (!$quote) { return $result; } $methods = $this->paymentMethodList->getKlarnaMethodInfo($quote); if (empty($methods)) { return $result; } foreach ($methods as $method) { $code = 'klarna_' . $method->identifier; $result[$code] = $result['klarna_kp']; $result[$code]['title'] = $method->name; } return $result; } /** * @return CartInterface|\Magento\Quote\Model\Quote|null */ private function getQuote() { if ($this->quote) { return $this->quote; } try { if ($order = $this->getOrder()) { $this->quote = $this->mageQuoteRepository->get($order->getQuoteId()); return $this->quote; } $this->quote = $this->session->getQuote(); } catch (NoSuchEntityException $e) { return null; } return $this->quote; } /** * @return \Magento\Sales\Api\Data\OrderInterface|bool */ private function getOrder() { $id = $this->request->getParam('order_id'); if (!$id) { return false; } try { return $this->orderRepository->get($id); } catch (LocalizedException $e) { return false; } } /** * Modify results of getMethodInstance() call to add in details about Klarna payment methods * * @param \Magento\Payment\Helper\Data $subject * @param callable $proceed * @param string $code * @return MethodInterface * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PMD.UnusedFormalParameter) */ public function aroundGetMethodInstance(\Magento\Payment\Helper\Data $subject, callable $proceed, $code) { if (false === strpos($code, 'klarna_')) { return $proceed($code); } if ($code === 'klarna_kco') { return $proceed($code); } return $this->paymentMethodList->getPaymentMethod($code); } }