From 631a6c74672e3df43443630f6ee0c1a7a8bc4ebb Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Tue, 24 Dec 2024 13:49:53 +0100 Subject: [PATCH 1/2] [ECP-9561] Implement ChargedCurrency helper to use the correct currency --- Helper/Invoice.php | 127 ++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 93 deletions(-) diff --git a/Helper/Invoice.php b/Helper/Invoice.php index 1a71d6540a..607b809b65 100644 --- a/Helper/Invoice.php +++ b/Helper/Invoice.php @@ -45,107 +45,47 @@ class Invoice extends AbstractHelper { /** - * @var AdyenLogger + * @param Context $context + * @param AdyenLogger $adyenLogger + * @param Data $adyenDataHelper + * @param InvoiceRepositoryInterface $invoiceRepository + * @param InvoiceFactory $adyenInvoiceFactory + * @param AdyenInvoiceResourceModel $adyenInvoiceResourceModel + * @param OrderPaymentResourceModel $orderPaymentResourceModel + * @param PaymentFactory $adyenOrderPaymentFactory + * @param Collection $adyenInvoiceCollection + * @param MagentoInvoiceFactory $magentoInvoiceFactory + * @param \Magento\Sales\Model\ResourceModel\Order $magentoOrderResourceModel + * @param Config $configHelper + * @param InvoiceSender $invoiceSender + * @param Transaction $transaction + * @param ChargedCurrency $chargedCurrencyHelper */ - protected $adyenLogger; - - /** - * @var Data - */ - protected $adyenDataHelper; - - /** - * @var InvoiceRepositoryInterface - */ - protected $invoiceRepository; - - /** - * @var \Magento\Sales\Model\ResourceModel\Order - */ - protected $magentoOrderResourceModel; - - /** - * @var InvoiceFactory - */ - protected $adyenInvoiceFactory; - - /** - * @var AdyenInvoiceResourceModel - */ - protected $adyenInvoiceResourceModel; - - /** - * @var Collection - */ - protected $adyenInvoiceCollection; - - /** - * @var OrderPaymentResourceModel - */ - protected $orderPaymentResourceModel; - - /** - * @var PaymentFactory - */ - protected $adyenOrderPaymentFactory; - - /** - * @var MagentoInvoiceFactory - */ - protected $magentoInvoiceFactory; - - /** - * @var Config - */ - protected $configHelper; - - /** - * @var InvoiceSender - */ - protected $invoiceSender; - - /** - * @var Transaction - */ - protected $transaction; - public function __construct( - Context $context, - AdyenLogger $adyenLogger, - Data $adyenDataHelper, - InvoiceRepositoryInterface $invoiceRepository, - InvoiceFactory $adyenInvoiceFactory, - AdyenInvoiceResourceModel $adyenInvoiceResourceModel, - OrderPaymentResourceModel $orderPaymentResourceModel, - PaymentFactory $paymentFactory, - Collection $adyenInvoiceCollection, - MagentoInvoiceFactory $magentoInvoiceFactory, - \Magento\Sales\Model\ResourceModel\Order $magentoOrderResourceModel, - Config $configHelper, - InvoiceSender $invoiceSender, - Transaction $transaction + protected readonly Context $context, + protected readonly AdyenLogger $adyenLogger, + protected readonly Data $adyenDataHelper, + protected readonly InvoiceRepositoryInterface $invoiceRepository, + protected readonly InvoiceFactory $adyenInvoiceFactory, + protected readonly AdyenInvoiceResourceModel $adyenInvoiceResourceModel, + protected readonly OrderPaymentResourceModel $orderPaymentResourceModel, + protected readonly PaymentFactory $adyenOrderPaymentFactory, + protected readonly Collection $adyenInvoiceCollection, + protected readonly MagentoInvoiceFactory $magentoInvoiceFactory, + protected readonly \Magento\Sales\Model\ResourceModel\Order $magentoOrderResourceModel, + protected readonly Config $configHelper, + protected readonly InvoiceSender $invoiceSender, + protected readonly Transaction $transaction, + protected readonly ChargedCurrency $chargedCurrencyHelper ) { parent::__construct($context); - $this->adyenLogger = $adyenLogger; - $this->adyenDataHelper = $adyenDataHelper; - $this->invoiceRepository = $invoiceRepository; - $this->adyenInvoiceFactory = $adyenInvoiceFactory; - $this->adyenInvoiceResourceModel = $adyenInvoiceResourceModel; - $this->orderPaymentResourceModel = $orderPaymentResourceModel; - $this->adyenOrderPaymentFactory = $paymentFactory; - $this->adyenInvoiceCollection = $adyenInvoiceCollection; - $this->magentoInvoiceFactory = $magentoInvoiceFactory; - $this->magentoOrderResourceModel = $magentoOrderResourceModel; - $this->configHelper = $configHelper; - $this->invoiceSender = $invoiceSender; - $this->transaction = $transaction; } /** * @param Order $order * @param Notification $notification * @param bool $isAutoCapture - * @return InvoiceModel + * @return InvoiceModel|null * @throws LocalizedException */ public function createInvoice(Order $order, Notification $notification, bool $isAutoCapture): ?InvoiceModel @@ -287,9 +227,10 @@ public function handleCaptureWebhook(Order $order, Notification $notification): { $invoiceFactory = $this->adyenInvoiceFactory->create(); $adyenInvoice = $this->adyenInvoiceResourceModel->getAdyenInvoiceByCaptureWebhook($order, $notification); + $chargedCurrency = $this->chargedCurrencyHelper->getOrderAmountCurrency($order, false); $formattedAdyenOrderAmount = $this->adyenDataHelper->formatAmount( - $order->getBaseGrandTotal(), - $order->getOrderCurrencyCode() + $chargedCurrency->getAmount(), + $chargedCurrency->getCurrencyCode() ); $notificationAmount = $notification->getAmountValue(); $isFullAmountCaptured = $formattedAdyenOrderAmount == $notificationAmount; From 362ab65fa7a69570dde4f068ccdbed73ab281cca Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Tue, 24 Dec 2024 14:09:37 +0100 Subject: [PATCH 2/2] [ECP-9561] Fix unit tests --- Test/Unit/Helper/InvoiceTest.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Test/Unit/Helper/InvoiceTest.php b/Test/Unit/Helper/InvoiceTest.php index b8068c479b..de963e1dbc 100644 --- a/Test/Unit/Helper/InvoiceTest.php +++ b/Test/Unit/Helper/InvoiceTest.php @@ -12,9 +12,11 @@ namespace Adyen\Payment\Test\Unit\Helper; use Adyen\Payment\Api\Data\InvoiceInterface; +use Adyen\Payment\Helper\ChargedCurrency; use Adyen\Payment\Helper\Config; use Adyen\Payment\Helper\Data; use Adyen\Payment\Helper\Invoice; +use Adyen\Payment\Model\AdyenAmountCurrency; use Adyen\Payment\Model\InvoiceFactory; use Adyen\Payment\Model\Order\Payment; use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; @@ -162,6 +164,13 @@ public function testHandleCaptureWebhook() $transactionMock = $this->createGeneratedMock(Transaction::class, ['addObject']); $transactionMock->method('addObject')->willReturn($invoiceMock); + $adyenAmountCurrencyMock = $this->createMock(AdyenAmountCurrency::class); + $adyenAmountCurrencyMock->method('getAmount')->willReturn(10); + $adyenAmountCurrencyMock->method('getCurrencyCode')->willReturn('EUR'); + + $chargedCurrencyMock = $this->createMock(ChargedCurrency::class); + $chargedCurrencyMock->method('getOrderAmountCurrency')->willReturn($adyenAmountCurrencyMock); + $invoiceHelper = $this->createInvoiceHelper( $contextMock, null, @@ -176,7 +185,8 @@ public function testHandleCaptureWebhook() $magentoOrderResourceModelMock, null, null, - $transactionMock + $transactionMock, + $chargedCurrencyMock ); $orderPaymentMock = $this->createConfiguredMock(MagentoOrder\Payment::class, [ @@ -425,7 +435,8 @@ protected function createInvoiceHelper( $magentoOrderResourceModelMock = null, $adyenConfigHelperMock = null, $invoiceSenderMock = null, - $transactionMock = null + $transactionMock = null, + $chargedCurrencyMock = null ): Invoice { if (is_null($contextMock)) { @@ -484,6 +495,10 @@ protected function createInvoiceHelper( $transactionMock = $this->createGeneratedMock(Transaction::class); } + if (is_null($chargedCurrencyMock)) { + $chargedCurrencyMock = $this->createMock(ChargedCurrency::class); + } + return new Invoice( $contextMock, $adyenLoggerMock, @@ -498,7 +513,8 @@ protected function createInvoiceHelper( $magentoOrderResourceModelMock, $adyenConfigHelperMock, $invoiceSenderMock, - $transactionMock + $transactionMock, + $chargedCurrencyMock ); } }