PHP - Creating an Invoice (Internal Server Error)

HI All

We are migrating from freshbooks to Freeagent, and need to migrate a load
of invoices.

So I have written some php to get all the invoices required to be moved
over, which is working fine.
For freeagent im using the code seen
here: http://outlandish.com/blog/freeagent-api-php-oauth2/

The code for the attempt to save a invoice into freeagent is here…

$fa_invoices_create = $fa_client->fetch(
$base_url . ‘/invoices’, //API path
$invoiceInfo, //request parameters
\OAuth2\Client::HTTP_METHOD_POST, //GET, PUT, POST, DELETE
array(‘User-Agent’ => ‘Example app’) //API requires UA header
);

$invoiceInfo is an array which has data such as this in it (print_r output)

Enter code here…

Array
(
[reference] => 0000868
[status] => Draft
[contact] => https://api.sandbox.freeagent.com/v2/contacts/5
=> 2014-10-29 00:00:00
[payment_terms_in_days] => 30
[invoice_items] => Array
(
[description] => Imported Invoice From Freshbooks
[item_type] => Services
[price] => 360.00
[quantity] => 1.0
)

[price] => 360.00
[description] => Imported Invoice From Freshbooks

)

And the response from freeagent to that request is…

Array
(
[result] => 500 Internal Server ErrorIf you are the administrator of this website, then please read this web application’s log file and/or the web server’s log file to find out what went wrong.
[code] => 500
[content_type] => text/html; charset=utf-8
)

Can anyone point me an direction to help with this? Really appreciated

BTW I will be making this migration script open source for all Freshbookers to use once its up and running.

Hi Matt,

The issue here is that you’re trying to directly assign a status value,
when statuses must be set using state transitions. From the docs
https://dev.freeagent.com/docs/invoices#create-an-invoice:

An invoice is always created with a status of Draft. You must use the

status transitions to mark an invoice as Draft, Sent, Scheduled or Cancelled

In other words, this is a two-step process — once you have created the
invoice, you must then issue a request to move it to the sent state by “marking
it as sent https://dev.freeagent.com/docs/invoices#mark-invoice-as-sent”:

PUT https://api.freeagent.com/v2/invoices/:id/transitions/mark_as_sent

Hope this helps!

-JOn Wednesday, 29 October 2014 19:22:52 UTC, Matt Hiscock wrote:

HI All

We are migrating from freshbooks to Freeagent, and need to migrate a load
of invoices.

So I have written some php to get all the invoices required to be moved
over, which is working fine.
For freeagent im using the code seen here:
http://outlandish.com/blog/freeagent-api-php-oauth2/

The code for the attempt to save a invoice into freeagent is here…

$fa_invoices_create = $fa_client->fetch(
$base_url . ‘/invoices’, //API path
$invoiceInfo, //request parameters
\OAuth2\Client::HTTP_METHOD_POST, //GET, PUT, POST, DELETE
array(‘User-Agent’ => ‘Example app’) //API requires UA header
);

$invoiceInfo is an array which has data such as this in it (print_r output)

Enter code here…

Array
(
[reference] => 0000868
[status] => Draft
[contact] => https://api.sandbox.freeagent.com/v2/contacts/5
=> 2014-10-29 00:00:00
[payment_terms_in_days] => 30
[invoice_items] => Array
(
[description] => Imported Invoice From Freshbooks
[item_type] => Services
[price] => 360.00
[quantity] => 1.0
)

[price] => 360.00
[description] => Imported Invoice From Freshbooks

)

And the response from freeagent to that request is…

Array
(
[result] => 500 Internal Server ErrorIf you are the administrator of this website, then please read this web application’s log file and/or the web server’s log file to find out what went wrong.
[code] => 500
[content_type] => text/html; charset=utf-8
)

Can anyone point me an direction to help with this? Really appreciated

BTW I will be making this migration script open source for all Freshbookers to use once its up and running.