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.
Na tej stronie:
Opis metody
Metoda zwraca publiczny klucz podpisujący dla wersji.
Parameters
Nazwa pola | Opis | Typ | Wymagalność | Dodatkowe uwagi |
| Wersja | string | Y |
|
Request – brak
Response
Nazwa pola | Opis | Typ | Wymagalność | Dodatkowe uwagi |
| Id merchanta nadawane przez InPost Pay | string | O |
|
| Obiekt zwracający informacje o kluczach publicznych dla danej wersji | object | O |
|
| Klucz publiczny (base64) | string | O |
|
| Wersja | string | O |
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, ]; } }