Payment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote;
  7. use Magento\Quote\Api\Data\PaymentInterface;
  8. /**
  9. * Quote payment information
  10. *
  11. * @api
  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. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  33. * @since 100.0.2
  34. */
  35. class Payment extends \Magento\Payment\Model\Info implements PaymentInterface
  36. {
  37. /**
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'sales_quote_payment';
  41. /**
  42. * @var string
  43. */
  44. protected $_eventObject = 'payment';
  45. /**
  46. * Quote model object
  47. *
  48. * @var \Magento\Quote\Model\Quote
  49. */
  50. protected $_quote;
  51. /**
  52. * @var \Magento\Payment\Model\Checks\SpecificationFactory
  53. */
  54. protected $methodSpecificationFactory;
  55. /**
  56. * @var array
  57. */
  58. private $additionalChecks;
  59. /**
  60. * @var \Magento\Framework\Serialize\Serializer\Json
  61. */
  62. private $serializer;
  63. /**
  64. * @var \Magento\Framework\Serialize\JsonValidator
  65. */
  66. private $jsonValidator;
  67. /**
  68. * @param \Magento\Framework\Model\Context $context
  69. * @param \Magento\Framework\Registry $registry
  70. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  71. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  72. * @param \Magento\Payment\Helper\Data $paymentData
  73. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  74. * @param \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory
  75. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  76. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  77. * @param array $data
  78. * @param array $additionalChecks
  79. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  80. * @param \Magento\Framework\Serialize\JsonValidator|null $jsonValidator
  81. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  82. */
  83. public function __construct(
  84. \Magento\Framework\Model\Context $context,
  85. \Magento\Framework\Registry $registry,
  86. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  87. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  88. \Magento\Payment\Helper\Data $paymentData,
  89. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  90. \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory,
  91. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  92. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  93. array $data = [],
  94. array $additionalChecks = [],
  95. \Magento\Framework\Serialize\Serializer\Json $serializer = null,
  96. \Magento\Framework\Serialize\JsonValidator $jsonValidator = null
  97. ) {
  98. $this->methodSpecificationFactory = $methodSpecificationFactory;
  99. $this->additionalChecks = $additionalChecks;
  100. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  101. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  102. $this->jsonValidator = $jsonValidator ?: \Magento\Framework\App\ObjectManager::getInstance()
  103. ->get(\Magento\Framework\Serialize\JsonValidator::class);
  104. parent::__construct(
  105. $context,
  106. $registry,
  107. $extensionFactory,
  108. $customAttributeFactory,
  109. $paymentData,
  110. $encryptor,
  111. $resource,
  112. $resourceCollection,
  113. $data
  114. );
  115. }
  116. /**
  117. * Initialize resource model
  118. *
  119. * @return void
  120. */
  121. protected function _construct()
  122. {
  123. $this->_init(\Magento\Quote\Model\ResourceModel\Quote\Payment::class);
  124. }
  125. /**
  126. * Declare quote model instance
  127. *
  128. * @param \Magento\Quote\Model\Quote $quote
  129. * @return $this
  130. */
  131. public function setQuote(\Magento\Quote\Model\Quote $quote)
  132. {
  133. $this->_quote = $quote;
  134. $this->setQuoteId($quote->getId());
  135. return $this;
  136. }
  137. /**
  138. * Retrieve quote model instance
  139. *
  140. * @codeCoverageIgnore
  141. *
  142. * @return \Magento\Quote\Model\Quote
  143. */
  144. public function getQuote()
  145. {
  146. return $this->_quote;
  147. }
  148. /**
  149. * Import data array to payment method object,
  150. * Method calls quote totals collect because payment method availability
  151. * can be related to quote totals
  152. *
  153. * @param array $data
  154. * @return $this
  155. * @throws \Magento\Framework\Exception\LocalizedException
  156. */
  157. public function importData(array $data)
  158. {
  159. $data = $this->convertPaymentData($data);
  160. $data = new \Magento\Framework\DataObject($data);
  161. $this->_eventManager->dispatch(
  162. $this->_eventPrefix . '_import_data_before',
  163. [$this->_eventObject => $this, 'input' => $data]
  164. );
  165. $this->setMethod($data->getMethod());
  166. $method = $this->getMethodInstance();
  167. $quote = $this->getQuote();
  168. /**
  169. * Payment availability related with quote totals.
  170. * We have to recollect quote totals before checking
  171. */
  172. $quote->collectTotals();
  173. $checks = array_merge($data->getChecks(), $this->additionalChecks);
  174. $methodSpecification = $this->methodSpecificationFactory->create($checks);
  175. if (!$method->isAvailable($quote) || !$methodSpecification->isApplicable($method, $quote)) {
  176. throw new \Magento\Framework\Exception\LocalizedException(
  177. __('The requested Payment Method is not available.')
  178. );
  179. }
  180. $method->assignData($data);
  181. /*
  182. * validating the payment data
  183. */
  184. $method->validate();
  185. return $this;
  186. }
  187. /**
  188. * Converts request to payment data
  189. *
  190. * @param array $rawData
  191. * @return array
  192. */
  193. private function convertPaymentData(array $rawData)
  194. {
  195. $paymentData = [
  196. PaymentInterface::KEY_METHOD => null,
  197. PaymentInterface::KEY_PO_NUMBER => null,
  198. PaymentInterface::KEY_ADDITIONAL_DATA => [],
  199. 'checks' => []
  200. ];
  201. foreach (array_keys($rawData) as $requestKey) {
  202. if (!array_key_exists($requestKey, $paymentData)) {
  203. $paymentData[PaymentInterface::KEY_ADDITIONAL_DATA][$requestKey] = $rawData[$requestKey];
  204. } elseif ($requestKey === PaymentInterface::KEY_ADDITIONAL_DATA) {
  205. $paymentData[PaymentInterface::KEY_ADDITIONAL_DATA] = array_merge(
  206. $paymentData[PaymentInterface::KEY_ADDITIONAL_DATA],
  207. (array) $rawData[$requestKey]
  208. );
  209. } else {
  210. $paymentData[$requestKey] = $rawData[$requestKey];
  211. }
  212. }
  213. return $paymentData;
  214. }
  215. /**
  216. * Prepare object for save
  217. *
  218. * @return $this
  219. */
  220. public function beforeSave()
  221. {
  222. if ($this->getQuote()) {
  223. $this->setQuoteId($this->getQuote()->getId());
  224. }
  225. return parent::beforeSave();
  226. }
  227. /**
  228. * Checkout redirect URL getter
  229. *
  230. * @return string
  231. */
  232. public function getCheckoutRedirectUrl()
  233. {
  234. $method = $this->getMethodInstance();
  235. if ($method) {
  236. return $method->getCheckoutRedirectUrl();
  237. }
  238. return '';
  239. }
  240. /**
  241. * Checkout order place redirect URL getter
  242. *
  243. * @return string
  244. */
  245. public function getOrderPlaceRedirectUrl()
  246. {
  247. $method = $this->getMethodInstance();
  248. if ($method) {
  249. return $method->getConfigData('order_place_redirect_url');
  250. }
  251. return '';
  252. }
  253. /**
  254. * Retrieve payment method model object
  255. *
  256. * @return \Magento\Payment\Model\MethodInterface
  257. */
  258. public function getMethodInstance()
  259. {
  260. $method = parent::getMethodInstance();
  261. $method->setStore($this->getQuote()->getStoreId());
  262. return $method;
  263. }
  264. /**
  265. * @codeCoverageIgnoreStart
  266. */
  267. /**
  268. * Get purchase order number
  269. *
  270. * @return string|null
  271. */
  272. public function getPoNumber()
  273. {
  274. return $this->getData(self::KEY_PO_NUMBER);
  275. }
  276. /**
  277. * Set purchase order number
  278. *
  279. * @param string $poNumber
  280. * @return $this
  281. */
  282. public function setPoNumber($poNumber)
  283. {
  284. return $this->setData(self::KEY_PO_NUMBER, $poNumber);
  285. }
  286. /**
  287. * Get payment method code
  288. *
  289. * @return string
  290. */
  291. public function getMethod()
  292. {
  293. return $this->getData(self::KEY_METHOD);
  294. }
  295. /**
  296. * Set payment method code
  297. *
  298. * @param string $method
  299. * @return $this
  300. */
  301. public function setMethod($method)
  302. {
  303. return $this->setData(self::KEY_METHOD, $method);
  304. }
  305. /**
  306. * Get payment additional details
  307. *
  308. * @return string[]|null
  309. */
  310. public function getAdditionalData()
  311. {
  312. $additionalDataValue = $this->getData(self::KEY_ADDITIONAL_DATA);
  313. if (is_array($additionalDataValue)) {
  314. return $additionalDataValue;
  315. }
  316. if (is_string($additionalDataValue) && $this->jsonValidator->isValid($additionalDataValue)) {
  317. $additionalData = $this->serializer->unserialize($additionalDataValue);
  318. if (is_array($additionalData)) {
  319. return $additionalData;
  320. }
  321. }
  322. return null;
  323. }
  324. /**
  325. * Set payment additional details
  326. *
  327. * @param string $additionalData
  328. * @return $this
  329. */
  330. public function setAdditionalData($additionalData)
  331. {
  332. return $this->setData(self::KEY_ADDITIONAL_DATA, $additionalData);
  333. }
  334. //@codeCoverageIgnoreEnd
  335. /**
  336. * {@inheritdoc}
  337. *
  338. * @return \Magento\Quote\Api\Data\PaymentExtensionInterface|null
  339. */
  340. public function getExtensionAttributes()
  341. {
  342. return $this->_getExtensionAttributes();
  343. }
  344. /**
  345. * {@inheritdoc}
  346. *
  347. * @param \Magento\Quote\Api\Data\PaymentExtensionInterface $extensionAttributes
  348. * @return $this
  349. */
  350. public function setExtensionAttributes(\Magento\Quote\Api\Data\PaymentExtensionInterface $extensionAttributes)
  351. {
  352. return $this->_setExtensionAttributes($extensionAttributes);
  353. }
  354. }