Skip to content

Commit

Permalink
MOL-969: fix new refund storage system and also delete refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Sep 28, 2023
1 parent c1d335f commit 681ec45
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 65 deletions.
7 changes: 7 additions & 0 deletions src/Components/RefundManager/Builder/RefundDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public function buildRefundData(OrderEntity $order, Context $context): RefundDat


try {

# **********************************************************************************
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# ATTENTION, this will load the refunds from Mollie, but also from the database
# we will add our database data to the Mollie metadata.composition and therefore "fake" a response of Mollie,
# so that we can reuse the old code from below, even though Mollie does not really have a metadata.composition.
$refunds = $this->refundService->getRefunds($order, $context);
} catch (PaymentNotFoundException $ex) {
# if we dont have a payment, then theres also no refunds
Expand Down
3 changes: 3 additions & 0 deletions src/Components/RefundManager/DAL/Refund/RefundDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
Expand Down Expand Up @@ -59,6 +60,8 @@ protected function defineFields(): FieldCollection

(new StringField('mollie_refund_id', 'mollieRefundId')),

(new StringField('type', 'type')),

new LongTextField('public_description', 'publicDescription'),
new LongTextField('internal_description', 'internalDescription'),
new OneToManyAssociationField('refundItems', RefundItemDefinition::class, 'refund_id')
Expand Down
19 changes: 19 additions & 0 deletions src/Components/RefundManager/DAL/Refund/RefundEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class RefundEntity extends Entity
*/
protected $mollieRefundId;

/**
* @var null|string
*/
protected $type;

/**
* @var null|string
*/
Expand All @@ -42,6 +47,10 @@ class RefundEntity extends Entity
*/
protected $refundItems;


/**
*
*/
public function __construct()
{
$this->refundItems = new RefundItemCollection();
Expand Down Expand Up @@ -99,6 +108,16 @@ public function setMollieRefundId(?string $mollieRefundId): void
$this->mollieRefundId = $mollieRefundId;
}

public function getType(): ?string
{
return $this->type;
}

public function setType(?string $type): void
{
$this->type = $type;
}

/**
* @return null|string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey(), new ApiAware()),
(new StringField('type', 'type'))->addFlags(new Required(), new ApiAware()),
(new FkField('refund_id', 'refundId', RefundDefinition::class))->addFlags(new ApiAware()),
(new ManyToOneAssociationField('refund', 'refund_id', RefundDefinition::class))->addFlags(new ApiAware()),
(new IntField('quantity', 'quantity'))->addFlags(new Required(), new ApiAware()),
Expand Down
32 changes: 5 additions & 27 deletions src/Components/RefundManager/DAL/RefundItem/RefundItemEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ final class RefundItemEntity extends Entity
{
use EntityIdTrait;

/**
* @var string
*/
protected $type;

/**
* @var string
*/
Expand Down Expand Up @@ -62,21 +57,6 @@ final class RefundItemEntity extends Entity
*/
protected $orderLineItem;

/**
* @return string
*/
public function getType(): string
{
return $this->type;
}

/**
* @param string $type
*/
public function setType(string $type): void
{
$this->type = $type;
}

/**
* @return string
Expand Down Expand Up @@ -223,7 +203,6 @@ public function setOrderLineItemVersionId(?string $orderLineItemVersionId): void
}

/**
* @param string $type
* @param string $mollieLineId
* @param string $label
* @param int $quantity
Expand All @@ -233,22 +212,21 @@ public function setOrderLineItemVersionId(?string $orderLineItemVersionId): void
* @param null|string $refundId
* @return array<mixed>
*/
public static function createEntryArray(string $type, string $mollieLineId, string $label, int $quantity, float $amount, ?string $oderLineItemId, ?string $oderLineItemVersionId, ?string $refundId): array
public static function createArray(string $mollieLineId, string $label, int $quantity, float $amount, ?string $oderLineItemId, ?string $oderLineItemVersionId, ?string $refundId): array
{
$row = [
'type' => $type,
$row = [
'mollieLineId' => $mollieLineId,
'label' => $label,
'quantity' => $quantity,
'amount' => $amount,
'oderLineItemId' => $oderLineItemId,
'oderLineItemVersionId' => $oderLineItemVersionId,
'orderLineItemId' => $oderLineItemId,
'orderLineItemVersionId' => $oderLineItemVersionId,
];

/**
* refundId is not given when we create a new entry because the id is created by shopware dal
*/
if ($refundId !== null) {
if ($refundId !== null && $refundId !== '') {
$row['refundId'] = $refundId;
}

Expand Down
21 changes: 21 additions & 0 deletions src/Components/RefundManager/DAL/Repository/RefundRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;

class RefundRepository implements RefundRepositoryInterface
{
Expand Down Expand Up @@ -43,4 +44,24 @@ public function search(Criteria $criteria, Context $context): EntitySearchResult
{
return $this->mollieRefundRepository->search($criteria, $context);
}

/**
* @param Criteria $criteria
* @param Context $context
* @return IdSearchResult
*/
public function searchIds(Criteria $criteria, Context $context): IdSearchResult
{
return $this->mollieRefundRepository->searchIds($criteria, $context);
}

/**
* @param array<mixed> $ids
* @param Context $context
* @return EntityWrittenContainerEvent
*/
public function delete(array $ids, Context $context): EntityWrittenContainerEvent
{
return $this->mollieRefundRepository->delete($ids, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;

interface RefundRepositoryInterface
{
Expand All @@ -23,4 +24,18 @@ public function create(array $data, Context $context): EntityWrittenContainerEve
* @return EntitySearchResult
*/
public function search(Criteria $criteria, Context $context): EntitySearchResult;

/**
* @param Criteria $criteria
* @param Context $context
* @return IdSearchResult
*/
public function searchIds(Criteria $criteria, Context $context): IdSearchResult;

/**
* @param array<mixed> $ids
* @param Context $context
* @return EntityWrittenContainerEvent
*/
public function delete(array $ids, Context $context): EntityWrittenContainerEvent;
}
56 changes: 48 additions & 8 deletions src/Components/RefundManager/RefundManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory;
use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderFactoryInterface;
use Kiener\MolliePayments\Components\RefundManager\Builder\RefundDataBuilder;
use Kiener\MolliePayments\Components\RefundManager\DAL\Order\OrderExtension;
use Kiener\MolliePayments\Components\RefundManager\DAL\Refund\RefundCollection;
use Kiener\MolliePayments\Components\RefundManager\DAL\Refund\RefundEntity;
use Kiener\MolliePayments\Components\RefundManager\DAL\RefundItem\RefundItemEntity;
use Kiener\MolliePayments\Components\RefundManager\DAL\Repository\RefundRepositoryInterface;
use Kiener\MolliePayments\Components\RefundManager\Integrators\StockManagerInterface;
Expand All @@ -32,6 +35,8 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

class RefundManager implements RefundManagerInterface
{
Expand Down Expand Up @@ -100,9 +105,8 @@ public function __construct(RefundDataBuilder $refundDataBuilder, OrderServiceIn
$this->refundService = $refundService;
$this->stockManager = $stockUpdater;
$this->refundRepository = $refundRepository;
$this->logger = $logger;

$this->flowBuilderEventFactory = $flowBuilderEventFactory;
$this->logger = $logger;

$this->flowBuilderDispatcher = $flowBuilderFactory->createDispatcher();
}
Expand Down Expand Up @@ -171,6 +175,7 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont

if ($orderAttributes->isTypeSubscription()) {
$refundType = RefundItemType::PARTIAL;

# we only have a transaction in the case of a subscription
$refund = $this->refundService->refundPartial(
$order,
Expand All @@ -182,6 +187,7 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont
);
} else {
$refundType = RefundItemType::FULL;

$refund = $this->refundService->refundFull(
$order,
$request->getDescription(),
Expand All @@ -192,8 +198,11 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont
}
} elseif ($request->isFullRefundWithItems($order)) {
$this->appendRoundingItemFromMollieOrder($request, $mollieOrder);

$serviceItems = $this->convertToRefundItems($request, $order, $mollieOrder);

$refundType = RefundItemType::FULL;

$refund = $this->refundService->refundFull(
$order,
$request->getDescription(),
Expand All @@ -203,6 +212,7 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont
);
} elseif ($request->isPartialAmountOnly()) {
$refundType = RefundItemType::PARTIAL;

$refund = $this->refundService->refundPartial(
$order,
$request->getDescription(),
Expand All @@ -213,6 +223,7 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont
);
} elseif ($request->isPartialAmountWithItems($order)) {
$refundType = RefundItemType::PARTIAL;

$refund = $this->refundService->refundPartial(
$order,
$request->getDescription(),
Expand All @@ -223,27 +234,28 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont
);
}

if (! $refund instanceof Refund) {
if (!$refund instanceof Refund) {
# a problem happened, lets finish with an exception
throw new CouldNotCreateMollieRefundException('', (string)$order->getOrderNumber());
}

$refundAmount = (float)$refund->amount->value;



$refundData = [
'orderId' => $order->getId(),
'orderVersionId' => $order->getVersionId(),
'mollieRefundId' => $refund->id,
'type' => $refundType,
'publicDescription' => $request->getDescription(),
'internalDescription' => $request->getInternalDescription(),

];

$refundItems = $this->convertToRepositoryArray($serviceItems, $refundType);
if (count($refundItems) > 0) {
$refundData['refundItems'] = $refundItems;
}

# SAVE LOCAL REFUND
# ---------------------------------------------------------------------------------------------
$this->refundRepository->create([$refundData], $context);
Expand Down Expand Up @@ -297,7 +309,33 @@ public function cancelRefund(string $orderId, string $refundId, Context $context
]
);

return $this->refundService->cancel($order, $refundId);
# first try to cancel on the mollie side
$success = $this->refundService->cancel($order, $refundId);

if (!$success) {
return false;
}


# if mollie worked, also delete our entry from the database
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('mollieRefundId', $refundId));

$foundSwRefunds = $this->refundRepository->searchIds($criteria, $context);

if ($foundSwRefunds->getTotal() === 0) {
return true;
}

$swRefundId = $foundSwRefunds->firstId();

$this->refundRepository->delete([
[
'id' => $swRefundId
],
], $context);

return true;
}

/**
Expand Down Expand Up @@ -463,12 +501,13 @@ private function convertToRefundItems(RefundRequest $request, OrderEntity $order
private function convertToRepositoryArray(array $serviceItems, string $type): array
{
$data = [];

foreach ($serviceItems as $item) {
if ($item->getQuantity() <= 0) {
continue;
}
$row = RefundItemEntity::createEntryArray(
$type,

$row = RefundItemEntity::createArray(
$item->getMollieLineID(),
$item->getShopwareReference(),
$item->getQuantity(),
Expand All @@ -480,6 +519,7 @@ private function convertToRepositoryArray(array $serviceItems, string $type): ar

$data[] = $row;
}

return $data;
}
}
Loading

0 comments on commit 681ec45

Please sign in to comment.