1

I am working on integrating a custom PHP system with XERO and I can create invoices, etc. Now we need to attach some files to the invoice.

The PHP API examples do not have a function for this, only a function to attach a file to an account, so I used that as a basis and looked at the online documentation for the createInvoiceAttachmentByFileName() function, which is similar to the createAccountAttachmentByFileName(), except that it has an extra parameter ($include_online).

The function succeeds and returns the correct result, but it seems to ignore the $include_online parameter. It does not matter what I set it to, it is never included.

Below is my code for the function.
Has anybody done this before and can maybe tell me what I am doing wrong?

I am using v 1.4.0 of the XERO PHP API

Thanks

Andre

public function addInvoiceAttachment($invoice_xero_id, $file_path)
{
    $res = "OK";
    // Get the file name
    $sep_pos = strrpos($file_path, '/');
    $filename = substr($file_path, $sep_pos+1);

    $handle = fopen($file_path, "r");
    $contents = fread($handle, filesize($file_path));
    fclose($handle);

    $include_online = True;

    try
    {
      $result = $this->apiInstance->createInvoiceAttachmentByFileName($this->xeroTenantId, $invoice_xero_id, $filename, $include_online, $contents);
      trigger_error("addInvoiceAttachment: result = ".print_r($result, true));
    }
    catch (Exception $e) 
    {
      trigger_error('Exception when calling AccountingApi->createInvoiceAttachmentByFileName: '.$e->getMessage());
      $res = $e->getMessage();
    }

        return $res;
  } // addInvoiceAttachment
  // --------------------------------------------------------------------------
AndreT
  • 11
  • 3
  • I originally had the issue that the file is corrupt after uploading it, but I realised that I was using an older version of the API that does not include the $include_online parameter. I updated to the latest version of the API, so now my only issue is that the include_online option does not seem to work – AndreT Apr 01 '20 at 01:09

1 Answers1

0

I had the same issue and there is a bug in the Xero API where this parameter was being ignored. The solution is to change the following in the lib/api/AccountingApi.php file

In the function createInvoiceAttachmentByFileNameRequest on line 8203 (at least in my file) change:

From:

$this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''),

To:

$this->config->getHost() . $resourcePath . "?IncludeOnline=" . ($include_online ? "true" : "false") . ($query ? "&{$query}" : ''),

It's a bit of a hack, but it works.

Leggy
  • 682
  • 7
  • 20