UpgradeData.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * This file is part of the Klarna Kp module
  4. *
  5. * (c) Klarna Bank AB (publ)
  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\Setup;
  11. use Magento\Framework\Setup\ModuleContextInterface;
  12. use Magento\Framework\Setup\ModuleDataSetupInterface;
  13. use Magento\Framework\Setup\UpgradeDataInterface;
  14. class UpgradeData implements UpgradeDataInterface
  15. {
  16. /**
  17. * Upgrades DB data for a module
  18. *
  19. * @param ModuleDataSetupInterface $installer
  20. * @param ModuleContextInterface $context
  21. * @return void
  22. */
  23. public function upgrade(ModuleDataSetupInterface $installer, ModuleContextInterface $context)
  24. {
  25. $installer->startSetup();
  26. if (version_compare($context->getVersion(), '4.0.2', '<')) {
  27. $this->disableAllQuotes($installer);
  28. }
  29. if (version_compare($context->getVersion(), '5.3.2', '<')) {
  30. $this->disableInvalidQuotes($installer);
  31. $methods = [
  32. 'klarna_pay_later',
  33. 'klarna_pay_now',
  34. 'klarna_pay_over_time',
  35. 'klarna_direct_debit',
  36. 'klarna_direct_bank_transfer'
  37. ];
  38. $methods = "'" . implode("','", $methods) . "'";
  39. $this->updateAdditionalInformation($installer, $methods);
  40. $this->changePaymentKeyToGeneric($installer, $methods);
  41. }
  42. if (version_compare($context->getVersion(), '5.4.5', '<')) {
  43. $this->removeStrongHtmlTag($installer);
  44. }
  45. if (version_compare($context->getVersion(), '5.5.4', '<')) {
  46. $this->clearDesignConfig($installer);
  47. }
  48. $installer->endSetup();
  49. }
  50. /**
  51. * Mark all quotes as inactive so that switch over to new payments endpoint happens
  52. *
  53. * @param ModuleDataSetupInterface $installer
  54. */
  55. private function disableAllQuotes(ModuleDataSetupInterface $installer)
  56. {
  57. $table = $installer->getTable('klarna_payments_quote');
  58. $installer->getConnection()->update($table, ['is_active' => 0]);
  59. }
  60. /**
  61. * Updating the additional information
  62. *
  63. * @param ModuleDataSetupInterface $installer
  64. * @param string $methods
  65. */
  66. private function updateAdditionalInformation(ModuleDataSetupInterface $installer, $methods)
  67. {
  68. $installer->getConnection()
  69. ->query("update `{$installer->getTable('sales_order_payment')}`" .
  70. " set `additional_information`=" .
  71. " replace(`additional_information`, '}', concat(',\"method_code\":\"', `method`, '\"}'))" .
  72. " where `method` in ({$methods})");
  73. }
  74. /**
  75. * Disabled klarna quotes where we don't have any payment method information
  76. *
  77. * @param ModuleDataSetupInterface $installer
  78. */
  79. private function disableInvalidQuotes(ModuleDataSetupInterface $installer)
  80. {
  81. $installer->getConnection()->update(
  82. $installer->getTable('klarna_payments_quote'),
  83. ['is_active' => 0, 'payment_method_info' => '{}'],
  84. '`payment_method_info` is null'
  85. );
  86. }
  87. /**
  88. * Change the klarna payment keys to a generic payment key: klarna_kp
  89. *
  90. * @param ModuleDataSetupInterface $installer
  91. * @param $methods
  92. */
  93. private function changePaymentKeyToGeneric(ModuleDataSetupInterface $installer, $methods)
  94. {
  95. $installer->getConnection()
  96. ->update(
  97. $installer->getTable('sales_order_payment'),
  98. ['method' => 'klarna_kp'],
  99. "`method` in ({$methods})"
  100. );
  101. foreach (['sales_order_grid', 'sales_invoice_grid', 'sales_creditmemo_grid'] as $table) {
  102. $installer->getConnection()
  103. ->update(
  104. $installer->getTable($table),
  105. ['payment_method' => 'klarna_kp'],
  106. "`payment_method` in ({$methods})"
  107. );
  108. }
  109. }
  110. /**
  111. * Remove the html tag 'strong' from the additional information of the payments
  112. *
  113. * @param ModuleDataSetupInterface $installer
  114. */
  115. private function removeStrongHtmlTag(ModuleDataSetupInterface $installer)
  116. {
  117. $values = [
  118. '<strong>',
  119. '<\/strong>'
  120. ];
  121. foreach ($values as $value) {
  122. $manipulation = new \Zend_Db_Expr("replace(`additional_information`, '$value', '')");
  123. $installer->getConnection()
  124. ->update(
  125. $installer->getTable('sales_order_payment'),
  126. ['additional_information' => $manipulation],
  127. "`method` = 'klarna_kp'"
  128. );
  129. }
  130. }
  131. /**
  132. * clear unused kp design config settings
  133. *
  134. * @param ModuleDataSetupInterface $installer
  135. */
  136. private function clearDesignConfig(ModuleDataSetupInterface $installer)
  137. {
  138. $configPaths = [
  139. 'checkout/klarna_kp_design/color_button',
  140. 'checkout/klarna_kp_design/color_button_text',
  141. 'checkout/klarna_kp_design/color_checkbox',
  142. 'checkout/klarna_kp_design/color_checkbox_checkmark',
  143. 'checkout/klarna_kp_design/color_header',
  144. 'checkout/klarna_kp_design/color_link',
  145. 'checkout/klarna_kp_design/color_text_secondary'
  146. ];
  147. $configTable = $installer->getTable('core_config_data');
  148. $keys = '\'' . implode('\',\'', $configPaths) . '\'';
  149. $installer->getConnection()->delete($configTable, "`path` in ({$keys})");
  150. }
  151. }