Skip to content

Commit 830d5e1

Browse files
authored
[ECP-9078] Implement is_open_invoice payment method configuration field (#2856)
* [ECP-9078] Implement is_open_invoice payment method configuration field * [ECP-9078] Write unit tests and clean-up unused imports * [ECP-9078] Deprecate unused method * [ECP-9078] Write unit tests * [ECP-9078] Write unit tests * [ECP-9078] Ignore CS issue on the test file * [ECP-9078] Update unit tests * [ECP-9078] PHPCS ignore * [ECP-9078] PHPCS remove ignore argument --------- Co-authored-by: Can Demiralp <can.demiralp@adyen.com>
1 parent a54c952 commit 830d5e1

17 files changed

+640
-217
lines changed

.github/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ magento:
1515
install:
1616
composer config --json repositories.local '{"type": "path", "url": "/data/extensions/workdir", "options": { "symlink": false } }'
1717
composer require "adyen/module-payment:*"
18-
vendor/bin/phpcs --standard=Magento2 --extensions=php,phtml --error-severity=10 --ignore-annotations -n -p vendor/adyen/module-payment
18+
vendor/bin/phpcs --standard=Magento2 --extensions=php,phtml --error-severity=10 -n -p vendor/adyen/module-payment
1919
bin/magento module:enable Adyen_Payment
2020
bin/magento setup:upgrade
2121
bin/magento setup:di:compile

Gateway/Data/Order/OrderAdapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function __construct(
3333
parent::__construct($order, $addressAdapterFactory);
3434
}
3535

36+
/**
37+
* @deprecated Use Magento\Sales\Api\Data\OrderInterface::getQuoteId() instead
38+
*
39+
* @return float|int|null
40+
*/
3641
public function getQuoteId()
3742
{
3843
return $this->order->getQuoteId();

Gateway/Request/CaptureDataBuilder.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
use Adyen\Payment\Helper\ChargedCurrency;
1919
use Adyen\Payment\Helper\Data as DataHelper;
2020
use Adyen\Payment\Helper\OpenInvoice;
21+
use Adyen\Payment\Helper\PaymentMethods;
2122
use Adyen\Payment\Logger\AdyenLogger;
2223
use Adyen\Payment\Model\ResourceModel\Order\Payment;
23-
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
2424
use Magento\Framework\App\Action\Context;
25+
use Magento\Framework\Exception\LocalizedException;
2526
use Magento\Payment\Gateway\Data\PaymentDataObject;
2627
use Magento\Payment\Gateway\Helper\SubjectReader;
2728
use Magento\Payment\Gateway\Request\BuilderInterface;
@@ -33,40 +34,36 @@
3334
*/
3435
class CaptureDataBuilder implements BuilderInterface
3536
{
36-
private DataHelper $adyenHelper;
37-
private ChargedCurrency $chargedCurrency;
38-
private Payment $orderPaymentResourceModel;
39-
private AdyenOrderPayment $adyenOrderPaymentHelper;
40-
private AdyenLogger $adyenLogger;
41-
private Context $context;
42-
protected OpenInvoice $openInvoiceHelper;
43-
37+
/**
38+
* @param DataHelper $adyenHelper
39+
* @param ChargedCurrency $chargedCurrency
40+
* @param AdyenOrderPayment $adyenOrderPaymentHelper
41+
* @param AdyenLogger $adyenLogger
42+
* @param Context $context
43+
* @param Payment $orderPaymentResourceModel
44+
* @param OpenInvoice $openInvoiceHelper
45+
* @param PaymentMethods $paymentMethodsHelper
46+
*/
4447
public function __construct(
45-
DataHelper $adyenHelper,
46-
ChargedCurrency $chargedCurrency,
47-
AdyenOrderPayment $adyenOrderPaymentHelper,
48-
AdyenLogger $adyenLogger,
49-
Context $context,
50-
Payment $orderPaymentResourceModel,
51-
OpenInvoice $openInvoiceHelper
52-
) {
53-
$this->adyenHelper = $adyenHelper;
54-
$this->chargedCurrency = $chargedCurrency;
55-
$this->adyenOrderPaymentHelper = $adyenOrderPaymentHelper;
56-
$this->adyenLogger = $adyenLogger;
57-
$this->context = $context;
58-
$this->orderPaymentResourceModel = $orderPaymentResourceModel;
59-
$this->openInvoiceHelper = $openInvoiceHelper;
60-
}
48+
private readonly DataHelper $adyenHelper,
49+
private readonly ChargedCurrency $chargedCurrency,
50+
private readonly AdyenOrderPayment $adyenOrderPaymentHelper,
51+
private readonly AdyenLogger $adyenLogger,
52+
private readonly Context $context,
53+
private readonly Payment $orderPaymentResourceModel,
54+
protected readonly OpenInvoice $openInvoiceHelper,
55+
private readonly PaymentMethods $paymentMethodsHelper
56+
) { }
6157

6258
/**
63-
* @throws AdyenException
59+
* @throws AdyenException|LocalizedException
6460
*/
6561
public function build(array $buildSubject): array
6662
{
6763
/** @var PaymentDataObject $paymentDataObject */
6864
$paymentDataObject = SubjectReader::readPayment($buildSubject);
6965
$payment = $paymentDataObject->getPayment();
66+
$paymentMethodInstance = $payment->getMethodInstance();
7067
/** @var Order $order */
7168
$order = $payment->getOrder();
7269
/** @var Invoice $latestInvoice */
@@ -78,7 +75,6 @@ public function build(array $buildSubject): array
7875
$orderAmountCents = $this->adyenHelper->formatAmount($orderAmountCurrency->getAmount(), $currency);
7976

8077
$pspReference = $payment->getCcTransId();
81-
$brandCode = $payment->getAdditionalInformation(AdyenPaymentMethodDataAssignObserver::BRAND_CODE);
8278

8379
// If total amount has not been authorized
8480
if (!$this->adyenOrderPaymentHelper->isFullAmountAuthorized($order)) {
@@ -116,7 +112,7 @@ public function build(array $buildSubject): array
116112
];
117113

118114
//Check additionaldata
119-
if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod($brandCode)) {
115+
if ($this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance)) {
120116
$openInvoiceFields = $this->openInvoiceHelper->getOpenInvoiceDataForInvoice($latestInvoice);
121117
$requestBody = array_merge($requestBody, $openInvoiceFields);
122118
}
@@ -139,6 +135,7 @@ public function buildPartialOrMultipleCaptureData($payment, $currency, $adyenOrd
139135
), $this->adyenLogger->getOrderContext($payment->getOrder()));
140136

141137
$captureAmountCents = $this->adyenHelper->formatAmount($captureAmount, $currency);
138+
$paymentMethodInstance = $payment->getMethodInstance();
142139
$captureData = [];
143140
$counterAmount = 0;
144141
$i = 0;
@@ -174,9 +171,7 @@ public function buildPartialOrMultipleCaptureData($payment, $currency, $adyenOrd
174171
"paymentPspReference" => $adyenOrderPayment[OrderPaymentInterface::PSPREFRENCE]
175172
];
176173

177-
if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod(
178-
$adyenOrderPayment[OrderPaymentInterface::PAYMENT_METHOD]
179-
)) {
174+
if ($this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance)) {
180175
$order = $payment->getOrder();
181176
$invoices = $order->getInvoiceCollection();
182177
// The latest invoice will contain only the selected items(and quantities) for the (partial) capture

Gateway/Request/CheckoutDataBuilder.php

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
use Adyen\Payment\Helper\ChargedCurrency;
1515
use Adyen\Payment\Helper\Config;
1616
use Adyen\Payment\Helper\Data;
17+
use Adyen\Payment\Helper\PaymentMethods;
1718
use Adyen\Payment\Helper\StateData;
1819
use Adyen\Payment\Helper\OpenInvoice;
1920
use Adyen\Payment\Model\Config\Source\ThreeDSFlow;
2021
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
2122
use Adyen\Payment\Observer\AdyenCcDataAssignObserver;
2223
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
24+
use Magento\Framework\Exception\LocalizedException;
2325
use Magento\Framework\Exception\NoSuchEntityException;
2426
use Magento\Payment\Gateway\Data\PaymentDataObject;
2527
use Magento\Payment\Gateway\Helper\SubjectReader;
@@ -36,38 +38,9 @@ class CheckoutDataBuilder implements BuilderInterface
3638
self::ADYEN_BOLETO
3739
];
3840

