Skip to content

Commit

Permalink
Merge pull request #1101 from BearGroup/release/5.10.0
Browse files Browse the repository at this point in the history
Release/5.10.0
  • Loading branch information
Christian Zichichi authored Jan 31, 2022
2 parents 9d591ac + 5ce5ddd commit 6fbc9a5
Show file tree
Hide file tree
Showing 37 changed files with 751 additions and 461 deletions.
13 changes: 13 additions & 0 deletions Api/CheckoutSessionManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public function updateCheckoutSession($amazonSessionId, $cartId = null);
* @return int
*/
public function completeCheckoutSession($amazonSessionId, $cartId = null);

/**
* @param mixed $buyerToken
* @return mixed
*/
public function signIn($buyerToken);

/**
* @param mixed $buyerToken
* @param string $password
* @return mixed
*/
public function setCustomerLink($buyerToken, $password);
}
15 changes: 10 additions & 5 deletions Block/Adminhtml/System/Config/Form/IpnUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ protected function _renderValue(AbstractElement $element)
$store = $this->_storeManager->getDefaultStoreView();
$valueReturn = '';

$baseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB, true);
if ($baseUrl) {
$value = $baseUrl . 'amazon_pay/payment/ipn/';
$valueReturn = "<div>".$this->escapeHtml($value)."</div>";
if ($store) {
$baseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB, true);
if ($baseUrl) {
$value = $baseUrl . 'amazon_pay/payment/ipn/';
$valueReturn = "<div>".$this->escapeHtml($value)."</div>";
}
} else {
$valueReturn = 'You do not have permission to view this setting. The IPN URL is managed
from the Default Store View.';
}

