This commit is contained in:
2026-04-22 22:00:50 +02:00
parent 16be247ce1
commit e979fbe755
46 changed files with 5302 additions and 274 deletions

View File

@@ -137,9 +137,12 @@ class Carei_Softra_API {
}
if ( $res['status'] < 200 || $res['status'] >= 300 ) {
$raw_msg = self::extract_softra_message( $res['body'] );
$mapped = self::map_error_message( $raw_msg );
$message = $mapped !== '' ? $mapped : sprintf( 'Softra API error: HTTP %d', $res['status'] );
return new WP_Error(
'carei_api_error',
'Softra API error: HTTP ' . $res['status'],
$message,
array( 'status' => $res['status'], 'body' => $res['body'] )
);
}
@@ -147,6 +150,69 @@ class Carei_Softra_API {
return $res['body'];
}
/**
* Extract human-readable message from Softra response body.
* Softra returns either JSON with `message` / `error` / `details` field, or raw string.
*/
public static function extract_softra_message( $body ) {
if ( is_array( $body ) ) {
foreach ( array( 'message', 'error', 'details', 'description' ) as $field ) {
if ( ! empty( $body[ $field ] ) && is_string( $body[ $field ] ) ) {
return trim( $body[ $field ] );
}
}
return '';
}
if ( is_string( $body ) && $body !== '' ) {
$decoded = json_decode( $body, true );
if ( is_array( $decoded ) ) {
return self::extract_softra_message( $decoded );
}
return trim( $body );
}
return '';
}
/**
* Map typical Softra PL error messages to localized strings.
* Exact match first, then fuzzy prefix match. Unknown messages passthrough.
*/
public static function map_error_message( $original_message ) {
if ( ! is_string( $original_message ) || '' === trim( $original_message ) ) {
return $original_message;
}
$dict = array(
'Brak dostępnego pojazdu w wybranym terminie' => __( 'Brak dostępnego pojazdu w wybranym terminie. Zmień daty lub segment.', 'carei-reservation' ),
'Nieprawidłowy zakres dat' => __( 'Nieprawidłowy zakres dat', 'carei-reservation' ),
'Nie znaleziono oddziału' => __( 'Nie znaleziono oddziału', 'carei-reservation' ),
'Klient o tych danych już istnieje' => __( 'Klient o tych danych już istnieje w systemie', 'carei-reservation' ),
'Nieprawidłowy numer PESEL' => __( 'Nieprawidłowy numer PESEL', 'carei-reservation' ),
'Cennik wygasł' => __( 'Cennik wygasł. Odśwież formularz i spróbuj ponownie.', 'carei-reservation' ),
'Token wygasł' => __( 'Sesja wygasła. Odśwież stronę.', 'carei-reservation' ),
'Nieprawidłowe dane logowania' => __( 'Błąd autoryzacji API. Skontaktuj się z administratorem.', 'carei-reservation' ),
'Brak uprawnień' => __( 'Brak uprawnień do wykonania operacji.', 'carei-reservation' ),
'Błąd serwera' => __( 'Błąd serwera. Spróbuj ponownie za chwilę.', 'carei-reservation' ),
'Przekroczono limit rezerwacji' => __( 'Przekroczono limit rezerwacji dla tego klienta.', 'carei-reservation' ),
'Nieprawidłowy numer telefonu' => __( 'Podaj poprawny numer telefonu (min. 9 cyfr).', 'carei-reservation' ),
'Wymagane pole' => __( 'To pole jest wymagane.', 'carei-reservation' ),
);
$trimmed = trim( $original_message );
if ( isset( $dict[ $trimmed ] ) ) {
return $dict[ $trimmed ];
}
foreach ( $dict as $pl_key => $translated ) {
if ( 0 === stripos( $trimmed, $pl_key ) ) {
return $translated;
}
}
return $original_message;
}
// ─── Public API Methods ───────────────────────────────────────
public function get_branches() {