39-
/**
40-
* @var Data
41-
*/
42-
private Data $adyenHelper;
43-
44-
/**
45-
* @var CartRepositoryInterface
46-
*/
47-
private CartRepositoryInterface $cartRepository;
48-
49-
/**
50-
* @var ChargedCurrency
51-
*/
52-
private ChargedCurrency $chargedCurrency;
53-
54-
/**
55-
* @var StateData
56-
*/
57-
private StateData $stateData;
58-
59-
/**
60-
* @var Config
61-
*/
62-
private Config $configHelper;
63-
64-
/**
65-
* @var OpenInvoice
66-
*/
67-
private OpenInvoice $openInvoiceHelper;
68-
6941
/**
7042
* CheckoutDataBuilder constructor.
43+
*
7144
* @param Data $adyenHelper
7245
* @param StateData $stateData
7346
* @param CartRepositoryInterface $cartRepository
@@ -76,31 +49,26 @@ class CheckoutDataBuilder implements BuilderInterface
7649
* @param OpenInvoice $openInvoiceHelper
7750
*/
7851
public function __construct(
79-
Data $adyenHelper,
80-
StateData $stateData,
81-
CartRepositoryInterface $cartRepository,
82-
ChargedCurrency $chargedCurrency,
83-
Config $configHelper,
84-
OpenInvoice $openInvoiceHelper
85-
) {
86-
$this->adyenHelper = $adyenHelper;
87-
$this->stateData = $stateData;
88-
$this->cartRepository = $cartRepository;
89-
$this->chargedCurrency = $chargedCurrency;
90-
$this->configHelper = $configHelper;
91-
$this->openInvoiceHelper = $openInvoiceHelper;
92-
}
52+
private readonly Data $adyenHelper,
53+
private readonly StateData $stateData,
54+
private readonly CartRepositoryInterface $cartRepository,
55+
private readonly ChargedCurrency $chargedCurrency,
56+
private readonly Config $configHelper,
57+
private readonly OpenInvoice $openInvoiceHelper,
58+
private readonly PaymentMethods $paymentMethodsHelper
59+
) { }
9360

