Skip to content

Commit

Permalink
[ECP-9078] Implement is_open_invoice payment method configuration fie…
Browse files Browse the repository at this point in the history
…ld (#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>
  • Loading branch information
candemiralp and Can Demiralp authored Feb 18, 2025
1 parent a54c952 commit 830d5e1
Show file tree
Hide file tree
Showing 17 changed files with 640 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .github/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ magento:
install:
composer config --json repositories.local '{"type": "path", "url": "/data/extensions/workdir", "options": { "symlink": false } }'
composer require "adyen/module-payment:*"
vendor/bin/phpcs --standard=Magento2 --extensions=php,phtml --error-severity=10 --ignore-annotations -n -p vendor/adyen/module-payment
vendor/bin/phpcs --standard=Magento2 --extensions=php,phtml --error-severity=10 -n -p vendor/adyen/module-payment
bin/magento module:enable Adyen_Payment
bin/magento setup:upgrade
bin/magento setup:di:compile
Expand Down
5 changes: 5 additions & 0 deletions Gateway/Data/Order/OrderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function __construct(
parent::__construct($order, $addressAdapterFactory);
}

/**
* @deprecated Use Magento\Sales\Api\Data\OrderInterface::getQuoteId() instead
*
* @return float|int|null
*/
public function getQuoteId()
{
return $this->order->getQuoteId();
Expand Down
57 changes: 26 additions & 31 deletions Gateway/Request/CaptureDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Data as DataHelper;
use Adyen\Payment\Helper\OpenInvoice;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\ResourceModel\Order\Payment;
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Request\BuilderInterface;
Expand All @@ -33,40 +34,36 @@
*/
class CaptureDataBuilder implements BuilderInterface
{
private DataHelper $adyenHelper;
private ChargedCurrency $chargedCurrency;
private Payment $orderPaymentResourceModel;
private AdyenOrderPayment $adyenOrderPaymentHelper;
private AdyenLogger $adyenLogger;
private Context $context;
protected OpenInvoice $openInvoiceHelper;

/**
* @param DataHelper $adyenHelper
* @param ChargedCurrency $chargedCurrency
* @param AdyenOrderPayment $adyenOrderPaymentHelper
* @param AdyenLogger $adyenLogger
* @param Context $context
* @param Payment $orderPaymentResourceModel
* @param OpenInvoice $openInvoiceHelper
* @param PaymentMethods $paymentMethodsHelper
*/
public function __construct(
DataHelper $adyenHelper,
ChargedCurrency $chargedCurrency,
AdyenOrderPayment $adyenOrderPaymentHelper,
AdyenLogger $adyenLogger,
Context $context,
Payment $orderPaymentResourceModel,
OpenInvoice $openInvoiceHelper
) {
$this->adyenHelper = $adyenHelper;
$this->chargedCurrency = $chargedCurrency;
$this->adyenOrderPaymentHelper = $adyenOrderPaymentHelper;
$this->adyenLogger = $adyenLogger;
$this->context = $context;
$this->orderPaymentResourceModel = $orderPaymentResourceModel;
$this->openInvoiceHelper = $openInvoiceHelper;
}
private readonly DataHelper $adyenHelper,
private readonly ChargedCurrency $chargedCurrency,
private readonly AdyenOrderPayment $adyenOrderPaymentHelper,
private readonly AdyenLogger $adyenLogger,
private readonly Context $context,
private readonly Payment $orderPaymentResourceModel,
protected readonly OpenInvoice $openInvoiceHelper,
private readonly PaymentMethods $paymentMethodsHelper
) { }

/**
* @throws AdyenException
* @throws AdyenException|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();
/** @var Invoice $latestInvoice */
Expand All @@ -78,7 +75,6 @@ public function build(array $buildSubject): array
$orderAmountCents = $this->adyenHelper->formatAmount($orderAmountCurrency->getAmount(), $currency);

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

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

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

$captureAmountCents = $this->adyenHelper->formatAmount($captureAmount, $currency);
$paymentMethodInstance = $payment->getMethodInstance();
$captureData = [];
$counterAmount = 0;
$i = 0;
Expand Down Expand Up @@ -174,9 +171,7 @@ public function buildPartialOrMultipleCaptureData($payment, $currency, $adyenOrd
"paymentPspReference" => $adyenOrderPayment[OrderPaymentInterface::PSPREFRENCE]
];

if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod(
$adyenOrderPayment[OrderPaymentInterface::PAYMENT_METHOD]
)) {
if ($this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance)) {
$order = $payment->getOrder();
$invoices = $order->getInvoiceCollection();
// The latest invoice will contain only the selected items(and quantities) for the (partial) capture
Expand Down
60 changes: 14 additions & 46 deletions Gateway/Request/CheckoutDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Helper\StateData;
use Adyen\Payment\Helper\OpenInvoice;
use Adyen\Payment\Model\Config\Source\ThreeDSFlow;
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Payment\Observer\AdyenCcDataAssignObserver;
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Helper\SubjectReader;
Expand All @@ -36,38 +38,9 @@ class CheckoutDataBuilder implements BuilderInterface
self::ADYEN_BOLETO
];

/**
* @var Data
*/
private Data $adyenHelper;

/**
* @var CartRepositoryInterface
*/
private CartRepositoryInterface $cartRepository;

/**
* @var ChargedCurrency
*/
private ChargedCurrency $chargedCurrency;

/**
* @var StateData
*/
private StateData $stateData;

/**
* @var Config
*/
private Config $configHelper;

/**
* @var OpenInvoice
*/
private OpenInvoice $openInvoiceHelper;

/**
* CheckoutDataBuilder constructor.
*
* @param Data $adyenHelper
* @param StateData $stateData
* @param CartRepositoryInterface $cartRepository
Expand All @@ -76,31 +49,26 @@ class CheckoutDataBuilder implements BuilderInterface
* @param OpenInvoice $openInvoiceHelper
*/
public function __construct(
Data $adyenHelper,
StateData $stateData,
CartRepositoryInterface $cartRepository,
ChargedCurrency $chargedCurrency,
Config $configHelper,
OpenInvoice $openInvoiceHelper
) {
$this->adyenHelper = $adyenHelper;
$this->stateData = $stateData;
$this->cartRepository = $cartRepository;
$this->chargedCurrency = $chargedCurrency;
$this->configHelper = $configHelper;
$this->openInvoiceHelper = $openInvoiceHelper;
}
private readonly Data $adyenHelper,
private readonly StateData $stateData,
private readonly CartRepositoryInterface $cartRepository,
private readonly ChargedCurrency $chargedCurrency,
private readonly Config $configHelper,
private readonly OpenInvoice $openInvoiceHelper,
private readonly PaymentMethods $paymentMethodsHelper
) { }

