Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-9561] Fix CAPTURE webhook processing for external capture attempts with different currency #2834

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 34 additions & 93 deletions Helper/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 19 additions & 3 deletions Test/Unit/Helper/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -176,7 +185,8 @@ public function testHandleCaptureWebhook()
$magentoOrderResourceModelMock,
null,
null,
$transactionMock
$transactionMock,
$chargedCurrencyMock
);

$orderPaymentMock = $this->createConfiguredMock(MagentoOrder\Payment::class, [
Expand Down Expand Up @@ -425,7 +435,8 @@ protected function createInvoiceHelper(
$magentoOrderResourceModelMock = null,
$adyenConfigHelperMock = null,
$invoiceSenderMock = null,
$transactionMock = null
$transactionMock = null,
$chargedCurrencyMock = null
): Invoice {

if (is_null($contextMock)) {
Expand Down Expand Up @@ -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,
Expand All @@ -498,7 +513,8 @@ protected function createInvoiceHelper(
$magentoOrderResourceModelMock,
$adyenConfigHelperMock,
$invoiceSenderMock,
$transactionMock
$transactionMock,
$chargedCurrencyMock
);
}
}
Loading