/
Weryfikacja powiązania koszyka

Weryfikacja powiązania koszyka

Artykuł zawiera opis metody wykorzystywanej do weryfikacji powiązania koszyka wraz z przykładem implementacji metody w języku PHP.

Opis metody

Metoda służy do weryfikacji, czy koszyk jest powiązany oraz czy przeglądarka, z której klient aktualnie korzysta jest dodana do zaufanych.  

W danej metodzie wymagamy implementacji wszystkich pól wymienionych w tabeli, ponieważ składają się na całość usługi InPost Pay. Część poniższych pól w kolumnie 'Wymagalność' jest oznaczona jako 'O' tj. opcjonalna ze względu na to, że nie wszystkie produkty/koszyki w sklepach internetowych mają przypisane wszystkie parametry, więc koszyk może zostać utworzony, a zamówienie złożone bez nich. Jednak implementacja/wdrożenie wszystkich pól jest biznesowo WYMAGANE.

Parameters 

Nazwa pola 

Opis  

Typ 

Wymagalność 

Dodatkowe uwagi 

basket_id

Unikalny identyfikator koszyka nadawany przez merchanta 

string 

 

browser_id

Id przeglądarki zaufanej nadany przez InPost Pay, który został zwrócony w procesie wiązania koszyka w request metody POST/v1/izi/basket/{basket_id}/confirmation.  

string 

 

 

Request – brak 

Response 

Nazwa pola 

Opis  

Typ 

Wymagalność 

Dodatkowe uwagi 

basket_linked

Flaga informująca, czy koszyk jest powiązany 

boolean 

"basket_linked":true

inpost_basket_id

Identyfikator koszyka nadawany przez aplikacje InPost Pay. Dana nie wykorzystywana przez merchanta. Identyfikator docelowo wykorzystywany przez widget w celu umożliwienia prawidłowego przekierowania z poziomu widget do aplikacji InPost Pay 

 

string 

"inpost_basket_id": " 866e5bf6-4e8a-443e-98f5-f9b0c5c149dd"

browser_trusted

Flaga informująca, czy przeglądarka jest zaufana 

boolean 

"basket_linked":true

client_details

Obiekt zawracający informacje o kliencie 

object 

