Skip to content

Commit

Permalink
[ECP-9489] Implement AdyenCreditmemoRepository and deprecated old met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
Can Demiralp committed Jan 15, 2025
1 parent 46b299a commit 4203483
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 156 deletions.
66 changes: 66 additions & 0 deletions Api/Repository/AdyenCreditmemoRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2025 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <[email protected]>
*/

namespace Adyen\Payment\Api\Repository;

use Adyen\Payment\Api\Data\CreditmemoInterface;
use Adyen\Payment\Api\Data\NotificationInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

interface AdyenCreditmemoRepositoryInterface
{
/**
* Retrieve adyen_creditmemo entity by the ID.
*
* @param int $entityId
* @return CreditmemoInterface Gift message.
* @throws NoSuchEntityException
*/
public function get(int $entityId): CreditmemoInterface;

/**
* Retrieve adyen_creditmemo entities by `adyen_order_payment_id`.
*
* @param int $adyenOrderPaymentId
* @return CreditmemoInterface[]|null
*/
public function getByAdyenOrderPaymentId(int $adyenOrderPaymentId): ?array;

/**
* Retrieve adyen_creditmemo entity by the given notification using the `pspreference` column.
*
* @param NotificationInterface $notification
* @return CreditmemoInterface|null
*/
public function getByRefundWebhook(NotificationInterface $notification): ?CreditmemoInterface;

/**
* Retrieve adyen_creditmemo entities which match a specified criteria.
*
* @param SearchCriteriaInterface $searchCriteria
* @return SearchResultsInterface
*
* @throws LocalizedException
*/
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface;

/**
* Performs persist operations for a specified adyen_creditmemo.
*
* @param CreditmemoInterface $entity adyen_creditmemo entity.
* @return CreditmemoInterface
*/
public function save(CreditmemoInterface $entity): CreditmemoInterface;
}
89 changes: 33 additions & 56 deletions Helper/Creditmemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@

namespace Adyen\Payment\Helper;

use Adyen\Payment\Api\Data\CreditmemoInterface;
use Adyen\Payment\Api\Data\OrderPaymentInterface;
use Adyen\Payment\Api\Repository\AdyenCreditmemoRepositoryInterface;
use Adyen\Payment\Model\Order\Payment;
use Adyen\Payment\Model\ResourceModel\Creditmemo\Creditmemo as CreditMemoResourceModel;
use Adyen\Payment\Model\CreditmemoFactory;
use Adyen\Payment\Model\Creditmemo as AdyenCreditmemoModel;
use Adyen\Payment\Model\ResourceModel\Order\Payment as OrderPaymentResourceModel;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Creditmemo as MagentoCreditMemoModel;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Sales\Model\Order\CreditmemoFactory as MagentoCreditMemoFactory;
use Magento\Sales\Model\ResourceModel\Order\Creditmemo as MagentoCreditMemoResourceModel;

/**
* Helper class for anything related to the creditmemo entity
Expand All @@ -34,46 +32,22 @@
class Creditmemo extends AbstractHelper
{
/**
* @var Data
*/
protected $adyenDataHelper;

/**
* @var OrderPaymentResourceModel
*/
protected $orderPaymentResourceModel;

/**
* @var CreditmemoResourceModel
*/
private $adyenCreditmemoResourceModel;

/**
* @var CreditmemoFactory
*/
private $adyenCreditmemoFactory;

/**
* Creditmemo constructor.
* @param Context $context
* @param Data $adyenDataHelper
* @param CreditmemoFactory $adyenCreditmemoFactory
* @param CreditmemoResourceModel $adyenCreditmemoResourceModel
* @param CreditMemoResourceModel $adyenCreditmemoResourceModel
* @param OrderPaymentResourceModel $orderPaymentResourceModel
* @param AdyenCreditmemoRepositoryInterface $adyenCreditmemoRepository
*/
public function __construct(
Context $context,
Data $adyenDataHelper,
CreditmemoFactory $adyenCreditmemoFactory,
CreditmemoResourceModel $adyenCreditmemoResourceModel,
OrderPaymentResourceModel $orderPaymentResourceModel
)
{
protected Data $adyenDataHelper,
private readonly CreditmemoFactory $adyenCreditmemoFactory,
private readonly CreditmemoResourceModel $adyenCreditmemoResourceModel,
protected OrderPaymentResourceModel $orderPaymentResourceModel,
private readonly AdyenCreditmemoRepositoryInterface $adyenCreditmemoRepository
) {
parent::__construct($context);
$this->adyenDataHelper = $adyenDataHelper;
$this->adyenCreditmemoFactory = $adyenCreditmemoFactory;
$this->adyenCreditmemoResourceModel = $adyenCreditmemoResourceModel;
$this->orderPaymentResourceModel = $orderPaymentResourceModel;
}

