From 6932bbff95c32df76cb107cb7ec3b670864e6edc Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Tue, 1 Oct 2024 12:20:23 +0200 Subject: [PATCH] Improve order of data checks on paypal redirect handling --- src/Library/Economy/Payment/PaypalGateway.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Library/Economy/Payment/PaypalGateway.php b/src/Library/Economy/Payment/PaypalGateway.php index e03a453..b3bbc95 100644 --- a/src/Library/Economy/Payment/PaypalGateway.php +++ b/src/Library/Economy/Payment/PaypalGateway.php @@ -112,22 +112,28 @@ class PaypalGateway implements GatewayInterface public function handleRedirect(Request $request): RedirectResponse { - $orderId = $request->query->get('token'); - - if (!$orderId || $request->query->get('type') !== GatewayCheckoutService::RESPONSE_TYPE_SUCCESS) { - throw new \Exception(sprintf("PayPal checkout '%s' was not completed successfully.", $orderId)); + if ($request->query->get('type') !== GatewayCheckoutService::RESPONSE_TYPE_SUCCESS) { + throw new \Exception(sprintf("Checkout was not completed successfully.")); } - $checkout = $this->checkoutRepository->find($request->query->get('checkoutId')); + $checkoutId = $request->query->get('checkoutId'); + + $checkout = $this->checkoutRepository->find($checkoutId); if ($checkout === null) { - throw new \Exception(sprintf("PayPal checkout '%s' exists but no GatewayCheckout with that reference was found.", $orderId)); + throw new \Exception(sprintf("GatewayCheckout '%s' could not be found.", $checkoutId)); } if ($checkout->getStatus() === GatewayCheckoutStatus::Charged) { return new RedirectResponse($this->iriConverter->getIriFromResource($checkout)); } + $orderId = $request->query->get('token'); + + if (!$orderId) { + throw new \Exception(sprintf("PayPal checkout order ID not provided by the gateway.")); + } + $order = $this->fetchPaypalOrder($orderId); if ($order['status'] !== self::PAYPAL_STATUS_APPROVED) {