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
<?php
/**
* This file is part of the Klarna KP module
*
* (c) Klarna Bank AB (publ)
*
* For the full copyright and license information, please view the NOTICE
* and LICENSE files that were distributed with this source code.
*/
namespace Klarna\Kp\Observer;
use Klarna\Kp\Api\QuoteInterface;
use Klarna\Kp\Api\QuoteRepositoryInterface;
use Klarna\Kp\Model\Payment\Kp;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
/**
* Class AssignData
*
* @package Klarna\Kp\Observer
*/
class AssignData extends AbstractDataAssignObserver
{
/**
* @var QuoteRepositoryInterface
*/
private $klarnaQuoteRepository;
/**
* @var LoggerInterface
*/
private $log;
/**
* @var DataObjectFactory
*/
private $dataObjectFactory;
/**
* @var ScopeConfigInterface
*/
private $config;
/**
* AssignData constructor.
*
* @param QuoteRepositoryInterface $klarnaQuoteRepository
* @param LoggerInterface $log
* @param DataObjectFactory $dataObjectFactory
* @param ScopeConfigInterface $config
*/
public function __construct(
QuoteRepositoryInterface $klarnaQuoteRepository,
LoggerInterface $log,
DataObjectFactory $dataObjectFactory,
ScopeConfigInterface $config
) {
$this->klarnaQuoteRepository = $klarnaQuoteRepository;
$this->log = $log;
$this->dataObjectFactory = $dataObjectFactory;
$this->config = $config;
}
/**
* Observer
*
* @param Observer $observer
* @return void
* @throws LocalizedException
*/
public function execute(Observer $observer)
{
$method = $this->readMethodArgument($observer);
if (false === strpos($method->getCode(), 'klarna_')) {
return;
}
$data = $this->readDataArgument($observer);
$additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
if (!is_array($additionalData)) {
return;
}
$additionalData = $this->dataObjectFactory->create(['data' => $additionalData]);
$payment = $this->readPaymentModelArgument($observer);
$quote = $payment->getQuote();
if (!$this->config->isSetFlag(
'payment/' . Kp::METHOD_CODE . '/active',
ScopeInterface::SCOPE_STORES,
$quote->getStore()
)) {
return;
}
try {
/** @var QuoteInterface $klarnaQuote */
$klarnaQuote = $this->klarnaQuoteRepository->getActiveByQuote($quote);
$klarnaQuote->setAuthorizationToken($additionalData->getData('authorization_token'));
$payment->setAdditionalInformation('method_title', $additionalData->getData('method_title'));
$payment->setAdditionalInformation('logo', $additionalData->getData('logo'));
$payment->setAdditionalInformation('method_code', $payment->getMethodInstance()->getCode());
$payment->setAdditionalInformation('klarna_order_id', $klarnaQuote->getSessionId());
$this->klarnaQuoteRepository->save($klarnaQuote);
} catch (NoSuchEntityException $npe) {
$data = ['klarna_id' => $additionalData->getData('authorization_token')];
$this->log->error($npe, $data);
}
}
}