11
11
/**
12
12
* Sales transaction resource model
13
13
*
14
- * @author Magento Core Team <core@magentocommerce.com>
15
14
*/
16
15
class Transaction extends EntityAbstract implements TransactionResourceInterface
17
16
{
@@ -34,7 +33,8 @@ protected function _construct()
34
33
35
34
/**
36
35
* Update transactions in database using provided transaction as parent for them
37
- * have to repeat the business logic to avoid accidental injection of wrong transactions
36
+ *
37
+ * Have to repeat the business logic to avoid accidental injection of wrong transactions
38
38
*
39
39
* @param \Magento\Sales\Model\Order\Payment\Transaction $transaction
40
40
* @return void
@@ -126,11 +126,14 @@ public function getOrderWebsiteId($orderId)
126
126
127
127
/**
128
128
* Lookup for parent_id in already saved transactions of this payment by the order_id
129
+ *
129
130
* Also serialize additional information, if any
130
131
*
131
132
* @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Model\Order\Payment\Transaction $transaction
132
133
* @throws \Magento\Framework\Exception\LocalizedException
133
134
* @return $this
135
+ *
136
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
134
137
*/
135
138
protected function _beforeSave (\Magento \Framework \Model \AbstractModel $ transaction )
136
139
{
@@ -139,16 +142,23 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transacti
139
142
$ orderId = $ transaction ->getData ('order_id ' );
140
143
$ paymentId = $ transaction ->getData ('payment_id ' );
141
144
$ idFieldName = $ this ->getIdFieldName ();
142
-
145
+ $ txnType = $ transaction -> getData ( ' txn_type ' );
143
146
if ($ parentTxnId ) {
144
147
if (!$ txnId || !$ orderId || !$ paymentId ) {
145
148
throw new \Magento \Framework \Exception \LocalizedException (
146
149
__ ('We don \'t have enough information to save the parent transaction ID. ' )
147
150
);
148
151
}
149
152
$ parentId = (int )$ this ->_lookupByTxnId ($ orderId , $ paymentId , $ parentTxnId , $ idFieldName );
150
- if ($ parentId ) {
153
+ if ($ parentId && $ txnType == ' authorization ' ) {
151
154
$ transaction ->setData ('parent_id ' , $ parentId );
155
+ $ transaction ->setData ('txn_type ' , \Magento \Sales \Model \Order \Payment \Transaction::TYPE_CAPTURE );
156
+ }
157
+ } else {
158
+ $ result = $ this ->getParentId ($ orderId );
159
+ if ($ result ) {
160
+ $ transaction ->setData ('parent_id ' , $ result [0 ]['transaction_id ' ]);
161
+ $ transaction ->setData ('parent_txn_id ' , $ result [0 ]['parent_txn_id ' ]);
152
162
}
153
163
}
154
164
@@ -169,7 +179,7 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transacti
169
179
* @param int $orderId
170
180
* @param int $paymentId
171
181
* @param string $txnId
172
- * @param mixed (array|string|object) $columns
182
+ * @param mixed $columns (array|string|object) $columns
173
183
* @param bool $isRow
174
184
* @param string $txnType
175
185
* @return array|string
@@ -211,4 +221,23 @@ private function _getLoadByUniqueKeySelect($orderId, $paymentId, $txnId, $column
211
221
$ txnId
212
222
);
213
223
}
224
+
225
+ /**
226
+ * Retrieve transaction by the unique key of order_id
227
+ *
228
+ * @param int $orderId
229
+ * @return array
230
+ */
231
+ protected function getParentId (int $ orderId ): array
232
+ {
233
+ $ connection = $ this ->getConnection ();
234
+ $ select = $ connection ->select ()->from (
235
+ $ this ->getMainTable (),
236
+ ['transaction_id ' ,'parent_txn_id ' ]
237
+ )->where (
238
+ 'order_id = ? ' ,
239
+ $ orderId
240
+ )->order ('transaction_id ' , 'ASC ' )->limit (1 );
241
+ return $ connection ->fetchAll ($ select );
242
+ }
214
243
}
0 commit comments