ConfigProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Ui;
  7. use Magento\Braintree\Gateway\Config\Config;
  8. use Magento\Braintree\Gateway\Request\PaymentDataBuilder;
  9. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  10. use Magento\Checkout\Model\ConfigProviderInterface;
  11. use Magento\Framework\Session\SessionManagerInterface;
  12. /**
  13. * Class ConfigProvider
  14. */
  15. class ConfigProvider implements ConfigProviderInterface
  16. {
  17. const CODE = 'braintree';
  18. const CC_VAULT_CODE = 'braintree_cc_vault';
  19. /**
  20. * @var Config
  21. */
  22. private $config;
  23. /**
  24. * @var BraintreeAdapterFactory
  25. */
  26. private $adapterFactory;
  27. /**
  28. * @var string
  29. */
  30. private $clientToken = '';
  31. /**
  32. * @var SessionManagerInterface
  33. */
  34. private $session;
  35. /**
  36. * Constructor
  37. *
  38. * @param Config $config
  39. * @param BraintreeAdapterFactory $adapterFactory
  40. * @param SessionManagerInterface $session
  41. */
  42. public function __construct(
  43. Config $config,
  44. BraintreeAdapterFactory $adapterFactory,
  45. SessionManagerInterface $session
  46. ) {
  47. $this->config = $config;
  48. $this->adapterFactory = $adapterFactory;
  49. $this->session = $session;
  50. }
  51. /**
  52. * Retrieve assoc array of checkout configuration
  53. *
  54. * @return array
  55. */
  56. public function getConfig()
  57. {
  58. $storeId = $this->session->getStoreId();
  59. return [
  60. 'payment' => [
  61. self::CODE => [
  62. 'isActive' => $this->config->isActive($storeId),
  63. 'clientToken' => $this->getClientToken(),
  64. 'ccTypesMapper' => $this->config->getCcTypesMapper(),
  65. 'sdkUrl' => $this->config->getSdkUrl(),
  66. 'countrySpecificCardTypes' => $this->config->getCountrySpecificCardTypeConfig($storeId),
  67. 'availableCardTypes' => $this->config->getAvailableCardTypes($storeId),
  68. 'useCvv' => $this->config->isCvvEnabled($storeId),
  69. 'environment' => $this->config->getEnvironment($storeId),
  70. 'kountMerchantId' => $this->config->getKountMerchantId($storeId),
  71. 'hasFraudProtection' => $this->config->hasFraudProtection($storeId),
  72. 'merchantId' => $this->config->getMerchantId($storeId),
  73. 'ccVaultCode' => self::CC_VAULT_CODE,
  74. ],
  75. Config::CODE_3DSECURE => [
  76. 'enabled' => $this->config->isVerify3DSecure($storeId),
  77. 'thresholdAmount' => $this->config->getThresholdAmount($storeId),
  78. 'specificCountries' => $this->config->get3DSecureSpecificCountries($storeId),
  79. ],
  80. ],
  81. ];
  82. }
  83. /**
  84. * Generate a new client token if necessary
  85. * @return string
  86. */
  87. public function getClientToken()
  88. {
  89. if (empty($this->clientToken)) {
  90. $params = [];
  91. $storeId = $this->session->getStoreId();
  92. $merchantAccountId = $this->config->getMerchantAccountId($storeId);
  93. if (!empty($merchantAccountId)) {
  94. $params[PaymentDataBuilder::MERCHANT_ACCOUNT_ID] = $merchantAccountId;
  95. }
  96. $this->clientToken = $this->adapterFactory->create($storeId)
  97. ->generate($params);
  98. }
  99. return $this->clientToken;
  100. }
  101. }