Callback.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Dotdigitalgroup\Email\Controller\Email;
  3. class Callback extends \Magento\Framework\App\Action\Action
  4. {
  5. /**
  6. * @var \Dotdigitalgroup\Email\Helper\Data
  7. */
  8. private $helper;
  9. /**
  10. * @var \Magento\User\Api\Data\UserInterfaceFactory
  11. */
  12. private $adminUser;
  13. /**
  14. * @var \Magento\Store\Model\StoreManagerInterface
  15. */
  16. private $storeManager;
  17. /**
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  19. */
  20. private $scopeConfig;
  21. /**
  22. * @var \Dotdigitalgroup\Email\Helper\Config
  23. */
  24. private $config;
  25. /**
  26. * @var \Magento\Backend\Helper\Data
  27. */
  28. private $adminHelper;
  29. /**
  30. * @var \Magento\User\Model\ResourceModel\User
  31. */
  32. private $userResource;
  33. /**
  34. * @var \Magento\Framework\View\Result\PageFactory
  35. */
  36. private $resultPageFactory;
  37. /**
  38. * Callback constructor.
  39. *
  40. * @param \Magento\User\Model\ResourceModel\User $userResource
  41. * @param \Magento\Backend\Helper\Data $backendData
  42. * @param \Dotdigitalgroup\Email\Helper\Config $config
  43. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface
  44. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  45. * @param \Magento\User\Api\Data\UserInterfaceFactory $adminUser
  46. * @param \Magento\Framework\App\Action\Context $context
  47. * @param \Dotdigitalgroup\Email\Helper\Data $helper,
  48. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  49. */
  50. public function __construct(
  51. \Magento\User\Model\ResourceModel\User $userResource,
  52. \Magento\Backend\Helper\Data $backendData,
  53. \Dotdigitalgroup\Email\Helper\Config $config,
  54. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface,
  55. \Magento\Store\Model\StoreManagerInterface $storeManager,
  56. \Magento\User\Api\Data\UserInterfaceFactory $adminUser,
  57. \Magento\Framework\App\Action\Context $context,
  58. \Dotdigitalgroup\Email\Helper\Data $helper,
  59. \Magento\Framework\View\Result\PageFactory $resultPageFactory
  60. ) {
  61. $this->adminHelper = $backendData;
  62. $this->config = $config;
  63. $this->scopeConfig = $scopeConfigInterface;
  64. $this->storeManager = $storeManager;
  65. $this->adminUser = $adminUser;
  66. $this->userResource = $userResource;
  67. $this->helper = $helper;
  68. $this->resultPageFactory = $resultPageFactory;
  69. parent::__construct($context);
  70. }
  71. /**
  72. * Execute method.
  73. *
  74. * @return null
  75. */
  76. public function execute()
  77. {
  78. $code = $this->getRequest()->getParam('code', false);
  79. $userId = $this->getRequest()->getParam('state');
  80. //load admin user
  81. $adminUser = $this->adminUser->create();
  82. $this->userResource->load($adminUser, $userId);
  83. //app code and admin user must be present
  84. if ($code && $adminUser->getId()) {
  85. $clientId = $this->scopeConfig->getValue(
  86. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_ID
  87. );
  88. $clientSecret = $this->scopeConfig->getValue(
  89. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_SECRET_ID
  90. );
  91. //callback uri if not set custom
  92. $redirectUri = $this->storeManager->getStore()
  93. ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, true);
  94. $redirectUri .= 'connector/email/callback';
  95. $data = 'client_id=' . $clientId .
  96. '&client_secret=' . $clientSecret .
  97. '&redirect_uri=' . $redirectUri .
  98. '&grant_type=authorization_code' .
  99. '&code=' . $code;
  100. //callback url
  101. $url = $this->config->getTokenUrl();
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  105. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  106. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  107. curl_setopt($ch, CURLOPT_POST, true);
  108. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  109. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
  110. $response = json_decode(curl_exec($ch));
  111. if ($response === false) {
  112. $this->helper->error('Error Number: ' . curl_errno($ch), []);
  113. }
  114. if (isset($response->error)) {
  115. $this->helper->error('OAUTH failed ' . $response->error, []);
  116. } elseif (isset($response->refresh_token)) {
  117. //save the refresh token to the admin user
  118. $adminUser->setRefreshToken(
  119. $this->helper->encryptor->encrypt($response->refresh_token)
  120. );
  121. $this->userResource->save($adminUser);
  122. }
  123. //redirect to automation index page
  124. return $this->_redirect($this->adminHelper->getUrl('dotdigitalgroup_email/studio'));
  125. }
  126. return $this->resultPageFactory->create()
  127. ->setStatusHeader(404, '1.1', 'Not Found')
  128. ->setHeader('Status', '404 File not found');
  129. }
  130. }