Verification whether basket is linked
This article describes a method used to verify whether a basket is linked, along with an example of its implementation in PHP.
On this page:
Description of the method
This method is used to verify whether a basket is linked and whether a browser currently being used by the customer is added as trusted.
For this method, we require the implementation of all the fields listed in the table, as they make up the entirety of the InPost Pay service. Some of the following fields in the 'Required' column are marked as 'O', i.e. optional, due to the fact that not all products/baskets in online stores have all parameters assigned, so a basket can be created and an order placed without them. However, the implementation/deployment of all fields is REQUIRED from a business perspective.
Parameters
Field name | Description | Type | Requirement status | Additional remarks |
| Unique basket identifier assigned by the merchant | string | Y |
|
| Trusted browser ID assigned by InPost Pay that was returned in the process of linking the basket in the request of the POST/v1/izi/basket/{basket_id}/confirmation method. | string | O |
|
Request – none
Response
Field name | Description | Type | Requirement status | Additional remarks |
| A flag indicating whether the basket is linked | boolean | Y |
|
| Basket identifier assigned by the InPost Pay app. Data not used by the merchant. Identifier will ultimately be used by the widget to enable proper redirection from the widget to the InPost Pay app
| string | O |
|
| A flag indicating whether the browser has been added to trusted | boolean | Y |
|
| An object that returns information about the customer | object | O |
|
| An object that returns the number linked to the basket and/or the number linked to the trusted browser | object | O | |
| Prefix | string | Y | |
| Phone number | string | Y | |
| Masked phone number in the 60*****00 format | string | O | |
| First name of the user linked to the phone number in the InPost Pay app | string | O | |
| Surname of the user linked to the phone number in the InPost Pay app | string | O |
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"
}
}
Example of implementation in PHP
Code -
Only the getBasketBinding function has been added 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 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
);
}
}
SDK.zip contains a code which includes the object of the request and of the endpoint response.