From 66d463fb599c387d429328e26aace15a8e8e8f14 Mon Sep 17 00:00:00 2001 From: L3RAZ Date: Mon, 4 Nov 2024 16:57:31 +0200 Subject: [PATCH] Fixed incorrect order status issues --- .../CheckoutEventSubscriber.php | 19 ++++++++++++++++--- ...lOrderForCheckoutCompletedQueryHandler.php | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Checkout/EventSubscriber/CheckoutEventSubscriber.php b/src/Checkout/EventSubscriber/CheckoutEventSubscriber.php index 1edbf115f..cb3bc979f 100644 --- a/src/Checkout/EventSubscriber/CheckoutEventSubscriber.php +++ b/src/Checkout/EventSubscriber/CheckoutEventSubscriber.php @@ -127,11 +127,14 @@ public function proceedToPayment(CheckoutCompletedEvent $event) $event->getPayPalOrderId()->getValue() )); + $payPalOrder = $getPayPalOrderForCheckoutCompletedQueryResult->getPayPalOrder(); + try { - $this->checkoutChecker->continueWithAuthorization($event->getCartId()->getValue(), $getPayPalOrderForCheckoutCompletedQueryResult->getPayPalOrder()); + $this->checkoutChecker->continueWithAuthorization($event->getCartId()->getValue(), $payPalOrder); } catch (PsCheckoutException $exception) { if ($exception->getCode() === PsCheckoutException::PAYPAL_ORDER_ALREADY_CAPTURED) { - $this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue())); + $capture = $payPalOrder['purchase_units'][0]['payments']['captures'][0] ?? null; + $this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue(), $capture)); return; } else { @@ -161,7 +164,17 @@ public function proceedToPayment(CheckoutCompletedEvent $event) throw $exception; } elseif ($exception->getCode() === PayPalException::ORDER_ALREADY_CAPTURED) { - $this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue())); + if (isset($payPalOrder['purchase_units'][0]['payments']['captures'][0])) { + $capture = $payPalOrder['purchase_units'][0]['payments']['captures'][0]; + } else { + /** @var GetPayPalOrderForCheckoutCompletedQueryResult $getPayPalOrderForCheckoutCompletedQueryResult */ + $getPayPalOrderForCheckoutCompletedQueryResult = $this->commandBus->handle(new GetPayPalOrderForCheckoutCompletedQuery( + $event->getPayPalOrderId()->getValue() + )); + $payPalOrder = $getPayPalOrderForCheckoutCompletedQueryResult->getPayPalOrder(); + $capture = $payPalOrder['purchase_units'][0]['payments']['captures'][0] ?? null; + } + $this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue(), $capture)); return; } else { diff --git a/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php b/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php index 5e209b4e1..61fd3aae6 100644 --- a/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php +++ b/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php @@ -55,7 +55,8 @@ public function handle(GetPayPalOrderForCheckoutCompletedQuery $getPayPalOrderQu try { $orderPayPal = new PaypalOrder($getPayPalOrderQuery->getOrderPayPalId()->getValue()); - $this->orderPayPalCache->set($getPayPalOrderQuery->getOrderPayPalId()->getValue(), $orderPayPal->getOrder()); + $orderToStoreInCache = !empty($order) ? array_replace_recursive($order, $orderPayPal->getOrder()) : $orderPayPal->getOrder(); + $this->orderPayPalCache->set($getPayPalOrderQuery->getOrderPayPalId()->getValue(), $orderToStoreInCache); } catch (HttpTimeoutException $exception) { throw $exception; } catch (Exception $exception) {