Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PT-1159] Fix 500 webhook responses when the order is not found. #59

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Components/StateMachine/Exception/MonduException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

class MonduException extends ShopwareHttpException
{
public function __construct($message)
private int $monduStatusCode;

public function __construct($message, $statusCode = Response::HTTP_BAD_REQUEST)
{
$this->monduStatusCode = $statusCode;
parent::__construct(
$message
);
Expand All @@ -23,6 +26,6 @@ public function getErrorCode(): string

public function getStatusCode(): int
{
return Response::HTTP_BAD_REQUEST;
return $this->monduStatusCode;
}
}
34 changes: 20 additions & 14 deletions src/Components/Webhooks/Controller/WebhooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,32 @@ public function process(Request $request, Context $context): Response

$signature = hash_hmac('sha256', $content, $this->configService->getWebhooksSecret());
if ($signature !== $headers->get('X-Mondu-Signature')) {
throw new \Exception('Signature mismatch');
return new Response(
json_encode([
'message' => 'Signature mismatch',
'code' => 401
]),
Response::HTTP_UNAUTHORIZED,
);
}

$params = json_decode($content, true);
$topic = $params['topic'];

switch ($topic) {
case 'order/confirmed':
[$resBody, $resStatus] = $this->webhookService->handleConfirmed($params, $context);
break;
case 'order/pending':
[$resBody, $resStatus] = $this->webhookService->handlePending($params, $context);
break;
case 'order/declined':
[$resBody, $resStatus] = $this->webhookService->handleDeclinedOrCanceled($params, $context);
break;
default:
throw new \Exception('Unregistered topic');
}

case 'order/confirmed':
[$resBody, $resStatus] = $this->webhookService->handleConfirmed($params, $context);
break;
case 'order/pending':
[$resBody, $resStatus] = $this->webhookService->handlePending($params, $context);
break;
case 'order/declined':
[$resBody, $resStatus] = $this->webhookService->handleDeclinedOrCanceled($params, $context);
break;
default:
$resBody = ['message' => 'Unregistered topic', 'code' => 200];
$resStatus = 200;
}

return new Response(
json_encode($resBody),
Expand Down
31 changes: 20 additions & 11 deletions src/Components/Webhooks/Service/WebhookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Mondu\MonduPayment\Components\MonduApi\Service\MonduClient;
use Mondu\MonduPayment\Components\PluginConfig\Service\ConfigService;



class WebhookService
{
private MonduClient $monduClient;
Expand Down Expand Up @@ -113,10 +111,10 @@ public function handleConfirmed($params, $context): array

$transitionResult = $this->transitionTransactionState($externalReferenceId, 'paid', $context);

return [[ 'status' => $transitionResult->last()->getTechnicalName(), 'error' => 0 ], Response::HTTP_OK];
return [[ 'message' => $transitionResult->last()->getTechnicalName(), 'code' => Response::HTTP_OK ], Response::HTTP_OK];
} catch (MonduException $e) {
$this->log('handleConfirmed Webhook Failed', [$params], $e);
return [[ 'status' => $e->getMessage(), 'error' => 10 ], Response::HTTP_BAD_REQUEST];
return [[ 'message' => $e->getMessage(), 'code' => $e->getStatusCode() ], $e->getStatusCode()];
}
}

Expand All @@ -133,10 +131,10 @@ public function handlePending($params, $context): array
$transitionResult = $this->transitionOrderState($externalReferenceId, 'process', $context);
$transitionResult = $this->transitionTransactionState($externalReferenceId, 'reopen', $context);

return [[ 'status' => $transitionResult->last()->getTechnicalName(), 'error' => 0 ], Response::HTTP_OK];
return [[ 'message' => $transitionResult->last()->getTechnicalName(), 'code' => Response::HTTP_OK ], Response::HTTP_OK];
} catch (MonduException $e) {
$this->log('handlePending Webhook Failed', [$params], $e);
return [[ 'status' => 'error', 'error' => 10 ], Response::HTTP_BAD_REQUEST];
return [[ 'message' => $e->getMessage(), 'code' => $e->getStatusCode() ], $e->getStatusCode()];
}
}

Expand All @@ -156,10 +154,10 @@ public function handleDeclinedOrCanceled($params, $context): array
$transitionResult = $this->transitionDeliveryState($externalReferenceId, 'cancel', $context);
$transitionResult = $this->transitionTransactionState($externalReferenceId, 'cancel', $context);

return [[ 'status' => $transitionResult->last()->getTechnicalName(), 'error' => 0 ], Response::HTTP_OK];
return [[ 'message' => $transitionResult->last()->getTechnicalName(), 'code' => Response::HTTP_OK ], Response::HTTP_OK];
} catch (MonduException $e) {
$this->log('handleDeclinedOrCanceled Webhook Failed', [$params], $e);
return [[ 'status' => $e->getMessage(), 'error' => 10 ], Response::HTTP_BAD_REQUEST];
return [[ 'message' => $e->getMessage(), 'code' => $e->getStatusCode() ], $e->getStatusCode()];
}
}

Expand All @@ -172,6 +170,8 @@ protected function transitionOrderState($externalReferenceId, $state, $context)
$state,
'stateId'
), $context);
} catch (MonduException $e) {
throw $e;
} catch (\Exception $e) {
$this->log('transitionOrderState Failed', [$externalReferenceId, $state], $e);
throw new MonduException($e->getMessage());
Expand All @@ -194,6 +194,8 @@ protected function transitionDeliveryState($externalReferenceId, $state, $contex
$state,
'stateId'
), $context);
} catch (MonduException $e) {
throw $e;
} catch (\Exception $e) {
$this->log('transitionDeliveryState Failed', [$externalReferenceId, $state], $e);
throw new MonduException($e->getMessage());
Expand All @@ -216,6 +218,8 @@ protected function transitionTransactionState($externalReferenceId, $state, $con
$state,
'stateId'
), $context);
} catch (MonduException $e) {
throw $e;
} catch (\Exception $e) {
$this->log('transitionTransactionState Failed', [$externalReferenceId, $state], $e);
throw new MonduException($e->getMessage());
Expand All @@ -227,10 +231,15 @@ protected function getOrderUuid($externalReferenceId, $context)
try {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderNumber', $externalReferenceId));
$order = $this->orderRepository->search($criteria, $context)->first();
if (!$order) {
throw new MonduException('Order not found', 404);
}

$orderId = $this->orderRepository->search($criteria, $context)->first()->getId();

return $orderId;
return $order->getId();
} catch (MonduException $e) {
$this->log('getOrderUuid Failed', [$externalReferenceId], $e);
throw $e;
} catch (\Exception $e) {
$this->log('getOrderUuid Failed', [$externalReferenceId], $e);
throw new MonduException($e->getMessage());
Expand Down
Loading