1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Paypal
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Payment transaction model
29: * Tracks transaction history
30: *
31: * @method Mage_Paypal_Model_Resource_Payment_Transaction _getResource()
32: * @method Mage_Paypal_Model_Resource_Payment_Transaction getResource()
33: * @method string getTxnId()
34: * @method string getCreatedAt()
35: * @method Mage_Paypal_Model_Payment_Transaction setCreatedAt(string $value)
36: *
37: * @category Mage
38: * @package Mage_Paypal
39: * @author Magento Core Team <core@magentocommerce.com>
40: */
41: class Mage_Paypal_Model_Payment_Transaction extends Mage_Core_Model_Abstract
42: {
43: /**
44: * Whether to throw exceptions on different operations
45: *
46: * @var bool
47: */
48: protected $_isFailsafe = false;
49:
50: /**
51: * Event object prefix
52: *
53: * @see Mage_Core_Model_Absctract::$_eventPrefix
54: * @var string
55: */
56: protected $_eventPrefix = 'paypal_payment_transaction';
57:
58: /**
59: * Event object prefix
60: *
61: * @see Mage_Core_Model_Absctract::$_eventObject
62: * @var string
63: */
64: protected $_eventObject = 'paypal_payment_transaction';
65:
66: /**
67: * Order website id
68: *
69: * @var int
70: */
71: protected $_orderWebsiteId = null;
72:
73: /**
74: * Initialize resource model
75: */
76: protected function _construct()
77: {
78: $this->_init('paypal/payment_transaction');
79: return parent::_construct();
80: }
81:
82: /**
83: * Transaction ID setter
84: * @param string $txnId
85: * @return Mage_Paypal_Model_Payment_Transaction
86: */
87: public function setTxnId($txnId)
88: {
89: $this->_verifyTxnId($txnId);
90: return $this->setData('txn_id', $txnId);
91: }
92:
93: /**
94: * Check object before loading by by specified transaction ID
95: * @param $txnId
96: * @return Mage_Paypal_Model_Payment_Transaction
97: */
98: protected function _beforeLoadByTxnId($txnId)
99: {
100: Mage::dispatchEvent(
101: $this->_eventPrefix . '_load_by_txn_id_before',
102: $this->_getEventData() + array('txn_id' => $txnId)
103: );
104: return $this;
105: }
106:
107: /**
108: * Load self by specified transaction ID. Requires the valid payment object to be set
109: * @param string $txnId
110: * @return Mage_Paypal_Model_Payment_Transaction
111: */
112: public function loadByTxnId($txnId)
113: {
114: $this->_beforeLoadByTxnId($txnId);
115: $this->getResource()->loadObjectByTxnId(
116: $this, $txnId
117: );
118: $this->_afterLoadByTxnId();
119: return $this;
120: }
121:
122: /**
123: * Check object after loading by by specified transaction ID
124: * @param $txnId
125: * @return Mage_Paypal_Model_Payment_Transaction
126: */
127: protected function _afterLoadByTxnId()
128: {
129: Mage::dispatchEvent($this->_eventPrefix . '_load_by_txn_id_after', $this->_getEventData());
130: return $this;
131: }
132:
133:
134: /**
135: * Additional information setter
136: * Updates data inside the 'additional_information' array
137: * Doesn't allow to set arrays
138: *
139: * @param string $key
140: * @param mixed $value
141: * @return Mage_Paypal_Model_Order_Payment_Transaction
142: * @throws Mage_Core_Exception
143: */
144: public function setAdditionalInformation($key, $value)
145: {
146: if (is_object($value)) {
147: Mage::throwException(Mage::helper('paypal')->__('Payment transactions disallow storing objects.'));
148: }
149: $info = $this->_getData('additional_information');
150: if (!$info) {
151: $info = array();
152: }
153: $info[$key] = $value;
154: return $this->setData('additional_information', $info);
155: }
156:
157: /**
158: * Getter for entire additional_information value or one of its element by key
159: * @param string $key
160: * @return array|null|mixed
161: */
162: public function getAdditionalInformation($key = null)
163: {
164: $info = $this->_getData('additional_information');
165: if (!$info) {
166: $info = array();
167: }
168: if ($key) {
169: return (isset($info[$key]) ? $info[$key] : null);
170: }
171: return $info;
172: }
173:
174: /**
175: * Unsetter for entire additional_information value or one of its element by key
176: * @param string $key
177: * @return Mage_Paypal_Model_Payment_Transaction
178: */
179: public function unsAdditionalInformation($key = null)
180: {
181: if ($key) {
182: $info = $this->_getData('additional_information');
183: if (is_array($info)) {
184: unset($info[$key]);
185: }
186: } else {
187: $info = array();
188: }
189: return $this->setData('additional_information', $info);
190: }
191:
192: /**
193: * Setter/Getter whether transaction is supposed to prevent exceptions on saving
194: *
195: * @param bool $failsafe
196: */
197: public function isFailsafe($setFailsafe = null)
198: {
199: if (null === $setFailsafe) {
200: return $this->_isFailsafe;
201: }
202: $this->_isFailsafe = (bool)$setFailsafe;
203: return $this;
204: }
205:
206: /**
207: * Verify data required for saving
208: * @return Mage_Paypal_Model_Payment_Transaction
209: * @throws Mage_Core_Exception
210: */
211: protected function _beforeSave()
212: {
213: if (!$this->getId()) {
214: $this->setCreatedAt(Mage::getModel('core/date')->gmtDate());
215: }
216: return parent::_beforeSave();
217: }
218:
219: /**
220: * Check whether specified transaction ID is valid
221: * @param string $txnId
222: * @throws Mage_Core_Exception
223: */
224: protected function _verifyTxnId($txnId)
225: {
226: if (null !== $txnId && 0 == strlen($txnId)) {
227: Mage::throwException(Mage::helper('paypal')->__('Transaction ID must not be empty.'));
228: }
229: }
230:
231: /**
232: * Make sure this object is a valid transaction
233: * TODO for more restriction we can check for data consistency
234: * @throws Mage_Core_Exception
235: */
236: protected function _verifyThisTransactionExists()
237: {
238: if (!$this->getId()) {
239: Mage::throwException(Mage::helper('paypal')->__('This operation requires an existing transaction object.'));
240: }
241: }
242: }
243: