0

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>';
Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • 1
    Looks like the API expects a JSON request body. – Phil Apr 20 '21 at 07:18
  • What have you tried to debug the problem? If an API returns an error like this, it's most likely that you haven't built the request properly. For example, according to the validation error, you've posted an empty body – Nico Haase Apr 20 '21 at 07:19
  • How can I "create" an JSON request body? – Trombone0904 Apr 20 '21 at 07:20
  • The docs you linked to show [the request body should be JSON](https://app.invoiz.de/api/documentation/#/invoices/PostInvoice). – Don't Panic Apr 20 '21 at 07:22
  • https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl – Don't Panic Apr 20 '21 at 07:23
  • I updated my first post. But the same result :( – Trombone0904 Apr 20 '21 at 07:25
  • 1
    Please read the linked answer carefully. You need to pass a JSON **string** for the `CURLOPT_POSTFIELDS` parameter. `json_encode()` produces a JSON string from an array or object. If you already have a JSON string, you do **not** want to decode it. You also need to set the appropriate `Content-type` header – Phil Apr 20 '21 at 07:34
  • Please look at my first post. Update V2 – Trombone0904 Apr 20 '21 at 08:41
  • Please check for errors on your own first - as you can see in the linked documentation, you should send an array of articles. You are sending a single article instead – Nico Haase Apr 20 '21 at 08:43
  • Is this not an array?? `"articles" => array ("id" => 45203,"amount" => 5,"discount" => 10 ) );` – Trombone0904 Apr 20 '21 at 08:47
  • Thank You ! Found my mistake. I will update my first post – Trombone0904 Apr 20 '21 at 08:52

0 Answers0