Skip to content

Commit

Permalink
pishps314 cypress (#836)
Browse files Browse the repository at this point in the history
* PISHPS-314: Added refund credit notes

* PISHPS-314: cypress test; fixed bug for refunds with no line items
  • Loading branch information
m-muxfeld-diw authored Sep 16, 2024
1 parent 1edeafe commit 84c837c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/Controller/Api/Order/RefundControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function refundOrderID(RequestDataBag $data, Context $context): JsonRespo
$context
);

if ($response->getStatusCode() === 200 && $response->getContent() !== false) {
if ($response->getStatusCode() === 200 && $response->getContent() !== false && count($items) > 0) {
$refundId = json_decode($response->getContent(), true)['refundId'];
try {
$this->creditNoteService->addCreditNoteToOrder($orderId, $refundId, $items, $context);
Expand Down Expand Up @@ -220,8 +220,13 @@ public function cancel(RequestDataBag $data, Context $context): JsonResponse
try {
$this->creditNoteService->cancelCreditNoteToOrder($orderId, $refundId, $context);
} catch (CreditNoteException $exception) {
$this->logger->error($exception->getMessage(), ['code' => $exception->getCode(),]);
return $this->buildErrorResponse($exception->getMessage());
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(),]);
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Service/Refund/Exceptions/CreditNoteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class CreditNoteException extends \Exception
{
public const CODE_ADDING_CREDIT_NOTE_LINE_ITEMS = 1;
public const CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS = 2;
public const CODE_WARNING_LEVEL = 3;

final private function __construct(string $message, int $code)
{
parent::__construct($message, $code);
Expand All @@ -17,8 +19,8 @@ public static function forAddingLineItems(string $message): CreditNoteException
return new self($message, self::CODE_ADDING_CREDIT_NOTE_LINE_ITEMS);
}

public static function forRemovingLineItems(string $message): CreditNoteException
public static function forRemovingLineItems(string $message, int $code = self::CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS): CreditNoteException
{
return new self($message, self::CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS);
return new self($message, $code);
}
}
10 changes: 8 additions & 2 deletions src/Service/Refund/RefundCreditNoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ public function cancelCreditNoteToOrder(string $orderId, string $refundId, Conte
$lineItems = $order->getLineItems();

if ($lineItems === null) {
throw CreditNoteException::forRemovingLineItems(sprintf('No line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId));
throw CreditNoteException::forRemovingLineItems(
sprintf('No line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId),
CreditNoteException::CODE_WARNING_LEVEL
);
}

$ids = [];
Expand All @@ -170,7 +173,10 @@ public function cancelCreditNoteToOrder(string $orderId, string $refundId, Conte
}

if (empty($ids)) {
throw CreditNoteException::forRemovingLineItems(sprintf('No credit note line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId));
throw CreditNoteException::forRemovingLineItems(
sprintf('No credit note line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId),
CreditNoteException::CODE_WARNING_LEVEL
);
}

$this->orderLineItemRepository->delete($ids, $context);
Expand Down
24 changes: 23 additions & 1 deletion tests/Cypress/cypress/e2e/storefront/refund/refund.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ context("Order Refunds", () => {
// now start the partial refund
refundManager.fullRefund(REFUND_DESCRIPTION, REFUND_INTERNAL_DESCRIPTION);

// verify that our refund now exists
cy.wait(5000); // wait for the page to reload

adminOrders.openRefundManager();

cy.wait(1500);

// // verify that our refund now exists
repoRefundManager.getFirstRefundStatusLabel().contains('Pending');

repoRefundManager.getFirstRefundPublicDescriptionLabel().contains(REFUND_DESCRIPTION);
Expand Down Expand Up @@ -108,6 +114,10 @@ context("Order Refunds", () => {
// now start the partial refund
refundManager.partialAmountRefund(2, REFUND_DESCRIPTION);

cy.wait(5000); // wait for the page to reload

adminOrders.openRefundManager();

// verify that our refund now exists
repoRefundManager.getFirstRefundStatusLabel().contains('Pending');
repoRefundManager.getFirstRefundPublicDescriptionLabel().contains(REFUND_DESCRIPTION);
Expand Down Expand Up @@ -144,6 +154,10 @@ context("Order Refunds", () => {
// now start the partial refund with a custom amount
refundManager.partialAmountRefund(2, REFUND_DESCRIPTION);

cy.wait(5000); // wait for the page to reload

adminOrders.openRefundManager();

// -------------------------------------------------------------------------------

repoRefundManager.getFirstRefundStatusLabel().contains('Pending');
Expand Down Expand Up @@ -188,6 +202,10 @@ context("Order Refunds", () => {
// now start the full refund
refundManager.fullRefund(REFUND_DESCRIPTION, '');

cy.wait(5000); // wait for the page to reload

adminOrders.openRefundManager();

// verify that our refund now exists
repoRefundManager.getFirstRefundStatusLabel().contains('Pending');
repoRefundManager.getFirstRefundPublicDescriptionLabel().contains(REFUND_DESCRIPTION);
Expand All @@ -204,6 +222,10 @@ context("Order Refunds", () => {
// now start another full refund
refundManager.fullRefund(REFUND_DESCRIPTION, '');

cy.wait(5000); // wait for the page to reload

adminOrders.openRefundManager();

cy.contains(CANCELED_REFUND_STATUS_LABEL).should('not.exist');

// second cancel should clear the history
Expand Down

0 comments on commit 84c837c

Please sign in to comment.