0, 'body' => null, 'raw' => '', 'error' => 'No API key set (WooCommerce > AfterHoursCouriers).' ); } $args = array( 'method' => $method, 'timeout' => $timeout, 'headers' => array( 'Authorization' => 'Bearer ' . $key, 'Content-Type' => 'application/json', 'User-Agent' => 'AHC-WooCommerce/' . AHCOURIERS_VERSION, ), ); if ( null !== $body ) { $args['body'] = wp_json_encode( $body ); } $res = wp_remote_request( apply_filters( 'ahcouriers_api_base', self::BASE ) . $path, $args ); if ( is_wp_error( $res ) ) { return array( 'code' => 0, 'body' => null, 'raw' => '', 'error' => $res->get_error_message() ); } $raw = wp_remote_retrieve_body( $res ); $json = json_decode( $raw, true ); $code = (int) wp_remote_retrieve_response_code( $res ); $err = null; if ( $code < 200 || $code >= 300 ) { $err = is_array( $json ) && ! empty( $json['error'] ) ? $json['error'] : 'HTTP ' . $code; } return array( 'code' => $code, 'body' => is_array( $json ) ? $json : null, 'raw' => $raw, 'error' => $err ); } public static function availability( $fresh = false ) { $cache_key = 'ahcouriers_availability'; if ( ! $fresh ) { $cached = get_transient( $cache_key ); if ( is_array( $cached ) ) { return $cached; } } $r = self::request( 'GET', '/api/v1/availability' ); if ( null === $r['error'] && is_array( $r['body'] ) ) { set_transient( $cache_key, $r['body'], self::AVAILABILITY_TTL ); return $r['body']; } self::log( 'Availability failed: ' . $r['error'] ); return null; } /** * @return array { ok:bool, quote:array|null, over_limit:bool, error:string|null } */ public static function quote( $request ) { $cache_key = 'ahcouriers_q_' . md5( wp_json_encode( $request ) ); $cached = get_transient( $cache_key ); if ( is_array( $cached ) ) { return $cached; } $r = self::request( 'POST', '/api/v1/quote', $request ); $result = array( 'ok' => false, 'quote' => null, 'over_limit' => false, 'error' => $r['error'] ); if ( 422 === $r['code'] && false !== stripos( (string) $r['error'], 'Over 100 km' ) ) { $result['over_limit'] = true; } elseif ( null === $r['error'] && is_array( $r['body'] ) && empty( $r['body']['blocked'] ) && isset( $r['body']['total'] ) ) { $result['ok'] = true; $result['quote'] = $r['body']; } elseif ( is_array( $r['body'] ) && ! empty( $r['body']['blocked'] ) ) { $result['error'] = 'Blocked: ' . ( $r['body']['reason'] ?? 'not available' ); } // Cache answers (including "over 100 km"), but not failures - a // temporary outage shouldn't hide the option for 30 minutes. if ( $result['ok'] || $result['over_limit'] ) { set_transient( $cache_key, $result, self::QUOTE_TTL ); } else { self::log( 'Quote failed: ' . $result['error'] ); } return $result; } public static function book( $request ) { return self::request( 'POST', '/api/v1/bookings', $request, 60 ); } public static function status( $ref ) { return self::request( 'GET', '/api/v1/bookings/' . rawurlencode( $ref ) ); } /** Raw PDF bytes or error. Not JSON - handled separately. */ public static function pod( $ref ) { $key = AHCouriers_Settings::get( 'api_key' ); $res = wp_remote_get( apply_filters( 'ahcouriers_api_base', self::BASE ) . '/api/v1/bookings/' . rawurlencode( $ref ) . '/pod', array( 'timeout' => 60, 'headers' => array( 'Authorization' => 'Bearer ' . $key ), ) ); if ( is_wp_error( $res ) ) { return array( 'ok' => false, 'error' => $res->get_error_message() ); } $code = (int) wp_remote_retrieve_response_code( $res ); $body = wp_remote_retrieve_body( $res ); if ( 200 !== $code ) { $json = json_decode( $body, true ); return array( 'ok' => false, 'error' => is_array( $json ) && ! empty( $json['error'] ) ? $json['error'] : 'HTTP ' . $code ); } return array( 'ok' => true, 'pdf' => $body ); } public static function log( $message ) { if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->warning( $message, array( 'source' => 'afterhourscouriers' ) ); } } }