Skip to content

Commit

Permalink
Release version 4.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
afterpayplugins committed Mar 12, 2024
1 parent 85a5e00 commit e47b709
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 53 deletions.
24 changes: 16 additions & 8 deletions Controller/Payment/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Message\ManagerInterface;
use Magento\Payment\Gateway\CommandInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

class Capture implements HttpGetActionInterface
{
Expand All @@ -23,15 +24,17 @@ class Capture implements HttpGetActionInterface
private $placeOrderProcessor;
private $validateCheckoutDataCommand;
private $storeManager;
private $logger;

public function __construct(
RequestInterface $request,
Session $session,
RedirectFactory $redirectFactory,
ManagerInterface $messageManager,
PlaceOrderProcessor $placeOrderProcessor,
CommandInterface $validateCheckoutDataCommand,
StoreManagerInterface $storeManager
RequestInterface $request,
Session $session,
RedirectFactory $redirectFactory,
ManagerInterface $messageManager,
PlaceOrderProcessor $placeOrderProcessor,
CommandInterface $validateCheckoutDataCommand,
StoreManagerInterface $storeManager,
LoggerInterface $logger
) {
$this->request = $request;
$this->session = $session;
Expand All @@ -40,10 +43,12 @@ public function __construct(
$this->placeOrderProcessor = $placeOrderProcessor;
$this->validateCheckoutDataCommand = $validateCheckoutDataCommand;
$this->storeManager = $storeManager;
$this->logger = $logger;
}

public function execute()
{
$afterpayOrderToken = $this->request->getParam('orderToken');
if ($this->request->getParam('status') == self::CHECKOUT_STATUS_CANCELLED) {
$this->messageManager->addErrorMessage(
(string)__('You have cancelled your Afterpay payment. Please select an alternative payment method.')
Expand All @@ -57,6 +62,10 @@ public function execute()
$this->messageManager->addErrorMessage(
(string)__('Afterpay payment is declined. Please select an alternative payment method.')
);
$this->logger->info(
'Afterpay payment(' . $afterpayOrderToken . ') response status is "' . $this->request->getParam('status')
. '".' . 'Customer has been redirected to the cart page.'
);

return $this->redirectFactory->create()->setPath('checkout/cart', [
'_scope' => $this->storeManager->getStore()
Expand All @@ -65,7 +74,6 @@ public function execute()

try {
$quote = $this->session->getQuote();
$afterpayOrderToken = $this->request->getParam('orderToken');
$this->placeOrderProcessor->execute($quote, $this->validateCheckoutDataCommand, $afterpayOrderToken);
} catch (\Throwable $e) {
$errorMessage = $e instanceof LocalizedException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public function handle(array $handlingSubject, array $response)

if (round(1 * $grandTotal, 2) != round(1 * $response['amount']['amount'], 2)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('There are issues when processing your payment. Invalid Amount')
__('There was a issue with the processing of your payment. Invalid amount.')
);
}

$quoteItems = $quote->getAllVisibleItems();
$responseItems = $response['items'];

$invalidCartItemsExceptionMessage = __('There are issues when processing your payment. Invalid Cart Items');
$invalidCartItemsExceptionMessage = __('There was a issue with the processing of your payment. Invalid cart items.');

if (count($quoteItems) != count($responseItems)) {
throw new \Magento\Framework\Exception\LocalizedException($invalidCartItemsExceptionMessage);
Expand Down
20 changes: 19 additions & 1 deletion Model/Payment/Capture/CancelOrderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Afterpay\Afterpay\Model\Payment\Capture;

use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Command\CommandException;
use Magento\Quote\Model\Quote\Payment;

class CancelOrderProcessor
{
private $paymentDataObjectFactory;
Expand All @@ -27,7 +31,17 @@ public function __construct(
$this->quotePaidStorage = $quotePaidStorage;
}

public function execute(\Magento\Quote\Model\Quote\Payment $payment, int $quoteId): void
/**
* @param Payment $payment
* @param int $quoteId
* @param \Throwable|null $e
*
* @return void
* @throws CommandException
* @throws LocalizedException
* @throws \Throwable
*/
public function execute(\Magento\Quote\Model\Quote\Payment $payment, int $quoteId, \Throwable $e = null): void
{
if (!$this->config->getIsReversalEnabled()) {
return;
Expand All @@ -48,6 +62,10 @@ public function execute(\Magento\Quote\Model\Quote\Payment $payment, int $quoteI

$afterpayPayment = $this->quotePaidStorage->getAfterpayPaymentIfQuoteIsPaid($quoteId);
if (!$afterpayPayment) {
if ($e instanceof LocalizedException) {
throw $e;
}

throw new \Magento\Framework\Exception\LocalizedException(
__(
'Afterpay payment declined. Please select an alternative payment method.'
Expand Down
8 changes: 4 additions & 4 deletions Model/Payment/Capture/PlaceOrderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public function execute(Quote $quote, CommandInterface $checkoutDataCommand, str

$payment = $quote->getPayment();
try {
$payment->setAdditionalInformation(CheckoutInterface::AFTERPAY_TOKEN, $afterpayOrderToken);
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
$payment->setAdditionalInformation(CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY, $isCBTCurrencyAvailable);
$payment->setAdditionalInformation(CheckoutInterface::AFTERPAY_CBT_CURRENCY, $quote->getQuoteCurrencyCode());
$payment->setAdditionalInformation(CheckoutInterface::AFTERPAY_TOKEN, $afterpayOrderToken);
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
$payment->setAdditionalInformation(CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY, $isCBTCurrencyAvailable);
$payment->setAdditionalInformation(CheckoutInterface::AFTERPAY_CBT_CURRENCY, $quote->getQuoteCurrencyCode());

if (!$quote->getCustomerId()) {
$quote->setCustomerEmail($quote->getBillingAddress()->getEmail())
Expand Down
4 changes: 2 additions & 2 deletions Model/Payment/PaymentErrorProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function execute(Quote $quote, \Throwable $e, Payment $payment): int
$this->orderRepository->save($order);

return (int)$order->getEntityId();
} catch (NoSuchEntityException $e) {
} catch (NoSuchEntityException $exception) {
throw $e;
}
}
Expand All @@ -58,7 +58,7 @@ public function execute(Quote $quote, \Throwable $e, Payment $payment): int
throw $e;
}

$this->cancelOrderProcessor->execute($payment, (int)$quote->getId());
$this->cancelOrderProcessor->execute($payment, (int)$quote->getId(), $e);

if ($e instanceof LocalizedException) {
throw $e;
Expand Down
4 changes: 2 additions & 2 deletions Model/Shipment/Express/CreateShippingOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ private function createShippingOptionByMethod(

return [
'id' => implode('_', [$shippingMethod->getCarrierCode(), $shippingMethod->getMethodCode()]),
'name' => $shippingMethod->getCarrierTitle(),
'description' => $shippingMethod->getCarrierTitle(),
'name' => !empty($shippingMethod->getCarrierTitle()) ? $shippingMethod->getCarrierTitle() : $shippingMethod->getMethodTitle(),
'description' => !empty($shippingMethod->getCarrierTitle()) ? $shippingMethod->getCarrierTitle() : $shippingMethod->getMethodTitle(),
'shippingAmount' => [
'amount' => $this->formatPrice($shippingAmount),
'currency' => $currency
Expand Down
53 changes: 36 additions & 17 deletions Model/StockItemsValidator/StockItemsValidatorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@

namespace Afterpay\Afterpay\Model\StockItemsValidator;

use Afterpay\Afterpay\Gateway\Validator\StockItemsValidatorFactory;
use Afterpay\Afterpay\Model\SourceValidatorServiceFactory;
use Afterpay\Afterpay\Model\Spi\StockItemsValidatorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Module\Manager;
use Magento\Framework\ObjectManager\NoninterceptableInterface;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException;
use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface;
use Magento\InventoryShipping\Model\GetItemsToDeductFromShipment;
use Magento\InventoryShipping\Model\SourceDeductionRequestFromShipmentFactory as SourceDeductionRequestFactory;
use Magento\InventorySourceDeductionApi\Model\GetSourceItemBySourceCodeAndSku;
use Magento\Sales\Model\Order\Shipment;

class StockItemsValidatorProxy implements \Afterpay\Afterpay\Model\Spi\StockItemsValidatorInterface, \Magento\Framework\ObjectManager\NoninterceptableInterface // @codingStandardsIgnoreLine

class StockItemsValidatorProxy implements StockItemsValidatorInterface, NoninterceptableInterface
{
private $subject = null;
private $stockItemValidatorFactory;
private $sourceValidatorServiceFactory;
private $moduleManager;

public function __construct(
\Afterpay\Afterpay\Gateway\Validator\StockItemsValidatorFactory $stockItemsValidatorFactory,
\Afterpay\Afterpay\Model\SourceValidatorServiceFactory $sourceValidatorServiceFactory,
\Magento\Framework\Module\Manager $moduleManager
StockItemsValidatorFactory $stockItemsValidatorFactory,
SourceValidatorServiceFactory $sourceValidatorServiceFactory,
Manager $moduleManager
) {
$this->stockItemValidatorFactory = $stockItemsValidatorFactory;
$this->sourceValidatorServiceFactory = $sourceValidatorServiceFactory;
Expand All @@ -23,11 +40,11 @@ public function __construct(

/**
* Check msi functionality existing if no then skip validation
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws SkuIsNotAssignedToStockException
*/
public function validate(\Magento\Sales\Model\Order\Shipment $shipment): void
public function validate(Shipment $shipment): void
{
if (!$this->moduleManager->isEnabled('Magento_InventoryCatalogApi') ||
!$this->moduleManager->isEnabled('Magento_InventoryShipping') ||
Expand All @@ -41,23 +58,25 @@ public function validate(\Magento\Sales\Model\Order\Shipment $shipment): void
$stockItemsValidator->validate($shipment);
}

private function getStockItemValidator(): \Afterpay\Afterpay\Model\Spi\StockItemsValidatorInterface
private function getStockItemValidator(): StockItemsValidatorInterface
{
if ($this->subject == null) {
$objectManager = ObjectManager::getInstance();
$sourceValidatorService = $this->sourceValidatorServiceFactory->create([
'getSourceItemBySourceCodeAndSku' => $objectManager->create('\\Magento\\InventorySourceDeductionApi\\Model\\GetSourceItemBySourceCodeAndSku'), // @codingStandardsIgnoreLine
'getStockItemConfiguration' => $objectManager->create('\\Magento\\InventoryConfigurationApi\\Api\\GetStockItemConfigurationInterface'), // @codingStandardsIgnoreLine
'getStockBySalesChannel' => $objectManager->create('\\Magento\\InventorySalesApi\\Api\\GetStockBySalesChannelInterface'),

'getSourceItemBySourceCodeAndSku' => $objectManager->create(GetSourceItemBySourceCodeAndSku::class),
'getStockItemConfiguration' => $objectManager->create(GetStockItemConfigurationInterface::class),
'getStockBySalesChannel' => $objectManager->create(GetStockBySalesChannelInterface::class),
]);
$this->subject = $this->stockItemValidatorFactory->create([
'isSingleSourceMode' => $objectManager->create('\\Magento\\InventoryCatalogApi\\Model\\IsSingleSourceModeInterface'), // @codingStandardsIgnoreLine
'defaultSourceProvider' => $objectManager->create('\\Magento\\InventoryCatalogApi\\Api\\DefaultSourceProviderInterface'), // @codingStandardsIgnoreLine
'getItemsToDeductFromShipment' => $objectManager->create('\\Magento\\InventoryShipping\\Model\\GetItemsToDeductFromShipment'), // @codingStandardsIgnoreLine
'sourceDeductionRequestFromShipmentFactory' => $objectManager->create('\\Magento\\InventoryShipping\\Model\\SourceDeductionRequestFromShipmentFactory'), // @codingStandardsIgnoreLine
'sourceValidatorService' => $sourceValidatorService,
'isSingleSourceMode' => $objectManager->create(IsSingleSourceModeInterface::class),
'defaultSourceProvider' => $objectManager->create(DefaultSourceProviderInterface::class),
'getItemsToDeductFromShipment' => $objectManager->create(GetItemsToDeductFromShipment::class),
'sourceDeductionRequestFromShipmentFactory' => $objectManager->create(SourceDeductionRequestFactory::class),
'sourceValidatorService' => $sourceValidatorService,
]);
}

return $this->subject;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "Apache-2.0",
"type": "magento2-module",
"description": "Magento 2 Afterpay Payment Module",
"version": "4.3.0",
"version": "4.3.1",
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0||~7.4.0",
"magento/framework": "^102.0",
Expand Down
16 changes: 7 additions & 9 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<policies>
<policy id="script-src">
<values>
<value id="clearpay-sandbox-consumer-portal" type="host">https://portal.sandbox.clearpay.co.uk</value>
<value id="clearpay-production-consumer-portal" type="host">https://portal.clearpay.co.uk</value>
<value id="afterpay-sandbox-consumer-portal" type="host">https://portal.sandbox.afterpay.com</value>
<value id="afterpay-production-consumer-portal" type="host">https://portal.afterpay.com</value>
<value id="afterpay-cdn" type="host">https://static.afterpay.com</value>
Expand All @@ -14,20 +12,16 @@
</policy>
<policy id="connect-src">
<values>
<value id="afterpay-cdn-connect" type="host">static.afterpay.com</value>
<value id="afterpay-sandbox-cdn-connect" type="host">static.sandbox.afterpay.com</value>
<value id="afterpay-connect" type="host">*.afterpay.com</value>
<value id="square-jslib-connect" type="host">*.squarecdn.com</value>
<value id="hbiq-jslib-connect" type="host">https://hbiq.net</value>
<value id="iq-sbox-connect" type="host">https://iq.afterpay-beta.com</value>
<value id="iq-prod-connect" type="host">https://iq.afterpay.com</value>
<value id="clearpay-cdn-connect" type="host">*.clearpay.co.uk</value>

</values>
</policy>
<policy id="frame-src">
<values>
<value id="afterpay-widget-connect" type="host">widgets.sandbox.afterpay.com</value>
<value id="clearpay-widget-connect" type="host">widgets.sandbox.clearpay.co.uk</value>
</values>
</policy>
<policy id="style-src">
Expand All @@ -38,8 +32,12 @@
</policy>
<policy id="img-src">
<values>
<value id="afterpay-cdn-base" type="host">https://static.afterpay.com</value>
<value id="afterpay-assets" type="host">https://site-assets.afterpay.com/</value>
<value id="afterpay-img" type="host">*.afterpay.com/</value>
</values>
</policy>
<policy id="font-src">
<values>
<value id="square-cdn-font" type="host">*.squarecdn.com</value>
</values>
</policy>
</policies>
Expand Down
15 changes: 11 additions & 4 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<type name="Afterpay\Afterpay\Controller\Payment\Capture">
<arguments>
<argument name="validateCheckoutDataCommand" xsi:type="object">Afterpay\Afterpay\Gateway\Command\ValidateCheckoutDataCommand</argument>
<argument name="logger" xsi:type="object">Afterpay\Afterpay\Logger</argument>
</arguments>
</type>
<type name="Magento\Sales\Block\Order\Totals">
Expand All @@ -27,7 +28,9 @@
<virtualType name="Afterpay\Afterpay\ViewModel\Container\ExpressCheckout\PDPLib" type="Afterpay\Afterpay\ViewModel\Container\Lib">
<arguments>
<argument name="libUrlProvider" xsi:type="object">Afterpay\Afterpay\Model\Url\Lib\ExpressCheckoutLibUrlProvider</argument>
<argument name="containerConfigPath" xsi:type="const">Afterpay\Afterpay\Model\Config::XML_PATH_ENABLE_EXPRESS_CHECKOUT_ACTION_PRODUCT</argument>
<argument name="containerConfigPath" xsi:type="const">
Afterpay\Afterpay\Model\Config::XML_PATH_ENABLE_EXPRESS_CHECKOUT_ACTION_PRODUCT
</argument>
</arguments>
</virtualType>
<virtualType name="Afterpay\Afterpay\ViewModel\WidgetCheckout\Lib" type="Afterpay\Afterpay\ViewModel\Container\Lib">
Expand All @@ -38,13 +41,17 @@
<virtualType name="Afterpay\Afterpay\ViewModel\Container\ExpressCheckout\CartLib" type="Afterpay\Afterpay\ViewModel\Container\Lib">
<arguments>
<argument name="libUrlProvider" xsi:type="object">Afterpay\Afterpay\Model\Url\Lib\ExpressCheckoutLibUrlProvider</argument>
<argument name="containerConfigPath" xsi:type="const">Afterpay\Afterpay\Model\Config::XML_PATH_ENABLE_EXPRESS_CHECKOUT_ACTION_CART_PAGE</argument>
<argument name="containerConfigPath" xsi:type="const">
Afterpay\Afterpay\Model\Config::XML_PATH_ENABLE_EXPRESS_CHECKOUT_ACTION_CART_PAGE
</argument>
</arguments>
</virtualType>
<virtualType name="Afterpay\Afterpay\ViewModel\Container\ExpressCheckout\MiniCartLib" type="Afterpay\Afterpay\ViewModel\Container\Lib">
<arguments>
<argument name="libUrlProvider" xsi:type="object">Afterpay\Afterpay\Model\Url\Lib\ExpressCheckoutLibUrlProvider</argument>
<argument name="containerConfigPath" xsi:type="const">Afterpay\Afterpay\Model\Config::XML_PATH_ENABLE_EXPRESS_CHECKOUT_ACTION_MINI_CART</argument>
<argument name="containerConfigPath" xsi:type="const">
Afterpay\Afterpay\Model\Config::XML_PATH_ENABLE_EXPRESS_CHECKOUT_ACTION_MINI_CART
</argument>
</arguments>
</virtualType>

Expand All @@ -67,7 +74,7 @@
</arguments>
</virtualType>
<type name="Magento\Checkout\CustomerData\AbstractItem">
<plugin name="afterpay_item_additional_attributes" type="Afterpay\Afterpay\Plugin\Checkout\CustomerData\Cart" />
<plugin name="afterpay_item_additional_attributes" type="Afterpay\Afterpay\Plugin\Checkout\CustomerData\Cart"/>
</type>
<type name="Magento\Checkout\Model\CompositeConfigProvider">
<arguments>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Afterpay_Afterpay" setup_version="4.3.0">
<module name="Afterpay_Afterpay" setup_version="4.3.1">
<sequence>
<module name="Magento_Payment"/>
<module name="Magento_Checkout"/>
Expand Down
Loading

0 comments on commit e47b709

Please sign in to comment.