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
<?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\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
/**
* Upgrades DB data for a module
*
* @param ModuleDataSetupInterface $installer
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(ModuleDataSetupInterface $installer, ModuleContextInterface $context)
{
$installer->startSetup();
if (version_compare($context->getVersion(), '4.0.2', '<')) {
$this->disableAllQuotes($installer);
}
if (version_compare($context->getVersion(), '5.3.2', '<')) {
$this->disableInvalidQuotes($installer);
$methods = [
'klarna_pay_later',
'klarna_pay_now',
'klarna_pay_over_time',
'klarna_direct_debit',
'klarna_direct_bank_transfer'
];
$methods = "'" . implode("','", $methods) . "'";
$this->updateAdditionalInformation($installer, $methods);
$this->changePaymentKeyToGeneric($installer, $methods);
}
if (version_compare($context->getVersion(), '5.4.5', '<')) {
$this->removeStrongHtmlTag($installer);
}
if (version_compare($context->getVersion(), '5.5.4', '<')) {
$this->clearDesignConfig($installer);
}
$installer->endSetup();
}
/**
* Mark all quotes as inactive so that switch over to new payments endpoint happens
*
* @param ModuleDataSetupInterface $installer
*/
private function disableAllQuotes(ModuleDataSetupInterface $installer)
{
$table = $installer->getTable('klarna_payments_quote');
$installer->getConnection()->update($table, ['is_active' => 0]);
}
/**
* Updating the additional information
*
* @param ModuleDataSetupInterface $installer
* @param string $methods
*/
private function updateAdditionalInformation(ModuleDataSetupInterface $installer, $methods)
{
$installer->getConnection()
->query("update `{$installer->getTable('sales_order_payment')}`" .
" set `additional_information`=" .
" replace(`additional_information`, '}', concat(',\"method_code\":\"', `method`, '\"}'))" .
" where `method` in ({$methods})");
}
/**
* Disabled klarna quotes where we don't have any payment method information
*
* @param ModuleDataSetupInterface $installer
*/
private function disableInvalidQuotes(ModuleDataSetupInterface $installer)
{
$installer->getConnection()->update(
$installer->getTable('klarna_payments_quote'),
['is_active' => 0, 'payment_method_info' => '{}'],
'`payment_method_info` is null'
);
}
/**
* Change the klarna payment keys to a generic payment key: klarna_kp
*
* @param ModuleDataSetupInterface $installer
* @param $methods
*/
private function changePaymentKeyToGeneric(ModuleDataSetupInterface $installer, $methods)
{
$installer->getConnection()
->update(
$installer->getTable('sales_order_payment'),
['method' => 'klarna_kp'],
"`method` in ({$methods})"
);
foreach (['sales_order_grid', 'sales_invoice_grid', 'sales_creditmemo_grid'] as $table) {
$installer->getConnection()
->update(
$installer->getTable($table),
['payment_method' => 'klarna_kp'],
"`payment_method` in ({$methods})"
);
}
}
/**
* Remove the html tag 'strong' from the additional information of the payments
*
* @param ModuleDataSetupInterface $installer
*/
private function removeStrongHtmlTag(ModuleDataSetupInterface $installer)
{
$values = [
'<strong>',
'<\/strong>'
];
foreach ($values as $value) {
$manipulation = new \Zend_Db_Expr("replace(`additional_information`, '$value', '')");
$installer->getConnection()
->update(
$installer->getTable('sales_order_payment'),
['additional_information' => $manipulation],
"`method` = 'klarna_kp'"
);
}
}
/**
* clear unused kp design config settings
*
* @param ModuleDataSetupInterface $installer
*/
private function clearDesignConfig(ModuleDataSetupInterface $installer)
{
$configPaths = [
'checkout/klarna_kp_design/color_button',
'checkout/klarna_kp_design/color_button_text',
'checkout/klarna_kp_design/color_checkbox',
'checkout/klarna_kp_design/color_checkbox_checkmark',
'checkout/klarna_kp_design/color_header',
'checkout/klarna_kp_design/color_link',
'checkout/klarna_kp_design/color_text_secondary'
];
$configTable = $installer->getTable('core_config_data');
$keys = '\'' . implode('\',\'', $configPaths) . '\'';
$installer->getConnection()->delete($configTable, "`path` in ({$keys})");
}
}