Class: FactPulse::Helpers::FactPulseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/factpulse/helpers/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email:, password:, api_url: nil, client_uid: nil, chorus_credentials: nil, afnor_credentials: nil, polling_interval: nil, polling_timeout: nil, max_retries: nil) ⇒ FactPulseClient

Returns a new instance of FactPulseClient.



168
169
170
171
172
173
174
# File 'lib/factpulse/helpers/client.rb', line 168

def initialize(email:, password:, api_url: nil, client_uid: nil, chorus_credentials: nil, afnor_credentials: nil,
               polling_interval: nil, polling_timeout: nil, max_retries: nil)
  @email, @password = email, password; @api_url = (api_url || 'https://factpulse.fr').chomp('/')
  @client_uid, @polling_interval, @polling_timeout, @max_retries = client_uid, polling_interval || 2000, polling_timeout || 120000, max_retries || 1
  @chorus_credentials, @afnor_credentials = chorus_credentials, afnor_credentials
  @access_token = @refresh_token = @token_expires_at = nil
end

Instance Attribute Details

#afnor_credentialsObject (readonly)

Returns the value of attribute afnor_credentials.



166
167
168
# File 'lib/factpulse/helpers/client.rb', line 166

def afnor_credentials
  @afnor_credentials
end

#chorus_credentialsObject (readonly)

Returns the value of attribute chorus_credentials.



166
167
168
# File 'lib/factpulse/helpers/client.rb', line 166

def chorus_credentials
  @chorus_credentials
end

Class Method Details

.format_montant(m) ⇒ Object



213
# File 'lib/factpulse/helpers/client.rb', line 213

def self.format_montant(m); MontantHelpers.montant(m); end

Instance Method Details

#afnor_credentials_for_apiObject



177
# File 'lib/factpulse/helpers/client.rb', line 177

def afnor_credentials_for_api; @afnor_credentials&.to_h; end

#chorus_credentials_for_apiObject



176
# File 'lib/factpulse/helpers/client.rb', line 176

def chorus_credentials_for_api; @chorus_credentials&.to_h; end

#consulter_facture_chorus(identifiant_facture_cpp) ⇒ Object

Consulte le statut d’une facture Chorus Pro.



540
541
542
# File 'lib/factpulse/helpers/client.rb', line 540

def consulter_facture_chorus(identifiant_facture_cpp)
  make_chorus_request('POST', '/factures/consulter', { 'identifiant_facture_cpp' => identifiant_facture_cpp })
end

#consulter_structure_chorus(id_structure_cpp) ⇒ Object

Consulte les détails d’une structure Chorus Pro.



520
521
522
# File 'lib/factpulse/helpers/client.rb', line 520

def consulter_structure_chorus(id_structure_cpp)
  make_chorus_request('POST', '/structures/consulter', { 'id_structure_cpp' => id_structure_cpp })
end

#ensure_authenticated(force_refresh: false) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/factpulse/helpers/client.rb', line 182

def ensure_authenticated(force_refresh: false)
  now = (Time.now.to_f * 1000).to_i
  if force_refresh || @access_token.nil? || (@token_expires_at && now >= @token_expires_at)
    payload = { 'username' => @email, 'password' => @password }; payload['client_uid'] = @client_uid if @client_uid
    response = http_post(URI("#{@api_url}/api/token/"), payload)
    raise FactPulseAuthError, "Auth failed" unless response.is_a?(Net::HTTPSuccess)
    tokens = JSON.parse(response.body); @access_token, @refresh_token = tokens['access'], tokens['refresh']
    @token_expires_at = now + (28 * 60 * 1000)
  end
end

#generer_certificat_test(**options) ⇒ Object

Génère un certificat de test (NON PRODUCTION).



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/factpulse/helpers/client.rb', line 628

def generer_certificat_test(**options)
  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/traitement/generer-certificat-test")
  body = {
    'cn' => options[:cn] || 'Test Organisation',
    'organisation' => options[:organisation] || 'Test Organisation',
    'email' => options[:email] || '[email protected]',
    'duree_jours' => options[:duree_jours] || 365,
    'taille_cle' => options[:taille_cle] || 2048
  }

  response = http_post_json(uri, body)
  raise FactPulseValidationError.new("Error: #{response.code}") unless response.is_a?(Net::HTTPSuccess)
  JSON.parse(response.body) rescue {}
end

#generer_facturx(facture_data, pdf_source, profil: 'EN16931', format_sortie: 'pdf', sync: true, timeout: nil) ⇒ Object

Génère une facture Factur-X à partir d’un dict/hash et d’un PDF source.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/factpulse/helpers/client.rb', line 216

