VaultConfigProvider.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Model\Ui;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Session\SessionManagerInterface;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Vault\Api\PaymentMethodListInterface;
  12. /**
  13. * Provides information about vault payment methods availability on storefront
  14. *
  15. * @api
  16. * @since 100.1.0
  17. */
  18. class VaultConfigProvider implements ConfigProviderInterface
  19. {
  20. const IS_ACTIVE_CODE = 'is_active_payment_token_enabler';
  21. /**
  22. * @var string
  23. */
  24. private static $vaultCode = 'vault';
  25. /**
  26. * @var StoreManagerInterface
  27. */
  28. private $storeManager;
  29. /**
  30. * @var SessionManagerInterface
  31. */
  32. private $session;
  33. /**
  34. * @var PaymentMethodListInterface
  35. */
  36. private $vaultPaymentList;
  37. /**
  38. * VaultConfigProvider constructor.
  39. * @param StoreManagerInterface $storeManager
  40. * @param SessionManagerInterface $session
  41. */
  42. public function __construct(
  43. StoreManagerInterface $storeManager,
  44. SessionManagerInterface $session
  45. ) {
  46. $this->storeManager = $storeManager;
  47. $this->session = $session;
  48. }
  49. /**
  50. * Retrieve assoc array of checkout configuration
  51. *
  52. * @return array
  53. * @since 100.1.0
  54. */
  55. public function getConfig()
  56. {
  57. $availableMethods = [];
  58. $storeId = $this->storeManager->getStore()->getId();
  59. $vaultPayments = $this->getVaultPaymentList()->getActiveList($storeId);
  60. $customerId = $this->session->getCustomerId();
  61. foreach ($vaultPayments as $method) {
  62. $availableMethods[$method->getCode()] = [
  63. 'is_enabled' => $customerId !== null && $method->isActive($storeId)
  64. ];
  65. }
  66. return [
  67. self::$vaultCode => $availableMethods
  68. ];
  69. }
  70. /**
  71. * Get vault payment list instance
  72. * @return PaymentMethodListInterface
  73. * @deprecated 100.2.0
  74. */
  75. private function getVaultPaymentList()
  76. {
  77. if ($this->vaultPaymentList === null) {
  78. $this->vaultPaymentList = ObjectManager::getInstance()->get(PaymentMethodListInterface::class);
  79. }
  80. return $this->vaultPaymentList;
  81. }
  82. }