Pobranie klucza dla wersji

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

 

Opis metody

Metoda zwraca publiczny klucz podpisujący dla wersji.

Parameters 

Nazwa pola 

Opis  

Typ 

Wymagalność 

Dodatkowe uwagi 

version

Wersja 

string 

  

  

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 

object 

 

public_keys.public_key_base64

Klucz publiczny (base64) 

string 

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

public_keys.version

Wersja  

string 


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

 

Przykład implementacji w języku PHP

 

  • Podanie kodu -

Do istniejącego kodu w pliku implementacjaKlienta.txt została dodana 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 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 ); } }
  • W implementacji tej funkcji użyta została klasa

    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, ]; } }