1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Sales_Model_Resource_Order_Payment_Transaction extends Mage_Sales_Model_Resource_Order_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_serializableFields = array(
43: 'additional_information' => array(null, array())
44: );
45:
46: 47: 48: 49:
50: protected function _construct()
51: {
52: $this->_init('sales/payment_transaction', 'transaction_id');
53: }
54:
55: 56: 57: 58: 59: 60:
61: public function injectAsParent(Mage_Sales_Model_Order_Payment_Transaction $transaction)
62: {
63: $txnId = $transaction->getTxnId();
64: if ($txnId && Mage_Sales_Model_Order_Payment_Transaction::TYPE_PAYMENT === $transaction->getTxnType()
65: && $id = $transaction->getId()
66: ) {
67: $adapter = $this->_getWriteAdapter();
68:
69:
70: $verificationRow = $adapter->fetchRow(
71: $adapter->select()->from($this->getMainTable(), array('payment_id', 'order_id'))
72: ->where("{$this->getIdFieldName()} = ?", (int)$id)
73: );
74: if (!$verificationRow) {
75: return;
76: }
77: list($paymentId, $orderId) = array_values($verificationRow);
78:
79:
80: $where = array(
81: $adapter->quoteIdentifier($this->getIdFieldName()) . '!=?' => $id,
82: new Zend_Db_Expr('parent_id IS NULL'),
83: 'payment_id = ?' => (int)$paymentId,
84: 'order_id = ?' => (int)$orderId,
85: 'parent_txn_id = ?' => $txnId
86: );
87: $adapter->update($this->getMainTable(),
88: array('parent_id' => $id),
89: $where
90: );
91: }
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101:
102: public function loadObjectByTxnId(Mage_Sales_Model_Order_Payment_Transaction $transaction, $orderId, $paymentId,
103: $txnId)
104: {
105: $select = $this->_getLoadByUniqueKeySelect($orderId, $paymentId, $txnId);
106: $data = $this->_getWriteAdapter()->fetchRow($select);
107: $transaction->setData($data);
108: $this->unserializeFields($transaction);
109: $this->_afterLoad($transaction);
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function getOrderWebsiteId($orderId)
119: {
120: $adapter = $this->_getReadAdapter();
121: $bind = array(':entity_id' => $orderId);
122: $select = $adapter->select()
123: ->from(array('so' => $this->getTable('sales/order')), 'cs.website_id')
124: ->joinInner(array('cs' => $this->getTable('core/store')), 'cs.store_id = so.store_id')
125: ->where('so.entity_id = :entity_id');
126: return $adapter->fetchOne($select, $bind);
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136: 137:
138: protected function _beforeSave(Mage_Core_Model_Abstract $transaction)
139: {
140: $parentTxnId = $transaction->getData('parent_txn_id');
141: $txnId = $transaction->getData('txn_id');
142: $orderId = $transaction->getData('order_id');
143: $paymentId = $transaction->getData('payment_id');
144: $idFieldName = $this->getIdFieldName();
145:
146: if ($parentTxnId) {
147: if (!$txnId || !$orderId || !$paymentId) {
148: Mage::throwException(
149: Mage::helper('sales')->__('Not enough valid data to save the parent transaction ID.'));
150: }
151: $parentId = (int)$this->_lookupByTxnId($orderId, $paymentId, $parentTxnId, $idFieldName);
152: if ($parentId) {
153: $transaction->setData('parent_id', $parentId);
154: }
155: }
156:
157:
158: if ($transaction->isFailsafe()) {
159: $autoincrementId = (int)$this->_lookupByTxnId($orderId, $paymentId, $txnId, $idFieldName);
160: if ($autoincrementId) {
161: $transaction->setData($idFieldName, $autoincrementId)->isObjectNew(false);
162: }
163: }
164:
165: return parent::_beforeSave($transaction);
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
179: private function _lookupByTxnId($orderId, $paymentId, $txnId, $columns, $isRow = false, $txnType = null)
180: {
181: $select = $this->_getLoadByUniqueKeySelect($orderId, $paymentId, $txnId, $columns);
182: if ($txnType) {
183: $select->where('txn_type = ?', $txnType);
184: }
185: if ($isRow) {
186: return $this->_getWriteAdapter()->fetchRow($select);
187: }
188: return $this->_getWriteAdapter()->fetchOne($select);
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199:
200: private function _getLoadByUniqueKeySelect($orderId, $paymentId, $txnId, $columns = '*')
201: {
202: return $this->_getWriteAdapter()->select()
203: ->from($this->getMainTable(), $columns)
204: ->where('order_id = ?', $orderId)
205: ->where('payment_id = ?', $paymentId)
206: ->where('txn_id = ?', $txnId);
207: }
208: }
209: