Для передачи оформленного заказа на МодульКассу необходимо отправить restapi запрос. Пытаюсь сделать через curl, но застреваю на моменте перечисления списка товаров. Как перечислить все товары из заказа woocommerce в CURLOPT_POSTFIELDS?
API МодульКассы
Плюс не совсем удобно ориентироваться в таком запросе:
foreach ($items as $item) {
$product = $item->get_product();
}
function send_order_modulkasa($order_id) {
$order = wc_get_order( $order_id );
$order_data = $order->get_data(); // The Order data
$order_id = $order_data['id'];
$phone = substr($order_data['billing']['phone'], 1);
$phone += '+7'.$phone;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://service.modulpos.ru/api/v2/retail-point/секретное-слово/order');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"id\": $order_id,\n \"documentNumber\": $order_id,\n \"documentType\": \"SALE\",\n \"documentDateTime\": \"2019-09-18T14:41:04+07:00\",\n \"customerContact\": \"+79296666666\",\n \"description\": null,\n \"retailPointId\": null,\n \"inventPositions\": [\n {\n \"name\": \"Молоко Лебедевское 2,5%\",\n \"description\": null,\n \"measure\": \"pcs\",\n \"price\": 56,\n \"quantity\": 1,\n \"vatTag\": 1102,\n \"taxMode\": \"COMMON\",\n \"inventoryType\": \"INVENTORY\"\n },\n {\n \"name\": \"Молоко Московское 2,5%\",\n \"description\": null,\n \"measure\": \"pcs\",\n \"price\": 56,\n \"quantity\": 1,\n \"vatTag\": 1102,\n \"taxMode\": \"COMMON\",\n \"inventoryType\": \"INVENTORY\"\n }\n ],\n \"clientInformation\": {\n \"name\": \"ФИО Клиента\",\n \"documentNumber\": \"1234 123456\",\n \"inn\": \"115272538160\"\n }\n}");
$headers = array();
$headers[] = 'Authorization: Basic =============';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);
Может быть есть более удобный вариант, вместо curl?
Думал сделать для удобства в таком виде, но почему то не отправляется на сервер МодульКассы:
spoiler$data = array(
'id' => '5435465464',
'documentNumber' => 'hjhvgfh',
'documentType' => 'SALE',
'documentDateTime' => '2019-09-18T14:41:04+07:00',
'customerContact' => '+79296333481',
'description' => null,
'retailPointId' => null,
'inventPositions' => array(
'barcode' => null,
'inventCode' => null,
'name' => 'Молоко Лебедевское 2,5%',
'description' => null,
'measure' => 'pcs',
'price' => 56,
'minPrice' => 0,
'quantity' => 1,
'vatSum' => null,
'sumWithVat' => null,
'vatTag' => 1102,
'taxMode' => 'COMMON',
'inventoryType' => 'INVENTORY'
),
'remoteId' => null,
'responseURL' => null,
'clientInformation' => array(
'name' => 'ФИО Клиента',
'documentNumber' => '1234 123456',
'inn' => '115272538160'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));