/**
Expand All @@ -84,7 +58,6 @@ public function __construct(
* @param string $originalReference
* @param float $refundAmount
* @return AdyenCreditmemoModel
* @throws AlreadyExistsException
*/
public function createAdyenCreditMemo(
Order\Payment $payment,
Expand All @@ -110,59 +83,63 @@ public function createAdyenCreditMemo(
$adyenCreditmemo->setAmount($refundAmount);
$adyenCreditmemo->setStatus(AdyenCreditmemoModel::WAITING_FOR_WEBHOOK_STATUS);

$this->adyenCreditmemoResourceModel->save($adyenCreditmemo);
$this->adyenCreditmemoRepository->save($adyenCreditmemo);

return $adyenCreditmemo;
}

/**
* Link all the adyen_creditmemos related to the adyen_order_payment with the given magento entity of the creditmemo
* @throws AlreadyExistsException
*
* @param Payment $adyenOrderPayment
* @param MagentoCreditMemoModel $magentoCreditmemo
* @return void
*/
public function linkAndUpdateAdyenCreditmemos(
Payment $adyenOrderPayment,
MagentoCreditmemoModel $magentoCreditmemo
): void {
$adyenCreditmemoLoader = $this->adyenCreditmemoFactory->create();

$adyenCreditmemos = $this->adyenCreditmemoResourceModel->getAdyenCreditmemosByAdyenPaymentid(
$adyenCreditmemos = $this->adyenCreditmemoRepository->getByAdyenOrderPaymentId(
$adyenOrderPayment->getEntityId()
);

if (isset($adyenCreditmemos)) {
foreach ($adyenCreditmemos as $adyenCreditmemo) {
/** @var AdyenCreditmemoModel $currAdyenCreditmemo */
$currAdyenCreditmemo = $adyenCreditmemoLoader->load(
$adyenCreditmemo[CreditmemoInterface::ENTITY_ID],
CreditmemoInterface::ENTITY_ID
);

if ($currAdyenCreditmemo->getCreditmemoId() !== null) {
continue;
}
if ($adyenCreditmemo->getAmount() == $magentoCreditmemo->getGrandTotal()) {
$adyenCreditmemo->setCreditmemoId($magentoCreditmemo->getEntityId());
$this->adyenCreditmemoRepository->save($adyenCreditmemo);

if ($currAdyenCreditmemo->getAmount() == $magentoCreditmemo->getGrandTotal()) {
$currAdyenCreditmemo->setCreditmemoId($magentoCreditmemo->getEntityId());
$this->adyenCreditmemoResourceModel->save($currAdyenCreditmemo);
break;
}
}
}
}

/**
* @param AdyenCreditmemoModel $adyenCreditmemo
* @param string $status
* @return void
*/
public function updateAdyenCreditmemosStatus(AdyenCreditmemoModel $adyenCreditmemo, string $status)
{
$adyenCreditmemo->setStatus($status);
$this->adyenCreditmemoResourceModel->save($adyenCreditmemo);
$this->adyenCreditmemoRepository->save($adyenCreditmemo);
}

/**
* @deprecated Use AdyenCreditmemoRepositoryInterface::getByRefundWebhook() instead.
*
* @param string $pspreference
* @return AdyenCreditmemoModel|null
* @throws NoSuchEntityException
*/
public function getAdyenCreditmemoByPspreference(string $pspreference): ?AdyenCreditmemoModel {
$results = $this->adyenCreditmemoResourceModel->getAdyenCreditmemoByPspreference($pspreference);

if (is_null($results)) {
return null;
}

return $this->adyenCreditmemoFactory->create()->load($results['entity_id']);
return $this->adyenCreditmemoRepository->get($results['entity_id']);
}
}
Loading

0 comments on commit 4203483

Please sign in to comment.