Add invoice_items when creating new invoice

I’ve tried this every which way I can conjure up, but I cannot get the API to add a line item when creating a new invoice.

The below array build creates the new invoice without a problem but it won’t add the invoice_items. Any suggestions or obvious mistakes?

$endpoint = ‘https://api.freeagent.com/v2/invoices’;
$data[‘contact’] = ‘https://api.freeagent.com/v2/contacts/’.$user->freeagent_id;
$data[‘dated_on’] = date(‘Y-m-d’);
$data[‘payment_terms_in_days’] = 30;
$data[‘invoice_items’] = array(
‘item_type’ => ‘Services’,
‘quantity’ => 1.0,
‘description’ => 'Order ‘.$order->order_id.’ - '.base_url(‘orders/order/’.$order->order_id),
‘price’ => $order->order_collector_fee
);

I’m not familiar with PHP (is it PHP?) but I think the problem may be that invoice_items needs to be an array of hashes / dictionaries / associative arrays / whatever, whereas you have a single one.

What Andrew said.

Justin, if you can convert $data to json and post it here, we could get a better idea if it’s formatted correctly.

Thanks for the reply Andrew, yes I’ve tried both approaches for invoice_items - no joy with either. Returning the data array, converting it to an object, json etc. but no luck

Hi Pat_George,

Returned the data as a var dump and it’s telling me my invoice_items is an array or arrays. Pulled it out as json below, think it’s semantically correct.

{
“contact”:“https://api.freeagent.com/v2/contacts/7388593”,
“dated_on”:“2018-10-30”,
“payment_terms_in_days”:30,
“net_total”:“123.00”,
“comments”:“Order 827 - https://www.reyooz.com/orders/order/827 - Collector fee = 123”,
“invoice_items”:[
{
“position”:1,
“item_type”:“Services”,
“quantity”:1,
“description”:“Order 827 https://www.reyooz.com/orders/order/827”,
“price”:“123”
},
{
“position”:2,
“item_type”:“Services”,
“quantity”:1,
“description”:“Something to keep testing multiple invoice_items”,
“price”:200.01
}
]
}

Hi, Justin. You will want to make sure the entire variable is wrapped in an “invoice” key. Like so:

{“invoice”:
{
“contact”:“https://api.freeagent.com/v2/contacts/7388593”,
“dated_on”:“2018-10-30”,
“payment_terms_in_days”:30,
“net_total”:“123.00”,
“comments”:“Order 827 - https://www.reyooz.com/orders/order/827 - Collector fee = 123”,
“invoice_items”:[
{
“position”:1,
“item_type”:“Services”,
“quantity”:1,
“description”:“Order 827 https://www.reyooz.com/orders/order/827”,
“price”:“123”
},
{
“position”:2,
“item_type”:“Services”,
“quantity”:1,
“description”:“Something to keep testing multiple invoice_items”,
“price”:200.01
}
]
}
}

I did reproduce your problem and wrapping it fixed it. I’ll make an internal card to either handle it as you’d expect with and without the wrapper or make sure the documentation is specific about the wrapper requirement.

Thanks for bringing it up and sorry about the problems it caused.