def generer_facturx(facture_data, pdf_source, profil: 'EN16931', format_sortie: 'pdf', sync: true, timeout: nil)
  # Conversion des données en JSON string
  json_data = case facture_data
              when String then facture_data
              when Hash then JSON.generate(facture_data)
              else
                if facture_data.respond_to?(:to_h)
                  JSON.generate(facture_data.to_h)
                elsif facture_data.respond_to?(:to_hash)
                  JSON.generate(facture_data.to_hash)
                else
                  raise FactPulseValidationError.new("Type de données non supporté: #{facture_data.class}")
                end
              end

  # Lecture du PDF source
  pdf_content = case pdf_source
                when String then File.binread(pdf_source)
                when File then pdf_source.read
                else
                  if pdf_source.respond_to?(:read)
                    pdf_source.read
                  else
                    raise FactPulseValidationError.new("Type de PDF non supporté: #{pdf_source.class}")
                  end
                end
  pdf_filename = pdf_source.is_a?(String) ? File.basename(pdf_source) : 'facture.pdf'

  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/traitement/generer-facture")

  # Construire la requête multipart
  boundary = "----RubyFormBoundary#{SecureRandom.hex(16)}"
  body = build_multipart_body(boundary, [
    { name: 'donnees_facture', content: json_data },
    { name: 'profil', content: profil },
    { name: 'format_sortie', content: format_sortie },
    { name: 'source_pdf', content: pdf_content, filename: pdf_filename, content_type: 'application/pdf' }
  ])

  response = http_multipart_post(uri, body, boundary)

  if response.code == '401'
    reset_auth; ensure_authenticated; response = http_multipart_post(uri, body, boundary)
  end

  unless response.is_a?(Net::HTTPSuccess)
    # Extraire les détails d'erreur du corps de la réponse
    error_msg = "Erreur API (#{response.code})"
    errors = []

    begin
      error_data = JSON.parse(response.body)
      # Format FastAPI/Pydantic: {"detail": [{"loc": [...], "msg": "...", "type": "..."}]}
      if error_data['detail'].is_a?(Array)
        error_msg = 'Erreur de validation'
        error_data['detail'].each do |err|
          next unless err.is_a?(Hash)
          loc = (err['loc'] || []).map(&:to_s).join(' -> ')
          errors << ValidationErrorDetail.new(
            level: 'ERROR',
            item: loc,
            reason: err['msg'] || err.to_s,
            source: 'validation',
            code: err['type']
          )
        end
      elsif error_data['detail'].is_a?(String)
        error_msg = error_data['detail']
      elsif error_data['errorMessage']
        error_msg = error_data['errorMessage']
      end
    rescue JSON::ParserError
      error_msg = "Erreur API (#{response.code}): #{response.body}"
    end

    warn "Erreur API #{response.code}: #{response.body}"
    raise FactPulseValidationError.new(error_msg, errors)
  end

  data = JSON.parse(response.body)

  if sync && data['id_tache']
    result = poll_task(data['id_tache'], timeout: timeout)
    if result['contenu_b64']
      return Base64.decode64(result['contenu_b64'])
    elsif result['contenu_xml']
      return result['contenu_xml']
    end
    raise FactPulseValidationError.new("Résultat inattendu: #{result.keys.join(', ')}")
  end

  data
end

#generer_facturx_complet(facture, pdf_source_path, **options) ⇒ Object

Génère un PDF Factur-X complet avec validation, signature et soumission optionnelles.



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/factpulse/helpers/client.rb', line 649