9461
/**
9562
* @param array $buildSubject
9663
* @return array
97-
* @throws NoSuchEntityException
64+
* @throws NoSuchEntityException|LocalizedException
9865
*/
9966
public function build(array $buildSubject): array
10067
{
10168
/** @var PaymentDataObject $paymentDataObject */
10269
$paymentDataObject = SubjectReader::readPayment($buildSubject);
10370
$payment = $paymentDataObject->getPayment();
71+
$paymentMethodInstance = $payment->getMethodInstance();
10472
/** @var Order $order */
10573
$order = $payment->getOrder();
10674
$storeId = $order->getStoreId();
@@ -130,7 +98,7 @@ public function build(array $buildSubject): array
13098

13199
$brandCode = $payment->getAdditionalInformation(AdyenPaymentMethodDataAssignObserver::BRAND_CODE);
132100
if (
133-
(isset($brandCode) && $this->adyenHelper->isPaymentMethodOpenInvoiceMethod($brandCode)) ||
101+
$this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance) ||
134102
$payment->getMethod() === AdyenPayByLinkConfigProvider::CODE
135103
) {
136104
$openInvoiceFields = $this->openInvoiceHelper->getOpenInvoiceDataForOrder($order);

Gateway/Request/RefundDataBuilder.php

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

1212
namespace Adyen\Payment\Gateway\Request;
1313

14+
use Adyen\Payment\Api\Data\OrderPaymentInterface;
1415
use Adyen\Payment\Helper\ChargedCurrency;
1516
use Adyen\Payment\Helper\Config;
1617
use Adyen\Payment\Helper\Data;
1718
use Adyen\Payment\Helper\OpenInvoice;
19+
use Adyen\Payment\Helper\PaymentMethods;
1820
use Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory;
1921
use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory as PaymentCollectionFactory;
20-
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
22+
use Magento\Framework\Exception\LocalizedException;
23+
use Magento\Payment\Gateway\Data\PaymentDataObject;
2124
use Magento\Payment\Gateway\Helper\SubjectReader;
2225
use Magento\Payment\Gateway\Request\BuilderInterface;
2326
use Magento\Sales\Model\Order\Payment;
@@ -31,46 +34,50 @@ class RefundDataBuilder implements BuilderInterface
3134
const REFUND_STRATEGY_DESCENDING_ORDER = '2';
3235
const REFUND_STRATEGY_BASED_ON_RATIO = '3';
3336

34-
private Data $adyenHelper;
35-
private Config $configHelper;
36-
private PaymentCollectionFactory $orderPaymentCollectionFactory;
37-
private ChargedCurrency $chargedCurrency;
38-
private OpenInvoice $openInvoiceHelper;
39-
37+
/**
38+
* @param Data $adyenHelper
39+
* @param PaymentCollectionFactory $orderPaymentCollectionFactory
40+
* @param ChargedCurrency $chargedCurrency
41+
* @param Config $configHelper
42+
* @param OpenInvoice $openInvoiceHelper
43+
* @param PaymentMethods $paymentMethodsHelper
44+
*/
4045
public function __construct(
41-
Data $adyenHelper,
42-
PaymentCollectionFactory $orderPaymentCollectionFactory,
43-
ChargedCurrency $chargedCurrency,
44-
Config $configHelper,
45-
OpenInvoice $openInvoiceHelper
46-
) {
47-
$this->adyenHelper = $adyenHelper;
48-
$this->orderPaymentCollectionFactory = $orderPaymentCollectionFactory;
49-
$this->chargedCurrency = $chargedCurrency;
50-
$this->configHelper = $configHelper;
51-
$this->openInvoiceHelper = $openInvoiceHelper;
52-
}
53-
46+
private readonly Data $adyenHelper,
47+
private readonly PaymentCollectionFactory $orderPaymentCollectionFactory,
48+
private readonly ChargedCurrency $chargedCurrency,
49+
private readonly Config $configHelper,
50+
private readonly OpenInvoice $openInvoiceHelper,
51+
private readonly PaymentMethods $paymentMethodsHelper
52+
) { }
53+
54+
/**
55+
* @param array $buildSubject
56+
* @return array
57+
* @throws LocalizedException
58+
*/
5459
public function build(array $buildSubject): array
5560
{
61+
/** @var PaymentDataObject $paymentDataObject */
5662
$paymentDataObject = SubjectReader::readPayment($buildSubject);
5763

58-
$order = $paymentDataObject->getOrder();
59-
/** @var Payment $payment */
64+
/** @var Payment $payment */
6065
$payment = $paymentDataObject->getPayment();
61-
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($payment->getOrder(), false);
66+
$order = $payment->getOrder();
67+
$paymentMethodInstance = $payment->getMethodInstance();
68+
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order, false);
6269