"client_details":{"phone_number":{"country_prefix":"+48","phone":"60000000"},"masked_phone_number":"60*****00","name":"Piotr","surname":"Nowak"

client_details.phone_number

Obiekt zwracający numer powiązany z koszykiem lub/i numer powiązany z przeglądarką zaufaną 

object 

client_details.phone_number.country_prefix

Prefix 

string 

client_details.phone_number.phone

Numer telefonu 

string 

client_details.masked_phone_number

Zamaskowany numer telefonu w formacie 60*****00 

string 

client_details.name

Imię użytkownika powiązane z numerem telefonu w aplikacji InPost Pay 

string 

client_details.surname

Nazwisko użytkownika powiązane z numerem telefonu w aplikacji InPost Pay 

string 

GET /v1/izi/basket/{basket_id}/binding

Response

{ "basket_linked": true, "browser_trusted": true, "inpost_basket_id": "38ca5c3e-3wsd-34rd-8651-12f7afe7cc24", "client_details": { "phone_number": { "country_prefix": "+48", "phone": "600000000" }, "masked_phone_number": "60*****00", "name": "Jan", "surname": "Kowalski" } }

Przykład implementacji w języku PHP

  • Podanie kodu -

W pliku implementacjaKlienta.txt została dodana tylko funkcja getBasketBinding.

/** * 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 getBasketBinding(string $basketId, string $browserId = null): BindingBasket { $path = sprintf('basket/%s/binding', $basketId); $uri = (new Uri($this->buildUri($path))) ->withQuery(Psr7\Query::build(['browser_id' => $browserId])); $request = new Request( 'GET', $uri, $this->provideDefaultClientHeaders() ); try { $response = $this->client->sendRequest($request); $content = $response->getBody()->getContents(); /** * @var BindingBasketArray $data */ $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); return BindingBasket::fromArray($data); } catch (\Throwable $throwable) { throw InpostPayGetBasketBindingException::createResponseException($throwable); } } public function postBasketBinding(string $basketId, BasketBindingRequest $requestBody): ?QrCodeData { if (BindingMethod::PHONE()->equals($requestBody->getBindingMethod()) && !$requestBody->getPhoneNumber()) { throw InpostPayPostBasketBindingException::createNoPhoneForPhoneBindingMethodException(); } $path = sprintf('basket/%s/binding', $basketId); $uri = new Uri($this->buildUri($path)); /** @var string $body */ $body = json_encode($requestBody); $request = new Request( 'POST', $uri, $this->provideDefaultClientHeaders(), $body ); try { $response = $this->client->sendRequest($request); $statusCode = $response->getStatusCode(); $content = $response->getBody()->getContents(); if (HttpResponseStatus::ACCEPTED()->getValue() === $statusCode) { return null; } if (HttpResponseStatus::OK()->getValue() === $statusCode) { /** * @var QrCodeDataArray $data */ $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); return QrCodeData::fromArray($data); } } catch (\Throwable $throwable) { throw InpostPayPostBasketBindingException::createResponseException($throwable); } throw InpostPayPostBasketBindingException::createBadStatusCodeException($statusCode, $content); } public function putBasket(string $basketId, Basket $basket): InpostBasketId { $path = sprintf('basket/%s', $basketId); $uri = new Uri($this->buildUri($path)); /** @var string $body */ $body = json_encode($basket); $request = new Request( 'PUT', $uri, $this->provideDefaultClientHeaders(), $body ); try { $response = $this->client->sendRequest($request); $content = $response->getBody()->getContents(); /** * @var array{inpost_basket_id: string} $responseData */ $responseData = json_decode($content, true, 512, JSON_THROW_ON_ERROR); return InpostBasketId::fromArray($responseData); } catch (\Throwable $throwable) { throw InpostPayPutBasketException::createResponseException($throwable); } } public function deleteBasketBinding(string $basketId, bool $ifBasketRealized = null): void { $path = sprintf('basket/%s/binding', $basketId); $uri = (new Uri($this->buildUri($path))) ->withQuery(Psr7\Query::build(['if_basket_realized' => $ifBasketRealized])); $request = new Request( 'DELETE', $uri, $this->provideDefaultClientHeaders() ); try { $response = $this->client->sendRequest($request); $statusCode = $response->getStatusCode(); $content = $response->getBody()->getContents(); } catch (\Throwable $throwable) { throw InpostPayDeleteBasketBindingException::createResponseException($throwable); } if (HttpResponseStatus::NO_CONTENT()->getValue() === $statusCode) { return; } throw InpostPayDeleteBasketBindingException::createBadStatusCodeException($statusCode, $content); } public function deleteBrowserBinding(string $browserId): void { $path = sprintf('browser/%s/binding', $browserId); $uri = new Uri($this->buildUri($path)); $request = new Request( 'DELETE', $uri, $this->provideDefaultClientHeaders() ); try { $response = $this->client->sendRequest($request); $statusCode = $response->getStatusCode(); $content = $response->getBody()->getContents(); } catch (\Throwable $throwable) { throw InpostPayDeleteBrowserBindingException::createResponseException($throwable); } if (HttpResponseStatus::NO_CONTENT()->getValue() === $statusCode) { return; } throw InpostPayDeleteBrowserBindingException::createBadStatusCodeException($statusCode, $content); } 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 SDK.zip zawiera się kod zawierający obiekt requestu oraz response endpointu.

 


 

Related content

Powiązanie koszyka
Powiązanie koszyka
More like this
Potwierdzenie powiązania koszyka
Potwierdzenie powiązania koszyka
More like this
Aktualizacja lub dodanie koszyka
Aktualizacja lub dodanie koszyka
More like this
InPost Pay (Basket App)
InPost Pay (Basket App)
Read with this
Aktualizacja zamówienia
Aktualizacja zamówienia
More like this
Widget 2.0
Read with this