We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit 9eff8baa authored by Alessio Zampatti's avatar Alessio Zampatti Committed by GitHub

PW-455: Support partial refunds for open invoice payment methods (#278)

* PW-455: Support partial refunds for open invoice payment methods

* PW-455: Use Plugin(Interceptor) instead of Event and Observer

* PW-455: Removed observer reference

* PW-455: Refactor

* PW-455: Refactor

* PW-455: Removed plugin and added check on invoice
parent 41143b07
...@@ -41,6 +41,11 @@ class RefundDataBuilder implements BuilderInterface ...@@ -41,6 +41,11 @@ class RefundDataBuilder implements BuilderInterface
*/ */
private $orderPaymentCollectionFactory; private $orderPaymentCollectionFactory;
/**
* @var \Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory
*/
protected $adyenInvoiceCollectionFactory;
/** /**
* RefundDataBuilder constructor. * RefundDataBuilder constructor.
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
...@@ -48,11 +53,12 @@ class RefundDataBuilder implements BuilderInterface ...@@ -48,11 +53,12 @@ class RefundDataBuilder implements BuilderInterface
*/ */
public function __construct( public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory \Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory,
) \Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory $adyenInvoiceCollectionFactory
{ ) {
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->orderPaymentCollectionFactory = $orderPaymentCollectionFactory; $this->orderPaymentCollectionFactory = $orderPaymentCollectionFactory;
$this->adyenInvoiceCollectionFactory = $adyenInvoiceCollectionFactory;
} }
/** /**
...@@ -63,7 +69,7 @@ class RefundDataBuilder implements BuilderInterface ...@@ -63,7 +69,7 @@ class RefundDataBuilder implements BuilderInterface
{ {
/** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */ /** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
$paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject); $paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
$amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject); $amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject);
$order = $paymentDataObject->getOrder(); $order = $paymentDataObject->getOrder();
$payment = $paymentDataObject->getPayment(); $payment = $paymentDataObject->getPayment();
...@@ -96,7 +102,7 @@ class RefundDataBuilder implements BuilderInterface ...@@ -96,7 +102,7 @@ class RefundDataBuilder implements BuilderInterface
$orderPaymentCollection->addPaymentFilterDescending($payment->getId()); $orderPaymentCollection->addPaymentFilterDescending($payment->getId());
} elseif ($refundStrategy == "3") { } elseif ($refundStrategy == "3") {
// refund based on ratio // refund based on ratio
$ratio = $amount / $grandTotal; $ratio = $amount / $grandTotal;
$orderPaymentCollection->addPaymentFilterAscending($payment->getId()); $orderPaymentCollection->addPaymentFilterAscending($payment->getId());
} }
...@@ -109,7 +115,7 @@ class RefundDataBuilder implements BuilderInterface ...@@ -109,7 +115,7 @@ class RefundDataBuilder implements BuilderInterface
// refund based on ratio calculate refund amount // refund based on ratio calculate refund amount
$modificationAmount = $ratio * ( $modificationAmount = $ratio * (
$splitPayment->getAmount() - $splitPayment->getTotalRefunded() $splitPayment->getAmount() - $splitPayment->getTotalRefunded()
); );
} else { } else {
// total authorised amount of the split payment // total authorised amount of the split payment
$splitPaymentAmount = $splitPayment->getAmount() - $splitPayment->getTotalRefunded(); $splitPaymentAmount = $splitPayment->getAmount() - $splitPayment->getTotalRefunded();
...@@ -119,7 +125,7 @@ class RefundDataBuilder implements BuilderInterface ...@@ -119,7 +125,7 @@ class RefundDataBuilder implements BuilderInterface
continue; continue;
} }
// if refunded amount is greather then split payment amount do a full refund // if refunded amount is greater than split payment amount do a full refund
if ($amount >= $splitPaymentAmount) { if ($amount >= $splitPaymentAmount) {
$modificationAmount = $splitPaymentAmount; $modificationAmount = $splitPaymentAmount;
} else { } else {
...@@ -155,8 +161,81 @@ class RefundDataBuilder implements BuilderInterface ...@@ -155,8 +161,81 @@ class RefundDataBuilder implements BuilderInterface
"merchantAccount" => $merchantAccount "merchantAccount" => $merchantAccount
] ]
]; ];
$brandCode = $payment->getAdditionalInformation(
\Adyen\Payment\Observer\AdyenHppDataAssignObserver::BRAND_CODE
);
if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod($brandCode)) {
$openInvoiceFields = $this->getOpenInvoiceData($payment);
//There is only one payment, so we add the fields to the first(and only) result
$result[0]["additionalData"] = $openInvoiceFields;
}
} }
return $result; return $result;
} }
protected function getOpenInvoiceData($payment)
{
$formFields = [];
$count = 0;
$currency = $payment->getOrder()->getOrderCurrencyCode();
/**
* Magento\Sales\Model\Order\Creditmemo
*/
$creditMemo = $payment->getCreditMemo();
foreach ($creditMemo->getAllItems() as $refundItem) {
++$count;
$numberOfItems = $refundItem->getQty();
$formFields = $this->adyenHelper->createOpenInvoiceLineItem(
$formFields,
$count,
$refundItem->getName(),
$refundItem->getPrice(),
$currency,
$refundItem->getTaxAmount(),
$refundItem->getPriceInclTax(),
$refundItem->getTaxPercent(),
$numberOfItems,
$payment
);
}
// Shipping cost
if ($creditMemo->getShippingAmount() > 0) {
++$count;
$formFields = $this->adyenHelper->createOpenInvoiceLineShipping(
$formFields,
$count,
$payment->getOrder(),
$creditMemo->getShippingAmount(),
$creditMemo->getShippingTaxAmount(),
$currency,
$payment
);
}
$formFields['openinvoicedata.numberOfLines'] = $count;
//Retrieve acquirerReference from the adyen_invoice
$invoiceId = $creditMemo->getInvoice()->getId();
$invoices = $this->adyenInvoiceCollectionFactory->create()
->addFieldToFilter('invoice_id', $invoiceId);
$invoice = $invoices->getFirstItem();
if($invoice) {
$formFields['acquirerReference'] = $invoice->getAcquirerReference();
}
return $formFields;
}
} }
\ No newline at end of file
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment Module
*
* Copyright (c) 2018 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Model\ResourceModel\Invoice;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Construct
*/
public function _construct()
{
$this->_init('Adyen\Payment\Model\Invoice', 'Adyen\Payment\Model\ResourceModel\Invoice');
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment