Skip to content
Merged
24 changes: 23 additions & 1 deletion Gateway/Request/RefundDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Adyen\Payment\Gateway\Request;

use Adyen\Payment\Api\Data\InvoiceInterface;
use Adyen\Payment\Api\Data\OrderPaymentInterface;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Config;
Expand All @@ -24,6 +25,7 @@
use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Sales\Model\Order\Payment;
use Adyen\Payment\Model\ResourceModel\Invoice\Collection as AdyenInvoiceCollection;

/**
* Class CustomerDataBuilder
Expand All @@ -41,14 +43,16 @@ class RefundDataBuilder implements BuilderInterface
* @param Config $configHelper
* @param OpenInvoice $openInvoiceHelper
* @param PaymentMethods $paymentMethodsHelper
* @param AdyenInvoiceCollection $adyenInvoiceCollection
*/
public function __construct(
private readonly Data $adyenHelper,
private readonly PaymentCollectionFactory $orderPaymentCollectionFactory,
private readonly ChargedCurrency $chargedCurrency,
private readonly Config $configHelper,
private readonly OpenInvoice $openInvoiceHelper,
private readonly PaymentMethods $paymentMethodsHelper
private readonly PaymentMethods $paymentMethodsHelper,
private readonly AdyenInvoiceCollection $adyenInvoiceCollection,
) { }

/**
Expand Down Expand Up @@ -167,6 +171,24 @@ public function build(array $buildSubject): array
]
];

if ($method === PaymentMethods::ADYEN_PAYPAL) {
$adyenInvoices = $this->adyenInvoiceCollection->getAdyenInvoicesLinkedToMagentoInvoice(
$creditMemo->getInvoiceId()
);
$firstAdyenInvoice = reset($adyenInvoices);

$isPaypalManualCapture = $this->configHelper->getConfigData(
'paypal_capture_mode',
'adyen_abstract',
$storeId,
true
);

if (!empty($adyenInvoices) && $isPaypalManualCapture) {
$requestBody[0]['capturePspReference'] = $firstAdyenInvoice[InvoiceInterface::PSPREFERENCE];
}
}

if ($this->paymentMethodsHelper->getRequiresLineItems($paymentMethodInstance)) {
$openInvoiceFieldsCreditMemo = $this->openInvoiceHelper->getOpenInvoiceDataForCreditMemo($creditMemo);
//There is only one payment, so we add the fields to the first(and only) result
Expand Down
5 changes: 0 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1155,11 +1155,6 @@ public function initializeAdyenClient(
$client->setApplicationName(self::APPLICATION_NAME);
$client->setXApiKey($apiKey);

$checkoutFrontendRegion = $this->configHelper->getCheckoutFrontendRegion($storeId);
if (isset($checkoutFrontendRegion)) {
$client->setRegion($checkoutFrontendRegion);
}

$client->setMerchantApplication($this->getModuleName(), $this->getModuleVersion());
$platformData = $this->getMagentoDetails();
$client->setExternalPlatform($platformData['name'], $platformData['version'], 'Adyen');
Expand Down
30 changes: 29 additions & 1 deletion Test/Unit/Gateway/Request/RefundDataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Adyen\Payment\Test\Gateway\Request;

use Adyen\Payment\Api\Data\InvoiceInterface;
use Adyen\Payment\Api\Data\OrderPaymentInterface;
use Adyen\Payment\Gateway\Request\RefundDataBuilder;
use Adyen\Payment\Helper\ChargedCurrency;
Expand All @@ -20,6 +21,7 @@
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Model\AdyenAmountCurrency;
use Adyen\Payment\Model\Order\Payment as AdyenOrderPayment;
use Adyen\Payment\Model\ResourceModel\Invoice\Collection as AdyenInvoiceCollection;
use Adyen\Payment\Model\ResourceModel\Order\Payment\Collection;
use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory as PaymentCollectionFactory;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
Expand All @@ -38,6 +40,7 @@ class RefundDataBuilderTest extends AbstractAdyenTestCase
protected Config $configHelperMock;
protected OpenInvoice $openInvoiceHelperMock;
protected PaymentMethods $paymentMethodsHelperMock;
protected AdyenInvoiceCollection $adyenInvoiceCollection;

/**
* @return void
Expand All @@ -53,14 +56,16 @@ protected function setUp(): void
$this->configHelperMock = $this->createMock(Config::class);
$this->openInvoiceHelperMock = $this->createMock(OpenInvoice::class);
$this->paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);
$this->adyenInvoiceCollection = $this->createMock(AdyenInvoiceCollection::class);

$this->refundDataBuilder = new RefundDataBuilder(
$this->adyenHelperMock,
$this->orderPaymentCollectionFactoryMock,
$this->chargedCurrencyMock,
$this->configHelperMock,
$this->openInvoiceHelperMock,
$this->paymentMethodsHelperMock
$this->paymentMethodsHelperMock,
$this->adyenInvoiceCollection
);
}

Expand All @@ -84,6 +89,15 @@ private static function dataProviderForRefundDataBuilder(): array
]
]
],
[
'paymentMethod' => 'adyen_paypal',
'orderPaymentCollectionData' => [
[
'amount' => 100.00,
'totalRefunded' => 0.0
]
]
],
[
'paymentMethod' => 'adyen_moto',
'orderPaymentCollectionData' => [
Expand Down Expand Up @@ -223,6 +237,17 @@ public function testBuild(
$creditMemoAmountCurrencyMock->method('getCurrencyCode')->willReturn($creditMemoCurrency);
$creditMemoAmountCurrencyMock->method('getAmount')->willReturn($creditMemoAmount);

$adyenInvoiceMock = [
InvoiceInterface::ENTITY_ID => 1,
InvoiceInterface::PSPREFERENCE => 'mock_capture_pspreference'
];

$this->adyenInvoiceCollection->method('getAdyenInvoicesLinkedToMagentoInvoice')
->willReturn([$adyenInvoiceMock]);
$this->configHelperMock->method('getConfigData')->willReturnMap([
['paypal_capture_mode', 'adyen_abstract', $storeId, true, true],
]);

$this->chargedCurrencyMock->method('getOrderAmountCurrency')
->with($orderMock)
->willReturn($orderAmountCurrencyMock);
Expand Down Expand Up @@ -281,5 +306,8 @@ public function testBuild(
$this->assertArrayHasKey('amount', $result['body'][0]);
$this->assertArrayHasKey('reference', $result['body'][0]);
$this->assertArrayHasKey('paymentPspReference', $result['body'][0]);
if ($paymentMethod === PaymentMethods::ADYEN_PAYPAL) {
$this->assertArrayHasKey('capturePspReference', $result['body'][0]);
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=8.1",
"adyen/php-api-library": "^20.4.0",
"adyen/php-api-library": "^27.0.0",
"adyen/php-webhook-module": "^1",
"magento/framework": ">=103.0.4",
"magento/module-vault": ">=101.2.4",
Expand Down
Loading