Skip to content

Commit

Permalink
Merge pull request #1111 from PrestaShopCorp/fix/PAYSHIP-2414
Browse files Browse the repository at this point in the history
Improve error display when card payment is refused - PS 8
  • Loading branch information
Matt75 authored Jul 31, 2023
2 parents 89ac34d + 2e6f7fc commit 9c6a4bd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
5 changes: 0 additions & 5 deletions controllers/front/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -189,8 +202,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;
Expand All @@ -201,8 +218,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;
Expand All @@ -217,8 +238,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;
Expand Down

0 comments on commit 9c6a4bd

Please sign in to comment.