Kostas Giannopoulos
Kostas Giannopoulos

Reputation: 57

Guzzle - Laravel Problem with external API POST request

I trying to connect to the following api

{
  "draft": false,
  "documenttype": "4400000040000123",
  "series": "4400000040000456",
  "number": "1",
  "date": "2018-10-25",
  "due_date": "2018-11-25",
  "client": "3300000040000100",
  "client_display_name": "Acme Inc.",
  "client_address": "4 Catherine St., Southaven, MS 38671, USA",
  "client_shipping_address": "",
  "client_profession": "",
  "client_vat_number": "",
  "client_tax_office": "string",
  "client_contact_person": "John Doe",
  "client_phone_number": "662-222-2222",
  "client_email": "[email protected]",
  "calculator_mode": "initial",
  "items": [
    {
      "product": "9900000040000600",
      "title": "Pair of socks",
      "description": "Super-cool expensive socks!",
      "quantity": "2.00",
      "unit_value": "40.00",
      "unit_discount": "0.00",
      "taxes": [
        "6600230050000705"
      ],
      "unit_total": "50.00",
      "unit_measure": 14
    }
  ],
  "withholding_taxes": [
    "6600230050000704"
  ],
  "currency_code": "USD",
  "exchange_rate": "1.000000",
  "terms": "Terms and conditions here",
  "public_notes": "Notes visible on the invoice",
  "notes": "Some notes",
  "template_settings": {
    "discount_appearance": 0,
    "hide_client_due": true,
    "hide_contact_information": true,
    "hide_creator_information": true,
    "hide_description": true,
    "hide_payments": true,
    "hide_product_code": true,
    "hide_quantity": true,
    "hide_vat": true,
    "hide_vat_table": true,
    "theme": "1100000022000129",
    "unit_measure_appearance": 0
  },
  "trackingcategories": [
    {
      "trackingcategory": "7000230020000553",
      "option": "My custom tag"
    }
  ]
}

and i am trying with guzzle (with laravel) to make a post request with the following code :

        $headers = ['authorization' => 'Token XXXXXXXXXXXXXXXXXX','x-elorus-organization' => 'XXXXXXXXXXXXXXXXXX','cookie' => 'gwshow=do',];
        $client = new \GuzzleHttp\Client();
        $url = "https://api.elorus.com/v1.0/invoices/";

        $myBody['company'] = "Demo";
        $request = $client->post($url,  [
            'form_params'=>[
                'draft'=>true,
                'documenttype' => 'XXXXXXXXXXXXXX',
                'number' => 0,
                'date' => '2020-03-18',
                'client' => 'XXXXXXXXXXXX',
                'calculator_mode' => 'initial',
                'template_settings' => array(
                    "discount_appearance" => 0,
                    "hide_client_due" => true,
                    "hide_contact_information" => true,
                    "hide_creator_information" => true,
                    "hide_description" => true,
                    "hide_payments" => true,
                    "hide_product_code" => true,
                    "hide_quantity" => true,
                    "hide_vat" => true,
                    "hide_vat_table" => true,
                    "theme" => "1100000022000129",
                    "unit_measure_appearance" => 0),         

                ],


        'headers' => $headers]);
        echo $request->getBody();

so when i execute the code i take back the following error

{
"template_settings": [
  "This field is required."
],
}

Probably i do not send, with the right way the template_settings I have been trying for 7 hours ago but without any happiness result.

Can anyone help please?

Thanks

Upvotes: 0

Views: 277

Answers (1)

Mohammedshafeek C S
Mohammedshafeek C S

Reputation: 1943

AS per API DOC create invoice expect body as json data. But you were sending it as 'form_params'.

You are missing 'items' & 'withholding_taxes' inputs and those are required fields.

You can check this guzzlephp reference for creating requests.

change your code to :

$request = $client->POST($url,  [
    /*'debug' => fopen('php://stderr', 'w'),*/
    'json'=>[
    'draft'=>true,
    'documenttype' => 'XXXXXXX',
    'number' => 0,
    'date' => '2020-03-18',
    'client' => 'XXXXXXX',
    'calculator_mode' => 'initial',
    'template_settings' => array(
        "discount_appearance" => 0,
        "hide_client_due" => true,
        "hide_contact_information" => true,
        "hide_creator_information" => true,
        "hide_description" => true,
        "hide_payments" => true,
        "hide_product_code" => true,
        "hide_quantity" => true,
        "hide_vat" => true,
        "hide_vat_table" => true,
        "theme" => "1100000022000129",
        "unit_measure_appearance" => 0),        
    ],
    'headers' => $headers
]);

You can uncomment line /*'debug' => fopen('php://stderr', 'w'),*/ if any debug needed in development.

You can check Elorus API using Postman and can match results with Laravel Application.

Use Postman for API

Upvotes: 1

Related Questions