def generer_facturx_complet(facture, pdf_source_path, **options)
  profil = options[:profil] || 'EN16931'
  valider = options.fetch(:valider, true)
  signer = options.fetch(:signer, false)
  soumettre_afnor = options.fetch(:soumettre_afnor, false)
  timeout = options[:timeout] || 120000

  result = {}

  # 1. Génération
  pdf_bytes = generer_facturx(facture, pdf_source_path, profil: profil, format_sortie: 'pdf', sync: true, timeout: timeout)
  result[:pdf_bytes] = pdf_bytes

  # Créer un fichier temporaire pour les opérations suivantes
  temp_file = Tempfile.new(['facturx_', '.pdf'])
  begin
    temp_file.binmode
    temp_file.write(pdf_bytes)
    temp_file.flush

    # 2. Validation
    if valider
      validation = valider_pdf_facturx(temp_file.path, profil: profil)
      result[:validation] = validation
      unless validation['est_conforme']
        if options[:output_path]
          File.binwrite(options[:output_path], pdf_bytes)
          result[:pdf_path] = options[:output_path]
        end
        return result
      end
    end

    # 3. Signature
    if signer
      pdf_bytes = signer_pdf(temp_file.path, **options)
      result[:pdf_bytes] = pdf_bytes
      result[:signature] = { 'signe' => true }
      temp_file.rewind
      temp_file.write(pdf_bytes)
      temp_file.flush
    end

    # 4. Soumission AFNOR
    if soumettre_afnor
      numero_facture = facture['numeroFacture'] || facture['numero_facture'] || 'FACTURE'
      flow_name = options[:afnor_flow_name] || "Facture #{numero_facture}"
      tracking_id = options[:afnor_tracking_id] || numero_facture
      afnor_result = soumettre_facture_afnor(temp_file.path, flow_name, tracking_id: tracking_id)
      result[:afnor] = afnor_result
    end

    # Sauvegarde finale
    if options[:output_path]
      File.binwrite(options[:output_path], pdf_bytes)
      result[:pdf_path] = options[:output_path]
    end
  ensure
    temp_file.close
    temp_file.unlink
  end

  result
end

#get_afnor_credentialsObject



180
# File 'lib/factpulse/helpers/client.rb', line 180

def get_afnor_credentials; afnor_credentials_for_api; end

#get_chorus_pro_credentialsObject

Alias plus courts



179
# File 'lib/factpulse/helpers/client.rb', line 179

def get_chorus_pro_credentials; chorus_credentials_for_api; end

#healthcheck_afnorObject

Vérifie la disponibilité du Flow Service AFNOR.



459
460
461
# File 'lib/factpulse/helpers/client.rb', line 459

def healthcheck_afnor
  make_afnor_request('GET', '/flow/v1/healthcheck')
end

#lister_codes_routage_afnor(siren) ⇒ Object

Liste les codes de routage disponibles pour un SIREN.



476
477
478
# File 'lib/factpulse/helpers/client.rb', line 476

def lister_codes_routage_afnor(siren)
  make_afnor_request('GET', "/directory/siren/#{siren}/routing-codes")
end

#lister_services_structure_chorus(id_structure_cpp) ⇒ Object

Liste les services d’une structure Chorus Pro.



530
531
532
# File 'lib/factpulse/helpers/client.rb', line 530

def lister_services_structure_chorus(id_structure_cpp)
  make_chorus_request('GET', "/structures/#{id_structure_cpp}/services")
end

#obtenir_facture_entrante_afnor(flow_id, include_document: false) ⇒ Hash

Récupère les métadonnées JSON d’un flux entrant (facture fournisseur). Télécharge un flux entrant depuis la PDP AFNOR et extrait les métadonnées de la facture vers un format JSON unifié. Supporte Factur-X, CII et UBL.

Note: Cet endpoint utilise l’authentification JWT FactPulse (pas OAuth AFNOR). Le serveur FactPulse se charge d’appeler la PDP avec les credentials stockés.

Examples:

facture = client.obtenir_facture_entrante_afnor("550e8400-...")
puts "Fournisseur: #{facture['fournisseur']['nom']}"
puts "Montant TTC: #{facture['montant_ttc']} #{facture['devise']}"

Parameters:

  • flow_id (String)

    Identifiant du flux (UUID)

  • include_document (Boolean) (defaults to: false)

    Si true, inclut le document en base64

Returns:

  • (Hash)

    Métadonnées de la facture (fournisseur, montants, dates, etc.)

Raises:



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/factpulse/helpers/client.rb', line 441

def obtenir_facture_entrante_afnor(flow_id, include_document: false)
  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/afnor/flux-entrants/#{flow_id}")
  uri.query = "include_document=true" if include_document

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.read_timeout = 60

  request = Net::HTTP::Get.new(uri)
  request['Authorization'] = "Bearer #{@access_token}"

  response = http.request(request)
  raise FactPulseValidationError.new("Erreur flux entrant: #{response.code}") unless response.is_a?(Net::HTTPSuccess)
  JSON.parse(response.body) rescue {}
end

#obtenir_id_chorus_depuis_siret(siret, type_identifiant: 'SIRET') ⇒ Object

Obtient l’ID Chorus Pro d’une structure depuis son SIRET.



525
526
527
# File 'lib/factpulse/helpers/client.rb', line 525

def obtenir_id_chorus_depuis_siret(siret, type_identifiant: 'SIRET')
  make_chorus_request('POST', '/structures/obtenir-id-depuis-siret', { 'siret' => siret, 'type_identifiant' => type_identifiant })
end

