methodSpecificationFactory = $methodSpecificationFactory; $this->additionalChecks = $additionalChecks; $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\Serializer\Json::class); $this->jsonValidator = $jsonValidator ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\JsonValidator::class); parent::__construct( $context, $registry, $extensionFactory, $customAttributeFactory, $paymentData, $encryptor, $resource, $resourceCollection, $data ); } /** * Initialize resource model * * @return void */ protected function _construct() { $this->_init(\Magento\Quote\Model\ResourceModel\Quote\Payment::class); } /** * Declare quote model instance * * @param \Magento\Quote\Model\Quote $quote * @return $this */ public function setQuote(\Magento\Quote\Model\Quote $quote) { $this->_quote = $quote; $this->setQuoteId($quote->getId()); return $this; } /** * Retrieve quote model instance * * @codeCoverageIgnore * * @return \Magento\Quote\Model\Quote */ public function getQuote() { return $this->_quote; } /** * Import data array to payment method object, * Method calls quote totals collect because payment method availability * can be related to quote totals * * @param array $data * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function importData(array $data) { $data = $this->convertPaymentData($data); $data = new \Magento\Framework\DataObject($data); $this->_eventManager->dispatch( $this->_eventPrefix . '_import_data_before', [$this->_eventObject => $this, 'input' => $data] ); $this->setMethod($data->getMethod()); $method = $this->getMethodInstance(); $quote = $this->getQuote(); /** * Payment availability related with quote totals. * We have to recollect quote totals before checking */ $quote->collectTotals(); $checks = array_merge($data->getChecks(), $this->additionalChecks); $methodSpecification = $this->methodSpecificationFactory->create($checks); if (!$method->isAvailable($quote) || !$methodSpecification->isApplicable($method, $quote)) { throw new \Magento\Framework\Exception\LocalizedException( __('The requested Payment Method is not available.') ); } $method->assignData($data); /* * validating the payment data */ $method->validate(); return $this; } /** * Converts request to payment data * * @param array $rawData * @return array */ private function convertPaymentData(array $rawData) { $paymentData = [ PaymentInterface::KEY_METHOD => null, PaymentInterface::KEY_PO_NUMBER => null, PaymentInterface::KEY_ADDITIONAL_DATA => [], 'checks' => [] ]; foreach (array_keys($rawData) as $requestKey) { if (!array_key_exists($requestKey, $paymentData)) { $paymentData[PaymentInterface::KEY_ADDITIONAL_DATA][$requestKey] = $rawData[$requestKey]; } elseif ($requestKey === PaymentInterface::KEY_ADDITIONAL_DATA) { $paymentData[PaymentInterface::KEY_ADDITIONAL_DATA] = array_merge( $paymentData[PaymentInterface::KEY_ADDITIONAL_DATA], (array) $rawData[$requestKey] ); } else { $paymentData[$requestKey] = $rawData[$requestKey]; } } return $paymentData; } /** * Prepare object for save * * @return $this */ public function beforeSave() { if ($this->getQuote()) { $this->setQuoteId($this->getQuote()->getId()); } return parent::beforeSave(); } /** * Checkout redirect URL getter * * @return string */ public function getCheckoutRedirectUrl() { $method = $this->getMethodInstance(); if ($method) { return $method->getCheckoutRedirectUrl(); } return ''; } /** * Checkout order place redirect URL getter * * @return string */ public function getOrderPlaceRedirectUrl() { $method = $this->getMethodInstance(); if ($method) { return $method->getConfigData('order_place_redirect_url'); } return ''; } /** * Retrieve payment method model object * * @return \Magento\Payment\Model\MethodInterface */ public function getMethodInstance() { $method = parent::getMethodInstance(); $method->setStore($this->getQuote()->getStoreId()); return $method; } /** * @codeCoverageIgnoreStart */ /** * Get purchase order number * * @return string|null */ public function getPoNumber() { return $this->getData(self::KEY_PO_NUMBER); } /** * Set purchase order number * * @param string $poNumber * @return $this */ public function setPoNumber($poNumber) { return $this->setData(self::KEY_PO_NUMBER, $poNumber); } /** * Get payment method code * * @return string */ public function getMethod() { return $this->getData(self::KEY_METHOD); } /** * Set payment method code * * @param string $method * @return $this */ public function setMethod($method) { return $this->setData(self::KEY_METHOD, $method); } /** * Get payment additional details * * @return string[]|null */ public function getAdditionalData() { $additionalDataValue = $this->getData(self::KEY_ADDITIONAL_DATA); if (is_array($additionalDataValue)) { return $additionalDataValue; } if (is_string($additionalDataValue) && $this->jsonValidator->isValid($additionalDataValue)) { $additionalData = $this->serializer->unserialize($additionalDataValue); if (is_array($additionalData)) { return $additionalData; } } return null; } /** * Set payment additional details * * @param string $additionalData * @return $this */ public function setAdditionalData($additionalData) { return $this->setData(self::KEY_ADDITIONAL_DATA, $additionalData); } //@codeCoverageIgnoreEnd /** * {@inheritdoc} * * @return \Magento\Quote\Api\Data\PaymentExtensionInterface|null */ public function getExtensionAttributes() { return $this->_getExtensionAttributes(); } /** * {@inheritdoc} * * @param \Magento\Quote\Api\Data\PaymentExtensionInterface $extensionAttributes * @return $this */ public function setExtensionAttributes(\Magento\Quote\Api\Data\PaymentExtensionInterface $extensionAttributes) { return $this->_setExtensionAttributes($extensionAttributes); } }