$html = '<td class="value">';
$html .= $valueReturn;
if ($element->getComment()) {
if ($element->getComment() && $store) {
$html .= '<p class="note"><span>' . $element->getComment() . '</span></p>';
}
$html .= '</td>';
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

##5.10.0
* Added signin REST endpoint
* Fixed an issue that could occur when rendering the Amazon Pay button more than once
* Fixed an issue with configuring payment methods at a store scope when the admin user doesn’t have access to the default scope (thanks @barbazul!)
* Fixed an issue with configuration wizard executed in a store where the admin doesn’t have access to the default store
* Fixed MFTF tests to allow for different flow on the Amazon authentication popup
* Updated to allow partial/split capture in EU/UK regions
* Updated REST endpoints to allow loading session from the user context instead of passing masked cart ID

## 5.9.1
* Fixed issue with umlauts in PayNow button flow
* Updated config labels for Magento Checkout redirect paths
Expand Down
82 changes: 46 additions & 36 deletions Controller/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
use Amazon\Pay\Model\Validator\AccessTokenRequestValidator;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Amazon\Pay\Helper\Session;
use Amazon\Pay\Helper\Customer as CustomerHelper;
use Amazon\Pay\Model\Adapter\AmazonPayAdapter;
use Amazon\Pay\Domain\ValidationCredentials;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Customer\Model\Url;
Expand Down Expand Up @@ -110,8 +113,16 @@ abstract class Login extends Action
*/
protected $accountManagement;

/**
* @var CheckoutSessionManagement
*/
protected $checkoutSessionManagement;

/**
* @var CustomerHelper
*/
protected $customerHelper;

/**
* Login constructor.
* @param Context $context
Expand All @@ -129,12 +140,14 @@ abstract class Login extends Action
* @param StoreManager $storeManager
* @param UrlInterface $url
* @param AccountManagementInterface $accountManagement
* @param CheckoutSessionManagementInterface $checkoutSessionManagement
* @param customerHelper $customerHelper
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Context $context,
AmazonCustomerFactory $amazonCustomerFactory,
\Amazon\Pay\Model\Adapter\AmazonPayAdapter $amazonAdapter,
AmazonPayAdapter $amazonAdapter,
AmazonConfig $amazonConfig,
Url $customerUrl,
AccessTokenRequestValidator $accessTokenRequestValidator,
Expand All @@ -147,7 +160,8 @@ public function __construct(
StoreManager $storeManager,
UrlInterface $url,
AccountManagementInterface $accountManagement,
CheckoutSessionManagementInterface $checkoutSessionManagement
CheckoutSessionManagementInterface $checkoutSessionManagement,
CustomerHelper $customerHelper
) {
$this->amazonCustomerFactory = $amazonCustomerFactory;
$this->amazonAdapter = $amazonAdapter;
Expand All @@ -164,43 +178,10 @@ public function __construct(
$this->url = $url;
$this->accountManagement = $accountManagement;
$this->checkoutSessionManagement = $checkoutSessionManagement;
$this->customerHelper = $customerHelper;
parent::__construct($context);
}

/**
* Load userinfo from access token
*
* @return AmazonCustomerInterface|false
*/
protected function getAmazonCustomer($token)
{
try {
$userInfo = $this->amazonAdapter
->getBuyer($token);

if (is_array($userInfo) && array_key_exists('buyerId', $userInfo) && !empty($userInfo['buyerId'])) {
$data = [
'id' => $userInfo['buyerId'],
'email' => $userInfo['email'],
'name' => $userInfo['name'],
'country' => $this->amazonConfig->getRegion(),
];
$amazonCustomer = $this->amazonCustomerFactory->create($data);

return $amazonCustomer;

} else {
$this->logger->error('Amazon buyerId is empty. Token: ' . $token);
}

} catch (\Exception $e) {
$this->logger->error($e);
$this->messageManager->addErrorMessage(__('Error processing Amazon Login'));
}

return false;
}

/**
* @return bool
*/
Expand All @@ -224,4 +205,33 @@ protected function getRedirectAccount()
{
return $this->accountRedirect->getRedirect();
}

protected function getAmazonCustomer($buyerInfo)
{
return $this->customerHelper->getAmazonCustomer($buyerInfo);
}

protected function processAmazonCustomer(AmazonCustomerInterface $amazonCustomer)
{
$customerData = $this->matcher->match($amazonCustomer);

if (null === $customerData) {
return $this->createCustomer($amazonCustomer);
}

if ($amazonCustomer->getId() != $customerData->getExtensionAttributes()->getAmazonId()) {
if (! $this->session->isLoggedIn()) {
return new ValidationCredentials($customerData->getId(), $amazonCustomer->getId());
}

$this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());
}

return $customerData;
}

protected function createCustomer(AmazonCustomerInterface $amazonCustomer)
{
return $this->customerHelper->createCustomer($amazonCustomer);
}
}
40 changes: 5 additions & 35 deletions Controller/Login/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
namespace Amazon\Pay\Controller\Login;

use Amazon\Pay\Api\Data\AmazonCustomerInterface;
use Amazon\Pay\Domain\ValidationCredentials;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Exception\NotFoundException;
use Zend_Validate;

class Authorize extends \Amazon\Pay\Controller\Login
{
Expand All @@ -37,9 +35,10 @@ public function execute()
}

$token = $this->getRequest()->getParam('buyerToken');

try {
$amazonCustomer = $this->getAmazonCustomer($token);
$buyerInfo = $this->amazonAdapter->getBuyer($token);
$amazonCustomer = $this->getAmazonCustomer($buyerInfo);
if ($amazonCustomer) {
$processed = $this->processAmazonCustomer($amazonCustomer);

Expand All @@ -50,6 +49,8 @@ public function execute()
} else {
$this->session->login($processed);
}
} else {
$this->logger->error('Amazon buyerId is empty. Token: ' . $token);
}
} catch (ValidatorException $e) {
$this->logger->error($e);
Expand All @@ -64,35 +65,4 @@ public function execute()

return $this->getRedirectAccount();
}

