0

I am currently integrating an application with the Sage One API and am having a problem. The API says that invoice lines may not be empty, but the data is there. What am I doing wrong?

Here is my method of getting the line items:

$lineItems = [];
    foreach ($invoiceItems as $invoiceItem){
      $lineItems[] =
        [
          "SelectionId" => "35175771",
          "TaxTypeId" => "6194291",
          "Description" => $invoiceItem->name,
          "Quantity" => $invoiceItem->duration,
          "UnitPriceExclusive" => $invoiceItem->total_excl_vat
        ];
    }

    foreach ($invoiceOtherItems as $invoiceOtherItem){
      $lineItems[] =
        [
           "SelectionId" => "35175771",
          "TaxTypeId" => "6194291",
          "Description" => $invoiceOtherItem->otherItem->name,
          "Quantity" => $invoiceOtherItem->quantity,
          "UnitPriceExclusive" => $invoiceOtherItem->total_excl_vat
        ];
    }

//dd($lineItems)

And here is the part in the post data to the API where I post the invoice items (removed majority items for the sake of brevity here):

$invoice = [
      "Date" => Carbon::now()->toDateString(),
      "Lines" => $lineItems,
      "DueDate" => "2021-08-29"
    ];

Performing a dump and die where I commented the dd returns all the arrays, yet the API is telling me lines are required. Am I doing anything wrong? The code seems correct to me and I can't find anything to help on this matter.

jelhan
  • 6,149
  • 1
  • 19
  • 35
Mike
  • 67
  • 1
  • 7
  • The official API documentation is a good place to find help: https://developer.sage.com/api/accounting/api/invoicing-sales/#operation/getSalesInvoices At first glance it looks like you pass "Lines", but it should be "invoice_lines". – Christoph Petschnig Jul 30 '21 at 10:58
  • It is "Lines". I have managed to solve the problem on my own. There was nothing wrong with my code, but rather their API which is not standardized. Some endpoints seek encoded JSON while others take raw JSON. Their API documentation is useless in this instance. – Mike Jul 30 '21 at 12:36

1 Answers1

0

For anyone that might run across this problem here is the solution:

In your response, define your headers like this:

'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json']

And do not use form_params, but use this instead:

'body' => json_encode($invoice)
Mike
  • 67
  • 1
  • 7