UpgradeSchema.php 9.28 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 38

    const ADYEN_ORDER_PAYMENT = 'adyen_order_payment';

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

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

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

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

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

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

66 67 68
        $setup->endSetup();
    }

69
    /**
70 71
     * Upgrade to 1.0.0.1
     *
72
     * @param SchemaSetupInterface $setup
73
     * @return void
74 75
     */
    public function updateSchemaVersion1001(SchemaSetupInterface $setup)
76 77 78 79 80 81 82 83 84 85
    {
        $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'
        ];
86 87 88 89 90 91

        $connection->addColumn(
            $setup->getTable('sales_order'),
            'adyen_notification_event_code_success',
            $adyenNotificationEventCodeSuccessColumn
        );
92 93 94 95 96 97 98 99 100 101 102

        // 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);
    }
103 104
    
    /**
105 106
     * Upgrade to 1.0.0.2
     *
107
     * @param SchemaSetupInterface $setup
108
     * @return void
109 110
     */
    public function updateSchemaVersion1002(SchemaSetupInterface $setup)
111 112 113 114 115 116 117 118 119
    {
        $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'
        ];
120 121 122
        $connection->addColumn(
            $setup->getTable('paypal_billing_agreement'), 'agreement_data', $adyenAgreementDataColumn
        );
123
    }
124 125

    /**
126 127
     * Upgrade to 2.0.0
     *
128
     * @param SchemaSetupInterface $setup
129
     * @return void
130
     */
Rik ter Beek's avatar
Rik ter Beek committed
131
    public function updateSchemaVersion200(SchemaSetupInterface $setup)
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 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
    {
        /**
         * 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');
        
        $setup->getConnection()->createTable($table);

226 227
        // add originalReference to notification table
        $connection = $setup->getConnection();
228

229 230 231 232 233 234 235
        $column = [
            'type' => Table::TYPE_TEXT,
            'length' => 255,
            'nullable' => true,
            'comment' => 'Original Reference',
            'after'     => \Adyen\Payment\Model\Notification::PSPREFRENCE
        ];
236

237 238 239 240
        $connection->addColumn(
            $setup->getTable('adyen_notification'),
            \Adyen\Payment\Model\Notification::ORIGINAL_REFERENCE, $column
        );
241
    }
Rik ter Beek's avatar
Rik ter Beek committed
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    /**
     * 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'
            ]
        );
    }
269 270 271


     /**
272
     * Upgrade to 2.0.7
273 274 275 276
     * 
     * @param SchemaSetupInterface $setup
     * @return void
     */
277
    public function updateSchemaVersion207(SchemaSetupInterface $setup)
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    {
        $connection = $setup->getConnection();
        $tableName = $setup->getTable('adyen_notification');

        $adyenNotificationProcessingColumn = [
            'type' => Table::TYPE_BOOLEAN,
            'length' => 1,
            'nullable' => true,
            'default' => 0,
            'comment' => 'Adyen Notification Cron Processing',
            'after'     => \Adyen\Payment\Model\Notification::DONE
        ];

        $connection->addColumn(
            $tableName,
            'processing',
            $adyenNotificationProcessingColumn
        );
    }
297
}