Pobranie klucza

Artykuł zawiera opis metody wykorzystywanej do pobrania publicznych kluczy podpisujących wraz z przykładem implementacji metody w języku PHP.

Opis metody

Metoda zwraca publiczne klucze podpisujące

Parameters – brak 

Request – brak 

Response 

Nazwa pola 

Opis  

Typ 

Wymagalność 

Dodatkowe uwagi 

merchant_external_id

Id merchanta nadawane przez Inpost Pay 

string 

"merchant_external_id":"e2c8a85..."}

public_keys

Obiekt zwracający informacje o kluczach publicznych dla danej wersji (lista) 

array 

 

public_keys.public_key_base64

Klucz publiczny (base64) 

string 

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

public_keys.version

Wersja  

string 


GET /v1/izi/signing-keys/public

Przykład implementacji w języku PHP

 

  • Podanie kodu - .

Do istniejącego kodu w pliku implementacjaKlienta.txt została dodana jedynie funkcja getSigningKeys.

/** * 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 ); } }
  • W podanej funkcji korzystano z poniższych klas:

    • 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, ]; } }
  • W podanej funkcji korzystano z exceptiona: