>> $totals Totaux (Report::totals()). * @param string[] $blockedDays Jours à corriger avant envoi à la plateforme. * @param string $fromDay Premier jour (AAAA-MM-JJ). * @param string $toDay Dernier jour (AAAA-MM-JJ). * @param array{vat_regime?: string, start_date?: string, vat_on_debits?: bool} $settings Régime de TVA, début de l'obligation, option débits * (le service règle la plateforme en conséquence). */ public static function build( array $totals, array $blockedDays, string $fromDay, string $toDay, string $pluginVersion, array $settings = array(), array $payments = array() ): string { $days = array(); $day = new \DateTimeImmutable( $fromDay . 'T12:00:00Z' ); $end = new \DateTimeImmutable( $toDay . 'T12:00:00Z' ); while ( $day <= $end ) { $key = $day->format( 'Y-m-d' ); $lines = array(); if ( isset( $totals[ $key ] ) ) { foreach ( $totals[ $key ] as $category => $rates ) { foreach ( $rates as $rate => $amounts ) { $lines[] = array( 'category' => (string) $category, 'rate_bp' => (int) $rate, 'taxable_cents' => (int) $amounts['taxable'], 'vat_cents' => (int) $amounts['vat'], ); } } } $entry = array( 'day' => $key, 'blocked' => in_array( $key, $blockedDays, true ), 'lines' => $lines, ); // Encaissements des prestations de services (TTC, par taux), seulement s'il y en a. if ( isset( $payments[ $key ] ) ) { $entry['payments'] = array(); foreach ( $payments[ $key ] as $rate => $cents ) { $entry['payments'][] = array( 'rate_bp' => (int) $rate, 'amount_cents' => (int) $cents, ); } } $days[] = $entry; $day = $day->modify( '+1 day' ); } $payload = array( 'plugin_version' => $pluginVersion ); foreach ( array( 'vat_regime', 'start_date' ) as $field ) { if ( ! empty( $settings[ $field ] ) ) { $payload[ $field ] = (string) $settings[ $field ]; } } if ( isset( $settings['vat_on_debits'] ) ) { $payload['vat_on_debits'] = (bool) $settings['vat_on_debits']; } $payload['days'] = $days; return (string) json_encode( $payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); } /** Fenêtre d'envoi : les WINDOW_DAYS derniers jours jusqu'à aujourd'hui inclus (heure de Paris). */ public static function window( \DateTimeImmutable $now ): array { $today = $now->setTimezone( new \DateTimeZone( 'Europe/Paris' ) )->format( 'Y-m-d' ); $from = ( new \DateTimeImmutable( $today . 'T12:00:00Z' ) )->modify( '-' . ( self::WINDOW_DAYS - 1 ) . ' days' )->format( 'Y-m-d' ); return array( $from, $today ); } /** Signature HMAC-SHA256 de « horodatage.nonce.corps », identique à celle vérifiée par le service. */ public static function sign( string $secret, int $timestamp, string $nonce, string $body ): string { return hash_hmac( 'sha256', $timestamp . '.' . $nonce . '.' . $body, $secret ); } /** * En-têtes d'un envoi signé. * * @return array */ public static function headers( string $secret, string $body, int $timestamp, string $nonce ): array { return array( 'Content-Type' => 'application/json', 'X-Ereporting-Timestamp' => (string) $timestamp, 'X-Ereporting-Nonce' => $nonce, 'X-Ereporting-Signature' => self::sign( $secret, $timestamp, $nonce, $body ), ); } }