Skip to content

Commit 8531110

Browse files
committed
Merge remote-tracking branch 'origin/AC-10042v1' into spartans_pr_04072024
2 parents 76362cf + 2298b76 commit 8531110

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

app/code/Magento/Sales/Model/ResourceModel/Order/Payment/Transaction.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
/**
1212
* Sales transaction resource model
1313
*
14-
* @author Magento Core Team <core@magentocommerce.com>
1514
*/
1615
class 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
}

dev/tests/integration/testsuite/Magento/Sales/_files/transactions_detailed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
$payment->setIsTransactionClosed(false);
2626
$payment->setTransactionAdditionalInfo('capture_key', 'data');
2727
$payment->setParentTransactionId('trx_auth');
28-
$payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE);
28+
$payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_AUTH);
2929

3030
$order->save();

dev/tests/integration/testsuite/Magento/Sales/_files/transactions_list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
/** @var array $transactionData */
5555
foreach ($transactions as $transactionData) {
5656
$payment->addData($transactionData);
57-
$payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE);
57+
$payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_AUTH);
5858
}
5959

6060
$order->save();

0 commit comments

Comments
 (0)