-
Notifications
You must be signed in to change notification settings - Fork 50
/
m210903_040320_payment_intent_unique_on_transaction.php
66 lines (55 loc) · 2.15 KB
/
m210903_040320_payment_intent_unique_on_transaction.php
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
<?php
namespace craft\commerce\stripe\migrations;
use craft\db\Migration;
use craft\helpers\MigrationHelper;
/**
* m210903_040320_payment_intent_unique_on_transaction migration.
*/
class m210903_040320_payment_intent_unique_on_transaction extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
if (!$this->db->columnExists('{{%stripe_paymentintents}}', 'transactionHash')) {
$this->addColumn('{{%stripe_paymentintents}}', 'transactionHash', $this->string()->after('orderId'));
}
MigrationHelper::dropAllForeignKeysOnTable('{{%stripe_paymentintents}}', $this);
MigrationHelper::dropAllIndexesOnTable('{{%stripe_paymentintents}}', $this);
$transactionsTable = '{{%commerce_transactions}}';
$stripePaymentIntentsTable = '{{%stripe_paymentintents}}';
if ($this->db->getIsMysql()) {
$sql = <<<SQL
UPDATE $stripePaymentIntentsTable [[pi]]
INNER JOIN $transactionsTable [[t]] ON [[t.reference]] = [[pi.reference]]
SET [[pi.transactionHash]] = [[t.hash]]
WHERE [[pi.transactionHash]] IS NULL
SQL;
} else {
$sql = <<<SQL
UPDATE $stripePaymentIntentsTable [[pi]]
SET [[transactionHash]] = [[t.hash]]
FROM $transactionsTable [[t]]
WHERE
[[pi.transactionHash]] IS NULL AND
[[t.reference]] = [[pi.reference]]
SQL;
}
$this->execute($sql);
$this->addForeignKey(null, '{{%stripe_paymentintents}}', 'gatewayId', '{{%commerce_gateways}}', 'id', 'CASCADE', null);
$this->addForeignKey(null, '{{%stripe_paymentintents}}', 'customerId', '{{%stripe_customers}}', 'id', 'CASCADE', null);
$this->addForeignKey(null, '{{%stripe_paymentintents}}', 'orderId', '{{%commerce_orders}}', 'id', 'CASCADE', null);
$this->createIndex(null, '{{%stripe_paymentintents}}', 'reference', true);
$this->createIndex(null, '{{%stripe_paymentintents}}', ['orderId', 'gatewayId', 'customerId', 'transactionHash'], true);
return true;
}
/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m210903_040320_payment_intent_unique_on_transaction cannot be reverted.\n";
return false;
}
}