#poll_task(task_id, timeout: nil, interval: nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/factpulse/helpers/client.rb', line 195

def poll_task(task_id, timeout: nil, interval: nil)
  timeout_ms, interval_ms = timeout || @polling_timeout, interval || @polling_interval
  start_time, current_interval = (Time.now.to_f * 1000).to_i, interval_ms.to_f
  loop do
    raise FactPulsePollingTimeout.new(task_id, timeout_ms) if (Time.now.to_f * 1000).to_i - start_time > timeout_ms
    ensure_authenticated; response = http_get(URI("#{@api_url}/api/v1/traitement/taches/#{task_id}/statut"))
    reset_auth and next if response.code == '401'
    data = JSON.parse(response.body)
    return data['resultat'] || {} if data['statut'] == 'SUCCESS'
    if data['statut'] == 'FAILURE'
      # Format AFNOR: errorMessage, details
      r = data['resultat'] || {}
      raise FactPulseValidationError.new("Task #{task_id} failed: #{r['errorMessage'] || '?'}", (r['details'] || []).map { |e| ValidationErrorDetail.from_hash(e) })
    end
    sleep(current_interval / 1000.0); current_interval = [current_interval * 1.5, 10000].min
  end
end

#rechercher_flux_afnor(**criteria) ⇒ Object

Recherche des flux de facturation AFNOR.



408
409
410
411
412
413
414
415
416
417
418
# File 'lib/factpulse/helpers/client.rb', line 408

def rechercher_flux_afnor(**criteria)
  search_body = {
    'offset' => criteria[:offset] || 0,
    'limit' => criteria[:limit] || 25,
    'where' => {}
  }
  search_body['where']['trackingId'] = criteria[:tracking_id] if criteria[:tracking_id]
  search_body['where']['status'] = criteria[:status] if criteria[:status]

  make_afnor_request('POST', '/flow/v1/flows/search', json_data: search_body)
end

#rechercher_siren_afnor(siren) ⇒ Object

Recherche une entreprise par SIREN dans l’annuaire AFNOR.



471
472
473
# File 'lib/factpulse/helpers/client.rb', line 471

def rechercher_siren_afnor(siren)
  make_afnor_request('GET', "/directory/siren/#{siren}")
end

#rechercher_siret_afnor(siret) ⇒ Object

Recherche une entreprise par SIRET dans l’annuaire AFNOR.



466
467
468
# File 'lib/factpulse/helpers/client.rb', line 466

def rechercher_siret_afnor(siret)
  make_afnor_request('GET', "/directory/siret/#{siret}")
end

#rechercher_structure_chorus(identifiant_structure: nil, raison_sociale: nil, type_identifiant: 'SIRET', restreindre_privees: true) ⇒ Object

Recherche des structures sur Chorus Pro.



510
511
512
513
514
515
516
517
# File 'lib/factpulse/helpers/client.rb', line 510

def rechercher_structure_chorus(identifiant_structure: nil, raison_sociale: nil, type_identifiant: 'SIRET', restreindre_privees: true)
  body = { 'restreindre_structures_privees' => restreindre_privees }
  body['identifiant_structure'] = identifiant_structure if identifiant_structure
  body['raison_sociale_structure'] = raison_sociale if raison_sociale
  body['type_identifiant_structure'] = type_identifiant if type_identifiant

  make_chorus_request('POST', '/structures/rechercher', body)
end

#reset_authObject



193
# File 'lib/factpulse/helpers/client.rb', line 193

def reset_auth; @access_token = @refresh_token = @token_expires_at = nil; end

#signer_pdf(pdf_path, **options) ⇒ Object

Signe un PDF avec le certificat configuré côté serveur.



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/factpulse/helpers/client.rb', line 602

def signer_pdf(pdf_path, **options)
  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/traitement/signer-pdf")
  pdf_content = File.binread(pdf_path)

  parts = [
    { name: 'fichier_pdf', content: pdf_content, filename: File.basename(pdf_path), content_type: 'application/pdf' },
    { name: 'use_pades_lt', content: (options[:use_pades_lt] ? 'true' : 'false') },
    { name: 'use_timestamp', content: (options.key?(:use_timestamp) ? (options[:use_timestamp] ? 'true' : 'false') : 'true') }
  ]
  parts << { name: 'raison', content: options[:raison] } if options[:raison]
  parts << { name: 'localisation', content: options[:localisation] } if options[:localisation]
  parts << { name: 'contact', content: options[:contact] } if options[:contact]

  boundary = "----RubyFormBoundary#{SecureRandom.hex(16)}"
  body = build_multipart_body(boundary, parts)

  response = http_multipart_post(uri, body, boundary)
  raise FactPulseValidationError.new("Signature error: #{response.code}") unless response.is_a?(Net::HTTPSuccess)

  result = JSON.parse(response.body) rescue {}
  raise FactPulseValidationError.new("Invalid signature response") unless result['pdf_signe_base64']
  Base64.decode64(result['pdf_signe_base64'])
