PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Payment/Model/Info.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 195 lines | 94 code | 10 blank | 91 comment | 21 complexity | 82ab3dd95fba350d64302774a5e755c1 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Payment
  23. * @copyright Copyright (c) 2010 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. * Payment information model
  28. *
  29. * @category Mage
  30. * @package Mage_Payment
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Payment_Model_Info extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Additional information container
  37. *
  38. * @var array
  39. */
  40. protected $_additionalInformation = -1;
  41. /**
  42. * Retrieve data
  43. *
  44. * @param string $key
  45. * @param mixed $index
  46. * @return unknown
  47. */
  48. public function getData($key='', $index=null)
  49. {
  50. if ('cc_number'===$key) {
  51. if (empty($this->_data['cc_number']) && !empty($this->_data['cc_number_enc'])) {
  52. $this->_data['cc_number'] = $this->decrypt($this->getCcNumberEnc());
  53. }
  54. }
  55. if ('cc_cid'===$key) {
  56. if (empty($this->_data['cc_cid']) && !empty($this->_data['cc_cid_enc'])) {
  57. $this->_data['cc_cid'] = $this->decrypt($this->getCcCidEnc());
  58. }
  59. }
  60. return parent::getData($key, $index);
  61. }
  62. /**
  63. * Retrieve payment method model object
  64. *
  65. * @return Mage_Payment_Model_Method_Abstract
  66. */
  67. public function getMethodInstance()
  68. {
  69. if (!$this->hasMethodInstance()) {
  70. if ($method = $this->getMethod()) {
  71. if ($instance = Mage::helper('payment')->getMethodInstance($this->getMethod())) {
  72. $instance->setInfoInstance($this);
  73. $this->setMethodInstance($instance);
  74. return $instance;
  75. }
  76. }
  77. } else {
  78. return $this->_getData('method_instance');
  79. }
  80. Mage::throwException(Mage::helper('payment')->__('Cannot retrieve payment method instance.'));
  81. }
  82. /**
  83. * Encrypt data
  84. *
  85. * @param string $data
  86. * @return string
  87. */
  88. public function encrypt($data)
  89. {
  90. if ($data) {
  91. return Mage::helper('core')->encrypt($data);
  92. }
  93. return $data;
  94. }
  95. /**
  96. * Decrypt data
  97. *
  98. * @param string $data
  99. * @return string
  100. */
  101. public function decrypt($data)
  102. {
  103. if ($data) {
  104. return Mage::helper('core')->decrypt($data);
  105. }
  106. return $data;
  107. }
  108. /**
  109. * Additional information setter
  110. * Updates data inside the 'additional_information' array
  111. * or all 'additional_information' if key is data array
  112. *
  113. * @param string|array $key
  114. * @param mixed $value
  115. * @return Mage_Payment_Model_Info
  116. * @throws Mage_Core_Exception
  117. */
  118. public function setAdditionalInformation($key, $value = null)
  119. {
  120. if (is_object($value)) {
  121. Mage::throwException(Mage::helper('sales')->__('Payment disallow storing objects.'));
  122. }
  123. $this->_initAdditionalInformation();
  124. if (is_array($key) && is_null($value)) {
  125. $this->_additionalInformation = $key;
  126. } else {
  127. $this->_additionalInformation[$key] = $value;
  128. }
  129. return $this->setData('additional_information', $this->_additionalInformation);
  130. }
  131. /**
  132. * Getter for entire additional_information value or one of its element by key
  133. *
  134. * @param string $key
  135. * @return array|null|mixed
  136. */
  137. public function getAdditionalInformation($key = null)
  138. {
  139. $this->_initAdditionalInformation();
  140. if (null === $key) {
  141. return $this->_additionalInformation;
  142. }
  143. return isset($this->_additionalInformation[$key]) ? $this->_additionalInformation[$key] : null;
  144. }
  145. /**
  146. * Unsetter for entire additional_information value or one of its element by key
  147. *
  148. * @param string $key
  149. * @return Mage_Payment_Model_Info
  150. */
  151. public function unsAdditionalInformation($key = null)
  152. {
  153. if ($key && isset($this->_additionalInformation[$key])) {
  154. unset($this->_additionalInformation[$key]);
  155. return $this->setData('additional_information', $this->_additionalInformation);
  156. }
  157. $this->_additionalInformation = -1;
  158. return $this->unsetData('additional_information');
  159. }
  160. /**
  161. * Check whether there is additional information by specified key
  162. *
  163. * @param $key
  164. * @return bool
  165. */
  166. public function hasAdditionalInformation($key = null)
  167. {
  168. $this->_initAdditionalInformation();
  169. return null === $key
  170. ? !empty($this->_additionalInformation)
  171. : array_key_exists($key, $this->_additionalInformation);
  172. }
  173. /**
  174. * Make sure _additionalInformation is an array
  175. */
  176. protected function _initAdditionalInformation()
  177. {
  178. if (-1 === $this->_additionalInformation) {
  179. $this->_additionalInformation = $this->_getData('additional_information');
  180. }
  181. if (null === $this->_additionalInformation) {
  182. $this->_additionalInformation = array();
  183. }
  184. }
  185. }