IntegrationsManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\PaymentMethodIntegration;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. use Magento\Vault\Api\Data\PaymentTokenInterface;
  10. use Magento\Vault\Model\PaymentMethodList;
  11. use Magento\Vault\Model\VaultPaymentInterface;
  12. /**
  13. * Payment method integrations management.
  14. *
  15. * Avoid usage of methods for automatic store detection if possible.
  16. */
  17. class IntegrationsManager
  18. {
  19. /**
  20. * @var PaymentMethodList
  21. */
  22. private $paymentMethodList;
  23. /**
  24. * @var IntegrationFactory
  25. */
  26. private $integrationFactory;
  27. /**
  28. * @var StoreManagerInterface
  29. */
  30. private $storeManager;
  31. /**
  32. * @var array
  33. */
  34. private $integrations;
  35. /**
  36. * IntegrationsManager constructor.
  37. * @param PaymentMethodList $paymentMethodList
  38. * @param IntegrationFactory $integrationFactory
  39. * @param StoreManagerInterface $storeManager
  40. */
  41. public function __construct(
  42. PaymentMethodList $paymentMethodList,
  43. IntegrationFactory $integrationFactory,
  44. StoreManagerInterface $storeManager
  45. ) {
  46. $this->paymentMethodList = $paymentMethodList;
  47. $this->integrationFactory = $integrationFactory;
  48. $this->storeManager = $storeManager;
  49. $this->integrations = [];
  50. }
  51. /**
  52. * Provides list of implemented integrations.
  53. *
  54. * @param int $storeId
  55. * @return array
  56. */
  57. public function getList(int $storeId): array
  58. {
  59. if (!isset($this->integrations[$storeId])) {
  60. $this->integrations[$storeId] = $this->findIntegrations($storeId);
  61. }
  62. return $this->integrations[$storeId];
  63. }
  64. /**
  65. * Determines integration that may be used with token.
  66. *
  67. * @param PaymentTokenInterface $paymentToken
  68. * @param int $storeId
  69. * @return Integration
  70. * @throws LocalizedException if integration is not implemented.
  71. */
  72. public function getByToken(PaymentTokenInterface $paymentToken, int $storeId): Integration
  73. {
  74. foreach ($this->getList($storeId) as $integration) {
  75. if ($integration->getVaultProviderCode() === $paymentToken->getPaymentMethodCode()) {
  76. return $integration;
  77. }
  78. }
  79. throw new LocalizedException(__('Instant purchase integration not available for token.'));
  80. }
  81. /**
  82. * Provides list of implemented integrations with store detection.
  83. * This method rely on global state. Use getList if possible.
  84. *
  85. * @return array
  86. */
  87. public function getListForCurrentStore(): array
  88. {
  89. return $this->getList($this->storeManager->getStore()->getId());
  90. }
  91. /**
  92. * Determines integration that may be used with token with store detection.
  93. * This method rely on global state. Use getByToken if possible.
  94. *
  95. * @param PaymentTokenInterface $paymentToken
  96. * @return Integration
  97. * @throws LocalizedException if integration is not implemented.
  98. */
  99. public function getByTokenForCurrentStore(PaymentTokenInterface $paymentToken): Integration
  100. {
  101. return $this->getByToken($paymentToken, $this->storeManager->getStore()->getId());
  102. }
  103. /**
  104. * Find implemented integrations for active vault payment methods.
  105. *
  106. * @param int $storeId
  107. * @return array
  108. */
  109. private function findIntegrations(int $storeId): array
  110. {
  111. $integrations = [];
  112. foreach ($this->paymentMethodList->getActiveList($storeId) as $paymentMethod) {
  113. if ($this->isIntegrationAvailable($paymentMethod, $storeId)) {
  114. $integrations[] = $this->integrationFactory->create($paymentMethod, $storeId);
  115. }
  116. }
  117. return $integrations;
  118. }
  119. /**
  120. * Checks if integration implemented for vault payment method.
  121. *
  122. * Implemented integration is if it configured in payment config:
  123. * 1. Basic integration (not recommended):
  124. * <instant_purchase>
  125. * <supported>1</supported>
  126. * </instant_purchase>
  127. * 2. Customized integration (at least one option is required):
  128. * <instant_purchase>
  129. * <available>Implementation_Of_AvailabilityCheckerInterface</available>
  130. * <tokenFormat>Implementation_Of_PaymentTokenFormatterInterface</tokenFormat>
  131. * <additionalInformation>
  132. * Implementation_Of_PaymentAdditionalInformationProviderInterface
  133. * </additionalInformation>
  134. * </instant_purchase>
  135. *
  136. * @param VaultPaymentInterface $paymentMethod
  137. * @param int|string|null|\Magento\Store\Model\Store $storeId
  138. * @return bool
  139. */
  140. private function isIntegrationAvailable(VaultPaymentInterface $paymentMethod, $storeId): bool
  141. {
  142. $data = $paymentMethod->getConfigData('instant_purchase', $storeId);
  143. if (!is_array($data)) {
  144. return false;
  145. }
  146. if (isset($data['supported']) && $data['supported'] !== '1') {
  147. return false;
  148. }
  149. return true;
  150. }
  151. }