end

#soumettre_facture_afnor(pdf_path, flow_name, **options) ⇒ Object

Soumet une facture à une PDP via l’API AFNOR.



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/factpulse/helpers/client.rb', line 389

def soumettre_facture_afnor(pdf_path, flow_name, **options)
  pdf_content = File.binread(pdf_path)
  sha256 = Digest::SHA256.hexdigest(pdf_content)

  flow_info = {
    'name' => flow_name,
    'flowSyntax' => options[:flow_syntax] || 'CII',
    'flowProfile' => options[:flow_profile] || 'EN16931',
    'sha256' => sha256
  }
  flow_info['trackingId'] = options[:tracking_id] if options[:tracking_id]

  make_afnor_request('POST', '/flow/v1/flows', multipart: [
    { name: 'file', content: pdf_content, filename: File.basename(pdf_path), content_type: 'application/pdf' },
    { name: 'flowInfo', content: JSON.generate(flow_info), content_type: 'application/json' }
  ])
end

#soumettre_facture_chorus(facture_data) ⇒ Object

Soumet une facture à Chorus Pro.



535
536
537
# File 'lib/factpulse/helpers/client.rb', line 535

def soumettre_facture_chorus(facture_data)
  make_chorus_request('POST', '/factures/soumettre', facture_data)
end

#telecharger_flux_afnor(flow_id) ⇒ Object

Télécharge le fichier PDF d’un flux AFNOR.



421
422
423
424
# File 'lib/factpulse/helpers/client.rb', line 421

def telecharger_flux_afnor(flow_id)
  result = make_afnor_request('GET', "/flow/v1/flows/#{flow_id}")
  result['_raw'] || ''
end

#valider_pdf_facturx(pdf_path, profil: 'EN16931') ⇒ Object

Valide un PDF Factur-X.



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/factpulse/helpers/client.rb', line 549

def valider_pdf_facturx(pdf_path, profil: 'EN16931')
  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/traitement/valider-pdf-facturx")
  pdf_content = File.binread(pdf_path)

  boundary = "----RubyFormBoundary#{SecureRandom.hex(16)}"
  body = build_multipart_body(boundary, [
    { name: 'fichier_pdf', content: pdf_content, filename: File.basename(pdf_path), content_type: 'application/pdf' },
    { name: 'profil', content: profil }
  ])

  response = http_multipart_post(uri, body, boundary)
  raise FactPulseValidationError.new("Validation error: #{response.code}") unless response.is_a?(Net::HTTPSuccess)
  JSON.parse(response.body) rescue {}
end

#valider_signature_pdf(pdf_path) ⇒ Object

Valide la signature d’un PDF signé.



582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/factpulse/helpers/client.rb', line 582

def valider_signature_pdf(pdf_path)
  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/traitement/valider-signature-pdf")
  pdf_content = File.binread(pdf_path)

  boundary = "----RubyFormBoundary#{SecureRandom.hex(16)}"
  body = build_multipart_body(boundary, [
    { name: 'fichier_pdf', content: pdf_content, filename: File.basename(pdf_path), content_type: 'application/pdf' }
  ])

  response = http_multipart_post(uri, body, boundary)
  raise FactPulseValidationError.new("Validation error: #{response.code}") unless response.is_a?(Net::HTTPSuccess)
  JSON.parse(response.body) rescue {}
end

#valider_xml_facturx(xml_content, profil: 'EN16931') ⇒ Object

Valide un XML Factur-X.



566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/factpulse/helpers/client.rb', line 566

def valider_xml_facturx(xml_content, profil: 'EN16931')
  ensure_authenticated
  uri = URI("#{@api_url}/api/v1/traitement/valider-xml")

  boundary = "----RubyFormBoundary#{SecureRandom.hex(16)}"
  body = build_multipart_body(boundary, [
    { name: 'fichier_xml', content: xml_content, filename: 'facture.xml', content_type: 'application/xml' },
    { name: 'profil', content: profil }
  ])

  response = http_multipart_post(uri, body, boundary)
  raise FactPulseValidationError.new("Validation error: #{response.code}") unless response.is_a?(Net::HTTPSuccess)
  JSON.parse(response.body) rescue {}
end