Skip to content

Commit

Permalink
Release version 5.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
clearpayplugins committed Mar 12, 2024
1 parent ece0ce4 commit d5a869a
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 50 deletions.
12 changes: 10 additions & 2 deletions Controller/Payment/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Payment\Gateway\CommandInterface;
use Psr\Log\LoggerInterface;

class Capture implements HttpGetActionInterface
{
Expand All @@ -21,25 +22,29 @@ class Capture implements HttpGetActionInterface
private ManagerInterface $messageManager;
private PlaceOrderProcessor $placeOrderProcessor;
private CommandInterface $validateCheckoutDataCommand;
private LoggerInterface $logger;

public function __construct(
RequestInterface $request,
Session $session,
RedirectFactory $redirectFactory,
ManagerInterface $messageManager,
PlaceOrderProcessor $placeOrderProcessor,
CommandInterface $validateCheckoutDataCommand
CommandInterface $validateCheckoutDataCommand,
LoggerInterface $logger
) {
$this->request = $request;
$this->session = $session;
$this->redirectFactory = $redirectFactory;
$this->messageManager = $messageManager;
$this->placeOrderProcessor = $placeOrderProcessor;
$this->validateCheckoutDataCommand = $validateCheckoutDataCommand;
$this->logger = $logger;
}

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

return $this->redirectFactory->create()->setPath('checkout/cart');
}

