使用PHP oAuth2在Freeagent中创建发票时出现问题

时间:2019-05-08 07:21:31

标签: php oauth oauth-2.0

我正在使用https://github.com/adoy/PHP-OAuth2中的PHP-OAuth2,这是Freeagent在他们的文档中建议的。我已经能够设置身份验证,检索令牌,存储到期日期,然后设置一个过程以在当前令牌过期时自动获取新令牌。

到目前为止,太好了!

我还能够在Sandbox中设置联系人,然后在基于Web的应用程序中列出这些联系人,将其从Freeagent中拉出。

我遇到的问题是创建新发票,尽管阅读了API文档并四处寻找解决方案,但我怀疑是由于我缺乏oAuth知识导致了该问题。

这就是我要创建发票(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"
    )
);

我希望以XML发送发票请求并以JSON接收响应。

我的XML(上面的$ xml)的格式如下:

<?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>

发送回去:

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

没有创建发票(我怀疑发票数组中的空括号是无用的赠品,尽管没有任何错误可以帮助我)。

然后我希望使用以下方式检索发票编号(我认为在Freeagent中称为“参考”):

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

但是我还没走那么远,我怀疑当我最终这样做时,有一种更好的方法来检索它。

Freeagent API没有提供代码示例,互联网对像我这样的oAuth新手也没有太大帮助。非常感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

我对PHP-OAuth2或FreeAgent一无所知,但确实查看了invoices的API文档。

我注意到,获取,创建和更新发票之间唯一明显的区别是 GET POST PUT 请求:

GET https://api.freeagent.com/v2/invoices
POST https://api.freeagent.com/v2/invoices
PUT https://api.freeagent.com/v2/invoices/:id  

您说要“创建发票”,但是您在'GET'中使用了$client->fetch()

为什么不尝试'POST'?我不确定是否还会是fetch()

答案 1 :(得分:0)

我找到了一个解决方案,那就是使用CURL发送我的请求。以防万一有人遇到类似问题,以下示例代码可为您提供帮助:

$ch=curl_init('https://api.sandbox.freeagent.com/v2/invoices');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Authorization: Bearer '.$freeagentaccesstoken,
   'Content-Type: application/xml',
   'Accept: application/json',
   'User-Agent: My web app',
   'Content-Length: '.strlen($xml))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

//execute post
$result=curl_exec($ch);

//close connection
curl_close($ch);

echo $result;

我还删除了$ xml变量的某些部分,但是不知道这有什么区别。这是一个完整的解决方案:

$xml = "
<?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>
      <total-value type=\"decimal\">20.0</total-value>
      <due-value type=\"decimal\">20.0</due-value>                      
      <invoice-items type=\"array\">
         <invoice-item>
            <description>Description will go here</description>
            <item-type>Services</item-type>
            <price type=\"decimal\">20.0</price>
            <quantity type=\"integer\">1</quantity>
         </invoice-item>
      </invoice-items>
   </invoice>";