Info.php 7.0 KB

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