Liaison enregistrée (vide si la boutique n'est pas reliée). */ public static function connection(): array { $saved = get_option( self::OPTION, array() ); return is_array( $saved ) ? $saved : array(); } public static function isConnected(): bool { $c = self::connection(); return ! empty( $c['site_id'] ) && ! empty( $c['site_secret'] ); } public static function register(): void { add_action( self::SYNC_HOOK, array( self::class, 'syncFromCron' ) ); // Planification à « init » (pas pendant plugins_loaded), comme les rappels. add_action( 'init', array( self::class, 'schedule' ) ); } public static function schedule(): void { if ( self::isConnected() && ! wp_next_scheduled( self::SYNC_HOOK ) ) { wp_schedule_event( time() + 5 * MINUTE_IN_SECONDS, 'twicedaily', self::SYNC_HOOK ); } } /** * Relie la boutique avec le code obtenu dans l'espace marchand. * * @return array{0: bool, 1: string} [réussite, message à afficher] */ public static function pair( string $code ): array { $response = wp_remote_post( self::apiBase() . '/api/v1/pair', array( 'timeout' => 20, 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => (string) wp_json_encode( array( 'code' => $code, 'site_url' => home_url( '/' ), 'plugin_version' => EREPORTING_VERSION, ) ), ) ); if ( is_wp_error( $response ) ) { return array( false, __( 'Le service en ligne ne répond pas. Réessayez dans un instant.', 'totalio-ereporting' ) ); } $status = (int) wp_remote_retrieve_response_code( $response ); $json = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( 200 !== $status || ! is_array( $json ) || empty( $json['site_id'] ) || empty( $json['site_secret'] ) ) { $message = is_array( $json ) && isset( $json['error']['message'] ) ? (string) $json['error']['message'] : __( 'La liaison a échoué.', 'totalio-ereporting' ); return array( false, $message ); } update_option( self::OPTION, array( 'api_base' => self::apiBase(), 'site_id' => sanitize_text_field( (string) $json['site_id'] ), 'site_secret' => sanitize_text_field( (string) $json['site_secret'] ), 'connected_at' => time(), ), false ); if ( ! wp_next_scheduled( self::SYNC_HOOK ) ) { wp_schedule_event( time() + 5 * MINUTE_IN_SECONDS, 'twicedaily', self::SYNC_HOOK ); } $sync = self::sync(); return array( true, $sync[0] ? __( 'Boutique reliée : vos totaux ont été envoyés.', 'totalio-ereporting' ) : __( 'Boutique reliée. Le premier envoi a échoué : il sera retenté automatiquement.', 'totalio-ereporting' ) ); } /** Déliaison : la clé est effacée de la boutique, plus rien n'est envoyé. */ public static function unlink(): void { delete_option( self::OPTION ); wp_clear_scheduled_hook( self::SYNC_HOOK ); } public static function syncFromCron(): void { self::sync(); } /** * Envoie les totaux des 40 derniers jours (heure de Paris), signés avec la clé de la boutique. * * @param \DateTimeImmutable|null $now Date de référence (tests uniquement ; par défaut : maintenant). * @return array{0: bool, 1: string} */ public static function sync( ?\DateTimeImmutable $now = null ): array { $connection = self::connection(); if ( empty( $connection['site_id'] ) || empty( $connection['site_secret'] ) ) { return array( false, 'not_connected' ); } list( $from, $to ) = SyncPayload::window( $now ?? new \DateTimeImmutable( 'now' ) ); $values = Admin::settings(); $result = Totals::compute( $from, $to, SettingsInput::toSettings( $values ) ); $body = SyncPayload::build( $result['totals'], $result['blockedDays'], $from, $to, EREPORTING_VERSION, array( 'vat_regime' => (string) $values['regime'], 'start_date' => (string) $values['start_date'], 'vat_on_debits' => ! empty( $values['vat_on_debits'] ), ), $result['payments'] ); $headers = SyncPayload::headers( (string) $connection['site_secret'], $body, time(), bin2hex( random_bytes( 16 ) ) ); $base = ! empty( $connection['api_base'] ) ? (string) $connection['api_base'] : self::apiBase(); $response = wp_remote_post( $base . '/api/v1/sites/' . rawurlencode( (string) $connection['site_id'] ) . '/totals', array( 'timeout' => 30, 'headers' => $headers, 'body' => $body, ) ); $ok = ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ); $problem = ''; if ( is_wp_error( $response ) ) { $problem = 'network'; } elseif ( ! $ok ) { $json = json_decode( (string) wp_remote_retrieve_body( $response ), true ); $problem = is_array( $json ) && isset( $json['error']['code'] ) ? sanitize_key( (string) $json['error']['code'] ) : 'http_' . (int) wp_remote_retrieve_response_code( $response ); } $connection['last_sync_at'] = time(); $connection['last_sync_ok'] = $ok; $connection['last_error'] = $problem; update_option( self::OPTION, $connection, false ); return array( $ok, $problem ); } }