Problem creating an invoice using PHP oAuth. Just need a nudge in the right direction!


#1

I’m an experienced dev, but haven’t used oAuth widely so I expect I’m making a silly mistake!

I’m using the light PHP oAuth library suggested by Freeagent in the docs (the one right at the bottom of the list). I’ve been able to set up authentication, retrieve a token, store the expiry date and then set up a process to automatically fetch a new token when the current one expires.

So far, so good!

I’ve also been able to set up contacts in the Sandbox and then list these in my web based application pulling them down from Freeagent.

Again, all good.

The problem I’m having is creating a new invoice and I suspect it’s my lack of oAuth knowledge that’s causing the problem, despite reading the API docs and having a good look around for a solution.

Here’s what I’m trying to do to create the invoice (PHP):

$client = new OAuth2\Client(CLIENTID, SECRET);
$params = array($xml); // See below

$response = $client->fetch("https://api.sandbox.freeagent.com/v2/invoices",
    $params,
    'GET',
    array(
        "Authorization" => "Bearer $freeagentaccesstoken",
    	"User-Agent" => "My web app",
    	"Content-Type" => "application/xml",
    	"Accept" => "application/json"
    )
);

I’m looking to send the invoice request in XML and receive the response in JSON.

My XML ($xml in the above) is formatted as follows:

<?xml version="1.0" encoding="UTF-8"?>
 <invoice>
<contact>https://api.sandbox.freeagent.com/v2/contacts/58019</contact>
<dated-on type="datetime">2019-05-07</dated-on>
<payment_terms_in_days>15<payment_terms_in_days>
<currency>GBP</currency>
<net-value type="decimal">0.0</net-value>
<total-value type="decimal">20.00</total-value>
<paid-value type="decimal">0.0</paid-value>
<due-value type="decimal">20.00</due-value>			            
<invoice-items type="array">
   <invoice-item>
      <description>Description will go here</description>
      <item-type>Hours</item-type>
      <price type="decimal">20.00</price>
      <quantity type="decimal">1</quantity>
  </invoice-item>
</invoice-items>
</invoice>

Sending this gets back:

Array ( [result] => Array ( [invoices] => Array ( ) ) [code] => 200 [content_type] => application/Jon; charset=utf-8 )

No invoices are created (I suspect the empty brackets in the invoices array are a dead giveaway although there are no errors being given to help me).

I was then hoping to retrieve the invoice number (which I think is termed “reference” in Freeagent) using something like this:

foreach ($response['result']['invoices'] as $item) {
  $invoicenumber=$item['reference'];
}

But I don’t get that far, and I suspect there’s a better way of retrieving that when I eventually do get that far.

The Freeagent API doesn’t give code specific examples and the internet hasn’t been much help for an oAuth newbie like me. I’d really appreciate some help here. Thank you!


#2

Someone on Stack Overflow suggested using a POST rather than a GET for my request. This results in an “Invalid XML” being returned from the server. My XML is syntactically correct and I can’t see in the docs that I’m not sending across correct details so I’m still stuck! Would welcome anyone’s further input to push me along a bit further.


#3

I’ve resolved my issue by using CURL for my invoice creation request. If anyone wants more details and sample code, check my Stack Overflow post where I’ve included relevant source: https://stackoverflow.com/questions/56035531/problem-creating-an-invoice-in-freeagent-using-php-oauth2