protected function processAmazonCustomer(AmazonCustomerInterface $amazonCustomer)
{
$customerData = $this->matcher->match($amazonCustomer);

if (null === $customerData) {
return $this->createCustomer($amazonCustomer);
}

if ($amazonCustomer->getId() != $customerData->getExtensionAttributes()->getAmazonId()) {
if (! $this->session->isLoggedIn()) {
return new ValidationCredentials($customerData->getId(), $amazonCustomer->getId());
}

$this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());
}

return $customerData;
}

protected function createCustomer(AmazonCustomerInterface $amazonCustomer)
{
if (! Zend_Validate::is($amazonCustomer->getEmail(), 'EmailAddress')) {
throw new ValidatorException(__('the email address for your Amazon account is invalid'));
}

$customerData = $this->customerLinkManagement->create($amazonCustomer);
$this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());

return $customerData;
}
}
75 changes: 4 additions & 71 deletions Controller/Login/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
namespace Amazon\Pay\Controller\Login;

use Amazon\Pay\Api\Data\AmazonCustomerInterface;
use Amazon\Pay\Domain\ValidationCredentials;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Quote\Api\Data\CartInterface;
use Zend_Validate;

class Checkout extends \Amazon\Pay\Controller\Login
{
Expand Down Expand Up @@ -53,7 +51,8 @@ public function execute()
}
}
} else {
$amazonCustomer = $this->createAmazonCustomerFromSession($checkoutSession);
$buyerInfo = $checkoutSession['buyer'];
$amazonCustomer = $this->getAmazonCustomer($buyerInfo);
if ($amazonCustomer) {
$processed = $this->processAmazonCustomer($amazonCustomer);

Expand All @@ -69,6 +68,8 @@ public function execute()
} elseif (!$this->customerSession->isLoggedIn()) {
$this->session->login($processed);
}
} else {
$this->logger->error('Amazon buyerId is empty');
}
}
} catch (ValidatorException $e) {
Expand All @@ -85,72 +86,4 @@ public function execute()
$checkoutUrl = $this->amazonConfig->getCheckoutReviewUrlPath();
return $this->_redirect($checkoutUrl, ['_query' => ['amazonCheckoutSessionId' => $checkoutSessionId]]);
}

protected function processAmazonCustomer(AmazonCustomerInterface $amazonCustomer)
{
$customerData = $this->matcher->match($amazonCustomer);

if (null === $customerData) {
return $this->createCustomer($amazonCustomer);
}

if ($amazonCustomer->getId() != $customerData->getExtensionAttributes()->getAmazonId()) {
if (! $this->session->isLoggedIn()) {
return new ValidationCredentials($customerData->getId(), $amazonCustomer->getId());
}

$this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());
}

return $customerData;
}

protected function createCustomer(AmazonCustomerInterface $amazonCustomer)
{
if (! Zend_Validate::is($amazonCustomer->getEmail(), 'EmailAddress')) {
throw new ValidatorException(__('the email address for your Amazon account is invalid'));
}

$customerData = $this->customerLinkManagement->create($amazonCustomer);
$this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());

return $customerData;
}

/**
* @param $checkoutSession
* @return array|false
*/
protected function getAmazonCustomerFromSession($checkoutSession)
{
$userInfo = $checkoutSession['buyer'];
if (is_array($userInfo) && array_key_exists('buyerId', $userInfo) && !empty($userInfo['buyerId'])) {
$data = [
'id' => $userInfo['buyerId'],
'email' => $userInfo['email'],
'name' => $userInfo['name'],
'country' => $this->amazonConfig->getRegion(),
];

return $data;
} else {
$this->logger->error('Amazon buyerId is empty');
}

return false;
}

/**
* @param $checkoutSession
* @return \Amazon\Pay\Domain\AmazonCustomer
*/
protected function createAmazonCustomerFromSession($checkoutSession)
{
$data = $this->getAmazonCustomerFromSession($checkoutSession);
if ($data) {
return $this->amazonCustomerFactory->create($data);
} else {
return false;
}
}
}
Loading

0 comments on commit 6fbc9a5

Please sign in to comment.