/**
* @param array $buildSubject
* @return array
* @throws NoSuchEntityException
* @throws NoSuchEntityException|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();
$storeId = $order->getStoreId();
Expand Down Expand Up @@ -130,7 +98,7 @@ public function build(array $buildSubject): array

$brandCode = $payment->getAdditionalInformation(AdyenPaymentMethodDataAssignObserver::BRAND_CODE);
if (
(isset($brandCode) && $this->adyenHelper->isPaymentMethodOpenInvoiceMethod($brandCode)) ||
$this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance) ||
$payment->getMethod() === AdyenPayByLinkConfigProvider::CODE
) {
$openInvoiceFields = $this->openInvoiceHelper->getOpenInvoiceDataForOrder($order);
Expand Down
78 changes: 40 additions & 38 deletions Gateway/Request/RefundDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace Adyen\Payment\Gateway\Request;

use Adyen\Payment\Api\Data\OrderPaymentInterface;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\OpenInvoice;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory;
use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory as PaymentCollectionFactory;
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
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\Payment;
Expand All @@ -31,46 +34,50 @@ class RefundDataBuilder implements BuilderInterface
const REFUND_STRATEGY_DESCENDING_ORDER = '2';
const REFUND_STRATEGY_BASED_ON_RATIO = '3';

private Data $adyenHelper;
private Config $configHelper;
private PaymentCollectionFactory $orderPaymentCollectionFactory;
private ChargedCurrency $chargedCurrency;
private OpenInvoice $openInvoiceHelper;

/**
* @param Data $adyenHelper
* @param PaymentCollectionFactory $orderPaymentCollectionFactory
* @param ChargedCurrency $chargedCurrency
* @param Config $configHelper
* @param OpenInvoice $openInvoiceHelper
* @param PaymentMethods $paymentMethodsHelper
*/
public function __construct(
Data $adyenHelper,
PaymentCollectionFactory $orderPaymentCollectionFactory,
ChargedCurrency $chargedCurrency,
Config $configHelper,
OpenInvoice $openInvoiceHelper
) {
$this->adyenHelper = $adyenHelper;
$this->orderPaymentCollectionFactory = $orderPaymentCollectionFactory;
$this->chargedCurrency = $chargedCurrency;
$this->configHelper = $configHelper;
$this->openInvoiceHelper = $openInvoiceHelper;
}

