/** * Performs validation before save * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { parent::_beforeSave($object); $errors = $this->validator->validate($object); if (!empty($errors)) { throw new \Magento\Framework\Model\Exception(__("Cannot save comment") . ":\n" . implode("\n", $errors)); } return $this; }
/** * Lookup for parent_id in already saved transactions of this payment by the order_id * Also serialize additional information, if any * * @param \Magento\Sales\Model\Order\Payment\Transaction $transaction * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transaction) { $parentTxnId = $transaction->getData('parent_txn_id'); $txnId = $transaction->getData('txn_id'); $orderId = $transaction->getData('order_id'); $paymentId = $transaction->getData('payment_id'); $idFieldName = $this->getIdFieldName(); if ($parentTxnId) { if (!$txnId || !$orderId || !$paymentId) { throw new \Magento\Framework\Model\Exception(__('We don\'t have enough information to save the parent transaction ID.')); } $parentId = (int) $this->_lookupByTxnId($orderId, $paymentId, $parentTxnId, $idFieldName); if ($parentId) { $transaction->setData('parent_id', $parentId); } } // make sure unique key won't cause trouble if ($transaction->isFailsafe()) { $autoincrementId = (int) $this->_lookupByTxnId($orderId, $paymentId, $txnId, $idFieldName); if ($autoincrementId) { $transaction->setData($idFieldName, $autoincrementId)->isObjectNew(false); } } return parent::_beforeSave($transaction); }