1

I am trying to create a Sales Invoice through Sage Accounting API calls (its documentation can be found here: https://developer.sage.com/api/accounting/api/)

To make my code clearer I have created a class that helps me make those calls accordingly.

Here is the method I use to make those calls:

public function postRequest()
{
    $url = $this->baseEndpoint . $this->request;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if (isset($this->params)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
    }

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer $this->token",
        "Host: api.accounting.sage.com",
        "Content-Type: application/json"
    ));
    $response = json_decode(curl_exec($ch), true);
    curl_close($ch);
    return $response;
}

How I call this method:

$params = array(
"sales_invoice" => array(
    "contact_id" => "485fdfe0be154f9c9af44351de16e5be",
    "date" => "2019-06-13",
    "invoice_lines" => array(
        array(
            "description" => "description",
            "ledger_account_id" => "f04157c90ff0496ab3a22f2558e46010",
            "unit_price" => 10   ,
            "quantity" => 1,
            "tax_rate_id" => "ES_RE_STANDARD",
            "tax_rate" => 0.1
        )
    )
)
);

$params = json_encode($params);
$request = "v3.1/sales_invoices";
$sageRequest = new SageRequest($token, $request, $params);
$sageRequest->postRequest();

According to the API documentation, that should work, but still I get this error:

[$severity] => error
[$dataCode] => UnexpectedError
[$message] => An unexpected error occurred.
[$source] => 

If there is anyone who has some experience with the Sage Accounting API, I would be more than grateful to know what I have done wrong.

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

This example works for me on a Spanish business:

{
  "sales_invoice": {
    "contact_id": "22b609fba11642238f2ecd0f5fe3e0b5",
    "date": "2019-06-12",
    "invoice_lines": [
      {
        "description": "Description",
        "ledger_account_id": "829739738de811e996c90122ae3d08ca",
        "quantity": 1,
        "unit_price": 100,
        "tax_rate_id": "ES_STANDARD"
      }
    ],
    "main_address": {
      "city": "Madrid"
    }
  }
}

Make sure your contact is listed in the contact endpoint. Use GET https://api.accounting.sage.com/v3.1/ledger_accounts?visible_in=sales to get a list of all valid ledger accounts for sales objects.

I see your question uses ES_RE_STANDARD as tax rate. I will update this answer soon with an example for the "recargo de equivalencia" tax rate.

Christoph Petschnig
  • 4,047
  • 1
  • 37
  • 46
  • Hello, thank you for the answer. I have used your example (using one of my contacts and ledger accounts, of course) and got an error saying: Entschuldigung - Zurzeit besteht ein technisches Problem. I stubmled upon this error before, but it looks like it's a technical problem. I find it hard to believe though... – CiprianIvan Jun 14 '19 at 07:07
  • This error probably comes from posting invalid JSON. Can you ensure you are posting valid JSON? This is definitely poor error handling and will be fixed soon for sure. – Christoph Petschnig Jun 14 '19 at 08:36
  • Yes, it is valid. – CiprianIvan Jun 14 '19 at 09:42
  • Can you please send the request with some details to sageonedevelopers@sage.com, including the `x-request-id` response header? This will help us to query the logs to help you better. – Christoph Petschnig Jun 14 '19 at 10:28
  • Yes, I have just sent you the email. – CiprianIvan Jun 14 '19 at 11:17