1111/**
1212 * Sales transaction resource model
1313 *
14- * @author Magento Core Team <core@magentocommerce.com>
1514 */
1615class Transaction extends EntityAbstract implements TransactionResourceInterface
1716{
@@ -34,7 +33,8 @@ protected function _construct()
3433
3534 /**
3635 * 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
3838 *
3939 * @param \Magento\Sales\Model\Order\Payment\Transaction $transaction
4040 * @return void
@@ -126,11 +126,14 @@ public function getOrderWebsiteId($orderId)
126126
127127 /**
128128 * Lookup for parent_id in already saved transactions of this payment by the order_id
129+ *
129130 * Also serialize additional information, if any
130131 *
131132 * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Model\Order\Payment\Transaction $transaction
132133 * @throws \Magento\Framework\Exception\LocalizedException
133134 * @return $this
135+ *
136+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
134137 */
135138 protected function _beforeSave (\Magento \Framework \Model \AbstractModel $ transaction )
136139 {
@@ -139,16 +142,23 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transacti
139142 $ orderId = $ transaction ->getData ('order_id ' );
140143 $ paymentId = $ transaction ->getData ('payment_id ' );
141144 $ idFieldName = $ this ->getIdFieldName ();
142-
145+ $ txnType = $ transaction -> getData ( ' txn_type ' );
143146 if ($ parentTxnId ) {
144147 if (!$ txnId || !$ orderId || !$ paymentId ) {
145148 throw new \Magento \Framework \Exception \LocalizedException (
146149 __ ('We don \'t have enough information to save the parent transaction ID. ' )
147150 );
148151 }
149152 $ parentId = (int )$ this ->_lookupByTxnId ($ orderId , $ paymentId , $ parentTxnId , $ idFieldName );
150- if ($ parentId ) {
153+ if ($ parentId && $ txnType == ' authorization ' ) {
151154 $ 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 ' ]);
152162 }
153163 }
154164
@@ -169,7 +179,7 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transacti
169179 * @param int $orderId
170180 * @param int $paymentId
171181 * @param string $txnId
172- * @param mixed (array|string|object) $columns
182+ * @param mixed $columns (array|string|object) $columns
173183 * @param bool $isRow
174184 * @param string $txnType
175185 * @return array|string
@@ -211,4 +221,23 @@ private function _getLoadByUniqueKeySelect($orderId, $paymentId, $txnId, $column
211221 $ txnId
212222 );
213223 }
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+ }
214243}
0 commit comments