PHP Example for /sales API Request
<?php
require 'vendor/autoload.php'; // Include Composer autoload
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
// Define constants for key values
define('API_BASE_URI', 'https://vcr.am/api/v1/');
define('API_KEY', 'YOUR_API_KEY'); // In production, better to store in .env file
define('CASHIER_ID', 1); // Cashier ID
define('DEPARTMENT_ID', 3); // Department ID
define('OFFER_EXTERNAL_ID', 'ABC123'); // External ID for the offer
$client = new Client([
'base_uri' => API_BASE_URI,
'timeout' => 10.0, // Set timeout for requests
]);
// Data for the POST request
$data = [
'cashier' => [
'id' => CASHIER_ID // Use constant for cashier ID
],
'items' => [
[
'offer' => [
'externalId' => OFFER_EXTERNAL_ID // Use constant for External ID
],
'department' => [
'id' => DEPARTMENT_ID // Use constant for department ID
],
'quantity' => 1.0,
'price' => 20000.0,
'unit' => 'pc'
]
],
'amount' => [
'nonCash' => 20000.0
],
'buyer' => [
'type' => 'individual',
'receipt' => [
'email' => '[email protected]',
'language' => 'en'
]
]
];
try {
// Execute POST request
$response = $client->post('sales', [
'headers' => [
'X-API-KEY' => API_KEY, // API key from constant
'Content-Type' => 'application/json',
],
'json' => $data, // Automatically encodes the array to JSON
]);
// Process the response
$responseData = json_decode($response->getBody()->getContents(), true);
echo "Server Response: ";
print_r($responseData);
} catch (RequestException $e) {
// Handle request errors
if ($e->hasResponse()) {
$errorResponse = $e->getResponse();
$errorMessage = $errorResponse->getBody()->getContents();
echo "Server Error: " . $errorMessage;
} else {
echo "Request Error: " . $e->getMessage();
}
}
?>