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

Improve error display when card payment is refused - PS 8 #1111

Merged
merged 1 commit into from
Jul 31, 2023
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
5 changes: 0 additions & 5 deletions controllers/front/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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 @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading