PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Quote/Model/Quote/Payment.php

https://gitlab.com/AlexandrSy/magento.xxx
PHP | 327 lines | 139 code | 26 blank | 162 comment | 7 complexity | 6e7b8d8120063f05da2b2539defcb896 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote;
  7. /**
  8. * Quote payment information
  9. *
  10. * @method \Magento\Quote\Model\ResourceModel\Quote\Payment _getResource()
  11. * @method \Magento\Quote\Model\ResourceModel\Quote\Payment getResource()
  12. * @method int getQuoteId()
  13. * @method \Magento\Quote\Model\Quote\Payment setQuoteId(int $value)
  14. * @method string getCreatedAt()
  15. * @method \Magento\Quote\Model\Quote\Payment setCreatedAt(string $value)
  16. * @method string getUpdatedAt()
  17. * @method \Magento\Quote\Model\Quote\Payment setUpdatedAt(string $value)
  18. * @method string getCcNumberEnc()
  19. * @method \Magento\Quote\Model\Quote\Payment setCcNumberEnc(string $value)
  20. * @method string getCcLast4()
  21. * @method \Magento\Quote\Model\Quote\Payment setCcLast4(string $value)
  22. * @method string getCcCidEnc()
  23. * @method \Magento\Quote\Model\Quote\Payment setCcCidEnc(string $value)
  24. * @method string getCcSsOwner()
  25. * @method \Magento\Quote\Model\Quote\Payment setCcSsOwner(string $value)
  26. * @method int getCcSsStartMonth()
  27. * @method \Magento\Quote\Model\Quote\Payment setCcSsStartMonth(int $value)
  28. * @method int getCcSsStartYear()
  29. * @method \Magento\Quote\Model\Quote\Payment setCcSsStartYear(int $value)
  30. * @method string getCcSsIssue()
  31. * @method \Magento\Quote\Model\Quote\Payment setCcSsIssue(string $value)
  32. *
  33. * @author Magento Core Team <core@magentocommerce.com>
  34. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  35. */
  36. class Payment extends \Magento\Payment\Model\Info implements \Magento\Quote\Api\Data\PaymentInterface
  37. {
  38. /**
  39. * @var string
  40. */
  41. protected $_eventPrefix = 'sales_quote_payment';
  42. /**
  43. * @var string
  44. */
  45. protected $_eventObject = 'payment';
  46. /**
  47. * Quote model object
  48. *
  49. * @var \Magento\Quote\Model\Quote
  50. */
  51. protected $_quote;
  52. /**
  53. * @var \Magento\Payment\Model\Checks\SpecificationFactory
  54. */
  55. protected $methodSpecificationFactory;
  56. /**
  57. * @param \Magento\Framework\Model\Context $context
  58. * @param \Magento\Framework\Registry $registry
  59. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  60. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  61. * @param \Magento\Payment\Helper\Data $paymentData
  62. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  63. * @param \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory
  64. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  65. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  66. * @param array $data
  67. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  68. */
  69. public function __construct(
  70. \Magento\Framework\Model\Context $context,
  71. \Magento\Framework\Registry $registry,
  72. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  73. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  74. \Magento\Payment\Helper\Data $paymentData,
  75. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  76. \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory,
  77. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  78. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  79. array $data = []
  80. ) {
  81. $this->methodSpecificationFactory = $methodSpecificationFactory;
  82. parent::__construct(
  83. $context,
  84. $registry,
  85. $extensionFactory,
  86. $customAttributeFactory,
  87. $paymentData,
  88. $encryptor,
  89. $resource,
  90. $resourceCollection,
  91. $data
  92. );
  93. }
  94. /**
  95. * Initialize resource model
  96. *
  97. * @return void
  98. */
  99. protected function _construct()
  100. {
  101. $this->_init('Magento\Quote\Model\ResourceModel\Quote\Payment');
  102. }
  103. /**
  104. * Declare quote model instance
  105. *
  106. * @param \Magento\Quote\Model\Quote $quote
  107. * @return $this
  108. */
  109. public function setQuote(\Magento\Quote\Model\Quote $quote)
  110. {
  111. $this->_quote = $quote;
  112. $this->setQuoteId($quote->getId());
  113. return $this;
  114. }
  115. /**
  116. * Retrieve quote model instance
  117. *
  118. * @codeCoverageIgnore
  119. *
  120. * @return \Magento\Quote\Model\Quote
  121. */
  122. public function getQuote()
  123. {
  124. return $this->_quote;
  125. }
  126. /**
  127. * Import data array to payment method object,
  128. * Method calls quote totals collect because payment method availability
  129. * can be related to quote totals
  130. *
  131. * @param array $data
  132. * @return $this
  133. * @throws \Magento\Framework\Exception\LocalizedException
  134. */
  135. public function importData(array $data)
  136. {
  137. $data = new \Magento\Framework\DataObject($data);
  138. $this->_eventManager->dispatch(
  139. $this->_eventPrefix . '_import_data_before',
  140. [$this->_eventObject => $this, 'input' => $data]
  141. );
  142. $this->setMethod($data->getMethod());
  143. $method = $this->getMethodInstance();
  144. $quote = $this->getQuote();
  145. /**
  146. * Payment availability related with quote totals.
  147. * We have to recollect quote totals before checking
  148. */
  149. $quote->collectTotals();
  150. $methodSpecification = $this->methodSpecificationFactory->create($data->getChecks());
  151. if (!$method->isAvailable($quote) || !$methodSpecification->isApplicable($method, $quote)) {
  152. throw new \Magento\Framework\Exception\LocalizedException(
  153. __('The requested Payment Method is not available.')
  154. );
  155. }
  156. $method->assignData($data);
  157. /*
  158. * validating the payment data
  159. */
  160. $method->validate();
  161. return $this;
  162. }
  163. /**
  164. * Prepare object for save
  165. *
  166. * @return $this
  167. */
  168. public function beforeSave()
  169. {
  170. if ($this->getQuote()) {
  171. $this->setQuoteId($this->getQuote()->getId());
  172. }
  173. return parent::beforeSave();
  174. }
  175. /**
  176. * Checkout redirect URL getter
  177. *
  178. * @return string
  179. */
  180. public function getCheckoutRedirectUrl()
  181. {
  182. $method = $this->getMethodInstance();
  183. if ($method) {
  184. return $method->getCheckoutRedirectUrl();
  185. }
  186. return '';
  187. }
  188. /**
  189. * Checkout order place redirect URL getter
  190. *
  191. * @return string
  192. */
  193. public function getOrderPlaceRedirectUrl()
  194. {
  195. $method = $this->getMethodInstance();
  196. if ($method) {
  197. return $method->getConfigData('order_place_redirect_url');
  198. }
  199. return '';
  200. }
  201. /**
  202. * Retrieve payment method model object
  203. *
  204. * @return \Magento\Payment\Model\MethodInterface
  205. */
  206. public function getMethodInstance()
  207. {
  208. $method = parent::getMethodInstance();
  209. $method->setStore($this->getQuote()->getStore()->getStoreId());
  210. return $method;
  211. }
  212. /**
  213. * @codeCoverageIgnoreStart
  214. */
  215. /**
  216. * Get purchase order number
  217. *
  218. * @return string|null
  219. */
  220. public function getPoNumber()
  221. {
  222. return $this->getData(self::KEY_PO_NUMBER);
  223. }
  224. /**
  225. * Set purchase order number
  226. *
  227. * @param string $poNumber
  228. * @return $this
  229. */
  230. public function setPoNumber($poNumber)
  231. {
  232. return $this->setData(self::KEY_PO_NUMBER, $poNumber);
  233. }
  234. /**
  235. * Get payment method code
  236. *
  237. * @return string
  238. */
  239. public function getMethod()
  240. {
  241. return $this->getData(self::KEY_METHOD);
  242. }
  243. /**
  244. * Set payment method code
  245. *
  246. * @param string $method
  247. * @return $this
  248. */
  249. public function setMethod($method)
  250. {
  251. return $this->setData(self::KEY_METHOD, $method);
  252. }
  253. /**
  254. * Get payment additional details
  255. *
  256. * @return string[]|null
  257. */
  258. public function getAdditionalData()
  259. {
  260. $additionalDataValue = $this->getData(self::KEY_ADDITIONAL_DATA);
  261. if (is_string($additionalDataValue)) {
  262. $additionalData = @unserialize($additionalDataValue);
  263. if (is_array($additionalData)) {
  264. return $additionalData;
  265. }
  266. } elseif (is_array($additionalDataValue)) {
  267. return $additionalDataValue;
  268. }
  269. return null;
  270. }
  271. /**
  272. * Set payment additional details
  273. *
  274. * @param string $additionalData
  275. * @return $this
  276. */
  277. public function setAdditionalData($additionalData)
  278. {
  279. return $this->setData(self::KEY_ADDITIONAL_DATA, $additionalData);
  280. }
  281. //@codeCoverageIgnoreEnd
  282. /**
  283. * {@inheritdoc}
  284. *
  285. * @return \Magento\Quote\Api\Data\PaymentExtensionInterface|null
  286. */
  287. public function getExtensionAttributes()
  288. {
  289. return $this->_getExtensionAttributes();
  290. }
  291. /**
  292. * {@inheritdoc}
  293. *
  294. * @param \Magento\Quote\Api\Data\PaymentExtensionInterface $extensionAttributes
  295. * @return $this
  296. */
  297. public function setExtensionAttributes(\Magento\Quote\Api\Data\PaymentExtensionInterface $extensionAttributes)
  298. {
  299. return $this->_setExtensionAttributes($extensionAttributes);
  300. }
  301. }