Skip to content

Commit

Permalink
[ECP-9078] Implement is_open_invoice payment method configuration field
Browse files Browse the repository at this point in the history
  • Loading branch information
Can Demiralp committed Jan 20, 2025
1 parent 37143ce commit 3f312e6
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 171 deletions.
57 changes: 27 additions & 30 deletions Gateway/Request/CaptureDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
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;
use Magento\Payment\Model\MethodInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;

Expand All @@ -33,40 +36,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 +77,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 +114,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 +137,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 +173,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
51 changes: 25 additions & 26 deletions Gateway/Request/RefundDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
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\Helper\SubjectReader;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Sales\Model\Order\Payment;
Expand All @@ -31,33 +32,36 @@ 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
{
$paymentDataObject = SubjectReader::readPayment($buildSubject);

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

// Construct AdyenAmountCurrency from creditmemo
Expand Down Expand Up @@ -160,11 +164,7 @@ public function build(array $buildSubject): array
]
];

$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);
Expand All @@ -182,5 +182,4 @@ public function build(array $buildSubject): array

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
30 changes: 25 additions & 5 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PaymentMethods extends AbstractHelper
const FUNDING_SOURCE_CREDIT = 'credit';

const ADYEN_GROUP_ALTERNATIVE_PAYMENT_METHODS = 'adyen-alternative-payment-method';
const CONFIG_FIELD_IS_OPEN_INVOICE = 'is_open_invoice';

const VALID_CHANNELS = ["iOS", "Android", "Web"];

Expand Down Expand Up @@ -638,7 +639,7 @@ protected function showLogosPaymentMethods(array $paymentMethods, array $payment

$paymentMethodsExtraDetails[$paymentMethodCode]['icon'] = $icon;

//todo check if it is needed
// TODO::This field is not relevant anymore and can be removed during cleaning-up deprecated methods.
// check if payment method is an open invoice method
$paymentMethodsExtraDetails[$paymentMethodCode]['isOpenInvoice'] =
$this->adyenHelper->isPaymentMethodOpenInvoiceMethod($paymentMethodCode);
Expand Down Expand Up @@ -767,12 +768,20 @@ public function getCcAvailableTypesByAlt(): array
}

/**
* @param Order $order
* @param string $notificationPaymentMethod
* Checks whether if the capture mode is auto on an order with the given notification `paymentMethod`.
* Note that, only a `notificationPaymentMethod` related to the order should be provided.
*
* @param Order $order Order object
* @param string $notificationPaymentMethod `paymentMethod` provided on the webhook of the given order
* @return bool
*/
public function isAutoCapture(Order $order, string $notificationPaymentMethod): bool
{
// TODO::Add a validation checking `$notificationPaymentMethod` belongs to the correct order (webhook) or not.

$payment = $order->getPayment();
$paymentMethodInstance = $payment->getMethodInstance();

// validate if payment methods allows manual capture
if (PaymentMethodUtil::isManualCaptureSupported($notificationPaymentMethod)) {
$captureMode = trim(
Expand Down Expand Up @@ -854,7 +863,7 @@ public function isAutoCapture(Order $order, string $notificationPaymentMethod):
}

// if auto capture mode for openinvoice is turned on then use auto capture
if ($autoCaptureOpenInvoice && $this->adyenHelper->isPaymentMethodOpenInvoiceMethod($notificationPaymentMethod)) {
if ($autoCaptureOpenInvoice && $this->isOpenInvoice($paymentMethodInstance)) {
$this->adyenLogger->addAdyenNotification(
'This payment method is configured to be working as auto capture ',
array_merge(
Expand Down Expand Up @@ -905,7 +914,7 @@ public function isAutoCapture(Order $order, string $notificationPaymentMethod):
* online capture after delivery, use Magento backend to online invoice
* (if the option auto capture mode for openinvoice is not set)
*/
if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod($notificationPaymentMethod)) {
if ($this->isOpenInvoice($paymentMethodInstance)) {
$this->adyenLogger->addAdyenNotification(
'Capture mode for klarna is by default set to manual',
array_merge(
Expand Down Expand Up @@ -1061,4 +1070,15 @@ public function buildPaymentMethodIcon(string $paymentMethodCode, array $params)

return ['url' => $url, 'width' => 77, 'height' => 50];
}

/**
* Checks whether if the payment method is open invoice or not based on `is_open_invoice` configuration field.
*
* @param MethodInterface $paymentMethodInstance
* @return bool
*/
public function isOpenInvoice(MethodInterface $paymentMethodInstance): bool
{
return boolval($paymentMethodInstance->getConfigData(self::CONFIG_FIELD_IS_OPEN_INVOICE));
}
}
Loading

0 comments on commit 3f312e6

Please sign in to comment.