Guest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Login\Controller\Login;
  17. use Amazon\Core\Client\ClientFactoryInterface;
  18. use Amazon\Core\Helper\Data as AmazonCoreHelper;
  19. use Amazon\Login\Model\Validator\AccessTokenRequestValidator;
  20. use Magento\Checkout\Model\Session;
  21. use Magento\Customer\Model\Url;
  22. use Magento\Framework\App\Action\Action;
  23. use Magento\Framework\App\Action\Context;
  24. use Psr\Log\LoggerInterface;
  25. class Guest extends Action
  26. {
  27. /**
  28. * @var AmazonCoreHelper
  29. */
  30. private $amazonCoreHelper;
  31. /**
  32. * @var Url
  33. */
  34. private $customerUrl;
  35. /**
  36. * @var AccessTokenRequestValidator
  37. */
  38. private $accessTokenRequestValidator;
  39. /**
  40. * @var Session
  41. */
  42. private $session;
  43. /**
  44. * @var ClientFactoryInterface
  45. */
  46. private $clientFactory;
  47. /**
  48. * @var LoggerInterface
  49. */
  50. private $logger;
  51. private $quoteRepository;
  52. /**
  53. * Guest constructor.
  54. * @param Context $context
  55. * @param AmazonCoreHelper $amazonCoreHelper
  56. * @param Url $customerUrl
  57. * @param AccessTokenRequestValidator $accessTokenRequestValidator
  58. * @param Session $session
  59. * @param ClientFactoryInterface $clientFactory
  60. * @param LoggerInterface $logger
  61. */
  62. public function __construct(
  63. Context $context,
  64. AmazonCoreHelper $amazonCoreHelper,
  65. Url $customerUrl,
  66. AccessTokenRequestValidator $accessTokenRequestValidator,
  67. Session $session,
  68. ClientFactoryInterface $clientFactory,
  69. LoggerInterface $logger
  70. ) {
  71. $this->amazonCoreHelper = $amazonCoreHelper;
  72. $this->customerUrl = $customerUrl;
  73. $this->accessTokenRequestValidator = $accessTokenRequestValidator;
  74. $this->session = $session;
  75. $this->clientFactory = $clientFactory;
  76. $this->logger = $logger;
  77. parent::__construct($context);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function execute()
  83. {
  84. if (!$this->isValidToken()) {
  85. return $this->getRedirectLogin();
  86. }
  87. $customerData = $this->getAmazonCustomer();
  88. if ($customerData && isset($customerData['email'])) {
  89. $quote = $this->session->getQuote();
  90. if ($quote) {
  91. $quote->setCustomerEmail($customerData['email']);
  92. $quote->save();
  93. }
  94. }
  95. return $this->_redirect('checkout');
  96. }
  97. /**
  98. * @return string
  99. */
  100. private function getRedirectLogin()
  101. {
  102. return $this->_redirect($this->customerUrl->getLoginUrl());
  103. }
  104. /**
  105. * @return bool
  106. */
  107. private function isValidToken()
  108. {
  109. $isValid = false;
  110. try {
  111. $isValid = $this->accessTokenRequestValidator->isValid($this->getRequest());
  112. } catch (\Zend_Validate_Exception $e) {
  113. $this->logger->error($e);
  114. }
  115. return $isValid;
  116. }
  117. /**
  118. * @return array
  119. */
  120. private function getAmazonCustomer()
  121. {
  122. try {
  123. $userInfo = $this->clientFactory
  124. ->create()
  125. ->getUserInfo($this->getRequest()->getParam('access_token'));
  126. if (is_array($userInfo) && isset($userInfo['user_id'])) {
  127. $data = [
  128. 'id' => $userInfo['user_id'],
  129. 'email' => $userInfo['email'],
  130. 'name' => $userInfo['name'],
  131. 'country' => $this->amazonCoreHelper->getRegion(),
  132. ];
  133. return $data;
  134. }
  135. } catch (\Exception $e) {
  136. $this->logger->error($e);
  137. }
  138. return [];
  139. }
  140. }