/** * Update transactions in database using provided transaction as parent for them * have to repeat the business logic to avoid accidental injection of wrong transactions * * @param \Magento\Sales\Model\Order\Payment\Transaction $transaction * @return void */ public function injectAsParent(\Magento\Sales\Model\Order\Payment\Transaction $transaction) { $txnId = $transaction->getTxnId(); if ($txnId && \Magento\Sales\Model\Order\Payment\Transaction::TYPE_PAYMENT === $transaction->getTxnType() && ($id = $transaction->getId())) { $connection = $this->getConnection(); // verify such transaction exists, determine payment and order id $verificationRow = $connection->fetchRow($connection->select()->from($this->getMainTable(), ['payment_id', 'order_id'])->where("{$this->getIdFieldName()} = ?", (int) $id)); if (!$verificationRow) { return; } list($paymentId, $orderId) = array_values($verificationRow); // inject $where = [$connection->quoteIdentifier($this->getIdFieldName()) . '!=?' => $id, new \Zend_Db_Expr('parent_id IS NULL'), 'payment_id = ?' => (int) $paymentId, 'order_id = ?' => (int) $orderId, 'parent_txn_id = ?' => $txnId]; $connection->update($this->getMainTable(), ['parent_id' => $id], $where); } }
/** * Parent transaction getter * May attempt to load it. * * @param bool $shouldLoad * @return bool|\Magento\Sales\Model\Order\Payment\Transaction */ public function getParentTransaction($shouldLoad = true) { if (null === $this->_parentTransaction) { $this->_verifyThisTransactionExists(); $this->_parentTransaction = false; $parentId = $this->getParentId(); if ($parentId) { $this->_parentTransaction = $this->_transactionFactory->create(); if ($shouldLoad) { $this->_parentTransaction->setOrderPaymentObject($this->_paymentObject)->load($parentId); if (!$this->_parentTransaction->getId()) { $this->_parentTransaction = false; } else { $this->_parentTransaction->hasChildTransaction(true); } } } } return $this->_parentTransaction; }
/** * @param Transaction $transaction * @return array */ private function getPreparedTransactionData(Transaction $transaction) { $additionalInfo = []; foreach ($transaction->getAdditionalInformation() as $value) { $additionalInfo[] = $value; } $expectedData = ['transaction_id' => (int) $transaction->getId()]; if ($transaction->getParentId() !== null) { $expectedData['parent_id'] = (int) $transaction->getParentId(); } $expectedData = array_merge($expectedData, ['order_id' => (int) $transaction->getOrderId(), 'payment_id' => (int) $transaction->getPaymentId(), 'txn_id' => $transaction->getTxnId(), 'parent_txn_id' => $transaction->getParentTxnId() ? (string) $transaction->getParentTxnId() : '', 'txn_type' => $transaction->getTxnType(), 'is_closed' => (int) $transaction->getIsClosed(), 'additional_information' => ['data'], 'created_at' => $transaction->getCreatedAt(), 'child_transactions' => []]); return $expectedData; }
/** * Register entity * * @param Transaction $object * @return TransactionRepository */ public function register(Transaction $object) { if ($object->getId() && !isset($this->registry[$object->getId()])) { $this->registry[$object->getId()] = $object; } return $this; }