1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* This file is part of the Klarna Kp module
*
* (c) Klarna AB
*
* For the full copyright and license information, please view the NOTICE
* and LICENSE files that were distributed with this source code.
*/
namespace Klarna\Kp\Plugin\Payment\Helper;
use Klarna\Kp\Api\PaymentMethodListInterface;
use Klarna\Kp\Model\Payment\Kp;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Model\MethodInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;
/**
* Class DataPlugin
*
* @package Klarna\Kp\Plugin\Payment\Helper
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DataPlugin
{
/**
* @var CartInterface
*/
private $quote;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var RequestInterface
*/
private $request;
/**
* @var CartRepositoryInterface
*/
private $mageQuoteRepository;
/**
* @var Session
*/
private $session;
/**
* @var PaymentMethodListInterface
*/
private $paymentMethodList;
/**
* @var ScopeConfigInterface
*/
private $config;
/**
* @var StoreManagerInterface $storeManager
*/
private $storeManager;
/**
* DataPlugin constructor.
*
* @param RequestInterface $request
* @param OrderRepositoryInterface $orderRepository
* @param CartRepositoryInterface $mageQuoteRepository
* @param Session $session
* @param ScopeConfigInterface $config
* @param PaymentMethodListInterface $paymentMethodList
*/
public function __construct(
RequestInterface $request,
OrderRepositoryInterface $orderRepository,
CartRepositoryInterface $mageQuoteRepository,
Session $session,
ScopeConfigInterface $config,
PaymentMethodListInterface $paymentMethodList,
StoreManagerInterface $storeManager
) {
$this->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);
}
}