123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Vault\Model\Method;
- use Magento\Framework\Event\ManagerInterface;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Payment\Gateway\Command;
- use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
- use Magento\Payment\Gateway\ConfigFactoryInterface;
- use Magento\Payment\Gateway\ConfigInterface;
- use Magento\Payment\Model\InfoInterface;
- use Magento\Payment\Model\MethodInterface;
- use Magento\Payment\Observer\AbstractDataAssignObserver;
- use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
- use Magento\Sales\Api\Data\OrderPaymentInterface;
- use Magento\Sales\Model\Order\Payment;
- use Magento\Vault\Api\Data\PaymentTokenInterface;
- use Magento\Vault\Api\PaymentTokenManagementInterface;
- use Magento\Vault\Block\Form;
- use Magento\Vault\Model\VaultPaymentInterface;
- /**
- * Class Vault
- *
- * @SuppressWarnings(PHPMD.ExcessivePublicCount)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- *
- * @api
- * @since 100.1.0
- */
- class Vault implements VaultPaymentInterface
- {
- /**
- * @deprecated
- */
- const TOKEN_METADATA_KEY = 'token_metadata';
- /**
- * @var string
- */
- private static $activeKey = 'active';
- /**
- * @var string
- */
- private static $titleKey = 'title';
- /**
- * @var ConfigFactoryInterface
- */
- private $configFactory;
- /**
- * @var ConfigInterface
- */
- private $config;
- /**
- * @var MethodInterface
- */
- private $vaultProvider;
- /**
- * @var ObjectManagerInterface
- */
- private $objectManager;
- /**
- * @var int
- */
- private $storeId;
- /**
- * @var ValueHandlerPoolInterface
- */
- private $valueHandlerPool;
- /**
- * @var ManagerInterface
- */
- private $eventManager;
- /**
- * @var Command\CommandManagerPoolInterface
- */
- private $commandManagerPool;
- /**
- * @var PaymentTokenManagementInterface
- */
- private $tokenManagement;
- /**
- * @var OrderPaymentExtensionInterfaceFactory
- */
- private $paymentExtensionFactory;
- /**
- * @var string
- */
- private $code;
- /**
- * Constructor
- *
- * @param ConfigInterface $config
- * @param ConfigFactoryInterface $configFactory
- * @param ObjectManagerInterface $objectManager
- * @param MethodInterface $vaultProvider
- * @param ManagerInterface $eventManager
- * @param ValueHandlerPoolInterface $valueHandlerPool
- * @param Command\CommandManagerPoolInterface $commandManagerPool
- * @param PaymentTokenManagementInterface $tokenManagement
- * @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
- * @param string $code
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- ConfigInterface $config,
- ConfigFactoryInterface $configFactory,
- ObjectManagerInterface $objectManager,
- MethodInterface $vaultProvider,
- ManagerInterface $eventManager,
- ValueHandlerPoolInterface $valueHandlerPool,
- Command\CommandManagerPoolInterface $commandManagerPool,
- PaymentTokenManagementInterface $tokenManagement,
- OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
- $code
- ) {
- $this->config = $config;
- $this->configFactory = $configFactory;
- $this->objectManager = $objectManager;
- $this->valueHandlerPool = $valueHandlerPool;
- $this->vaultProvider = $vaultProvider;
- $this->eventManager = $eventManager;
- $this->commandManagerPool = $commandManagerPool;
- $this->tokenManagement = $tokenManagement;
- $this->paymentExtensionFactory = $paymentExtensionFactory;
- $this->code = $code;
- }
- /**
- * @return MethodInterface
- */
- private function getVaultProvider()
- {
- return $this->vaultProvider;
- }
- /**
- * 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];
- return $handler->handle($subject, $storeId ?: $this->getStore());
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getCode()
- {
- return $this->code;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getFormBlockType()
- {
- return Form::class;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getTitle()
- {
- return $this->getConfiguredValue(self::$titleKey);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function setStore($storeId)
- {
- $this->storeId = (int)$storeId;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getStore()
- {
- return $this->storeId;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canOrder()
- {
- return false;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canAuthorize()
- {
- return $this->getVaultProvider()->canAuthorize()
- && $this->getVaultProvider()->getConfigData(static::CAN_AUTHORIZE);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canCapture()
- {
- return $this->getVaultProvider()->canCapture()
- && $this->getVaultProvider()->getConfigData(static::CAN_CAPTURE);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canCapturePartial()
- {
- return false;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canCaptureOnce()
- {
- return $this->getVaultProvider()->canCaptureOnce();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canRefund()
- {
- return false;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canRefundPartialPerInvoice()
- {
- return false;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canVoid()
- {
- return false;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canUseInternal()
- {
- $isInternalAllowed = $this->getConfiguredValue('can_use_internal');
- // if config has't been specified for Vault, need to check payment provider option
- if ($isInternalAllowed === null) {
- return $this->getVaultProvider()->canUseInternal();
- }
- return (bool) $isInternalAllowed;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canUseCheckout()
- {
- return $this->getVaultProvider()->canUseCheckout();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canEdit()
- {
- return $this->getVaultProvider()->canEdit();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canFetchTransactionInfo()
- {
- return false;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function isGateway()
- {
- return $this->getVaultProvider()->isGateway();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function isOffline()
- {
- return $this->getVaultProvider()->isOffline();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function isInitializeNeeded()
- {
- return $this->getVaultProvider()->isInitializeNeeded();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canUseForCountry($country)
- {
- return $this->getVaultProvider()->canUseForCountry($country);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canUseForCurrency($currencyCode)
- {
- return $this->getVaultProvider()->canUseForCurrency($currencyCode);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getInfoBlockType()
- {
- return $this->getVaultProvider()->getInfoBlockType();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getInfoInstance()
- {
- return $this->getVaultProvider()->getInfoInstance();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function setInfoInstance(InfoInterface $info)
- {
- $this->getVaultProvider()->setInfoInstance($info);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function validate()
- {
- return $this->getVaultProvider()->validate();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function order(\Magento\Payment\Model\InfoInterface $payment, $amount)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
- {
- if (!$payment instanceof OrderPaymentInterface) {
- throw new \DomainException('Not implemented');
- }
- /** @var $payment OrderPaymentInterface */
- $this->attachTokenExtensionAttribute($payment);
- $commandExecutor = $this->commandManagerPool->get(
- $this->getVaultProvider()->getCode()
- );
- $commandExecutor->executeByCode(
- VaultPaymentInterface::VAULT_AUTHORIZE_COMMAND,
- $payment,
- [
- 'amount' => $amount
- ]
- );
- $payment->setMethod($this->getVaultProvider()->getCode());
- return $this;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
- {
- if (!$payment instanceof OrderPaymentInterface) {
- throw new \DomainException('Not implemented');
- }
- /** @var $payment Payment */
- if ($payment->getAuthorizationTransaction()) {
- throw new \DomainException('Capture can not be performed through vault');
- }
- $this->attachTokenExtensionAttribute($payment);
- $commandExecutor = $this->commandManagerPool->get(
- $this->getVaultProvider()->getCode()
- );
- $commandExecutor->executeByCode(
- VaultPaymentInterface::VAULT_SALE_COMMAND,
- $payment,
- [
- 'amount' => $amount
- ]
- );
- $payment->setMethod($this->getVaultProvider()->getCode());
- }
- /**
- * @param OrderPaymentInterface $orderPayment
- * @return void
- */
- private function attachTokenExtensionAttribute(OrderPaymentInterface $orderPayment)
- {
- $additionalInformation = $orderPayment->getAdditionalInformation();
- if (empty($additionalInformation[PaymentTokenInterface::PUBLIC_HASH])) {
- throw new \LogicException('Public hash should be defined');
- }
- $customerId = isset($additionalInformation[PaymentTokenInterface::CUSTOMER_ID]) ?
- $additionalInformation[PaymentTokenInterface::CUSTOMER_ID] : null;
- $publicHash = $additionalInformation[PaymentTokenInterface::PUBLIC_HASH];
- $paymentToken = $this->tokenManagement->getByPublicHash($publicHash, $customerId);
- if ($paymentToken === null) {
- throw new \LogicException("No token found");
- }
- $extensionAttributes = $this->getPaymentExtensionAttributes($orderPayment);
- $extensionAttributes->setVaultPaymentToken($paymentToken);
- }
- /**
- * @param OrderPaymentInterface $payment
- * @return \Magento\Sales\Api\Data\OrderPaymentExtensionInterface
- */
- private function getPaymentExtensionAttributes(OrderPaymentInterface $payment)
- {
- $extensionAttributes = $payment->getExtensionAttributes();
- if ($extensionAttributes === null) {
- $extensionAttributes = $this->paymentExtensionFactory->create();
- $payment->setExtensionAttributes($extensionAttributes);
- }
- return $extensionAttributes;
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function cancel(\Magento\Payment\Model\InfoInterface $payment)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function void(\Magento\Payment\Model\InfoInterface $payment)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function canReviewPayment()
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function acceptPayment(InfoInterface $payment)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function denyPayment(InfoInterface $payment)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getConfigData($field, $storeId = null)
- {
- return $this->getConfiguredValue($field, $storeId);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function assignData(\Magento\Framework\DataObject $data)
- {
- $this->eventManager->dispatch(
- 'payment_method_assign_data_vault',
- [
- AbstractDataAssignObserver::METHOD_CODE => $this,
- AbstractDataAssignObserver::MODEL_CODE => $this->getInfoInstance(),
- AbstractDataAssignObserver::DATA_CODE => $data
- ]
- );
- $this->eventManager->dispatch(
- 'payment_method_assign_data_vault_' . $this->getProviderCode(),
- [
- AbstractDataAssignObserver::METHOD_CODE => $this,
- AbstractDataAssignObserver::MODEL_CODE => $this->getInfoInstance(),
- AbstractDataAssignObserver::DATA_CODE => $data
- ]
- );
- return $this->getVaultProvider()->assignData($data);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
- {
- return $this->getVaultProvider()->isAvailable($quote)
- && $this->config->getValue(self::$activeKey, $this->getStore() ?: ($quote ? $quote->getStoreId() : null));
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function isActive($storeId = null)
- {
- return $this->getVaultProvider()->isActive($storeId)
- && $this->config->getValue(self::$activeKey, $this->getStore() ?: $storeId);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function initialize($paymentAction, $stateObject)
- {
- throw new \DomainException("Not implemented");
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getConfigPaymentAction()
- {
- return $this->getVaultProvider()->getConfigPaymentAction();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getProviderCode()
- {
- return $this->getVaultProvider()->getCode();
- }
- }
|