Skip to content

Commit

Permalink
refs #49122 payer action redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan202 committed Dec 20, 2024
1 parent 82dbadb commit 5a48f0f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 0 deletions.
18 changes: 18 additions & 0 deletions classes/API/Request/PaypalOrderAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public function execute()
$resultDecoded = json_decode($exec->message);
$error->setMessage($resultDecoded->message);
$response->setSuccess(false)->setError($error);

if ($exec->statusCode == 422) {
if (!empty($exec->result->links)) {
$response->getError()->setErrorCode(PaypalException::PAYER_ACTION_REQUIRED);
$response->setPayerAction($this->getLink('payer-action', $exec->result->links));
}
}
}
} catch (PaypalException $e) {
$error = new Error();
Expand Down Expand Up @@ -165,4 +172,15 @@ protected function getMethodTransaction()

return $method;
}

protected function getLink($nameLink, $links)
{
foreach ($links as $link) {
if ($link->rel == $nameLink) {
return $link->href;
}
}

return '';
}
}
18 changes: 18 additions & 0 deletions classes/API/Request/PaypalOrderCaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public function execute()
$resultDecoded = json_decode($exec->message);
$error->setMessage($resultDecoded->message);
$response->setSuccess(false)->setError($error);

if ($exec->statusCode == 422) {
if (!empty($exec->result->links)) {
$response->getError()->setErrorCode(PaypalException::PAYER_ACTION_REQUIRED);
$response->setPayerAction($this->getLink('payer-action', $exec->result->links));
}
}
}
} catch (PaypalException $e) {
$error = new Error();
Expand Down Expand Up @@ -198,4 +205,15 @@ protected function getVaultInfo($response)

return null;
}

protected function getLink($nameLink, $links)
{
foreach ($links as $link) {
if ($link->rel == $nameLink) {
return $link->href;
}
}

return '';
}
}
15 changes: 15 additions & 0 deletions classes/API/Response/ResponseOrderCapture.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class ResponseOrderCapture extends Response
/** @var int */
protected $scaState;

/** @var string */
protected $payerAction = '';

/**
* @return string
*/
Expand Down Expand Up @@ -277,4 +280,16 @@ public function setScaState($scaState)

return $this;
}

public function getPayerAction()
{
return $this->payerAction;
}

public function setPayerAction($payerAction)
{
$this->payerAction = (string) $payerAction;

return $this;
}
}
6 changes: 6 additions & 0 deletions classes/AbstractMethodPaypal.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use PaypalAddons\classes\API\Response\ResponseWebhookEventDetail;
use PaypalAddons\classes\API\Response\ResponseWebhookEventList;
use PaypalAddons\classes\Constants\Vaulting;
use PaypalAddons\classes\Exception\PayerActionRequired;
use PaypalAddons\classes\PUI\SignupLink;
use PaypalAddons\classes\Shortcut\ShortcutCart;
use PaypalAddons\classes\Shortcut\ShortcutConfiguration;
Expand Down Expand Up @@ -257,6 +258,11 @@ public function validation()
$response = $this->completePayment();

if ($response->isSuccess() === false) {
if ($response->getError()->getCode() === PaypalException::PAYER_ACTION_REQUIRED) {
if ($response->getPayerAction()) {
throw new PayerActionRequired($response->getPayerAction(), 'Payer action required', PaypalException::PAYER_ACTION_REQUIRED);
}
}
throw new Exception($response->getError()->getMessage(), $response->getError()->getCode());
}

Expand Down
20 changes: 20 additions & 0 deletions classes/Exception/PayerActionRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PaypalAddons\classes\Exception;

class PayerActionRequired extends \Exception
{
protected $payerActionLink = '';

public function __construct($payerActionLink = '', $message = '', $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);

$this->payerActionLink = (string) $payerActionLink;
}

public function getPayerActionLink()
{
return $this->payerActionLink;
}
}
2 changes: 2 additions & 0 deletions classes/PaypalException.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class PaypalException extends Exception

const CAPTURE_PENDING = 1009;

const PAYER_ACTION_REQUIRED = 1011;

/** @var string Long detailed error message */
private $message_long;

Expand Down
4 changes: 4 additions & 0 deletions controllers/front/ecValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function postProcess()
$cart = Context::getContext()->cart;
$customer = new Customer($cart->id_customer);
$this->redirectUrl = 'index.php?controller=order-confirmation&id_cart=' . $cart->id . '&id_module=' . $paypal->id . '&id_order=' . $paypal->currentOrder . '&key=' . $customer->secure_key;
} catch (PaypalAddons\classes\Exception\PayerActionRequired $e) {
$this->redirectUrl = $e->getPayerActionLink();

return;
} catch (PaypalAddons\classes\PaypalException $e) {
$this->_errors['error_code'] = $e->getCode();
$this->_errors['error_msg'] = $e->getMessage();
Expand Down
4 changes: 4 additions & 0 deletions controllers/front/mbValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function postProcess()
$cart = Context::getContext()->cart;
$customer = new Customer($cart->id_customer);
$this->redirectUrl = 'index.php?controller=order-confirmation&id_cart=' . $cart->id . '&id_module=' . $paypal->id . '&id_order=' . $paypal->currentOrder . '&key=' . $customer->secure_key;
} catch (PaypalAddons\classes\Exception\PayerActionRequired $e) {
$this->redirectUrl = $e->getPayerActionLink();

return;
} catch (Exception $e) {
$this->_errors['error_code'] = $e->getCode();
$this->_errors['error_msg'] = $e->getMessage();
Expand Down
4 changes: 4 additions & 0 deletions controllers/front/pppValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function postProcess()
$cart = Context::getContext()->cart;
$customer = new Customer($cart->id_customer);
$this->redirectUrl = 'index.php?controller=order-confirmation&id_cart=' . $cart->id . '&id_module=' . $paypal->id . '&id_order=' . $paypal->currentOrder . '&key=' . $customer->secure_key;
} catch (PaypalAddons\classes\Exception\PayerActionRequired $e) {
$this->redirectUrl = $e->getPayerActionLink();

return;
} catch (Exception $e) {
$this->_errors['error_code'] = $e->getCode();
$this->_errors['error_msg'] = $e->getMessage();
Expand Down
4 changes: 4 additions & 0 deletions controllers/front/puiValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function postProcess()
$cart = Context::getContext()->cart;
$customer = new Customer($cart->id_customer);
$this->redirectUrl = 'index.php?controller=order-confirmation&id_cart=' . $cart->id . '&id_module=' . $paypal->id . '&id_order=' . $paypal->currentOrder . '&key=' . $customer->secure_key;
} catch (PaypalAddons\classes\Exception\PayerActionRequired $e) {
$this->redirectUrl = $e->getPayerActionLink();

return;
} catch (PaypalAddons\classes\PaypalException $e) {
$this->_errors['error_code'] = $e->getCode();
$this->_errors['error_msg'] = $e->getMessage();
Expand Down

0 comments on commit 5a48f0f

Please sign in to comment.