private readonly Data $adyenHelper,
private readonly PaymentCollectionFactory $orderPaymentCollectionFactory,
private readonly ChargedCurrency $chargedCurrency,
private readonly Config $configHelper,
private readonly OpenInvoice $openInvoiceHelper,
private readonly PaymentMethods $paymentMethodsHelper
) { }

/**
* @param array $buildSubject
* @return array
* @throws LocalizedException
*/
public function build(array $buildSubject): array
{
/** @var PaymentDataObject $paymentDataObject */
$paymentDataObject = SubjectReader::readPayment($buildSubject);

$order = $paymentDataObject->getOrder();
/** @var Payment $payment */
/** @var Payment $payment */
$payment = $paymentDataObject->getPayment();
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($payment->getOrder(), false);
$order = $payment->getOrder();
$paymentMethodInstance = $payment->getMethodInstance();
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order, false);

// Construct AdyenAmountCurrency from creditmemo
$creditMemo = $payment->getCreditMemo();
$creditMemoAmountCurrency = $this->chargedCurrency->getCreditMemoAmountCurrency($creditMemo, false);
$creditMemoAmountCurrency = $this->chargedCurrency->getCreditMemoAmountCurrency($creditMemo);

$pspReference = $payment->getCcTransId();
$currency = $creditMemoAmountCurrency->getCurrencyCode();
$amount = $creditMemoAmountCurrency->getAmount();


//Get Merchant Account
$storeId = $order ->getStoreId();
$storeId = $order->getStoreId();
$method = $payment->getMethod();

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

// partial refund if multiple payments check refund strategy
if ($orderPaymentCollection->getSize() > self::REFUND_STRATEGY_ASCENDING_ORDER) {
if ($orderPaymentCollection->getSize() > 1) {
$refundStrategy = $this->configHelper->getAdyenAbstractConfigData(
'partial_payments_refund_strategy',
$storeId
Expand Down Expand Up @@ -141,7 +148,7 @@ public function build(array $buildSubject): array
$requestBody[] = [
"merchantAccount" => $merchantAccount,
"amount" => $modificationAmountObject,
"reference" => $payment->getOrder()->getIncrementId(),
"reference" => $order->getIncrementId(),
"paymentPspReference" => $partialPayment->getPspreference(),
];
}
Expand All @@ -155,32 +162,27 @@ public function build(array $buildSubject): array
[
"merchantAccount" => $merchantAccount,
"amount" => $modificationAmount,
"reference" => $payment->getOrder()->getIncrementId(),
"reference" => $order->getIncrementId(),
"paymentPspReference" => $pspReference,
]
];

$brandCode = $payment->getAdditionalInformation(
AdyenPaymentMethodDataAssignObserver::BRAND_CODE
);

if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod($brandCode)) {
if ($this->paymentMethodsHelper->isOpenInvoice($paymentMethodInstance)) {
$openInvoiceFieldsCreditMemo = $this->openInvoiceHelper->getOpenInvoiceDataForCreditMemo($creditMemo);
//There is only one payment, so we add the fields to the first(and only) result
$requestBody[0] = array_merge($requestBody[0], $openInvoiceFieldsCreditMemo);
$requestBody[0] = array_merge($requestBody[0], $openInvoiceFieldsCreditMemo);
}
}

$request['clientConfig'] = ["storeId" => $payment->getOrder()->getStoreId()];
$request['clientConfig'] = ["storeId" => $storeId];
$request['body'] = $requestBody;

$request['headers'] = [
'idempotencyExtraData' => [
'totalRefunded' => $payment->getOrder()->getTotalRefunded() ?? 0
'totalRefunded' => $order->getTotalRefunded() ?? 0
]
];

return $request;
}

}
2 changes: 2 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ public function getModuleVersion()
}

/**
* @deprecated Use Adyen\Payment\Helper\PaymentMethods::isOpenInvoice() instead.
*
* @param $paymentMethod
* @return bool
*/
Expand Down
Loading

0 comments on commit 830d5e1

Please sign in to comment.