Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TokensExchange.php
Go to the documentation of this file.
1 <?php
9 
11 
13 {
21  protected function _setActivationInProcessMsg($isReauthorize, $integrationName)
22  {
23  $integrationName = $this->escaper->escapeHtml($integrationName);
24  $msg = $isReauthorize ? __(
25  "Integration '%1' has been sent for re-authorization.",
26  $integrationName
27  ) : __(
28  "Integration '%1' has been sent for activation.",
29  $integrationName
30  );
31  $this->messageManager->addNotice($msg);
32  }
33 
39  public function execute()
40  {
41  try {
42  $integrationId = $this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
43  $isReauthorize = (bool)$this->getRequest()->getParam(self::PARAM_REAUTHORIZE, 0);
44  $integration = $this->_integrationService->get($integrationId);
45  if ($isReauthorize) {
47  $this->_oauthService->deleteIntegrationToken($integration->getConsumerId());
48  $integration->setStatus(IntegrationModel::STATUS_INACTIVE)->save();
49  }
50  //Integration chooses to use Oauth for token exchange
51  $this->_oauthService->postToConsumer($integration->getConsumerId(), $integration->getEndpoint());
53  $this->_view->loadLayout(false);
54  // Activation or authorization is done only when the Oauth token exchange completes
55  $this->_setActivationInProcessMsg($isReauthorize, $integration->getName());
56  $this->_view->renderLayout();
57  $popupContent = $this->_response->getBody();
58  $consumer = $this->_oauthService->loadConsumer($integration->getConsumerId());
59  if (!$consumer->getId()) {
60  throw new \Magento\Framework\Oauth\Exception(
61  __(
62  'A consumer with "%1" ID doesn\'t exist. Verify the ID and try again.',
63  $integration->getConsumerId()
64  )
65  );
66  }
68  $result = [
69  IntegrationModel::IDENTITY_LINK_URL => $integration->getIdentityLinkUrl(),
70  'oauth_consumer_key' => $consumer->getKey(),
71  'popup_content' => $popupContent,
72  ];
73  $this->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
74  } catch (\Magento\Framework\Exception\LocalizedException $e) {
75  $this->messageManager->addError($e->getMessage());
76  $this->_redirect('*/*');
77  return;
78  } catch (\Exception $e) {
79  $this->_logger->critical($e);
80  $this->messageManager->addError(__('Internal error. Check exception log for details.'));
81  $this->_redirect('*/*');
82  return;
83  }
84  }
85 }
__()
Definition: __.php:13