We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

UpgradeSchema.php 12.9 KB
Newer Older
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
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * Upgrade the Catalog module DB scheme
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
36 37

    const ADYEN_ORDER_PAYMENT = 'adyen_order_payment';
38
    const ADYEN_INVOICE = 'adyen_invoice';
39

40 41 42 43 44 45 46 47 48 49 50
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.0.1', '<')) {
            $this->updateSchemaVersion1001($setup);
        }

51 52 53 54
        if (version_compare($context->getVersion(), '1.0.0.2', '<')) {
            $this->updateSchemaVersion1002($setup);
        }

Rik ter Beek's avatar
Rik ter Beek committed
55 56
        if (version_compare($context->getVersion(), '2.0.0', '<')) {
            $this->updateSchemaVersion200($setup);
57 58
        }

59 60 61 62
        if (version_compare($context->getVersion(), '2.0.4', '<')) {
            $this->updateSchemaVersion204($setup);
        }

63 64
        if (version_compare($context->getVersion(), '2.0.7', '<')) {
            $this->updateSchemaVersion207($setup);
65 66
        }

67 68 69 70
        if (version_compare($context->getVersion(), '2.2.1', '<')) {
            $this->updateSchemaVersion221($setup);
        }

71 72 73 74
        if (version_compare($context->getVersion(), '5.4.0', '<')) {
            $this->updateSchemaVersion540($setup);
        }

75 76 77
        $setup->endSetup();
    }

78
    /**
79 80
     * Upgrade to 1.0.0.1
     *
81
     * @param SchemaSetupInterface $setup
82
     * @return void
83 84
     */
    public function updateSchemaVersion1001(SchemaSetupInterface $setup)
85 86 87 88 89 90 91 92 93 94
    {
        $connection = $setup->getConnection();

        // Add column to indicate if last notification has success true or false
        $adyenNotificationEventCodeSuccessColumn = [
            'type' => Table::TYPE_BOOLEAN,
            'length' => 1,
            'nullable' => true,
            'comment' => 'Adyen Notification event code success flag'
        ];
95 96 97 98 99 100

        $connection->addColumn(
            $setup->getTable('sales_order'),
            'adyen_notification_event_code_success',
            $adyenNotificationEventCodeSuccessColumn
        );
101 102 103 104 105 106 107 108 109 110 111

        // add column to order_payment to save Adyen PspReference
        $pspReferenceColumn = [
            'type' => Table::TYPE_TEXT,
            'length' => 255,
            'nullable' => true,
            'comment' => 'Adyen PspReference of the payment'
        ];

        $connection->addColumn($setup->getTable('sales_order_payment'), 'adyen_psp_reference', $pspReferenceColumn);
    }
112

113
    /**
114 115
     * Upgrade to 1.0.0.2
     *
116
     * @param SchemaSetupInterface $setup
117
     * @return void
118 119
     */
    public function updateSchemaVersion1002(SchemaSetupInterface $setup)
120 121 122 123 124 125 126 127 128
    {
        $connection = $setup->getConnection();

        // Add column to indicate if last notification has success true or false
        $adyenAgreementDataColumn = [
            'type' => Table::TYPE_TEXT,
            'nullable' => true,
            'comment' => 'Agreement Data'
        ];
129
        $connection->addColumn(
130 131 132
            $setup->getTable('paypal_billing_agreement'),
            'agreement_data',
            $adyenAgreementDataColumn
133
        );
134
    }
135 136

    /**
137 138
     * Upgrade to 2.0.0
     *
139
     * @param SchemaSetupInterface $setup
140
     * @return void
141
     */
Rik ter Beek's avatar
Rik ter Beek committed
142
    public function updateSchemaVersion200(SchemaSetupInterface $setup)
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    {
        /**
         * Create table 'adyen_order_payment'
         */
        $table = $setup->getConnection()
            ->newTable($setup->getTable(self::ADYEN_ORDER_PAYMENT))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Adyen Payment ID'
            )
            ->addColumn(
                'pspreference',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['unsigned' => true, 'nullable' => false],
                'Pspreference'
            )
            ->addColumn(
                'merchant_reference',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['unsigned' => true, 'nullable' => false],
                'Merchant Reference'
            )
            ->addColumn(
                'payment_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                11,
                ['unsigned' => true, 'nullable' => false],
                'Order Payment Id'
            )
            ->addColumn(
                'payment_method',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['unsigned' => true, 'nullable' => true],
                'Payment Method'
            )
            ->addColumn(
                'amount',
                \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                '12,4',
                ['unsigned' => true, 'nullable' => false],
                'Amount'
            )
            ->addColumn(
                'total_refunded',
                \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                '12,4',
                ['unsigned' => true, 'nullable' => false],
                'Total Refunded'
            )
            ->addColumn(
                'created_at',
                Table::TYPE_DATETIME,
                null,
                ['nullable' => false],
                'Created at'
            )
            ->addColumn(
                'updated_at',
                Table::TYPE_DATETIME,
                null,
                ['nullable' => false],
                'Updated at'
            )
            ->addIndex(
                $setup->getIdxName(
                    self::ADYEN_ORDER_PAYMENT,
                    ['pspreference'],
                    \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['pspreference'],
                ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
            )
            ->addForeignKey(
                $setup->getFkName(
                    self::ADYEN_ORDER_PAYMENT,
                    'payment_id',
                    'sales_order_payment',
                    'entity_id'
                ),
                'payment_id',
                $setup->getTable('sales_order_payment'),
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
            )
            ->setComment('Adyen Order Payment');
234

235 236
        $setup->getConnection()->createTable($table);

237 238
        // add originalReference to notification table
        $connection = $setup->getConnection();
239

240 241 242 243 244
        $column = [
            'type' => Table::TYPE_TEXT,
            'length' => 255,
            'nullable' => true,
            'comment' => 'Original Reference',
245
            'after' => \Adyen\Payment\Model\Notification::PSPREFRENCE
246
        ];
247

248 249
        $connection->addColumn(
            $setup->getTable('adyen_notification'),
250 251
            \Adyen\Payment\Model\Notification::ORIGINAL_REFERENCE,
            $column
252
        );
253
    }
Rik ter Beek's avatar
Rik ter Beek committed
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    /**
     * Upgrade to 2.0.4
     * Update entity_id in notification from smallint to integer
     *
     * @param SchemaSetupInterface $setup
     * @return void
     */
    public function updateSchemaVersion204(SchemaSetupInterface $setup)
    {
        $connection = $setup->getConnection();
        $tableName = $setup->getTable('adyen_notification');

        $connection->changeColumn(
            $tableName,
            'entity_id',
            'entity_id',
            [
                'type' => Table::TYPE_INTEGER,
                'nullable' => false,
                'primary' => true,
                'identity' => true,
                'unsigned' => true,
                'comment' => 'Adyen Notification Entity ID'
            ]
        );
    }
281 282


283
    /**
284
     * Upgrade to 2.0.7
285
     *
286 287 288
     * @param SchemaSetupInterface $setup
     * @return void
     */
289
    public function updateSchemaVersion207(SchemaSetupInterface $setup)
290 291 292 293 294 295 296 297 298 299
    {
        $connection = $setup->getConnection();
        $tableName = $setup->getTable('adyen_notification');

        $adyenNotificationProcessingColumn = [
            'type' => Table::TYPE_BOOLEAN,
            'length' => 1,
            'nullable' => true,
            'default' => 0,
            'comment' => 'Adyen Notification Cron Processing',
300
            'after' => \Adyen\Payment\Model\Notification::DONE
301 302 303 304 305 306 307 308
        ];

        $connection->addColumn(
            $tableName,
            'processing',
            $adyenNotificationProcessingColumn
        );
    }
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

    public function updateSchemaVersion221(SchemaSetupInterface $setup)
    {

        $table = $setup->getConnection()
            ->newTable($setup->getTable(self::ADYEN_INVOICE))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Adyen Invoice Entity ID'
            )
            ->addColumn(
                'pspreference',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['unsigned' => true, 'nullable' => false],
                'Adyen pspreference of the capture'
            )
            ->addColumn(
                'original_reference',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['unsigned' => true, 'nullable' => true],
                'Adyen OriginalReference of the payment'
            )
336 337
            ->addColumn(
                'acquirer_reference',
338 339 340
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['unsigned' => true, 'nullable' => true],
341 342
                'Adyen AcquirerReference of the capture'
            )
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
            ->addColumn(
                'invoice_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                11,
                ['unsigned' => true, 'nullable' => false],
                'Invoice Id'
            )
            ->addForeignKey(
                $setup->getFkName(
                    self::ADYEN_INVOICE,
                    'invoice_id',
                    'sales_invoice',
                    'entity_id'
                ),
                'invoice_id',
                $setup->getTable('sales_invoice'),
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
            )
            ->setComment('Adyen Invoice');

        $setup->getConnection()->createTable($table);
    }
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    /**
     * Upgrade to 5.4.0
     *
     * @param SchemaSetupInterface $setup
     * @return void
     */
    public function updateSchemaVersion540(SchemaSetupInterface $setup)
    {
        $connection = $setup->getConnection();
        $tableName = $setup->getTable('adyen_notification');

        $adyenNotificationErrorCountColumn = [
            'type' => Table::TYPE_INTEGER,
            'length' => 1,
            'nullable' => true,
            'default' => 0,
            'comment' => 'Adyen Notification Process Error Count',
            'after' => \Adyen\Payment\Model\Notification::PROCESSING
        ];

        $adyenNotificationErrorMessageColumn = [
            'type' => Table::TYPE_TEXT,
            'length' => null,
            'nullable' => true,
            'default' => null,
            'comment' => 'Adyen Notification Process Error Message',
            'after' => \Adyen\Payment\Model\Notification::ERROR_COUNT
        ];

        $connection->addColumn(
            $tableName,
            \Adyen\Payment\Model\Notification::ERROR_COUNT,
            $adyenNotificationErrorCountColumn
        );

        $connection->addColumn(
            $tableName,
            \Adyen\Payment\Model\Notification::ERROR_MESSAGE,
            $adyenNotificationErrorMessageColumn
        );
    }
408
}