Treat callback json from ERP Bling in Laravel

Asked

Viewed 126 times

0

I’m having trouble treating the following json return from ERP Bling on Laravel:

array (
  'data' => '{"retorno":{"pedidos":[{"pedido":{"desconto":"0","observacoes":"","observacaointerna":"","data":"2018-10-20","numero":"6462","vendedor":"Mauricio Liell","valorfrete":"0.00","totalprodutos":"31.00","totalvenda":"31.00","situacao":"Atendido","cliente":{"nome":"Mauricio","cnpj":"xxx.xxx.xxx-xx","ie":"","rg":"","endereco":"Av. Dxxxxxx e","numero":"xxx","complemento":"","cidade":"Bom Principio","bairro":"Centro","cep":"95.765-000","uf":"RS","email":"xxxxxxxxx","celular":"","fone":"(51) xxxxxxxx},"itens":[{"item":{"codigo":"1","descricao":"Cimento Todas as Obras Votoran 50 KG","quantidade":"1.0000","valorunidade":"31.0000000000","precocusto":"23.5600000000","descontoItem":"0.00","un":"Sc"}}],"pagamento":{"categoria":"Venda de Mercadorias"},"parcelas":[{"parcela":{"valor":"31.00","dataVencimento":"2018-10-20 00:00:00","obs":"","forma_pagamento":{"id":455687,"descricao":"Ficha Financeira","codigoFiscal":99}}}]}}]}}',
)  

What I need: save the following order details: number, date, total sale, seller, situation, customer name.

I have the following code in the controller:

$json_string = $request->all();

$json_string = $json_string['data'];


        //Log::critical($json_string);


        foreach ($json_string['retorno']['pedidos'] as $data ) {


            $pedido = IndicadoresPedido::firstOrNew(array('numero_pedido' => $data['pedido']['numero']));


            $pedido->numero_pedido = $data['pedido']['numero'];
            $pedido->data =  $data['pedido']['data'];
            $pedido->valor_total = $data['pedido']['totalvenda'];
            $pedido->vendedor =  $data['pedido']['vendedor'];
            $pedido->situacao =  $data['pedido']['situacao'];
            $pedido->cliente =  $data['pedido']['cliente']['nome'];

            if ($pedido->save()){

                $sucesso[] = $pedido;
            };

        }

And I’m getting the bug:

production.ERROR: Illegal string offset 'retorno' {"exception":"[object] (ErrorException(code: 0): Illegal string offset 'retorno' at /var/www/interno/app/Http/Controllers/apiController.php:33)

If anyone has any suggestions, I’d really appreciate it.

  • The error says that the return index does not exist in $json_string. From a var_dump in $json_string to see what is inside the variable. If you have a string you have to parse it with json_decode. If you have an object of type Stdclass, you have to access the attributes with the syntax $json_string->return->requests.

  • Okay, thank you very much for your help. I’ve updated the information above. It’s actually returning an array with the information within 'date'. I entered the ['data'] and returns the mentioned error. How can I hit this?

  • I think that’s what the friend above said. Give a json_decode($json_string). It should work.

  • I managed to resolve from the above suggestions. Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.