I have been working with the square-connect api, and I am having issues trying to create an order. I have followed the docs for creating an order. Below is the code in question.
$api = new \SquareConnect\Api\OrdersApi();
$location = $this->get_location();
if (!empty($items)) {
if (is_string($items)) {
$items = json_decode($items);
}
// print_r($items);
if (!is_array($items)) {
// echo "Items is not an array -- exiting now";
return false;
}
$order_data = [];
$order_data['idempotency_key'] = uniqid();
$order_data['line_items'] = [];
foreach ($items as $key => $li) {
$order_data['line_items'][] = [
'name' => $li->name,
'base_price_money' => ['amount' => ($li->price * 100)],
'quantity' => $li->qty
];
}
$order_data['taxes'] = [
'type' => 'ADDITIVE',
'name' => 'State Sales Tax',
'percentage' => '7'
];
print_r($order_data);
// exit;
$apiResponse = $api->createOrder($location, new \SquareConnect\Model\CreateOrderRequest($order_data));
$order = $apiResponse->getOrder();
print_r($apiResponse);
print_r($order);
}
When I make the request I get the following error
Message: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_ARRAY","detail":"Expected an array.","field":"taxes"}]}
I have checked that the taxes field is an array
["taxes"]=>
array(3) {
["type"]=>
string(8) "ADDITIVE"
["name"]=>
string(15) "State Sales Tax"
["percentage"]=>
string(1) "7"
}
Any assistance would be greatly appreciated
EDIT: print_r($items) output
Array
(
[0] => stdClass Object
(
[id] => 24
[square_id] => L7PCKMIEDQFDR34IZ3E3VIDO
[variation_id] => SUJCAPZJQLM7VGAILP66NMMM
[variation_name] => Regular
[qty] => 1
[price] => 3.99
[name] => Ham & Cheese
)
[1] => stdClass Object
(
[id] => 4
[square_id] => 37RNDFXRVJPLR4UT7UXS5QOO
[variation_id] => 27Z5H3V6ZRHW2X5LVNGXOARY
[variation_name] => Starbucks DOUBLESHOT Expresso
[qty] => 1
[price] => 2.5
[name] => Starbucks DOUBLESHOT Expresso
)
)
var_dump(order_data)...
array(3) {
["idempotency_key"]=>
string(13) "59c138b7a67e2"
["line_items"]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(12) "Ham & Cheese"
["base_price_money"]=>
array(1) {
["amount"]=>
float(399)
}
["quantity"]=>
string(1) "1"
}
[1]=>
array(3) {
["name"]=>
string(29) "Starbucks DOUBLESHOT Expresso"
["base_price_money"]=>
array(1) {
["amount"]=>
float(250)
}
["quantity"]=>
string(1) "1"
}
}
["taxes"]=>
array(3) {
["type"]=>
string(8) "ADDITIVE"
["name"]=>
string(15) "State Sales Tax"
["percentage"]=>
string(1) "7"
}
}