Skip to content

Commit

Permalink
Improve error display when card payment is refused
Browse files Browse the repository at this point in the history
  • Loading branch information
bbool committed Jul 31, 2023
1 parent 1b63c4d commit 83dfee1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
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 @@ -78,19 +78,31 @@ public function handle(CapturePayPalOrderCommand $capturePayPalOrderCommand)
$orderPayPal = $response['body'];
$capturePayPal = $orderPayPal['purchase_units'][0]['payments']['captures'][0];

if ($orderPayPal['status'] === PayPalOrderStatus::COMPLETED) {
$this->eventDispatcher->dispatch(new PayPalOrderCompletedEvent($orderPayPal['id'], $orderPayPal));
}

if ($capturePayPal['status'] === PayPalCaptureStatus::PENDING) {
$this->eventDispatcher->dispatch(new PayPalCapturePendingEvent($capturePayPal['id'], $orderPayPal['id'], $capturePayPal));
}

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 (
'DECLINED' === $capturePayPal['status']
&& false === empty($response['body']['payment_source'])
&& false === empty($response['body']['payment_source']['card'])
PayPalCaptureStatus::DECLINED === $capturePayPal['status']
&& false === empty($orderPayPal['payment_source'])
&& false === empty($orderPayPal['payment_source']['card'])
&& false === empty($capturePayPal['processor_response'])
) {
$payPalProcessorResponse = new PayPalProcessorResponse(
isset($response['body']['payment_source']['card']['brand']) ? $response['body']['payment_source']['card']['brand'] : null,
isset($response['body']['payment_source']['card']['type']) ? $response['body']['payment_source']['card']['type'] : null,
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
Expand All @@ -99,17 +111,5 @@ public function handle(CapturePayPalOrderCommand $capturePayPalOrderCommand)
} elseif (PayPalCaptureStatus::DECLINED === $capturePayPal['status'] || PayPalCaptureStatus::FAILED === $capturePayPal['status']) {
throw new PsCheckoutException('PayPal declined the capture', PsCheckoutException::PAYPAL_PAYMENT_CAPTURE_DECLINED);
}

if ($orderPayPal['status'] === PayPalOrderStatus::COMPLETED) {
$this->eventDispatcher->dispatch(new PayPalOrderCompletedEvent($orderPayPal['id'], $orderPayPal));
}

if ($capturePayPal['status'] === PayPalCaptureStatus::PENDING) {
$this->eventDispatcher->dispatch(new PayPalCapturePendingEvent($capturePayPal['id'], $orderPayPal['id'], $capturePayPal));
}

if ($capturePayPal['status'] === PayPalCaptureStatus::COMPLETED) {
$this->eventDispatcher->dispatch(new PayPalCaptureCompletedEvent($capturePayPal['id'], $orderPayPal['id'], $capturePayPal));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,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 @@ -158,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 @@ -178,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 Down Expand Up @@ -217,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 @@ -233,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

0 comments on commit 83dfee1

Please sign in to comment.