Skip to content

Commit

Permalink
NTR: PISHPS-385: Update credit notes (#887)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Nov 7, 2024
1 parent 72244a9 commit a43d8fa
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 574 deletions.
10 changes: 8 additions & 2 deletions src/Components/RefundManager/Builder/RefundDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Kiener\MolliePayments\Struct\OrderLineItemEntity\OrderLineItemEntityAttributes;
use Mollie\Api\Resources\OrderLine;
use Mollie\Api\Resources\Refund;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
Expand Down Expand Up @@ -99,9 +100,14 @@ public function buildRefundData(OrderEntity $order, Context $context): RefundDat
$refundItems = [];
$refundPromotionItems = [];
$refundDeliveryItems = [];
$orderLineItems = $order->getLineItems();

if ($order->getLineItems() !== null) {
foreach ($order->getLineItems() as $item) {
if ($orderLineItems !== null) {
$orderLineItems = $orderLineItems->filter(function (OrderLineItemEntity $orderLineItemEntity) {
return $orderLineItemEntity->getType() !== LineItem::CREDIT_LINE_ITEM_TYPE;
});

foreach ($orderLineItems as $item) {
$lineItemAttribute = new OrderLineItemEntityAttributes($item);
$mollieOrderLineId = $lineItemAttribute->getMollieOrderLineID();

Expand Down
11 changes: 6 additions & 5 deletions src/Components/RefundManager/RefundManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ 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());
}
Expand Down Expand Up @@ -324,7 +324,7 @@ public function cancelRefund(string $orderId, string $refundId, Context $context
# first try to cancel on the mollie side
$success = $this->refundService->cancel($order, $refundId);

if (!$success) {
if (! $success) {
return false;
}

Expand Down Expand Up @@ -521,12 +521,13 @@ private function convertToRepositoryArray(array $serviceItems): array
if ($item->getQuantity() < 0) {
continue;
}

$quantity = max(1, $item->getQuantity());
$amount = round($item->getAmount() / $quantity, 2);
$row = RefundItemEntity::createArray(
$item->getMollieLineID(),
$item->getShopwareReference(),
$item->getQuantity(),
$item->getAmount(),
$quantity,
$amount,
$item->getShopwareLineID(),
$item->getShopwareLineVersionId(),
null
Expand Down
62 changes: 12 additions & 50 deletions src/Controller/Api/Order/RefundControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Kiener\MolliePayments\Components\RefundManager\Request\RefundRequestItem;
use Kiener\MolliePayments\Exception\PaymentNotFoundException;
use Kiener\MolliePayments\Service\OrderService;
use Kiener\MolliePayments\Service\Refund\Exceptions\CreditNoteException;
use Kiener\MolliePayments\Service\Refund\RefundCreditNoteService;
use Kiener\MolliePayments\Service\Refund\RefundService;
use Kiener\MolliePayments\Traits\Api\ApiTrait;
Expand Down Expand Up @@ -55,10 +54,10 @@ class RefundControllerBase extends AbstractController
* @param LoggerInterface $logger
*/
public function __construct(
OrderService $orderService,
RefundManagerInterface $refundManager,
RefundService $refundService,
LoggerInterface $logger,
OrderService $orderService,
RefundManagerInterface $refundManager,
RefundService $refundService,
LoggerInterface $logger,
RefundCreditNoteService $creditNoteService
) {
$this->orderService = $orderService;
Expand Down Expand Up @@ -181,7 +180,7 @@ public function refundOrderID(RequestDataBag $data, Context $context): JsonRespo
$items = $itemsBag->all();
}

$response = $this->refundAction(
return $this->refundAction(
$orderId,
'',
$description,
Expand All @@ -190,32 +189,6 @@ public function refundOrderID(RequestDataBag $data, Context $context): JsonRespo
$items,
$context
);

if ($response->getStatusCode() === 200 && $response->getContent() !== false && count($items) > 0) {
$refundId = json_decode($response->getContent(), true)['refundId'];
if ($this->creditNoteService->containsRefundedLineItems($items)) {
try {
$this->creditNoteService->addCreditNoteToOrder($orderId, $refundId, $items, $context);
} catch (CreditNoteException $exception) {
if ($exception->getCode() === CreditNoteException::CODE_ADDING_CREDIT_NOTE_LINE_ITEMS) {
$this->logger->error($exception->getMessage(), ['code' => $exception->getCode(),]);
return $this->buildErrorResponse($exception->getMessage());
}
if ($exception->getCode() === CreditNoteException::CODE_WARNING_LEVEL) {
$this->logger->warning($exception->getMessage(), ['code' => $exception->getCode(),]);
}
}
}
if ($this->creditNoteService->hasCustomAmounts($items, (float) $amount)) {
try {
$this->creditNoteService->addCustomAmountsCreditNote($orderId, $refundId, $items, (float)$amount, $context);
} catch (CreditNoteException $exception) {
$this->logger->warning($exception->getMessage(), ['code' => $exception->getCode(),]);
}
}
}

return $response;
}

/**
Expand All @@ -228,23 +201,7 @@ public function cancel(RequestDataBag $data, Context $context): JsonResponse
{
$orderId = $data->getAlnum('orderId');
$refundId = $data->get('refundId');
$response = $this->cancelRefundAction($orderId, $refundId, $context);

if ($response->getStatusCode() === 200) {
try {
$this->creditNoteService->cancelCreditNoteToOrder($orderId, $refundId, $context);
} catch (CreditNoteException $exception) {
if ($exception->getCode() === CreditNoteException::CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS) {
$this->logger->error($exception->getMessage(), ['code' => $exception->getCode(),]);
return $this->buildErrorResponse($exception->getMessage());
}
if ($exception->getCode() === CreditNoteException::CODE_WARNING_LEVEL) {
$this->logger->warning($exception->getMessage(), ['code' => $exception->getCode(),]);
}
}
}

return $response;
return $this->cancelRefundAction($orderId, $refundId, $context);
}

/**
Expand Down Expand Up @@ -374,7 +331,7 @@ private function listTotalAction(string $orderId, Context $context): JsonRespons
private function refundAction(string $orderId, string $orderNumber, string $description, string $internalDescription, ?float $amount, array $items, Context $context): JsonResponse
{
try {
if (!empty($orderId)) {
if (! empty($orderId)) {
$order = $this->orderService->getOrder($orderId, $context);
} else {
if (empty($orderNumber)) {
Expand Down Expand Up @@ -407,6 +364,8 @@ private function refundAction(string $orderId, string $orderNumber, string $desc
$context
);

$this->creditNoteService->createCreditNotes($order, $refund, $refundRequest, $context);

return $this->json([
'success' => true,
'refundId' => $refund->id
Expand All @@ -428,6 +387,9 @@ private function cancelRefundAction(string $orderId, string $refundId, Context $
{
try {
$success = $this->refundManager->cancelRefund($orderId, $refundId, $context);

$this->creditNoteService->cancelCreditNotes($orderId, $context);

return $this->json([
'success' => $success
]);
Expand Down
3 changes: 1 addition & 2 deletions src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@
<argument type="service" id="mollie_payments.logger"/>
</service>

<service id="Kiener\MolliePayments\Service\Refund\RefundSummarizationService"/>



<service id="Kiener\MolliePayments\Service\Refund\RefundCreditNoteService">
<argument type="service" id="order.repository"/>
<argument type="service" id="order_line_item.repository"/>
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Service\Refund\RefundSummarizationService"/>
<argument type="service" id="mollie_payments.logger"/>
</service>

Expand Down
Loading

0 comments on commit a43d8fa

Please sign in to comment.