2020-07-15 10:27:40 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Inpsyde\PayPalCommerce\Button\Helper;
|
|
|
|
|
2020-07-21 09:17:03 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult as AuthResult;
|
2020-07-15 10:27:40 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
|
|
|
|
|
|
|
class ThreeDSecure
|
|
|
|
{
|
|
|
|
|
|
|
|
public const NO_DECISION = 0;
|
|
|
|
public const PROCCEED = 1;
|
|
|
|
public const REJECT = 2;
|
|
|
|
public const RETRY = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine, how we proceed with a given order.
|
|
|
|
*
|
|
|
|
* @link https://developer.paypal.com/docs/business/checkout/add-capabilities/3d-secure/#authenticationresult
|
|
|
|
* @param Order $order
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function proceedWithOrder(Order $order): int
|
|
|
|
{
|
|
|
|
if (! $order->paymentSource()) {
|
|
|
|
return self::NO_DECISION;
|
|
|
|
}
|
|
|
|
if (! $order->paymentSource()->card()) {
|
|
|
|
return self::NO_DECISION;
|
|
|
|
}
|
|
|
|
if (! $order->paymentSource()->card()->authenticationResult()) {
|
|
|
|
return self::NO_DECISION;
|
|
|
|
}
|
|
|
|
$result = $order->paymentSource()->card()->authenticationResult();
|
2020-07-21 09:17:03 +03:00
|
|
|
if ($result->liabilityShift() === AuthResult::LIABILITY_SHIFT_POSSIBLE) {
|
2020-07-15 10:27:40 +03:00
|
|
|
return self::PROCCEED;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:17:03 +03:00
|
|
|
if ($result->liabilityShift() === AuthResult::LIABILITY_SHIFT_UNKNOWN) {
|
2020-07-15 10:27:40 +03:00
|
|
|
return self::RETRY;
|
|
|
|
}
|
2020-07-21 09:17:03 +03:00
|
|
|
if ($result->liabilityShift() === AuthResult::LIABILITY_SHIFT_NO) {
|
2020-07-15 10:27:40 +03:00
|
|
|
return $this->noLiabilityShift($result);
|
|
|
|
}
|
|
|
|
return self::NO_DECISION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2020-07-21 09:17:03 +03:00
|
|
|
private function noLiabilityShift(AuthResult $result): int
|
2020-07-15 10:27:40 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (
|
2020-07-21 09:17:03 +03:00
|
|
|
$result->enrollmentStatus() === AuthResult::ENROLLMENT_STATUS_BYPASS
|
2020-07-15 10:27:40 +03:00
|
|
|
&& ! $result->authenticationResult()
|
|
|
|
) {
|
|
|
|
return self::PROCCEED;
|
|
|
|
}
|
|
|
|
if (
|
2020-07-21 09:17:03 +03:00
|
|
|
$result->enrollmentStatus() === AuthResult::ENROLLMENT_STATUS_UNAVAILABLE
|
2020-07-15 10:27:40 +03:00
|
|
|
&& ! $result->authenticationResult()
|
|
|
|
) {
|
|
|
|
return self::PROCCEED;
|
|
|
|
}
|
|
|
|
if (
|
2020-07-21 09:17:03 +03:00
|
|
|
$result->enrollmentStatus() === AuthResult::ENROLLMENT_STATUS_NO
|
2020-07-15 10:27:40 +03:00
|
|
|
&& ! $result->authenticationResult()
|
|
|
|
) {
|
|
|
|
return self::PROCCEED;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:17:03 +03:00
|
|
|
if ($result->authenticationResult() === AuthResult::AUTHENTICATION_RESULT_REJECTED) {
|
2020-07-15 10:27:40 +03:00
|
|
|
return self::REJECT;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:17:03 +03:00
|
|
|
if ($result->authenticationResult() === AuthResult::AUTHENTICATION_RESULT_NO) {
|
2020-07-15 10:27:40 +03:00
|
|
|
return self::REJECT;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:17:03 +03:00
|
|
|
if ($result->authenticationResult() === AuthResult::AUTHENTICATION_RESULT_UNABLE) {
|
2020-07-15 10:27:40 +03:00
|
|
|
return self::RETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $result->authenticationResult()) {
|
|
|
|
return self::RETRY;
|
|
|
|
}
|
|
|
|
return self::NO_DECISION;
|
|
|
|
}
|
|
|
|
}
|