123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\InstantPurchase\PaymentMethodIntegration;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\Vault\Api\Data\PaymentTokenInterface;
- use Magento\Vault\Model\PaymentMethodList;
- use Magento\Vault\Model\VaultPaymentInterface;
- /**
- * Payment method integrations management.
- *
- * Avoid usage of methods for automatic store detection if possible.
- */
- class IntegrationsManager
- {
- /**
- * @var PaymentMethodList
- */
- private $paymentMethodList;
- /**
- * @var IntegrationFactory
- */
- private $integrationFactory;
- /**
- * @var StoreManagerInterface
- */
- private $storeManager;
- /**
- * @var array
- */
- private $integrations;
- /**
- * IntegrationsManager constructor.
- * @param PaymentMethodList $paymentMethodList
- * @param IntegrationFactory $integrationFactory
- * @param StoreManagerInterface $storeManager
- */
- public function __construct(
- PaymentMethodList $paymentMethodList,
- IntegrationFactory $integrationFactory,
- StoreManagerInterface $storeManager
- ) {
- $this->paymentMethodList = $paymentMethodList;
- $this->integrationFactory = $integrationFactory;
- $this->storeManager = $storeManager;
- $this->integrations = [];
- }
- /**
- * Provides list of implemented integrations.
- *
- * @param int $storeId
- * @return array
- */
- public function getList(int $storeId): array
- {
- if (!isset($this->integrations[$storeId])) {
- $this->integrations[$storeId] = $this->findIntegrations($storeId);
- }
- return $this->integrations[$storeId];
- }
- /**
- * Determines integration that may be used with token.
- *
- * @param PaymentTokenInterface $paymentToken
- * @param int $storeId
- * @return Integration
- * @throws LocalizedException if integration is not implemented.
- */
- public function getByToken(PaymentTokenInterface $paymentToken, int $storeId): Integration
- {
- foreach ($this->getList($storeId) as $integration) {
- if ($integration->getVaultProviderCode() === $paymentToken->getPaymentMethodCode()) {
- return $integration;
- }
- }
- throw new LocalizedException(__('Instant purchase integration not available for token.'));
- }
- /**
- * Provides list of implemented integrations with store detection.
- * This method rely on global state. Use getList if possible.
- *
- * @return array
- */
- public function getListForCurrentStore(): array
- {
- return $this->getList($this->storeManager->getStore()->getId());
- }
- /**
- * Determines integration that may be used with token with store detection.
- * This method rely on global state. Use getByToken if possible.
- *
- * @param PaymentTokenInterface $paymentToken
- * @return Integration
- * @throws LocalizedException if integration is not implemented.
- */
- public function getByTokenForCurrentStore(PaymentTokenInterface $paymentToken): Integration
- {
- return $this->getByToken($paymentToken, $this->storeManager->getStore()->getId());
- }
- /**
- * Find implemented integrations for active vault payment methods.
- *
- * @param int $storeId
- * @return array
- */
- private function findIntegrations(int $storeId): array
- {
- $integrations = [];
- foreach ($this->paymentMethodList->getActiveList($storeId) as $paymentMethod) {
- if ($this->isIntegrationAvailable($paymentMethod, $storeId)) {
- $integrations[] = $this->integrationFactory->create($paymentMethod, $storeId);
- }
- }
- return $integrations;
- }
- /**
- * Checks if integration implemented for vault payment method.
- *
- * Implemented integration is if it configured in payment config:
- * 1. Basic integration (not recommended):
- * <instant_purchase>
- * <supported>1</supported>
- * </instant_purchase>
- * 2. Customized integration (at least one option is required):
- * <instant_purchase>
- * <available>Implementation_Of_AvailabilityCheckerInterface</available>
- * <tokenFormat>Implementation_Of_PaymentTokenFormatterInterface</tokenFormat>
- * <additionalInformation>
- * Implementation_Of_PaymentAdditionalInformationProviderInterface
- * </additionalInformation>
- * </instant_purchase>
- *
- * @param VaultPaymentInterface $paymentMethod
- * @param int|string|null|\Magento\Store\Model\Store $storeId
- * @return bool
- */
- private function isIntegrationAvailable(VaultPaymentInterface $paymentMethod, $storeId): bool
- {
- $data = $paymentMethod->getConfigData('instant_purchase', $storeId);
- if (!is_array($data)) {
- return false;
- }
- if (isset($data['supported']) && $data['supported'] !== '1') {
- return false;
- }
- return true;
- }
- }
|