Good morning !
I would like to use the API of invoiz.de (via PHP). I have this documentation: https://app.invoiz.de/api/documentation/#/auth , an api key, secret api key and the InstallationID.
With this code I can take my "Bearer token":
// TOKEN
$curl = curl_init('https://app.invoiz.de/api/auth/token');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $secretApiKey);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'installationId' => $installationId
)));
$content = curl_exec($curl);
$token = json_decode($content);
curl_close($curl);
With this code I can show all my articles:
// GET ARTICEL
$curl = curl_init('https://app.invoiz.de/api/article');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $secretApiKey);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token->token
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'limit' => 20,
'orderBy' => 'title'
)));
$content = curl_exec($curl);
$article = json_decode($content);
curl_close($curl);
echo '<pre>';
print_r($article);
echo '</pre>';
Now I would like to create an invoice. The documentation need this request body:
{
"date": "2020-03-01T00:00:00.000Z",
"customerId": 542,
"priceKind": "net",
"deliveryDate": "2020-03-01T00:00:00.000Z",
"articles": [
{
"id": 143,
"amount": 5,
"discount": 10
}
]
}
I tried this:
// CREATE INVOICE
$curl = curl_init('https://app.invoiz.de/api/v2/invoice');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $secretApiKey);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token->token
));
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'date' => '2021-04-20T00:00:00.000Z',
'customerId' => 100110,
'deliveryDate' => '2021-04-20T00:00:00.000Z',
'priceKind' => 'net',
'articles' => array(
'id' => MY_ARTICLE_ID,
'amount' => 2,
'discount' => 10
)
));
$content = curl_exec($curl);
$invoice = json_decode($content);
curl_close($curl);
echo '<pre>';
print_r($invoice);
echo '</pre>';
Result:
stdClass Object
(
[name] => ValidationError
[message] => Please check your entries.
[meta] => stdClass Object
(
[body] => Array
(
[0] => stdClass Object
(
[code] => NOT_EMPTY
[message] => Please check your entries.
)
)
)
)
Where is my mistake ?
SOLUTION
// CREATE INVOICE
$values = array(
"date" => "2020-03-01T00:00:00.000Z",
"customerId" => 100110,
"priceKind" => "net",
"deliveryDate" => "2020-03-01T00:00:00.000Z",
"articles" => array [("id" => 45203 ,"amount" => 5,"discount" => 10 )] );
$data = json_encode($values);
$curl = curl_init('https://app.invoiz.de/api/v2/invoice');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $secretApiKey);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Authorization: Bearer '.$token->token
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($curl);
$invoice = json_decode($content);
curl_close($curl);
echo '<pre>';
print_r($invoice);
echo '</pre>';