Info.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Model;
  7. use Magento\Framework\Model\AbstractExtensibleModel;
  8. /**
  9. * Payment information model
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Info extends AbstractExtensibleModel implements InfoInterface
  15. {
  16. /**
  17. * Additional information container
  18. *
  19. * @var array
  20. */
  21. protected $_additionalInformation = [];
  22. /**
  23. * Payment data
  24. *
  25. * @var \Magento\Payment\Helper\Data
  26. */
  27. protected $_paymentData;
  28. /**
  29. * @var \Magento\Framework\Encryption\EncryptorInterface
  30. */
  31. protected $_encryptor;
  32. /**
  33. * @param \Magento\Framework\Model\Context $context
  34. * @param \Magento\Framework\Registry $registry
  35. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  36. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  37. * @param \Magento\Payment\Helper\Data $paymentData
  38. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  39. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  40. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Framework\Model\Context $context,
  45. \Magento\Framework\Registry $registry,
  46. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  47. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  48. \Magento\Payment\Helper\Data $paymentData,
  49. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  50. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  51. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  52. array $data = []
  53. ) {
  54. $this->_paymentData = $paymentData;
  55. $this->_encryptor = $encryptor;
  56. parent::__construct(
  57. $context,
  58. $registry,
  59. $extensionFactory,
  60. $customAttributeFactory,
  61. $resource,
  62. $resourceCollection,
  63. $data
  64. );
  65. }
  66. /**
  67. * Retrieve data
  68. *
  69. * @param string $key
  70. * @param mixed $index
  71. * @return mixed
  72. */
  73. public function getData($key = '', $index = null)
  74. {
  75. if ('cc_number' === $key) {
  76. if (empty($this->_data['cc_number']) && !empty($this->_data['cc_number_enc'])) {
  77. $this->_data['cc_number'] = $this->decrypt($this->getCcNumberEnc());
  78. }
  79. }
  80. if ('cc_cid' === $key) {
  81. if (empty($this->_data['cc_cid']) && !empty($this->_data['cc_cid_enc'])) {
  82. $this->_data['cc_cid'] = $this->decrypt($this->getCcCidEnc());
  83. }
  84. }
  85. return parent::getData($key, $index);
  86. }
  87. /**
  88. * Retrieve payment method model object
  89. *
  90. * @return \Magento\Payment\Model\MethodInterface
  91. * @throws \Magento\Framework\Exception\LocalizedException
  92. */
  93. public function getMethodInstance()
  94. {
  95. if (!$this->hasMethodInstance()) {
  96. if (!$this->getMethod()) {
  97. throw new \Magento\Framework\Exception\LocalizedException(
  98. __('The payment method you requested is not available.')
  99. );
  100. }
  101. try {
  102. $instance = $this->_paymentData->getMethodInstance($this->getMethod());
  103. } catch (\UnexpectedValueException $e) {
  104. $instance = $this->_paymentData->getMethodInstance(Method\Substitution::CODE);
  105. }
  106. $instance->setInfoInstance($this);
  107. $this->setMethodInstance($instance);
  108. }
  109. return $this->_getData('method_instance');
  110. }
  111. /**
  112. * Encrypt data
  113. *
  114. * @param string $data
  115. * @return string
  116. */
  117. public function encrypt($data)
  118. {
  119. return $this->_encryptor->encrypt($data);
  120. }
  121. /**
  122. * Decrypt data
  123. *
  124. * @param string $data
  125. * @return string
  126. */
  127. public function decrypt($data)
  128. {
  129. return $this->_encryptor->decrypt($data);
  130. }
  131. /**
  132. * Additional information setter
  133. * Updates data inside the 'additional_information' array
  134. * or all 'additional_information' if key is data array
  135. *
  136. * @param string|array $key
  137. * @param mixed $value
  138. * @return $this
  139. * @throws \Magento\Framework\Exception\LocalizedException
  140. */
  141. public function setAdditionalInformation($key, $value = null)
  142. {
  143. if (is_object($value)) {
  144. throw new \Magento\Framework\Exception\LocalizedException(__('The payment disallows storing objects.'));
  145. }
  146. $this->_initAdditionalInformation();
  147. if (is_array($key) && $value === null) {
  148. $this->_additionalInformation = $key;
  149. } else {
  150. $this->_additionalInformation[$key] = $value;
  151. }
  152. return $this->setData('additional_information', $this->_additionalInformation);
  153. }
  154. /**
  155. * Getter for entire additional_information value or one of its element by key
  156. *
  157. * @param string $key
  158. * @return array|null|mixed
  159. */
  160. public function getAdditionalInformation($key = null)
  161. {
  162. $this->_initAdditionalInformation();
  163. if (null === $key) {
  164. return $this->_additionalInformation;
  165. }
  166. return isset($this->_additionalInformation[$key]) ? $this->_additionalInformation[$key] : null;
  167. }
  168. /**
  169. * Unsetter for entire additional_information value or one of its element by key
  170. *
  171. * @param string $key
  172. * @return $this
  173. */
  174. public function unsAdditionalInformation($key = null)
  175. {
  176. if ($key && isset($this->_additionalInformation[$key])) {
  177. unset($this->_additionalInformation[$key]);
  178. return $this->setData('additional_information', $this->_additionalInformation);
  179. } elseif (null === $key) {
  180. $this->_additionalInformation = [];
  181. return $this->unsetData('additional_information');
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Check whether there is additional information by specified key
  187. *
  188. * @param mixed|null $key
  189. * @return bool
  190. */
  191. public function hasAdditionalInformation($key = null)
  192. {
  193. $this->_initAdditionalInformation();
  194. return null === $key ? !empty($this->_additionalInformation) : array_key_exists(
  195. $key,
  196. $this->_additionalInformation
  197. );
  198. }
  199. /**
  200. * Initialize _additionalInformation with $this->_data['additional_information'] if empty
  201. *
  202. * @return void
  203. */
  204. protected function _initAdditionalInformation()
  205. {
  206. $additionalInfo = $this->_getData('additional_information');
  207. if (empty($this->_additionalInformation) && $additionalInfo) {
  208. $this->_additionalInformation = $additionalInfo;
  209. }
  210. }
  211. }