Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions src/Unzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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.
*/
Expand All @@ -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.
*
Expand All @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions test/unit/UnzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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());
}

/**
Expand All @@ -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);
Expand Down
Loading