@@ -73,8 +73,20 @@ public static function sendInvoice($id_record)
7373 ]);
7474 $ body = static ::responseBody ($ response );
7575
76+ // Validazione della risposta
77+ if (empty ($ body ) || !isset ($ body ['status ' ])) {
78+ logger ()->error ('Risposta API non valida per fattura ' .$ fattura ->numero_esterno .': ' .json_encode ($ body ));
79+
80+ return [
81+ 'code ' => 500 ,
82+ 'message ' => tr ('Risposta non valida dal server ' ),
83+ ];
84+ }
85+
86+ $ status_code = (int ) $ body ['status ' ];
87+
7688 // Aggiornamento dello stato in base alla risposta
77- if ($ body [ ' status ' ] == 200 || $ body [ ' status ' ] == 301 ) {
89+ if ($ status_code == 200 || $ status_code == 301 ) {
7890 // Invio riuscito
7991 database ()->update ('co_documenti ' , [
8092 'codice_stato_fe ' => 'WAIT ' ,
@@ -91,7 +103,7 @@ public static function sendInvoice($id_record)
91103 }
92104
93105 return [
94- 'code ' => $ body [ ' status ' ] ,
106+ 'code ' => $ status_code ,
95107 'message ' => $ body ['message ' ] ?? tr ('Risposta non valida dal server ' ),
96108 ];
97109 } catch (\UnexpectedValueException $ e ) {
@@ -120,9 +132,19 @@ public static function getInvoiceRecepits($id_record)
120132 ]);
121133 $ body = static ::responseBody ($ response );
122134
135+ // Validazione della risposta
136+ if (empty ($ body ) || !isset ($ body ['status ' ])) {
137+ logger ()->error ('Risposta API non valida per ricevute fattura ID ' .$ id_record .': ' .json_encode ($ body ));
138+
139+ return [
140+ 'code ' => 500 ,
141+ 'message ' => tr ('Risposta non valida dal server ' ),
142+ ];
143+ }
144+
123145 return [
124- 'code ' => $ body ['status ' ],
125- 'results ' => $ body ['results ' ],
146+ 'code ' => ( int ) $ body ['status ' ],
147+ 'results ' => $ body ['results ' ] ?? [] ,
126148 ];
127149 } catch (\UnexpectedValueException ) {
128150 }
0 commit comments