diff --git a/classes/API/Request/PaypalOrderAuthorizeRequest.php b/classes/API/Request/PaypalOrderAuthorizeRequest.php index 487fbbb4..1f3ef190 100644 --- a/classes/API/Request/PaypalOrderAuthorizeRequest.php +++ b/classes/API/Request/PaypalOrderAuthorizeRequest.php @@ -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(); @@ -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 ''; + } } diff --git a/classes/API/Request/PaypalOrderCaptureRequest.php b/classes/API/Request/PaypalOrderCaptureRequest.php index b3a1304c..79878e3c 100644 --- a/classes/API/Request/PaypalOrderCaptureRequest.php +++ b/classes/API/Request/PaypalOrderCaptureRequest.php @@ -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(); @@ -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 ''; + } } diff --git a/classes/API/Response/ResponseOrderCapture.php b/classes/API/Response/ResponseOrderCapture.php index c831a19f..e43fc54c 100644 --- a/classes/API/Response/ResponseOrderCapture.php +++ b/classes/API/Response/ResponseOrderCapture.php @@ -71,6 +71,9 @@ class ResponseOrderCapture extends Response /** @var int */ protected $scaState; + /** @var string */ + protected $payerAction = ''; + /** * @return string */ @@ -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; + } } diff --git a/classes/AbstractMethodPaypal.php b/classes/AbstractMethodPaypal.php index df940a84..402240e5 100755 --- a/classes/AbstractMethodPaypal.php +++ b/classes/AbstractMethodPaypal.php @@ -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; @@ -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()); } diff --git a/classes/Exception/PayerActionRequired.php b/classes/Exception/PayerActionRequired.php new file mode 100644 index 00000000..6861eb4f --- /dev/null +++ b/classes/Exception/PayerActionRequired.php @@ -0,0 +1,20 @@ +payerActionLink = (string) $payerActionLink; + } + + public function getPayerActionLink() + { + return $this->payerActionLink; + } +} diff --git a/classes/PaypalException.php b/classes/PaypalException.php index bdfc6aae..93c8fa37 100644 --- a/classes/PaypalException.php +++ b/classes/PaypalException.php @@ -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; diff --git a/controllers/front/ecValidation.php b/controllers/front/ecValidation.php index 575d2cec..769897cf 100755 --- a/controllers/front/ecValidation.php +++ b/controllers/front/ecValidation.php @@ -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(); diff --git a/controllers/front/mbValidation.php b/controllers/front/mbValidation.php index 5b4014fe..2732e78d 100644 --- a/controllers/front/mbValidation.php +++ b/controllers/front/mbValidation.php @@ -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(); diff --git a/controllers/front/pppValidation.php b/controllers/front/pppValidation.php index b34534db..51ac357e 100755 --- a/controllers/front/pppValidation.php +++ b/controllers/front/pppValidation.php @@ -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(); diff --git a/controllers/front/puiValidate.php b/controllers/front/puiValidate.php index 7c168a9a..2e00dab2 100644 --- a/controllers/front/puiValidate.php +++ b/controllers/front/puiValidate.php @@ -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();