Estoy realizando un proyecto en Laravel 7, es una tienda on line. Para sincronizar la tienda con el catálogo de productos, stocks, etc, hago uso de la API que facilita el proveedor. Hasta ahora todo va bien, pero tengo un problema a la hora de hacer la petición a la url que crea una nueva orden(pedido). Debo enviar un json con el siguiente formato:
{
"order": {
"internalReference": "123456",
"language": "es",
"paymentMethod": "moneybox",
"carriers": [
{
"name": "correos"
},
],
"shippingAddress": {
"firstName": "John",
"lastName": "Doe",
"country": "ES",
"postcode": "46005",
"town": "Valencia",
"address": "C/ Altea",
"phone": "664869570",
"email": "[email protected]",
"comment": ""
},
"products": [
{
"reference": "V0100100",
"quantity": 1,
"internalReference": "LO5632"
},
{
"reference": "F1505138",
"quantity": 4,
"internalReference": "LA4289"
}
]
}
}
Por mucho que lo he intentado no consigo obtener el json con ese formato, lo he intentado de diferentes formas pero consigo esto:
{
"order": {
"internalReference": "20201212003435",
"language": "ES",
"paymentMethod": "moneybox",
"carriers": [
{
"name": "correos"
}
],
"shippingAddress": {
"firstName": "John",
"lastName": "Doe",
"country": "ES",
"postcode": "45005",
"town": "Valencia",
"address": "C/ Altea",
"phone": "662458333",
"email": "[email protected]",
"vatNumber": "",
"companyName": "",
"comment": ""
},
"products": [
{
"reference": [
"5fcf3cb6b690f",
"5fd10ee71e004"
],
"quantity": [
"1",
"1"
],
"internalReference": [
"5fcf3cb6b690f",
"5fd10ee71e004"
]
}
],
"dateAdd": "2020-12-12"
}
}
A continuación os muestro el formulario desde donde obtengo los datos. He omitido todos los divs, estilos, etc para que sea más legible:
<form class="form-checkout" id="form" action="{{route('orderCreate')}}" method="POST">
@csrf
<input type="text" name="firstName">
<input type="text" name="lastName">
<input type="text" name="company" >
<input type="text" name="vatNumber">
<input type="text" name="address">
<input type="text" name="town">
<input type="email" name="email">
<input type="text" name="phone">
<table class="table-cart" >
<thead>
<tr>
<th width="80%">Product name</th>
<th width="5%">Price</th>
<th width="5%">Quantity</th>
<th width="10%">Total</th>
<th hidden>Id</th>
<th hidden>InternalRef</th>
</tr>
</thead>
<tbody>
@foreach($cart as $item)
<tr>
<td>{{$item->name}}</td>
<td><input type="text" name="quantity[]" value="{{$item->quantity}}"></td>
<td>{{number_format($item->price * $item->quantity,2,",",".")}} {{ env('CURRENCY_SYMBOL') }}</td>
<td><input type="text" name="reference[]" value="{{$item->id}}"></td>
<td><input type="text" name="internalReference[]" value="{{$item->id}}"></td>
</tr>
@endforeach
Haciendo un dd de los datos que obtengo del formulario:
array:1 [▼
"order" => array:7 [▼
"internalReference" => "20201212122507"
"language" => "ES"
"paymentMethod" => "moneybox"
"carriers" => array:1 [▶]
"shippingAddress" => array:11 [▶]
"products" => array:1 [▼
0 => array:3 [▼
"reference" => array:2 [▼
0 => "5fcf3cb6b690f"
1 => "5fd10ee71e004"
]
"quantity" => array:2 [▼
0 => "1"
1 => "1"
]
"internalReference" => array:2 [▼
0 => "5fcf3cb6b690f"
1 => "5fd10ee71e004"
]
]
]
"dateAdd" => "2020-12-12"
]
]
Y este es el método del controlador:
public function createNewOrder(Request $request)
{
$order = array(
"order"=> array(
"internalReference"=>date('YmdHis'),
"language"=>"ES",
"paymentMethod" => "moneybox",
"carriers"=>array(
[
'name'=>'correos'
]
),
"shippingAddress" => array(
"firstName" => $request->firstName,
"lastName"=>$request->lastName,
"country"=>"ES",
"postcode"=>$request->postcode,
"town"=>$request->town,
"address"=>$request->address,
"phone"=>$request->phone,
"email"=>$request->email,
"vatNumber"=>$request->vatNumber,
"companyName"=>$request->companyName,
"comment" => ""
),
"products"=>array(
[
"reference"=> $request->reference,
"quantity"=>$request->quantity,
"internalReference"=>$request->internalReference
]
),
"dateAdd"=>date('Y-m-d'),
),
);
return json_encode($order);
}
Muchas gracias.
dd