add enableDebug() method

This commit is contained in:
ZerosDev
2023-03-03 08:38:24 +07:00
parent b1c15915df
commit 2705ac7adb
2 changed files with 29 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ namespace ZerosDev\TriPay;
use Exception; use Exception;
use UnexpectedValueException; use UnexpectedValueException;
use ZerosDev\TriPay\Exception\SignatureException; use ZerosDev\TriPay\Exception\SignatureException;
use ZerosDev\TriPay\Support\Constant;
class Callback class Callback
{ {
@@ -29,13 +30,20 @@ class Callback
*/ */
protected ?object $parsedJson; protected ?object $parsedJson;
/**
* Enable/disable debug mode
*
* @var boolean
*/
protected bool $debug = false;
/** /**
* Callback instance * Callback instance
* *
* @param Client $client * @param Client $client
* @param bool $verifyOnLoad * @param bool $verifyOnLoad
*/ */
public function __construct(Client $client, bool $verifyOnLoad = true) public function __construct(Client $client)
{ {
if (!function_exists('file_get_contents')) { if (!function_exists('file_get_contents')) {
throw new Exception('`file_get_contents` function is disabled on your system. Please contact your hosting provider'); throw new Exception('`file_get_contents` function is disabled on your system. Please contact your hosting provider');
@@ -44,10 +52,12 @@ class Callback
$this->client = $client; $this->client = $client;
$this->json = (string) file_get_contents("php://input"); $this->json = (string) file_get_contents("php://input");
$this->parsedJson = json_decode($this->json); $this->parsedJson = json_decode($this->json);
}
if ($verifyOnLoad) { public function enableDebug(): self
$this->validate(); {
} $this->debug = true;
return $this;
} }
/** /**
@@ -79,19 +89,26 @@ class Callback
*/ */
public function validate(): bool public function validate(): bool
{ {
$localSignature = $this->localSignature();
$incomingSignature = $this->incomingSignature();
$validSignature = hash_equals( $validSignature = hash_equals(
$this->localSignature(), $localSignature,
$this->incomingSignature() $incomingSignature
); );
if (!$validSignature) { if (!$validSignature) {
throw new SignatureException('Incoming signature does not match local signature'); $message = 'Incoming signature does not match local signature';
if ($this->debug) {
$message .= ': local(' . $localSignature . ') vs incoming(' . $incomingSignature . ')';
}
throw new SignatureException($message);
} }
$validData = !is_null($this->data()); $validData = !is_null($this->data());
if (!$validData) { if (!$validData) {
throw new UnexpectedValueException('Callback data is invalid'); throw new UnexpectedValueException('Callback data is invalid. Invalid or empty JSON');
} }
return true; return true;

View File

@@ -9,4 +9,8 @@ class Constant
public const MODE_DEVELOPMENT = 'development'; public const MODE_DEVELOPMENT = 'development';
public const MODE_PRODUCTION = 'production'; public const MODE_PRODUCTION = 'production';
public const LEVEL_LOW = 0;
public const LEVEL_MEDIUM = 1;
public const LEVEL_HIGH = 2;
} }