Downloading the key for the version

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

 

Description of the method

The method returns the public signature key for the version.

Parameters 

Field name

Description

Type

Requirement status

Additional remarks

version

Version

string

Y

  

  

Request – none 

Response 

Field name

Description

Type

Requirement status

Additional remarks

merchant_external_id

A merchant's Id being 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

object

O

 

public_keys.public_key_base64

Public key (base64)

string

O

"public_keys":{"public_key_base64":"MIIBIjANBgkqhkiG9w0...","version":"ubp..."}

public_keys.version

Version

string

O


GET /v1/izi/signing-keys/public/{version}

 

Example implementation in PHP

 

  • Entering the code -

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 getSigningKey(string $version): SigningKeyResponse { $path = sprintf('signing-keys/public/%s', $version); $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 SigningKeyResponseArray $data */ $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); return SigningKeyResponse::fromArray($data); } } catch (\Throwable $throwable) { throw InpostPayGetSigningKeyException::createResponseException($throwable); } throw InpostPayGetSigningKeyException::createBadStatusCodeException($statusCode, $content); } 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 ); } }
  • In the implementation of this function, the following class has been used

    class SigningKeyResponse implements \JsonSerializable { private ?string $merchantExternalId; private ?PublicKey $publicKey; public function __construct(string $merchantExternalId = null, PublicKey $publicKey = null) { $this->merchantExternalId = $merchantExternalId; $this->publicKey = $publicKey; } /** * @param SigningKeyResponseArray $data */ public static function fromArray(array $data): SigningKeyResponse { return new SigningKeyResponse( $data['merchant_external_id'] ?? null, isset($data['public_key']) ? PublicKey::fromArray($data['public_key']) : null, ); } public function getMerchantExternalId(): ?string { return $this->merchantExternalId; } public function getPublicKey(): ?PublicKey { return $this->publicKey; } /** * @return SigningKeyResponseArray */ public function jsonSerialize(): array { return [ 'merchant_external_id' => $this->merchantExternalId, 'public_key' => isset($this->publicKey) ? $this->publicKey->jsonSerialize() : null, ]; } }