Skip to content

Commit

Permalink
PISHPS-371: Return empty data instead of 500 (#877)
Browse files Browse the repository at this point in the history
Co-authored-by: Thilo Lindner <[email protected]>
  • Loading branch information
ThLind and Thilo Lindner authored Oct 24, 2024
1 parent 5d30ae0 commit bd7aa91
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/Components/RefundManager/RefundManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Kiener\MolliePayments\Components\RefundManager\Request\RefundRequestItem;
use Kiener\MolliePayments\Components\RefundManager\Request\RefundRequestItemRoundingDiff;
use Kiener\MolliePayments\Exception\CouldNotCreateMollieRefundException;
use Kiener\MolliePayments\Exception\CouldNotFetchMollieOrderException;
use Kiener\MolliePayments\Service\MollieApi\Fixer\RoundingDifferenceFixer;
use Kiener\MolliePayments\Service\MollieApi\Order;
use Kiener\MolliePayments\Service\OrderServiceInterface;
Expand Down Expand Up @@ -115,7 +116,20 @@ public function __construct(RefundDataBuilder $refundDataBuilder, OrderServiceIn
*/
public function getData(OrderEntity $order, Context $context): RefundData
{
return $this->builderData->buildRefundData($order, $context);
try {
return $this->builderData->buildRefundData($order, $context);
} catch (CouldNotFetchMollieOrderException $e) {
return new RefundData(
[],
[],
0.0,
0.0,
0.0,
0.0,
0.0,
$order->getTaxStatus(),
);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Components/ShipmentManager/ShipmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public function getStatus(string $orderId, Context $context): array
return $this->shipmentService->getStatus($mollieOrderId, $order->getSalesChannelId());
}

/**
* @param string $orderId
* @param Context $context
* @return array<mixed>
*/
public function getShopwareStatus(string $orderId, Context $context): array
{
$order = $this->orderService->getOrder($orderId, $context);

return $this->orderService->getStatus($order);
}

/**
* @param string $orderId
* @param Context $context
Expand Down
7 changes: 6 additions & 1 deletion src/Controller/Api/Order/CancelLineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Kiener\MolliePayments\Components\CancelManager\CancelItemFacade;
use Kiener\MolliePayments\Factory\MollieApiFactory;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\OrderLine;
use Shopware\Core\Framework\Context;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -28,7 +29,11 @@ public function statusAction(Request $request, Context $context): Response
$orderId = $request->get('mollieOrderId');
$result = [];
$client = $this->clientFactory->getClient();
$mollieOrder = $client->orders->get($orderId);
try {
$mollieOrder = $client->orders->get($orderId);
} catch (ApiException $e) {
return new JsonResponse($result);
}

$lines = $mollieOrder->lines();
if ($lines->count() > 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Api/Order/ShippingControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kiener\MolliePayments\Components\ShipmentManager\Models\ShipmentLineItem;
use Kiener\MolliePayments\Components\ShipmentManager\Models\TrackingData;
use Kiener\MolliePayments\Components\ShipmentManager\ShipmentManager;
use Kiener\MolliePayments\Exception\CouldNotFetchMollieOrderException;
use Kiener\MolliePayments\Service\OrderService;
use Kiener\MolliePayments\Struct\OrderLineItemEntity\OrderLineItemEntityAttributes;
use Kiener\MolliePayments\Traits\Api\ApiTrait;
Expand Down Expand Up @@ -580,6 +581,8 @@ private function getStatusResponse(string $orderId, Context $context): JsonRespo
{
try {
$status = $this->shipment->getStatus($orderId, $context);
} catch (CouldNotFetchMollieOrderException $e) {
$status = $this->shipment->getShopwareStatus($orderId, $context);
} catch (ShopwareHttpException $e) {
$this->logger->error($e->getMessage());
return $this->json(['message' => $e->getMessage()], $e->getStatusCode());
Expand Down
6 changes: 5 additions & 1 deletion src/Service/MollieApi/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,11 @@ private function createNewOrderPayment(MollieOrder $mollieOrder, string $payment

public function getPaymentUrl(string $mollieOrderId, string $salesChannelId): ?string
{
$mollieOrder = $this->getMollieOrder($mollieOrderId, $salesChannelId);
try {
$mollieOrder = $this->getMollieOrder($mollieOrderId, $salesChannelId);
} catch (CouldNotFetchMollieOrderException $e) {
return null;
}

return $mollieOrder->status === 'created' ? $mollieOrder->getCheckoutUrl() : null;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Service/MollieApi/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kiener\MolliePayments\Service\MollieApi;

use Kiener\MolliePayments\Exception\CouldNotFetchMollieOrderException;
use Kiener\MolliePayments\Exception\MollieOrderCouldNotBeShippedException;
use Kiener\MolliePayments\Service\MollieApi\Models\MollieShippingItem;
use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct;
Expand Down Expand Up @@ -174,7 +175,14 @@ public function getStatus(string $mollieOrderId, string $salesChannelId): array
*/
public function getTotals(string $mollieOrderId, string $salesChannelId): array
{
$mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId);
try {
$mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId);
} catch (CouldNotFetchMollieOrderException $e) {
return [
'amount' => 0.0,
'quantity' => 0,
];
}

$totalAmount = 0.0;
$totalQuantity = 0;
Expand Down
27 changes: 27 additions & 0 deletions src/Service/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,31 @@ public function updateMollieData(OrderEntity $order, string $orderTransactionId,
$context
);
}

/**
* @param OrderEntity $order
* @return array<string,array<string,mixed>>
*/
public function getStatus(OrderEntity $order): array
{
$lineItems = [];

$orderLineItems = $order->getLineItems();

if ($orderLineItems === null) {
return $lineItems;
}

foreach ($orderLineItems as $lineItem) {
$lineItems[$lineItem->getId()] = [
'id' => $lineItem->getId(),
'mollieOrderLineId' => null,
'quantity' => $lineItem->getQuantity(),
'quantityShippable' => 0,
'quantityShipped' => 0,
];
}

return $lineItems;
}
}

0 comments on commit bd7aa91

Please sign in to comment.