Skip to content

Commit

Permalink
Merge pull request #1127 from BearGroup/release/5.13.0
Browse files Browse the repository at this point in the history
Release/5.13.0
  • Loading branch information
Christian Zichichi authored May 10, 2022
2 parents 648f20b + 6962ebf commit af9abfd
Show file tree
Hide file tree
Showing 59 changed files with 1,130 additions and 353 deletions.
11 changes: 9 additions & 2 deletions Api/CheckoutSessionManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ interface CheckoutSessionManagementInterface
{
/**
* @param string|null $cartId
* @param boolean $omitPayloads
* @return mixed
*/
public function getConfig($cartId = null);
public function getConfig($cartId = null, $omitPayloads = false);

/**
* @param string $payloadType
* @return mixed
*/
public function getButtonPayload($payloadType = 'checkout');

/**
* @param mixed $amazonSessionId
Expand Down Expand Up @@ -54,7 +61,7 @@ public function updateCheckoutSession($amazonSessionId, $cartId = null);
/**
* @param mixed $amazonSessionId
* @param mixed|null $cartId
* @return int
* @return mixed
*/
public function completeCheckoutSession($amazonSessionId, $cartId = null);

Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## 5.13.0
* Added Graphql support
* Added endpoints to fetch individual config types
* Change how buttons are rendered so they are not blocked waiting for config
* Fixed an error when using the REST complete endpoint with a declined card
* Updated some translations

## 5.12.0
* Change to display billing address for US/JP regions to match EU/UK
* Fixed a regression with restricted categories
Expand Down
3 changes: 2 additions & 1 deletion Controller/Checkout/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function __construct(
*/
public function execute()
{
$data = $this->amazonCheckoutSession->getConfig();
$omitPayloads = filter_var($this->getRequest()->getParams()['omit_payloads'], FILTER_VALIDATE_BOOLEAN);
$data = $this->amazonCheckoutSession->getConfig($omitPayloads);
return $this->resultJsonFactory->create()->setData($data);
}
}
4 changes: 2 additions & 2 deletions CustomerData/CheckoutSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function __construct(
/**
* @return array
*/
public function getConfig()
public function getConfig($omitPayloads)
{
$data = $this->checkoutSessionManagement->getConfig();
$data = $this->checkoutSessionManagement->getConfig(null, $omitPayloads);
if (count($data) > 0) {
$data = $data[0];
}
Expand Down
126 changes: 106 additions & 20 deletions Model/CheckoutSessionManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Magento\Sales\Api\Data\TransactionInterface as Transaction;
use Magento\Integration\Model\Oauth\TokenFactory as TokenModelFactory;
use Magento\Authorization\Model\UserContextInterface as UserContext;
use Magento\Framework\Phrase\Renderer\Translate as Translate;

class CheckoutSessionManagement implements \Amazon\Pay\Api\CheckoutSessionManagementInterface
{
Expand Down Expand Up @@ -202,6 +203,11 @@ class CheckoutSessionManagement implements \Amazon\Pay\Api\CheckoutSessionManage
*/
private $session;

/**
* @var Translate
*/
private $translationRenderer;

/**
* CheckoutSessionManagement constructor.
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
Expand Down Expand Up @@ -233,6 +239,7 @@ class CheckoutSessionManagement implements \Amazon\Pay\Api\CheckoutSessionManage
* @param UserContext $userContext
* @param \Amazon\Pay\Logger\Logger $logger
* @param Session $session
* @param Translate $translationRenderer
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
Expand Down Expand Up @@ -263,7 +270,8 @@ public function __construct(
TokenModelFactory $tokenModelFactory,
UserContext $userContext,
\Amazon\Pay\Logger\Logger $logger,
Session $session
Session $session,
Translate $translationRenderer
) {
$this->storeManager = $storeManager;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
Expand Down Expand Up @@ -294,6 +302,7 @@ public function __construct(
$this->userContext = $userContext;
$this->logger = $logger;
$this->session = $session;
$this->translationRenderer = $translationRenderer;
}

/**
Expand Down Expand Up @@ -404,40 +413,33 @@ protected function convertToMagentoAddress(array $address, $isShippingAddress =
/**
* {@inheritdoc}
*/
public function getConfig($cartId = null)
public function getConfig($cartId = null, $omitPayloads = true)
{
$result = [];
$quote = $this->session->getQuoteFromIdOrSession($cartId);

if ($this->canCheckoutWithAmazon($quote)) {
$loginButtonPayload = $this->amazonAdapter->generateLoginButtonPayload();
$checkoutButtonPayload = $this->amazonAdapter->generateCheckoutButtonPayload();
$config = [
'merchant_id' => $this->amazonConfig->getMerchantId(),
'currency' => $this->amazonConfig->getCurrencyCode(),
'button_color' => $this->amazonConfig->getButtonColor(),
'language' => $this->amazonConfig->getLanguage(),
'sandbox' => $this->amazonConfig->isSandboxEnabled(),
'login_payload' => $loginButtonPayload,
'login_signature' => $this->amazonAdapter->signButton($loginButtonPayload),
'checkout_payload' => $checkoutButtonPayload,
'checkout_signature' => $this->amazonAdapter->signButton($checkoutButtonPayload),
'public_key_id' => $this->amazonConfig->getPublicKeyId(),
];

if (!$omitPayloads) {
$config = array_merge($config, $this->getLoginButtonPayload(), $this->getCheckoutButtonPayload());
}

if ($quote) {
// Ensure the totals are up to date, in case the checkout does something to update qty or shipping
// without collecting totals
$quote->collectTotals();

$payNowButtonPayload = $this->amazonAdapter->generatePayNowButtonPayload(
$quote,
$this->amazonConfig->getPaymentAction()
);

$config['pay_only'] = $this->amazonHelper->isPayOnly($quote);
$config['paynow_payload'] = $payNowButtonPayload;
$config['paynow_signature'] = $this->amazonAdapter->signButton($payNowButtonPayload);
if (!$omitPayloads) {
$config = array_merge($config, $this->getPayNowButtonPayload($quote));
}
}

$result[] = $config;
Expand All @@ -446,6 +448,71 @@ public function getConfig($cartId = null)
return $result;
}

public function getButtonPayload($payloadType = 'checkout')
{
switch ($payloadType) {
case 'paynow':
$quote = $this->session->getQuoteFromIdOrSession();
return $this->getPayNowButtonPayload($quote);
case 'login':
return $this->getLoginButtonPayload();
default:
return $this->getCheckoutButtonPayload();
}
}

/**
* Generate login button payload/signature pair.
*
* @return mixed
*/
private function getLoginButtonPayload()
{
$loginButtonPayload = $this->amazonAdapter->generateLoginButtonPayload();
$result = [
'login_payload' => $loginButtonPayload,
'login_signature' => $this->amazonAdapter->signButton($loginButtonPayload)
];

return $result;
}

/**
* Generate checkout button payload/signature pair.
*
* @return mixed
*/
private function getCheckoutButtonPayload()
{
$checkoutButtonPayload = $this->amazonAdapter->generateCheckoutButtonPayload();
$result = [
'checkout_payload' => $checkoutButtonPayload,
'checkout_signature' => $this->amazonAdapter->signButton($checkoutButtonPayload)
];

return $result;
}

/**
* Generate paynow payload/signature pair.
*
* @param CartInterface $quote
* @return mixed
*/
private function getPayNowButtonPayload($quote)
{
$payNowButtonPayload = $this->amazonAdapter->generatePayNowButtonPayload(
$quote,
$this->amazonConfig->getPaymentAction()
);
$result = [
'paynow_payload' => $payNowButtonPayload,
'paynow_signature' => $this->amazonAdapter->signButton($payNowButtonPayload)
];

return $result;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -650,7 +717,7 @@ public function completeCheckoutSession($amazonSessionId, $cartId = null)
$this->logger->debug("Unable to complete Amazon Pay checkout. Can't submit quote id: " . $quote->getId());
return [
'success' => false,
'message' => __("Unable to complete Amazon Pay checkout"),
'message' => $this->getTranslationString('Unable to complete Amazon Pay checkout'),
];
}
try {
Expand Down Expand Up @@ -739,7 +806,7 @@ public function completeCheckoutSession($amazonSessionId, $cartId = null)

return [
'success' => false,
'message' => __(
'message' => $this->getTranslationString(
'Something went wrong. Choose another payment method for checkout and try again.'
),
];
Expand Down Expand Up @@ -835,14 +902,33 @@ public function completeCheckoutSession($amazonSessionId, $cartId = null)
protected function getCanceledMessage($amazonSession)
{
if ($amazonSession['statusDetails']['reasonCode'] == 'BuyerCanceled') {
return __("This transaction was cancelled. Please try again.");
return $this->getTranslationString('This transaction was cancelled. Please try again.');
} elseif ($amazonSession['statusDetails']['reasonCode'] == 'Declined') {
return __("This transaction was declined. Please try again using a different payment method.");
return $this->getTranslationString(
'This transaction was declined. Please try again using a different payment method.'
);
}

return $amazonSession['statusDetails']['reasonDescription'];
}

/**
* Get a translated string to return through the web API
*
* @param string $message
* @return string
*/
private function getTranslationString($message)
{
try {
$translation = $this->translationRenderer->render([$message], []);
} catch (\Exception $e) {
$translation = $message;
}

return $translation;
}

/**
* @param mixed $buyerToken
* @return mixed
Expand Down
4 changes: 2 additions & 2 deletions Model/Config/Source/PaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class PaymentAction implements \Magento\Framework\Data\OptionSourceInterface
public function toOptionArray()
{
return [
['value' => static::AUTHORIZE, 'label' => __('Charge on Shipment')],
['value' => static::AUTHORIZE_AND_CAPTURE, 'label' => __('Charge on Order')],
['value' => static::AUTHORIZE, 'label' => __('Charge when order is shipped')],
['value' => static::AUTHORIZE_AND_CAPTURE, 'label' => __('Charge when order is placed')],
];
}
}
44 changes: 44 additions & 0 deletions Model/Resolver/CheckoutSessionConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Amazon\Pay\Model\Resolver;

use Amazon\Pay\Model\CheckoutSessionManagement;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class CheckoutSessionConfig implements ResolverInterface
{

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

/**
* @param CheckoutSessionManagement $checkoutSessionManagement
*/
public function __construct(
CheckoutSessionManagement $checkoutSessionManagement
) {
$this->checkoutSessionManagement = $checkoutSessionManagement;
}

/**
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array|Value|mixed
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$cartId = $args['cartId'] ?? null;

$response = $this->checkoutSessionManagement->getConfig($cartId);
return array_shift($response);
}
}
Loading

0 comments on commit af9abfd

Please sign in to comment.