Downloading a key

The article contains a description of the method used to download public signature keys together with an example implementation of the method in PHP.

Description of the method

The method returns signature public keys

Parameters – none

Request – none

Response 

Field name

Description

Type

Requirement status

Additional remarks

merchant_external_id

A merchant's Id assigned by Inpost Pay

string

O

"merchant_external_id":"e2c8a85..."}

public_keys

An object which returns any information regarding the public keys for the particular version (list)

array

O

 

public_keys.public_key_base64

Public key (base64)

string

O

"public_keys":[{"public_key_base64":"MIIBIjANBg...","version":"ub..."}]

public_keys.version

Version

string

O


GET /v1/izi/signing-keys/public

Example implementation in PHP

 

  • Entering the code - .

Only the getSigningKeys function has been added to the existing code in the implementacjaklienta.txt file.

/** * Client symfony implementation used for communication between Merchant and InPost Pay application. * * @param InpostPayBearerServiceInterface $inpostPayBearerService <p> Bearer service interface, * with contains creating bearer token which is required for communication with Inpost Pay </p> * */ final class InpostPayClient implements InpostPayClientInterface { private ClientInterface $client; private InpostPayURLCreatorInterface $URLCreator; private InpostPayBearerServiceInterface $bearerService; public function __construct( ClientInterface $client, InpostPayURLCreatorInterface $URLCreator, InpostPayBearerServiceInterface $bearerService ) { $this->client = $client; $this->URLCreator = $URLCreator; $this->bearerService = $bearerService; } public function getSigningKeys(): SigningKeysResponse { $path = 'signing-keys/public'; $uri = new Uri($this->buildUri($path)); $request = new Request( 'GET', $uri, $this->provideDefaultClientHeaders() ); try { $response = $this->client->sendRequest($request); $statusCode = $response->getStatusCode(); $content = $response->getBody()->getContents(); if (HttpResponseStatus::OK()->getValue() === $statusCode) { /** * @var SigningKeysResponseArray $data */ $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); return SigningKeysResponse::fromArray($data); } } catch (\Throwable $throwable) { throw InpostPayGetSigningKeysException::createResponseException($throwable); } throw InpostPayGetSigningKeysException::createBadStatusCodeException($statusCode, $content); } /** * @return array{ * Authorization: string, * Content-Type: string * } * * @throws InpostPayEndpointException */ private function provideDefaultClientHeaders(): array { return [ 'Authorization' => sprintf('Bearer %s', $this->bearerService->getBearerToken()), 'Content-Type' => 'application/json', ]; } private function buildUri(string $path): string { return sprintf( '%s%s', $this->URLCreator->create()->getUrl(), $path ); } }
  • The following classes have been used in the given function:

    • class SigningKeysResponse implements \JsonSerializable { private ?string $merchantExternalId; /** * @var PublicKey[] */ private array $publicKeys; /** * @param PublicKey[] $publicKeys */ public function __construct(string $merchantExternalId = null, array $publicKeys = []) { $this->merchantExternalId = $merchantExternalId; $this->publicKeys = $publicKeys; } /** * @param SigningKeysResponseArray $data */ public static function fromArray(array $data): SigningKeysResponse { $publicKeys = []; if (isset($data['public_keys'])) { $publicKeys = array_map(function ($publicKey) { return PublicKey::fromArray($publicKey); }, $data['public_keys']); } return new SigningKeysResponse( $data['merchant_external_id'] ?? null, $publicKeys ); } public function getMerchantExternalId(): ?string { return $this->merchantExternalId; } /** * @return PublicKey[] */ public function getPublicKeys(): array { return $this->publicKeys; } /** * @return SigningKeysResponseArray */ public function jsonSerialize(): array { return [ 'merchant_external_id' => $this->merchantExternalId, 'public_keys' => array_map(static fn ($item) => $item->jsonSerialize(), $this->publicKeys), ]; } }
    • class PublicKey implements \JsonSerializable { private ?string $publicKeyBase64; private ?string $version; public function __construct(string $publicKeyBase64 = null, string $version = null) { $this->publicKeyBase64 = $publicKeyBase64; $this->version = $version; } /** * @param PublicKeyArray $data */ public static function fromArray(array $data): PublicKey { return new PublicKey( $data['public_key_base64'] ?? null, $data['version'] ?? null, ); } public function getPublicKeyBase64(): ?string { return $this->publicKeyBase64; } public function getVersion(): ?string { return $this->version; } /** * @return PublicKeyArray */ public function jsonSerialize(): array { return [ 'public_key_base64' => $this->publicKeyBase64, 'version' => $this->version, ]; } }
  • The following exception has been used in the given function: