We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) 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
*/
private $orderPaymentCollectionFactory;
/**
* @var \Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory
*/
protected $adyenInvoiceCollectionFactory;
/**
* RefundDataBuilder constructor.
* @param \Adyen\Payment\Helper\Data $adyenHelper
......@@ -48,11 +53,12 @@ class RefundDataBuilder implements BuilderInterface
*/
public function __construct(
\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->orderPaymentCollectionFactory = $orderPaymentCollectionFactory;
$this->adyenInvoiceCollectionFactory = $adyenInvoiceCollectionFactory;
}
/**
......@@ -119,7 +125,7 @@ class RefundDataBuilder implements BuilderInterface
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) {
$modificationAmount = $splitPaymentAmount;
} else {
......@@ -155,8 +161,81 @@ class RefundDataBuilder implements BuilderInterface
"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;
}
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