From 084269848d3cb856da05bf801efded268bbebd39 Mon Sep 17 00:00:00 2001 From: Kajetan Nobel Date: Wed, 4 Oct 2023 12:06:03 +0200 Subject: [PATCH] feat: add mockHeaders --- src/Exceptions/SchemaValidationException.php | 2 +- src/Lib/Utils.php | 18 +++++++++++++++++- tests/Lib/UtilsTest.php | 4 +++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Exceptions/SchemaValidationException.php b/src/Exceptions/SchemaValidationException.php index fe70f47..58107e8 100644 --- a/src/Exceptions/SchemaValidationException.php +++ b/src/Exceptions/SchemaValidationException.php @@ -13,7 +13,7 @@ class SchemaValidationException extends Exception public function __construct(array $errors) { $this->errors = $errors; - parent::__construct('Given data doesnt fullfill JSON schema requirements', 422); + parent::__construct('Given data doesnt fulfill JSON schema requirements.', 422); } public function context(): array diff --git a/src/Lib/Utils.php b/src/Lib/Utils.php index 572e45c..6b40910 100644 --- a/src/Lib/Utils.php +++ b/src/Lib/Utils.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; +use Routegroup\Imoje\Payment\DTO\BaseDto; use Routegroup\Imoje\Payment\Types\HashMethod; class Utils @@ -69,7 +70,22 @@ public function transformValues(array $values): array return $computed; } - public function isEqualWithStructure(array $data, array $structure): bool + public function mockHeaders( + BaseDto $dto, + HashMethod $hashMethod = HashMethod::SHA256 + ): array { + $body = json_encode($dto->toArray(), JSON_UNESCAPED_SLASHES); + $signature = hash($hashMethod->value, $body.$this->config->serviceKey); + + $value = "merchantid={$this->config->merchantId};"; + $value .= "serviceid={$this->config->serviceId};"; + $value .= "signature=$signature;"; + $value .= "alg=$hashMethod->value"; + + return ['x-imoje-signature' => $value]; + } + + public function hasStructure(array $data, array $structure): bool { $structure = Arr::dot($structure); $data = Arr::dot($data); diff --git a/tests/Lib/UtilsTest.php b/tests/Lib/UtilsTest.php index 8e05f81..a0590c4 100644 --- a/tests/Lib/UtilsTest.php +++ b/tests/Lib/UtilsTest.php @@ -24,4 +24,6 @@ it('transforms values')->todo(); -it('checks if contains a structure')->todo(); +it('mock headers')->todo(); + +it('checks if contains structure')->todo();