0

I have checked documents and tried various combinations (to the best of my ability) I am attempting to create an invoice in Xero using PHP. I found this answer the closet I've got to completing the task:

xero PHP variable which starts with {

However I am still unable to pass data correctly (resulting in a 400 error: "ErrorNumber": 17, "Type": "NoDataProcessedException", "Message": "No data has been processed for this endpoi (truncated...)"):

    $invoices = '{"invoices":[{"type":"Invoice.TypeEnum.ACCREC","contact":{"contactID":"00000000-0000-0000-000-000000000000"},"lineItems":[{"description":"Acme Tires","quantity":2,"unitAmount":20,"accountCode":"000","taxType":"NONE","lineAmount":40}],"date":"2019-03-11","dueDate":"2018-12-10","reference":"Website Design","status":"Invoice.StatusEnum.DRAFT"}]}';
    $invoices = json_encode($invoices);
    
$summarize_errors = true; // bool | If false return 200 OK and mix of successfully created objects and any with validation errors
$unitdp = 4; // int | e.g. unitdp=4 – (Unit Decimal Places) You can opt in to use four decimal places for unit amounts

try {
    $result = $apiInstance->createInvoices($xeroTenantId, $invoices, TRUE, 4);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling AccountingApi->createInvoices: ', $e->getMessage(), PHP_EOL;
}

I have also attempted (resulting in a 500 error: "Title":"An error occurred","Detail":"An error occurred in Xero. Check the API Status page http://status.developer.xero (truncated...)"):

$result = $apiInstance->getContacts($xeroTenantId);
$contactId = $result->getContacts()[0]->getContactId();

$contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
$contact->setContactId($contactId);

$arr_invoices = []; 
$lineitems = "TEST";
$invoice_1 = new XeroAPI\XeroPHP\Models\Accounting\Invoice;
$invoice_1->setReference('Ref-456')
    ->setDueDate(new DateTime('2019-12-10'))
    ->setContact($contact)
    ->setLineItems($lineitems)
    ->setStatus(XeroAPI\XeroPHP\Models\Accounting\Invoice::STATUS_AUTHORISED)
    ->setType(XeroAPI\XeroPHP\Models\Accounting\Invoice::TYPE_ACCREC)
    ->setLineAmountTypes(\XeroAPI\XeroPHP\Models\Accounting\LineAmountTypes::EXCLUSIVE);    
array_push($arr_invoices, $invoice_1);


$invoices = new XeroAPI\XeroPHP\Models\Accounting\Invoices;
$invoices->setInvoices($arr_invoices);

try {
$result = $apiInstance->createInvoices($xeroTenantId,$invoices, TRUE , 4); 
    print_r($result);
} catch (Exception $e) {
    echo 'Error : ', $e->getMessage(), PHP_EOL;
}

If anyone can shine light or provide an update to either snippet I would be extremely grateful, thank you.

pbytes
  • 1
  • 8
  • Isn't that first string `$invoices` already in a JSON-encoded format, and then you encode it again? I use XML myself so I can't really be sure, and I do it from VB so I can't share a code snippet. – droopsnoot Dec 14 '20 at 09:20
  • In the second code block, your line items don't look correct to me: `$lineitems = "TEST";` is just a string, you need to create an array containing one or more lineitem arrays, each of which must have at least a description and possibly some more fields that I've forgotten. If you look here there are examples of the minimum required to create an invoice: https://developer.xero.com/documentation/api/invoices#LineItemsPOST – droopsnoot Dec 14 '20 at 09:25
  • You could also look here, someone asked an almost-identical question recently and got some sample PHP code: https://stackoverflow.com/questions/65232512/xero-api-struggling-to-add-a-basic-invoice – droopsnoot Dec 14 '20 at 09:26

0 Answers0