-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECP-8888] Implement LineItemsDataBuilder and add lineItems to PayPal…
… requests (#2892) * [ECP-8888] Implement new data builder for lineItems and set new fields in the lineItems array * [ECP-8888] Use isOpenInvoice() method to determine the open invoice characteristic of the PM * [ECP-8888] Update PHPDocs * [ECP-8888] Remove unused import * [ECP-8888] Update the condition and remove unnecessary cast * [ECP-8888] Update unit tests * [ECP-8888] Fix maintainability issue * [ECP-8888] Write unit tests * [ECP-8888] Write unit tests * [ECP-8888] Remove lineItems builder from the CheckoutDataBuilder * [ECP-8888] Check the requirement of lineItems based on the new method * [ECP-8888] Set itemCategory field only for PayPal * [ECP-8888] Fix unit failing tests * [ECP-8888] Update the log message * [ECP-8888] Write unit tests --------- Co-authored-by: Can Demiralp <can.demiralp@adyen.com>
- Loading branch information
1 parent
f37a2d8
commit 0f6ddab
Showing
12 changed files
with
407 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* | ||
* Adyen Payment module (https://www.adyen.com/) | ||
* | ||
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Adyen <magento@adyen.com> | ||
*/ | ||
|
||
namespace Adyen\Payment\Gateway\Request; | ||
|
||
use Adyen\Payment\Helper\OpenInvoice; | ||
use Adyen\Payment\Helper\PaymentMethods; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Payment\Gateway\Data\PaymentDataObject; | ||
use Magento\Payment\Gateway\Helper\SubjectReader; | ||
use Magento\Payment\Gateway\Request\BuilderInterface; | ||
use Magento\Sales\Model\Order; | ||
|
||
class LineItemsDataBuilder implements BuilderInterface | ||
{ | ||
/** | ||
* @param PaymentMethods $paymentMethodsHelper | ||
* @param OpenInvoice $openInvoiceHelper | ||
*/ | ||
public function __construct( | ||
private readonly PaymentMethods $paymentMethodsHelper, | ||
private readonly OpenInvoice $openInvoiceHelper | ||
) { } | ||
|
||
/** | ||
* Add `lineItems` to the request if the payment method requires this field | ||
* | ||
* @param array $buildSubject | ||
* @return array | ||
* @throws LocalizedException | ||
*/ | ||
public function build(array $buildSubject): array | ||
{ | ||
/** @var PaymentDataObject $paymentDataObject */ | ||
$paymentDataObject = SubjectReader::readPayment($buildSubject); | ||
$payment = $paymentDataObject->getPayment(); | ||
$paymentMethodInstance = $payment->getMethodInstance(); | ||
/** @var Order $order */ | ||
$order = $payment->getOrder(); | ||
|
||
$requestBody = []; | ||
|
||
$isLineItemsRequired = $this->paymentMethodsHelper->getRequiresLineItems($paymentMethodInstance); | ||
if ($isLineItemsRequired === true) { | ||
$requestLineItems = $this->openInvoiceHelper->getOpenInvoiceDataForOrder($order); | ||
$requestBody = array_merge($requestBody, $requestLineItems); | ||
} | ||
|
||
return [ | ||
'body' => $requestBody | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
/** | ||
* | ||
* Adyen Payment module (https://www.adyen.com/) | ||
* | ||
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Adyen <magento@adyen.com> | ||
*/ | ||
|
||
namespace Adyen\Payment\Test\Gateway\Request; | ||
|
||
use Adyen\Payment\Gateway\Request\LineItemsDataBuilder; | ||
use Adyen\Payment\Helper\OpenInvoice; | ||
use Adyen\Payment\Helper\PaymentMethods; | ||
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Payment\Gateway\Data\PaymentDataObject; | ||
use Magento\Payment\Model\MethodInterface; | ||
use Magento\Sales\Model\Order; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class LineItemsDataBuilderTest extends AbstractAdyenTestCase | ||
{ | ||
protected ?LineItemsDataBuilder $lineItemsDataBuilder; | ||
protected PaymentMethods|MockObject $paymentMethodsHelperMock; | ||
protected OpenInvoice|MockObject $openInvoiceHelperMock; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->paymentMethodsHelperMock = $this->createMock(PaymentMethods::class); | ||
$this->openInvoiceHelperMock = $this->createMock(OpenInvoice::class); | ||
|
||
$this->lineItemsDataBuilder = new LineItemsDataBuilder( | ||
$this->paymentMethodsHelperMock, | ||
$this->openInvoiceHelperMock | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
$this->lineItemsDataBuilder = null; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private static function buildDataProvider(): array | ||
{ | ||
return [ | ||
['isLineItemsRequired' => true], | ||
['isLineItemsRequired' => false] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider buildDataProvider() | ||
* | ||
* @param bool $isLineItemsRequired | ||
* @return void | ||
* @throws LocalizedException | ||
*/ | ||
public function testBuild(bool $isLineItemsRequired) | ||
{ | ||
$orderMock = $this->createMock(Order::class); | ||
|
||
$paymentMethodInstanceMock = $this->createMock(MethodInterface::class); | ||
|
||
$paymentMock = $this->createMock(Order\Payment::class); | ||
$paymentMock->expects($this->once()) | ||
->method('getOrder') | ||
->willReturn($orderMock); | ||
$paymentMock->method('getMethodInstance')->willReturn($paymentMethodInstanceMock); | ||
|
||
$paymentDataObjectMock = $this->createMock(PaymentDataObject::class); | ||
$paymentDataObjectMock->expects($this->once()) | ||
->method('getPayment') | ||
->willReturn($paymentMock); | ||
|
||
$this->paymentMethodsHelperMock->expects($this->once()) | ||
->method('getRequiresLineItems') | ||
->willReturn($isLineItemsRequired); | ||
|
||
if ($isLineItemsRequired) { | ||
$this->openInvoiceHelperMock->expects($this->once()) | ||
->method('getOpenInvoiceDataForOrder') | ||
->with($orderMock) | ||
->willReturn(['lineItems' => [['id' => 1], ['id' => 2]]]); | ||
} | ||
|
||
$buildSubject = ['payment' => $paymentDataObjectMock]; | ||
$result = $this->lineItemsDataBuilder->build($buildSubject); | ||
|
||
$this->assertIsArray($result); | ||
$this->assertArrayHasKey('body', $result); | ||
|
||
if ($isLineItemsRequired) { | ||
$this->assertArrayHasKey('lineItems', $result['body']); | ||
$this->assertArrayHasKey('id', $result['body']['lineItems'][0]); | ||
} else { | ||
$this->assertEmpty($result['body']); | ||
} | ||
} | ||
} |
Oops, something went wrong.