From 70dca69a148fcd9ab206b0886dc6cc54e0583dd9 Mon Sep 17 00:00:00 2001 From: "david.owusu" Date: Fri, 8 May 2026 15:29:30 +0200 Subject: [PATCH] [CC-3640] `\UnzerSDK\Unzer` can now be initialised with a public key. --- CHANGELOG.md | 6 ++++++ src/Unzer.php | 15 ++++++++------- test/unit/UnzerTest.php | 19 +++++++++++++++---- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e28978ab..1e63b3134 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [4.0.1](https://github.com/unzerdev/php-sdk/compare/4.0.0..4.0.1) + +### Changed + +* `\UnzerSDK\Unzer` can now be initialised with a public key (`s-pub-*` / `p-pub-*`), enabling `fetchConfig()` to be called without a private key. + ## [4.0.0](https://github.com/unzerdev/php-sdk/compare/3.15.0..4.0.0) ### Added diff --git a/src/Unzer.php b/src/Unzer.php index 846010d89..23f2366b5 100644 --- a/src/Unzer.php +++ b/src/Unzer.php @@ -44,6 +44,7 @@ use UnzerSDK\Services\ResourceService; use UnzerSDK\Services\WebhookService; use UnzerSDK\Validators\PrivateKeyValidator; +use UnzerSDK\Validators\PublicKeyValidator; /** * This is the Unzer object which is the base object providing all functionalities needed to @@ -95,10 +96,10 @@ class Unzer implements /** * Construct a new Unzer object. * - * @param string $key The private key your received from your Unzer contact person. + * @param string $key The private or public key your received from your Unzer contact person. * @param string|null $locale The locale of the customer defining defining the translation (e.g. 'en-GB' or 'de-DE'). * - * @throws RuntimeException A RuntimeException will be thrown if the key is not of type private. + * @throws RuntimeException A RuntimeException will be thrown if the key is not a valid private or public key. * * @link https://docs.unzer.com/integrate/web-integration/#section-localization-and-languages * @@ -116,7 +117,7 @@ public function __construct(string $key, ?string $locale = '') } /** - * Returns the set private key used to connect to the API. + * Returns the set key used to connect to the API. * * @return string The key that is currently set. */ @@ -126,9 +127,9 @@ public function getKey(): string } /** - * Sets your private key used to connect to the API. + * Sets your private or public key used to connect to the API. * - * @param string $key The private key. + * @param string $key The private or public key. * * @return Unzer This Unzer object. * @@ -138,8 +139,8 @@ public function getKey(): string */ public function setKey(string $key): Unzer { - if (!PrivateKeyValidator::validate($key)) { - throw new RuntimeException('Illegal key: Use a valid private key with this SDK!'); + if (!PrivateKeyValidator::validate($key) && !PublicKeyValidator::validate($key)) { + throw new RuntimeException('Illegal key: Use a valid private or public key with this SDK!'); } $this->key = $key; diff --git a/test/unit/UnzerTest.php b/test/unit/UnzerTest.php index 771196bce..c9c22488e 100644 --- a/test/unit/UnzerTest.php +++ b/test/unit/UnzerTest.php @@ -12,7 +12,8 @@ namespace UnzerSDK\test\unit; use DateTime; -use UnzerSDK\Unzer; +use PHPUnit\Framework\MockObject\MockObject; +use RuntimeException; use UnzerSDK\Resources\Basket; use UnzerSDK\Resources\Customer; use UnzerSDK\Resources\Metadata; @@ -30,8 +31,7 @@ use UnzerSDK\Services\WebhookService; use UnzerSDK\test\BasePaymentTest; use UnzerSDK\test\unit\Services\DummyDebugHandler; -use PHPUnit\Framework\MockObject\MockObject; -use RuntimeException; +use UnzerSDK\Unzer; class UnzerTest extends BasePaymentTest { @@ -56,6 +56,12 @@ public function constructorShouldInitPropertiesProperly(): void $unzerGerman = new Unzer('s-priv-1234', 'de-DE'); $this->assertEquals('de-DE', $unzerGerman->getLocale()); + + $unzerPub = new Unzer('s-pub-1234'); + $this->assertEquals('s-pub-1234', $unzerPub->getKey()); + + $unzerProdPub = new Unzer('p-pub-1234'); + $this->assertEquals('p-pub-1234', $unzerProdPub->getKey()); } /** @@ -75,9 +81,14 @@ public function gettersAndSettersShouldWorkProperly(): void $unzer->setKey('this is not a valid key'); $this->assertTrue(false, 'This exception should have been thrown'); } catch (RuntimeException $e) { - $this->assertEquals('Illegal key: Use a valid private key with this SDK!', $e->getMessage()); + $this->assertEquals('Illegal key: Use a valid private or public key with this SDK!', $e->getMessage()); } + $unzer->setKey('s-pub-1234'); + $this->assertEquals('s-pub-1234', $unzer->getKey()); + $unzer->setKey('p-pub-1234'); + $this->assertEquals('p-pub-1234', $unzer->getKey()); + $httpService = new HttpService(); $this->assertNotSame($httpService, $unzer->getHttpService()); $unzer->setHttpService($httpService);