6370
// Construct AdyenAmountCurrency from creditmemo
6471
$creditMemo = $payment->getCreditMemo();
65-
$creditMemoAmountCurrency = $this->chargedCurrency->getCreditMemoAmountCurrency($creditMemo, false);
72+
$creditMemoAmountCurrency = $this->chargedCurrency->getCreditMemoAmountCurrency($creditMemo);
6673

6774
$pspReference = $payment->getCcTransId();
6875
$currency = $creditMemoAmountCurrency->getCurrencyCode();
6976
$amount = $creditMemoAmountCurrency->getAmount();
7077

7178

7279
//Get Merchant Account
73-
$storeId = $order ->getStoreId();
80+
$storeId = $order->getStoreId();
7481
$method = $payment->getMethod();
7582

7683
if (isset($method) && $method === 'adyen_moto') {
@@ -82,10 +89,10 @@ public function build(array $buildSubject): array
8289
// check if it contains a partial payment
8390
$orderPaymentCollection = $this->orderPaymentCollectionFactory
8491
->create()
85-
->addFieldToFilter('payment_id', $payment->getId());
92+
->addFieldToFilter(OrderPaymentInterface::PAYMENT_ID, $payment->getId());
8693

8794
// partial refund if multiple payments check refund strategy
88-
if ($orderPaymentCollection->getSize() > self::REFUND_STRATEGY_ASCENDING_ORDER) {
95+
if ($orderPaymentCollection->getSize() > 1) {
8996
$refundStrategy = $this->configHelper->getAdyenAbstractConfigData(
9097
'partial_payments_refund_strategy',
9198
$storeId
@@ -141,7 +148,7 @@ public function build(array $buildSubject): array
141148
$requestBody[] = [
142149
"merchantAccount" => $merchantAccount,
143150
"amount" => $modificationAmountObject,
144-
"reference" => $payment->getOrder()->getIncrementId(),
151+
"reference" => $order->getIncrementId(),
145152
"paymentPspReference" => $partialPayment->getPspreference(),
146153
];
147154
}
@@ -155,32 +162,27 @@ public function build(array $buildSubject): array
155162
[
156163
"merchantAccount" => $merchantAccount,
157164
"amount" => $modificationAmount,
158-
"reference" => $payment->getOrder()->getIncrementId(),
165+
"reference" => $order->getIncrementId(),
159166
"paymentPspReference" => $pspReference,
160167
]
161168
];
162169

163-
$brandCode = $payment->getAdditionalInformation(
164-
AdyenPaymentMethodDataAssignObserver::BRAND_CODE
165-
);
166-
167-
if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod($brandCode)) {
170+
if ($this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance)) {
168171
$openInvoiceFieldsCreditMemo = $this->openInvoiceHelper->getOpenInvoiceDataForCreditMemo($creditMemo);
169172
//There is only one payment, so we add the fields to the first(and only) result
170-
$requestBody[0] = array_merge($requestBody[0], $openInvoiceFieldsCreditMemo);
173+
$requestBody[0] = array_merge($requestBody[0], $openInvoiceFieldsCreditMemo);
171174
}
172175
}
173176

174-
$request['clientConfig'] = ["storeId" => $payment->getOrder()->getStoreId()];
177+
$request['clientConfig'] = ["storeId" => $storeId];
175178
$request['body'] = $requestBody;
176179

177180
$request['headers'] = [
178181
'idempotencyExtraData' => [
179-
'totalRefunded' => $payment->getOrder()->getTotalRefunded() ?? 0
182+
'totalRefunded' => $order->getTotalRefunded() ?? 0
180183
]
181184
];
182185

183186
return $request;
184187
}
185-
186188
}

Helper/Data.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ public function getModuleVersion()
680680
}
681681

682682
/**
683+
* @deprecated Use Adyen\Payment\Helper\PaymentMethods::isOpenInvoice() instead.
684+
*
683685
* @param $paymentMethod
684686
* @return bool
685687
*/

0 commit comments

Comments
 (0)