diff --git a/controllers/front/validate.php b/controllers/front/validate.php index fe9d3f1a5..87a2cdf33 100644 --- a/controllers/front/validate.php +++ b/controllers/front/validate.php @@ -89,11 +89,6 @@ public function postProcess() $this->sendOkResponse($this->generateResponse()); } catch (Exception $exception) { - $this->module->getLogger()->error('CheckoutCompletedEvent failed', [ - 'exception_class' => get_class($exception), - 'exception_message' => $exception->getMessage(), - 'exception_code' => $exception->getCode(), - ]); $response = $this->generateResponse(); if (!empty($response)) { diff --git a/src/PayPal/Order/CommandHandler/CapturePayPalOrderCommandHandler.php b/src/PayPal/Order/CommandHandler/CapturePayPalOrderCommandHandler.php index 07be2921d..fef3eb10f 100644 --- a/src/PayPal/Order/CommandHandler/CapturePayPalOrderCommandHandler.php +++ b/src/PayPal/Order/CommandHandler/CapturePayPalOrderCommandHandler.php @@ -30,6 +30,7 @@ use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Event\PayPalOrderCompletedEvent; use PrestaShop\Module\PrestashopCheckout\PayPal\Order\PayPalOrderStatus; use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\Event\PayPalCaptureCompletedEvent; +use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\Event\PayPalCaptureDeclinedEvent; use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\Event\PayPalCapturePendingEvent; use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\PayPalCaptureStatus; use PrestaShop\Module\PrestashopCheckout\PayPalError; @@ -77,22 +78,6 @@ public function handle(CapturePayPalOrderCommand $capturePayPalOrderCommand) $orderPayPal = $response['body']; $capturePayPal = $orderPayPal['purchase_units'][0]['payments']['captures'][0]; - if ( - 'DECLINED' === $capturePayPal['status'] - && false === empty($response['body']['payment_source']) - && false === empty($response['body']['payment_source'][0]['card']) - && false === empty($capturePayPal['processor_response']) - ) { - $payPalProcessorResponse = new PayPalProcessorResponse( - isset($response['body']['payment_source'][0]['card']['brand']) ? $response['body']['payment_source'][0]['card']['brand'] : null, - isset($response['body']['payment_source'][0]['card']['type']) ? $response['body']['payment_source'][0]['card']['type'] : null, - isset($capturePayPal['processor_response']['avs_code']) ? $capturePayPal['processor_response']['avs_code'] : null, - isset($capturePayPal['processor_response']['cvv_code']) ? $capturePayPal['processor_response']['cvv_code'] : null, - isset($capturePayPal['processor_response']['response_code']) ? $capturePayPal['processor_response']['response_code'] : null - ); - $payPalProcessorResponse->throwException(); - } - if ($orderPayPal['status'] === PayPalOrderStatus::COMPLETED) { $this->eventDispatcher->dispatch(new PayPalOrderCompletedEvent($orderPayPal['id'], $orderPayPal)); } @@ -104,5 +89,27 @@ public function handle(CapturePayPalOrderCommand $capturePayPalOrderCommand) if ($capturePayPal['status'] === PayPalCaptureStatus::COMPLETED) { $this->eventDispatcher->dispatch(new PayPalCaptureCompletedEvent($capturePayPal['id'], $orderPayPal['id'], $capturePayPal)); } + + if ($capturePayPal['status'] === PayPalCaptureStatus::DECLINED || $capturePayPal['status'] === PayPalCaptureStatus::FAILED) { + $this->eventDispatcher->dispatch(new PayPalCaptureDeclinedEvent($capturePayPal['id'], $orderPayPal['id'], $capturePayPal)); + } + + if ( + PayPalCaptureStatus::DECLINED === $capturePayPal['status'] + && false === empty($orderPayPal['payment_source']) + && false === empty($orderPayPal['payment_source']['card']) + && false === empty($capturePayPal['processor_response']) + ) { + $payPalProcessorResponse = new PayPalProcessorResponse( + isset($orderPayPal['payment_source']['card']['brand']) ? $orderPayPal['payment_source']['card']['brand'] : null, + isset($orderPayPal['payment_source']['card']['type']) ? $orderPayPal['payment_source']['card']['type'] : null, + isset($capturePayPal['processor_response']['avs_code']) ? $capturePayPal['processor_response']['avs_code'] : null, + isset($capturePayPal['processor_response']['cvv_code']) ? $capturePayPal['processor_response']['cvv_code'] : null, + isset($capturePayPal['processor_response']['response_code']) ? $capturePayPal['processor_response']['response_code'] : null + ); + $payPalProcessorResponse->throwException(); + } elseif (PayPalCaptureStatus::DECLINED === $capturePayPal['status'] || PayPalCaptureStatus::FAILED === $capturePayPal['status']) { + throw new PsCheckoutException('PayPal declined the capture', PsCheckoutException::PAYPAL_PAYMENT_CAPTURE_DECLINED); + } } } diff --git a/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php b/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php index 59b1df49b..265faa609 100644 --- a/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php +++ b/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php @@ -24,6 +24,7 @@ use PrestaShop\Module\PrestashopCheckout\Order\Command\AddOrderPaymentCommand; use PrestaShop\Module\PrestashopCheckout\Order\Command\CreateOrderCommand; use PrestaShop\Module\PrestashopCheckout\Order\Command\UpdateOrderStatusCommand; +use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentCompletedQuery; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentCompletedQueryResult; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentDeniedQuery; @@ -136,8 +137,12 @@ public function createOrder(PayPalCaptureEvent $event) public function createOrderPayment(PayPalCaptureCompletedEvent $event) { - /** @var GetOrderForPaymentCompletedQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentCompletedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + try { + /** @var GetOrderForPaymentCompletedQueryResult $order */ + $order = $this->commandBus->handle(new GetOrderForPaymentCompletedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + } catch (OrderNotFoundException $exception) { + return; + } if ($order->getOrderPaymentId()) { return; @@ -157,8 +162,12 @@ public function createOrderPayment(PayPalCaptureCompletedEvent $event) public function setPaymentCompletedOrderStatus(PayPalCaptureCompletedEvent $event) { - /** @var GetOrderForPaymentCompletedQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentCompletedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + try { + /** @var GetOrderForPaymentCompletedQueryResult $order */ + $order = $this->commandBus->handle(new GetOrderForPaymentCompletedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + } catch (OrderNotFoundException $exception) { + return; + } if ($order->hasBeenPaid()) { return; @@ -177,8 +186,12 @@ public function setPaymentCompletedOrderStatus(PayPalCaptureCompletedEvent $even public function setPaymentPendingOrderStatus(PayPalCapturePendingEvent $event) { - /** @var GetOrderForPaymentPendingQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentPendingQuery($event->getPayPalOrderId()->getValue())); + try { + /** @var GetOrderForPaymentPendingQueryResult $order */ + $order = $this->commandBus->handle(new GetOrderForPaymentPendingQuery($event->getPayPalOrderId()->getValue())); + } catch (OrderNotFoundException $exception) { + return; + } if ($order->isInPending()) { return; @@ -200,8 +213,12 @@ public function setPaymentPendingOrderStatus(PayPalCapturePendingEvent $event) public function setPaymentDeclinedOrderStatus(PayPalCaptureDeclinedEvent $event) { - /** @var GetOrderForPaymentDeniedQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentDeniedQuery($event->getPayPalOrderId()->getValue())); + try { + /** @var GetOrderForPaymentDeniedQueryResult $order */ + $order = $this->commandBus->handle(new GetOrderForPaymentDeniedQuery($event->getPayPalOrderId()->getValue())); + } catch (OrderNotFoundException $exception) { + return; + } if ($order->hasBeenError()) { return; @@ -212,8 +229,12 @@ public function setPaymentDeclinedOrderStatus(PayPalCaptureDeclinedEvent $event) public function setPaymentRefundedOrderStatus(PayPalCaptureRefundedEvent $event) { - /** @var GetOrderForPaymentRefundedQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentRefundedQuery($event->getPayPalOrderId()->getValue())); + try { + /** @var GetOrderForPaymentRefundedQueryResult $order */ + $order = $this->commandBus->handle(new GetOrderForPaymentRefundedQuery($event->getPayPalOrderId()->getValue())); + } catch (OrderNotFoundException $exception) { + return; + } if (!$order->hasBeenPaid() || $order->hasBeenTotallyRefund()) { return; @@ -228,8 +249,12 @@ public function setPaymentRefundedOrderStatus(PayPalCaptureRefundedEvent $event) public function setPaymentReversedOrderStatus(PayPalCaptureReversedEvent $event) { - /** @var GetOrderForPaymentReversedQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentReversedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + try { + /** @var GetOrderForPaymentReversedQueryResult $order */ + $order = $this->commandBus->handle(new GetOrderForPaymentReversedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + } catch (OrderNotFoundException $exception) { + return; + } if (!$order->hasBeenPaid() || $order->hasBeenTotallyRefund()) { return;