amazonCoreHelper = $amazonCoreHelper; $this->customerUrl = $customerUrl; $this->accessTokenRequestValidator = $accessTokenRequestValidator; $this->session = $session; $this->clientFactory = $clientFactory; $this->logger = $logger; parent::__construct($context); } /** * {@inheritdoc} */ public function execute() { if (!$this->isValidToken()) { return $this->getRedirectLogin(); } $customerData = $this->getAmazonCustomer(); if ($customerData && isset($customerData['email'])) { $quote = $this->session->getQuote(); if ($quote) { $quote->setCustomerEmail($customerData['email']); $quote->save(); } } return $this->_redirect('checkout'); } /** * @return string */ private function getRedirectLogin() { return $this->_redirect($this->customerUrl->getLoginUrl()); } /** * @return bool */ private function isValidToken() { $isValid = false; try { $isValid = $this->accessTokenRequestValidator->isValid($this->getRequest()); } catch (\Zend_Validate_Exception $e) { $this->logger->error($e); } return $isValid; } /** * @return array */ private function getAmazonCustomer() { try { $userInfo = $this->clientFactory ->create() ->getUserInfo($this->getRequest()->getParam('access_token')); if (is_array($userInfo) && isset($userInfo['user_id'])) { $data = [ 'id' => $userInfo['user_id'], 'email' => $userInfo['email'], 'name' => $userInfo['name'], 'country' => $this->amazonCoreHelper->getRegion(), ]; return $data; } } catch (\Exception $e) { $this->logger->error($e); } return []; } }