From 45b0f1690a8d5906cabe0e295d07c6a4ceca7dac Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 13 Feb 2025 11:24:22 +0400 Subject: [PATCH 1/2] feat(RequestFactory, V4): Add methode to set define an offset on OrderId Add possibility to set an offset on orderId from EbicsClient Object. --- src/EbicsClient.php | 10 ++++++++++ src/Factories/RequestFactory.php | 12 ++++++++++++ src/Factories/RequestFactoryV24.php | 2 +- src/Services/CryptService.php | 19 ++++++++++++++++--- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/EbicsClient.php b/src/EbicsClient.php index 81e83c2d..39894d38 100644 --- a/src/EbicsClient.php +++ b/src/EbicsClient.php @@ -1436,4 +1436,14 @@ public function changeKeyringPassword(string $newPassword): void $this->keyring->setPassword($newPassword); } + + public function setOrderIdOffset(int $offset): void + { + $this->requestFactory->setOrderIdOffset($offset); + } + + public function getOrderIdOffset(): int + { + return $this->requestFactory->getOrderIdOffset(); + } } diff --git a/src/Factories/RequestFactory.php b/src/Factories/RequestFactory.php index fc47387c..2288aa83 100644 --- a/src/Factories/RequestFactory.php +++ b/src/Factories/RequestFactory.php @@ -49,6 +49,8 @@ abstract class RequestFactory protected UserSignatureHandler $userSignatureHandler; protected CryptService $cryptService; protected ZipService $zipService; + protected int $orderIdOffset = 0; + public function __construct( Bank $bank, @@ -1020,6 +1022,16 @@ public function prepareStandardContext(RequestContext $requestContext = null): R return $requestContext; } + public function setOrderIdOffset(int $orderIdOffset): void + { + $this->orderIdOffset = $orderIdOffset; + } + + public function getOrderIdOffset(): int + { + return $this->orderIdOffset; + } + abstract public function prepareDownloadContext(RequestContext $requestContext = null): RequestContext; abstract public function prepareUploadContext(RequestContext $requestContext = null): RequestContext; diff --git a/src/Factories/RequestFactoryV24.php b/src/Factories/RequestFactoryV24.php index a27e6ad1..a49b0b73 100644 --- a/src/Factories/RequestFactoryV24.php +++ b/src/Factories/RequestFactoryV24.php @@ -44,7 +44,7 @@ protected function addOrderType( $orderAttribute = OrderDetailsBuilder::ORDER_ATTRIBUTE_DZHNN; } - $orderId = $this->cryptService->generateOrderId($this->user->getPartnerId()); + $orderId = $this->cryptService->generateOrderId($this->user->getPartnerId(), $this->getOrderIdOffset()); return $orderDetailsBuilder ->addOrderType($orderType) diff --git a/src/Services/CryptService.php b/src/Services/CryptService.php index 9fcd2b9c..e0018671 100644 --- a/src/Services/CryptService.php +++ b/src/Services/CryptService.php @@ -450,17 +450,30 @@ public function getPublicKeyDetails(string $publicKey): array } /** - * Generate order id from A000 to ZZZZ + * Generate order id from A000 to ZZZZ with offset. * Unique value per partner for customer. * * @param string $partnerId + * @param int $offset * * @return string */ - public function generateOrderId(string $partnerId): string + public function generateOrderId(string $partnerId, int $offset = 0): string { $hash = $this->hash($partnerId, 'crc32', false); + $hashToDesc = hexdec($hash); + + $hasDeschWithOffset = $hashToDesc + $offset; + + if ($hasDeschWithOffset > 0xffffffff) { + $hasDeschWithOffset = $hasDeschWithOffset - 0xffffffff; + } + + $hash = dechex($hasDeschWithOffset); + + $hash = str_pad($hash, 8, '0', STR_PAD_LEFT); + $decs = str_split($hash, 2); $first = true; @@ -480,7 +493,7 @@ public function generateOrderId(string $partnerId): string continue; } - $chrCode = $dec % $letDecRng; + $chrCode = ($dec + $offset) % $letDecRng; if ($chrCode > $letRng) { $chrs[] = $chrCode - $letRng; From fbf73ce56b4bce5f33e746a0789173e10a3ae4d8 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 13 Feb 2025 11:27:46 +0400 Subject: [PATCH 2/2] fix(CryptService): Remove useless offset calculation from character code --- src/Services/CryptService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/CryptService.php b/src/Services/CryptService.php index e0018671..f36ec669 100644 --- a/src/Services/CryptService.php +++ b/src/Services/CryptService.php @@ -493,7 +493,7 @@ public function generateOrderId(string $partnerId, int $offset = 0): string continue; } - $chrCode = ($dec + $offset) % $letDecRng; + $chrCode = $dec % $letDecRng; if ($chrCode > $letRng) { $chrs[] = $chrCode - $letRng;