try {
$quote = $this->session->getQuote();
$clearpayOrderToken = $this->request->getParam('orderToken');
$this->placeOrderProcessor->execute($quote, $this->validateCheckoutDataCommand, $clearpayOrderToken);
} catch (\Throwable $e) {
$errorMessage = $e instanceof LocalizedException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,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
19 changes: 16 additions & 3 deletions Model/Payment/Capture/CancelOrderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Clearpay\Clearpay\Model\Payment\Capture;

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

class CancelOrderProcessor
{
private \Magento\Payment\Gateway\Data\PaymentDataObjectFactoryInterface $paymentDataObjectFactory;
Expand All @@ -28,10 +32,15 @@ public function __construct(
}

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

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

throw new \Magento\Framework\Exception\LocalizedException(
__(
'Clearpay payment is declined. Please select an alternative payment method.'
Expand Down
4 changes: 2 additions & 2 deletions Model/Payment/PaymentErrorProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ 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;
}
}
if ($e instanceof CommandException && $e->getMessage() === CaptureErrorMessageMapper::STATUS_DECLINED_ERROR_MESSAGE) {
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
61 changes: 39 additions & 22 deletions Model/StockItemsValidator/StockItemsValidatorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@

namespace Clearpay\Clearpay\Model\StockItemsValidator;

use Clearpay\Clearpay\Gateway\Validator\StockItemsValidatorFactory;
use Clearpay\Clearpay\Model\SourceValidatorServiceFactory;
use Clearpay\Clearpay\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 \Clearpay\Clearpay\Model\Spi\StockItemsValidatorInterface, \Magento\Framework\ObjectManager\NoninterceptableInterface// @codingStandardsIgnoreLine
class StockItemsValidatorProxy implements StockItemsValidatorInterface, NoninterceptableInterface
{
private ?\Clearpay\Clearpay\Model\Spi\StockItemsValidatorInterface $subject = null;
private \Clearpay\Clearpay\Gateway\Validator\StockItemsValidatorFactory $stockItemValidatorFactory;
private \Clearpay\Clearpay\Model\SourceValidatorServiceFactory $sourceValidatorServiceFactory;
private \Magento\Framework\Module\Manager $moduleManager;
private ?StockItemsValidatorInterface $subject = null;
private StockItemsValidatorFactory $stockItemValidatorFactory;
private SourceValidatorServiceFactory $sourceValidatorServiceFactory;
private Manager $moduleManager;

public function __construct(
\Clearpay\Clearpay\Gateway\Validator\StockItemsValidatorFactory $stockItemsValidatorFactory,
\Clearpay\Clearpay\Model\SourceValidatorServiceFactory $sourceValidatorServiceFactory,
\Magento\Framework\Module\Manager $moduleManager
StockItemsValidatorFactory $stockItemsValidatorFactory,
SourceValidatorServiceFactory $sourceValidatorServiceFactory,
Manager $moduleManager
) {
$this->stockItemValidatorFactory = $stockItemsValidatorFactory;
$this->sourceValidatorServiceFactory = $sourceValidatorServiceFactory;
Expand All @@ -23,41 +39,42 @@ 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') ||
!$this->moduleManager->isEnabled('Magento_InventorySourceDeductionApi') ||
!$this->moduleManager->isEnabled('Magento_InventoryConfigurationApi') ||
!$this->moduleManager->isEnabled('Magento_InventorySalesApi')
) {
return ;
return;
}
$stockItemsValidator = $this->getStockItemValidator();
$stockItemsValidator->validate($shipment);
}

private function getStockItemValidator(): \Clearpay\Clearpay\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 Clearpay Payment Module",
"version": "5.3.0",
"version": "5.3.1",
"require": {
"php": ">=7.4.0",
"magento/framework": "^103.0",
Expand Down
20 changes: 7 additions & 13 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,37 @@
<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>
<value id="clearpay-scripts" type="host">*.afterpay.com</value>
<value id="afterpay-scripts" type="host">*.clearpay.co.uk</value>
<value id="square-jslib" type="host">*.squarecdn.com</value>
<value id="hbiq-jslib" type="host">https://hbiq.net</value>
</values>
</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="clearpay-connect" type="host">*.clearpay.co.uk</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>
<value id="clearpay-frame" type="host">*.clearpay.co.uk</value>
</values>
</policy>
<policy id="style-src">
<values>
<value id="afterpay-cdn-style" type="host">static.afterpay.com/</value>
<value id="afterpay-style" type="host">*.afterpay.com/</value>
<value id="square-jslib-style" type="host">*.squarecdn.com</value>
</values>
</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>
<value id="clearpay-img" type="host">*.clearpay.co.uk</value>
</values>
</policy>
</policies>
Expand Down
1 change: 1 addition & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<type name="Clearpay\Clearpay\Controller\Payment\Capture">
<arguments>
<argument name="validateCheckoutDataCommand" xsi:type="object">Clearpay\Clearpay\Gateway\Command\ValidateCheckoutDataCommand</argument>
<argument name="logger" xsi:type="object">Clearpay\Clearpay\Logger</argument>
</arguments>
</type>
<type name="Magento\Sales\Block\Order\Totals">
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="Clearpay_Clearpay" setup_version="5.3.0">
<module name="Clearpay_Clearpay" setup_version="5.3.1">
<sequence>
<module name="Magento_Payment"/>
<module name="Magento_Checkout"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ define([
},
updatePrice: function (newPrices) {
const res = this._super(newPrices);
if (this._checkIsFinalPriceDefined() && this.element.closest('product-info-main')) {
if (this._checkIsFinalPriceDefined() && this.element.closest('.product-info-main').length > 0) {
containerModel.setCurrentProductId(this.element.data('productId'));
containerModel.setPrice(this.cache.displayPrices.finalPrice.amount);
}
Expand Down
6 changes: 6 additions & 0 deletions view/frontend/web/js/view/payment/method-renderer/clearpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ define([
getMPID: function () {
return window.checkoutConfig.payment.clearpay.mpid;
},
getAmount: function() {
let amount = window .checkoutConfig.payment.clearpay.isCBTCurrency
? quote.totals().grand_total : quote.totals().base_grand_total;

return amount;
},
continueToClearpay: function (data, event) {
const self = this;

Expand Down
3 changes: 2 additions & 1 deletion view/frontend/web/template/payment/clearpay.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
'data-mpid':getMPID(),
'data-platform': 'Magento',
'data-page-type': 'checkout',
'data-type':'logo'}
'data-type':'logo',
'data-amount':getAmount()}
"></square-placement>
</label>
</div>
Expand Down

0 comments on commit d5a869a

Please sign in to comment.