-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECP-9489] Implement AdyenOrderPaymentRepository
- Loading branch information
Can Demiralp
committed
Jan 17, 2025
1 parent
894b9e3
commit 9215392
Showing
9 changed files
with
264 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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\OrderPaymentInterface; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Framework\Api\SearchResultsInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
|
||
interface AdyenOrderPaymentRepositoryInterface | ||
{ | ||
const AVAILABLE_CAPTURE_STATUSES = [ | ||
OrderPaymentInterface::CAPTURE_STATUS_AUTO_CAPTURE, | ||
OrderPaymentInterface::CAPTURE_STATUS_MANUAL_CAPTURE, | ||
OrderPaymentInterface::CAPTURE_STATUS_PARTIAL_CAPTURE, | ||
OrderPaymentInterface::CAPTURE_STATUS_NO_CAPTURE | ||
]; | ||
|
||
/** | ||
* Retrieve adyen_order_payment entity by the ID. | ||
* | ||
* @param int $entityId | ||
* @return OrderPaymentInterface Adyen order payment entity | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function get(int $entityId): OrderPaymentInterface; | ||
|
||
/** | ||
* Retrieve adyen_order_payment entities by `payment_id`. | ||
* | ||
* @param int $paymentId | ||
* @param array $captureStatuses | ||
* @return OrderPaymentInterface[]|null | ||
*/ | ||
public function getByPaymentId(int $paymentId, array $captureStatuses = []): ?array; | ||
|
||
/** | ||
* Retrieve adyen_order_payment entities which match a specified criteria. | ||
* | ||
* @param SearchCriteriaInterface $searchCriteria | ||
* @return SearchResultsInterface | ||
* | ||
* @throws LocalizedException | ||
*/ | ||
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
/** | ||
* | ||
* Adyen Payment module (https://www.adyen.com/) | ||
* | ||
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Adyen <[email protected]> | ||
*/ | ||
|
||
namespace Adyen\Payment\Model; | ||
|
||
use Adyen\AdyenException; | ||
use Adyen\Payment\Api\Data\OrderPaymentInterface; | ||
use Adyen\Payment\Api\Repository\AdyenOrderPaymentRepositoryInterface; | ||
use Adyen\Payment\Logger\AdyenLogger; | ||
use Adyen\Payment\Model\Order\PaymentFactory as AdyenOrderPaymentFactory; | ||
use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory; | ||
use Adyen\Payment\Model\ResourceModel\Order\Payment as AdyenOrderPaymentResourceModel; | ||
use Magento\Framework\Api\FilterBuilder; | ||
use Magento\Framework\Api\Search\FilterGroupBuilder; | ||
use Magento\Framework\Api\Search\SearchResultFactory; | ||
use Magento\Framework\Api\SearchCriteria\CollectionProcessor; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Framework\Api\SearchResultsInterface; | ||
|
||
class AdyenOrderPaymentRepository implements AdyenOrderPaymentRepositoryInterface | ||
{ | ||
/** | ||
* @param AdyenOrderPaymentResourceModel $resourceModel | ||
* @param AdyenOrderPaymentFactory $adyenOrderPaymentFactory | ||
* @param SearchResultFactory $searchResultsFactory | ||
* @param CollectionFactory $collectionFactory | ||
* @param CollectionProcessor $collectionProcessor | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
* @param FilterBuilder $filterBuilder | ||
* @param FilterGroupBuilder $filterGroupBuilder | ||
* @param AdyenLogger $adyenLogger | ||
*/ | ||
public function __construct( | ||
private readonly AdyenOrderPaymentResourceModel $resourceModel, | ||
private readonly AdyenOrderPaymentFactory $adyenOrderPaymentFactory, | ||
private readonly SearchResultFactory $searchResultsFactory, | ||
private readonly CollectionFactory $collectionFactory, | ||
private readonly CollectionProcessor $collectionProcessor, | ||
private readonly SearchCriteriaBuilder $searchCriteriaBuilder, | ||
private readonly FilterBuilder $filterBuilder, | ||
private readonly FilterGroupBuilder $filterGroupBuilder, | ||
private readonly AdyenLogger $adyenLogger | ||
) { } | ||
|
||
/** | ||
* @param SearchCriteriaInterface $searchCriteria | ||
* @return SearchResultsInterface | ||
*/ | ||
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface | ||
{ | ||
$searchResult = $this->searchResultsFactory->create(); | ||
$collection = $this->collectionFactory->create(); | ||
$this->collectionProcessor->process($searchCriteria, $collection); | ||
$searchResult->setItems($collection->getItems()); | ||
$searchResult->setTotalCount($collection->getSize()); | ||
|
||
return $searchResult; | ||
} | ||
|
||
/** | ||
* @param int $paymentId | ||
* @param array $captureStatuses | ||
* @return OrderPaymentInterface[]|null | ||
* @throws AdyenException | ||
*/ | ||
public function getByPaymentId(int $paymentId, array $captureStatuses = []): ?array | ||
{ | ||
$paymentIdFilter = $this->filterBuilder->setField(OrderPaymentInterface::PAYMENT_ID) | ||
->setConditionType('eq') | ||
->setValue($paymentId) | ||
->create(); | ||
|
||
$captureStatusFilters = []; | ||
|
||
if (!empty($captureStatuses)) { | ||
$this->validateCaptureStatuses($captureStatuses); | ||
|
||
foreach ($captureStatuses as $captureStatus) { | ||
$captureStatusFilters[] = $this->filterBuilder->setField(OrderPaymentInterface::CAPTURE_STATUS) | ||
->setConditionType('eq') | ||
->setValue($captureStatus) | ||
->create(); | ||
} | ||
} | ||
|
||
/* | ||
* Create two different filter groups for logical AND operation between `payment_id` and `capture_status` | ||
* fields. Filter group `$captureStatusFilters` provides logical OR between `capture_status` values. | ||
*/ | ||
$paymentIdFilterGroup = $this->filterGroupBuilder->setFilters([$paymentIdFilter])->create(); | ||
$captureStatusFilterGroup = $this->filterGroupBuilder->setFilters($captureStatusFilters)->create(); | ||
|
||
$searchCriteria = $this->searchCriteriaBuilder | ||
->setFilterGroups([$paymentIdFilterGroup, $captureStatusFilterGroup]) | ||
->create(); | ||
|
||
return $this->getList($searchCriteria)->getItems(); | ||
} | ||
|
||
/** | ||
* @param int $entityId | ||
* @return OrderPaymentInterface | ||
*/ | ||
public function get(int $entityId): OrderPaymentInterface | ||
{ | ||
$entity = $this->adyenOrderPaymentFactory->create(); | ||
$this->resourceModel->load($entity, $entityId, 'entity_id'); | ||
|
||
return $entity; | ||
} | ||
|
||
/** | ||
* @param array $captureStatuses | ||
* @return void | ||
* @throws AdyenException | ||
*/ | ||
private function validateCaptureStatuses(array $captureStatuses): void | ||
{ | ||
foreach ($captureStatuses as $captureStatus) { | ||
if (!array_contains($captureStatuses, $captureStatus)) { | ||
$message = sprintf( | ||
"Invalid capture status %s has been provided for adyen_order_payment repository!", | ||
$captureStatus | ||
); | ||
|
||
$this->adyenLogger->error($message); | ||
throw new AdyenException($message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.