123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Payment\Model\Method;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\DataObject;
- use Magento\Framework\Event\ManagerInterface;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Payment\Gateway\Command\CommandManagerInterface;
- use Magento\Payment\Gateway\Command\CommandPoolInterface;
- use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
- use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
- use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
- use Magento\Payment\Model\InfoInterface;
- use Magento\Payment\Model\MethodInterface;
- use Magento\Payment\Observer\AbstractDataAssignObserver;
- use Magento\Quote\Api\Data\CartInterface;
- use Psr\Log\LoggerInterface;
- /**
- * Payment method facade. Abstract method adapter
- *
- * @SuppressWarnings(PHPMD.ExcessivePublicCount)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- *
- * @api Use this class as a base for virtual types declaration
- * @since 100.0.2
- */
- class Adapter implements MethodInterface
- {
- /**
- * @var ValueHandlerPoolInterface
- */
- private $valueHandlerPool;
- /**
- * @var ValidatorPoolInterface
- */
- private $validatorPool;
- /**
- * @var CommandPoolInterface
- */
- private $commandPool;
- /**
- * @var int
- */
- private $storeId;
- /**
- * @var string
- */
- private $formBlockType;
- /**
- * @var string
- */
- private $infoBlockType;
- /**
- * @var InfoInterface
- */
- private $infoInstance;
- /**
- * @var string
- */
- private $code;
- /**
- * @var ManagerInterface
- */
- private $eventManager;
- /**
- * @var PaymentDataObjectFactory
- */
- private $paymentDataObjectFactory;
- /**
- * @var \Magento\Payment\Gateway\Command\CommandManagerInterface
- */
- private $commandExecutor;
- /**
- * Logger for exception details
- *
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @param ManagerInterface $eventManager
- * @param ValueHandlerPoolInterface $valueHandlerPool
- * @param PaymentDataObjectFactory $paymentDataObjectFactory
- * @param string $code
- * @param string $formBlockType
- * @param string $infoBlockType
- * @param CommandPoolInterface|null $commandPool
- * @param ValidatorPoolInterface|null $validatorPool
- * @param CommandManagerInterface|null $commandExecutor
- * @param LoggerInterface|null $logger
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- ManagerInterface $eventManager,
- ValueHandlerPoolInterface $valueHandlerPool,
- PaymentDataObjectFactory $paymentDataObjectFactory,
- $code,
- $formBlockType,
- $infoBlockType,
- CommandPoolInterface $commandPool = null,
- ValidatorPoolInterface $validatorPool = null,
- CommandManagerInterface $commandExecutor = null,
- LoggerInterface $logger = null
- ) {
- $this->valueHandlerPool = $valueHandlerPool;
- $this->validatorPool = $validatorPool;
- $this->commandPool = $commandPool;
- $this->code = $code;
- $this->infoBlockType = $infoBlockType;
- $this->formBlockType = $formBlockType;
- $this->eventManager = $eventManager;
- $this->paymentDataObjectFactory = $paymentDataObjectFactory;
- $this->commandExecutor = $commandExecutor;
- $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
- }
- /**
- * Returns Validator pool
- *
- * @return ValidatorPoolInterface
- * @throws \DomainException
- */
- public function getValidatorPool()
- {
- if ($this->validatorPool === null) {
- throw new \DomainException('Validator pool is not configured for use.');
- }
- return $this->validatorPool;
- }
- /**
- * @inheritdoc
- */
- public function canOrder()
- {
- return $this->canPerformCommand('order');
- }
- /**
- * @inheritdoc
- */
- public function canAuthorize()
- {
- return $this->canPerformCommand('authorize');
- }
- /**
- * @inheritdoc
- */
- public function canCapture()
- {
- return $this->canPerformCommand('capture');
- }
- /**
- * @inheritdoc
- */
- public function canCapturePartial()
- {
- return $this->canPerformCommand('capture_partial');
- }
- /**
- * @inheritdoc
- */
- public function canCaptureOnce()
- {
- return $this->canPerformCommand('capture_once');
- }
- /**
- * @inheritdoc
- */
- public function canRefund()
- {
- return $this->canPerformCommand('refund');
- }
- /**
- * @inheritdoc
- */
- public function canRefundPartialPerInvoice()
- {
- return $this->canPerformCommand('refund_partial_per_invoice');
- }
- /**
- * @inheritdoc
- */
- public function canVoid()
- {
- return $this->canPerformCommand('void');
- }
- /**
- * @inheritdoc
- */
- public function canUseInternal()
- {
- return (bool)$this->getConfiguredValue('can_use_internal');
- }
- /**
- * @inheritdoc
- */
- public function canUseCheckout()
- {
- return (bool)$this->getConfiguredValue('can_use_checkout');
- }
- /**
- * @inheritdoc
- */
- public function canEdit()
- {
- return (bool)$this->getConfiguredValue('can_edit');
- }
- /**
- * @inheritdoc
- */
- public function canFetchTransactionInfo()
- {
- return $this->canPerformCommand('fetch_transaction_info');
- }
- /**
- * @inheritdoc
- */
- public function canReviewPayment()
- {
- return $this->canPerformCommand('review_payment');
- }
- /**
- * @inheritdoc
- */
- public function isGateway()
- {
- return (bool)$this->getConfiguredValue('is_gateway');
- }
- /**
- * @inheritdoc
- */
- public function isOffline()
- {
- return (bool)$this->getConfiguredValue('is_offline');
- }
- /**
- * @inheritdoc
- */
- public function isInitializeNeeded()
- {
- return (bool)(int)$this->getConfiguredValue('can_initialize');
- }
- /**
- * @inheritdoc
- */
- public function isAvailable(CartInterface $quote = null)
- {
- if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
- return false;
- }
- $checkResult = new DataObject();
- $checkResult->setData('is_available', true);
- try {
- $infoInstance = $this->getInfoInstance();
- if ($infoInstance !== null) {
- $validator = $this->getValidatorPool()->get('availability');
- $result = $validator->validate(
- [
- 'payment' => $this->paymentDataObjectFactory->create($infoInstance)
- ]
- );
- $checkResult->setData('is_available', $result->isValid());
- }
- } catch (\Exception $e) {
- // pass
- }
- // for future use in observers
- $this->eventManager->dispatch(
- 'payment_method_is_active',
- [
- 'result' => $checkResult,
- 'method_instance' => $this,
- 'quote' => $quote
- ]
- );
- return $checkResult->getData('is_available');
- }
- /**
- * @inheritdoc
- */
- public function isActive($storeId = null)
- {
- return (bool)$this->getConfiguredValue('active', $storeId);
- }
- /**
- * @inheritdoc
- */
- public function canUseForCountry($country)
- {
- try {
- $validator = $this->getValidatorPool()->get('country');
- } catch (\Exception $e) {
- return true;
- }
- $result = $validator->validate(['country' => $country, 'storeId' => $this->getStore()]);
- return $result->isValid();
- }
- /**
- * @inheritdoc
- */
- public function canUseForCurrency($currencyCode)
- {
- try {
- $validator = $this->getValidatorPool()->get('currency');
- } catch (\Exception $e) {
- return true;
- }
- $result = $validator->validate(['currency' => $currencyCode, 'storeId' => $this->getStore()]);
- return $result->isValid();
- }
- /**
- * Whether payment command is supported and can be executed
- *
- * @param string $commandCode
- * @return bool
- */
- private function canPerformCommand($commandCode)
- {
- return (bool)$this->getConfiguredValue('can_' . $commandCode);
- }
- /**
- * Unifies configured value handling logic
- *
- * @param string $field
- * @param null $storeId
- * @return mixed
- */
- private function getConfiguredValue($field, $storeId = null)
- {
- $handler = $this->valueHandlerPool->get($field);
- $subject = [
- 'field' => $field
- ];
- if ($this->getInfoInstance()) {
- $subject['payment'] = $this->paymentDataObjectFactory->create($this->getInfoInstance());
- }
- return $handler->handle($subject, $storeId ?: $this->getStore());
- }
- /**
- * @inheritdoc
- */
- public function getConfigData($field, $storeId = null)
- {
- return $this->getConfiguredValue($field, $storeId);
- }
- /**
- * @inheritdoc
- */
- public function validate()
- {
- try {
- $validator = $this->getValidatorPool()->get('global');
- } catch (\Exception $e) {
- return $this;
- }
- $result = $validator->validate(
- ['payment' => $this->getInfoInstance(), 'storeId' => $this->getStore()]
- );
- if (!$result->isValid()) {
- throw new LocalizedException(
- __(implode("\n", $result->getFailsDescription()))
- );
- }
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
- {
- return $this->executeCommand(
- 'fetch_transaction_information',
- ['payment' => $payment, 'transactionId' => $transactionId]
- );
- }
- /**
- * @inheritdoc
- */
- public function order(InfoInterface $payment, $amount)
- {
- $this->executeCommand(
- 'order',
- ['payment' => $payment, 'amount' => $amount]
- );
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function authorize(InfoInterface $payment, $amount)
- {
- $this->executeCommand(
- 'authorize',
- ['payment' => $payment, 'amount' => $amount]
- );
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function capture(InfoInterface $payment, $amount)
- {
- $this->executeCommand(
- 'capture',
- ['payment' => $payment, 'amount' => $amount]
- );
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function refund(InfoInterface $payment, $amount)
- {
- $this->executeCommand(
- 'refund',
- ['payment' => $payment, 'amount' => $amount]
- );
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function cancel(InfoInterface $payment)
- {
- $this->executeCommand('cancel', ['payment' => $payment]);
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function void(InfoInterface $payment)
- {
- $this->executeCommand('void', ['payment' => $payment]);
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function acceptPayment(InfoInterface $payment)
- {
- $this->executeCommand('accept_payment', ['payment' => $payment]);
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function denyPayment(InfoInterface $payment)
- {
- $this->executeCommand('deny_payment', ['payment' => $payment]);
- return $this;
- }
- /**
- * @inheritdoc
- */
- private function executeCommand($commandCode, array $arguments = [])
- {
- if (!$this->canPerformCommand($commandCode)) {
- return null;
- }
- /** @var InfoInterface|null $payment */
- $payment = null;
- if (isset($arguments['payment']) && $arguments['payment'] instanceof InfoInterface) {
- $payment = $arguments['payment'];
- $arguments['payment'] = $this->paymentDataObjectFactory->create($arguments['payment']);
- }
- if ($this->commandExecutor !== null) {
- return $this->commandExecutor->executeByCode($commandCode, $payment, $arguments);
- }
- if ($this->commandPool === null) {
- throw new \DomainException("The command pool isn't configured for use.");
- }
- $command = $this->commandPool->get($commandCode);
- return $command->execute($arguments);
- }
- /**
- * @inheritdoc
- */
- public function getCode()
- {
- return $this->code;
- }
- /**
- * @inheritdoc
- */
- public function getTitle()
- {
- return $this->getConfiguredValue('title');
- }
- /**
- * @inheritdoc
- */
- public function setStore($storeId)
- {
- $this->storeId = (int)$storeId;
- }
- /**
- * @inheritdoc
- */
- public function getStore()
- {
- return $this->storeId;
- }
- /**
- * @inheritdoc
- */
- public function getFormBlockType()
- {
- return $this->formBlockType;
- }
- /**
- * @inheritdoc
- */
- public function getInfoBlockType()
- {
- return $this->infoBlockType;
- }
- /**
- * @inheritdoc
- */
- public function getInfoInstance()
- {
- return $this->infoInstance;
- }
- /**
- * @inheritdoc
- */
- public function setInfoInstance(InfoInterface $info)
- {
- $this->infoInstance = $info;
- }
- /**
- * @inheritdoc
- * @param DataObject $data
- * @return $this
- */
- public function assignData(\Magento\Framework\DataObject $data)
- {
- $this->eventManager->dispatch(
- 'payment_method_assign_data_' . $this->getCode(),
- [
- AbstractDataAssignObserver::METHOD_CODE => $this,
- AbstractDataAssignObserver::MODEL_CODE => $this->getInfoInstance(),
- AbstractDataAssignObserver::DATA_CODE => $data
- ]
- );
- $this->eventManager->dispatch(
- 'payment_method_assign_data',
- [
- AbstractDataAssignObserver::METHOD_CODE => $this,
- AbstractDataAssignObserver::MODEL_CODE => $this->getInfoInstance(),
- AbstractDataAssignObserver::DATA_CODE => $data
- ]
- );
- return $this;
- }
- /**
- * @inheritdoc
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function initialize($paymentAction, $stateObject)
- {
- $this->executeCommand(
- 'initialize',
- [
- 'payment' => $this->getInfoInstance(),
- 'paymentAction' => $paymentAction,
- 'stateObject' => $stateObject
- ]
- );
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function getConfigPaymentAction()
- {
- return $this->getConfiguredValue('payment_action');
- }
- }
|