GetClientToken.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Controller\Adminhtml\Payment;
  7. use Magento\Backend\App\Action;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Backend\Model\Session\Quote;
  10. use Magento\Braintree\Gateway\Config\Config;
  11. use Magento\Braintree\Gateway\Request\PaymentDataBuilder;
  12. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  13. use Magento\Framework\Controller\ResultFactory;
  14. /**
  15. * Retrieves client token.
  16. */
  17. class GetClientToken extends Action
  18. {
  19. const ADMIN_RESOURCE = 'Magento_Braintree::get_client_token';
  20. /**
  21. * @var Config
  22. */
  23. private $config;
  24. /**
  25. * @var BraintreeAdapterFactory
  26. */
  27. private $adapterFactory;
  28. /**
  29. * @var Quote
  30. */
  31. private $quoteSession;
  32. /**
  33. * @param Context $context
  34. * @param Config $config
  35. * @param BraintreeAdapterFactory $adapterFactory
  36. * @param Quote $quoteSession
  37. */
  38. public function __construct(
  39. Context $context,
  40. Config $config,
  41. BraintreeAdapterFactory $adapterFactory,
  42. Quote $quoteSession
  43. ) {
  44. parent::__construct($context);
  45. $this->config = $config;
  46. $this->adapterFactory = $adapterFactory;
  47. $this->quoteSession = $quoteSession;
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function execute()
  53. {
  54. $params = [];
  55. $response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  56. $storeId = $this->quoteSession->getStoreId();
  57. $merchantAccountId = $this->config->getMerchantAccountId($storeId);
  58. if (!empty($merchantAccountId)) {
  59. $params[PaymentDataBuilder::MERCHANT_ACCOUNT_ID] = $merchantAccountId;
  60. }
  61. $clientToken = $this->adapterFactory->create($storeId)
  62. ->generate($params);
  63. $response->setData(['clientToken' => $clientToken]);
  64. return $response;
  65. }
  66. }