fiscalisto 0.1.225

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fiscalisto
4
+ VERSION = "0.1.225"
5
+ # La versión de la API para la que se hizo este SDK: va en cada solicitud (Fiscalisto-Version).
6
+ API_VERSION = "2026-11-25"
7
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "openssl"
5
+
6
+ module Fiscalisto
7
+ # Verificar que un webhook lo mandó Fiscalisto (Standard Webhooks): la firma HMAC-SHA256 de
8
+ # `webhook-id.webhook-timestamp.cuerpo` con el secreto whsec_ del endpoint.
9
+ #
10
+ # # Rails: request.raw_post y request.headers; Sinatra/Rack: request.body.read y request.env
11
+ # event = Fiscalisto::Webhook.verify(request.raw_post, request.headers, ENV["FISCALISTO_WEBHOOK_SECRET"])
12
+ # case event.type
13
+ # when "invoice.stamped" then puts event.data.object.uuid
14
+ # end
15
+ module Webhook
16
+ module_function
17
+
18
+ # Revisa la firma y la hora de un webhook y le devuelve el evento (un FiscalistoObject).
19
+ # Pase el cuerpo exactamente como llegó, no el JSON ya interpretado: un espacio de diferencia
20
+ # cambia la firma. headers: un Hash (cualquier mayúscula), el env de Rack o request.headers
21
+ # de Rails. Lanza WebhookSignatureError si no es de Fiscalisto.
22
+ def verify(payload, headers, secret, tolerance: 300, now: nil)
23
+ unless payload.is_a?(String)
24
+ raise WebhookSignatureError,
25
+ "Pase el cuerpo del webhook tal como llegó (texto), no el JSON ya interpretado."
26
+ end
27
+
28
+ body = payload.b
29
+ msg_id = header(headers, "webhook-id")
30
+ timestamp = header(headers, "webhook-timestamp")
31
+ signatures = header(headers, "webhook-signature")
32
+ if [msg_id, timestamp, signatures].any? { |v| v.nil? || v.empty? }
33
+ raise WebhookSignatureError, "Faltan los encabezados webhook-id, webhook-timestamp o webhook-signature."
34
+ end
35
+
36
+ current = now || Time.now.to_f
37
+ if !timestamp.match?(/\A\d+\z/) || (current - timestamp.to_i).abs > tolerance
38
+ raise WebhookSignatureError, "La hora del webhook está fuera de tolerancia: puede ser una repetición."
39
+ end
40
+ raise WebhookSignatureError, "El secreto del endpoint empieza con whsec_." unless secret.to_s.start_with?("whsec_")
41
+
42
+ begin
43
+ key = secret.delete_prefix("whsec_").unpack1("m0") # strict Base64 (no base64 gem: Ruby 3.4 unbundled it)
44
+ rescue ArgumentError
45
+ raise WebhookSignatureError, "El secreto del endpoint no es válido."
46
+ end
47
+ expected = OpenSSL::HMAC.digest("SHA256", key, "#{msg_id}.#{timestamp}.".b + body)
48
+ # Several during a secret change, separated by spaces: any one valid is enough.
49
+ signatures.split(" ").each do |part|
50
+ version, signature = part.split(",", 2)
51
+ next unless version == "v1" && signature && !signature.empty?
52
+
53
+ given = begin
54
+ signature.unpack1("m0")
55
+ rescue ArgumentError
56
+ next
57
+ end
58
+ next unless given.bytesize == expected.bytesize && OpenSSL.fixed_length_secure_compare(given, expected)
59
+
60
+ return FiscalistoObject.construct(JSON.parse(body.force_encoding(Encoding::UTF_8)))
61
+ end
62
+ raise WebhookSignatureError, "La firma del webhook no coincide con el secreto."
63
+ end
64
+
65
+ # Rails' request.headers and Rack's env (HTTP_WEBHOOK_ID) or any Hash, in any case.
66
+ def header(headers, name)
67
+ rack = "HTTP_#{name.upcase.tr("-", "_")}"
68
+ [name, name.split("-").map(&:capitalize).join("-"), rack].each do |key|
69
+ value = begin
70
+ headers[key]
71
+ rescue StandardError
72
+ nil
73
+ end
74
+ value = value.first if value.is_a?(Array)
75
+ return value.to_s unless value.nil?
76
+ end
77
+ return nil unless headers.respond_to?(:each)
78
+
79
+ headers.each do |key, value|
80
+ next unless key.to_s.downcase == name
81
+
82
+ return (value.is_a?(Array) ? value.first : value).to_s
83
+ end
84
+ nil
85
+ end
86
+ end
87
+
88
+ # fiscalisto.webhooks: verificar lo que llega a su endpoint.
89
+ class Webhooks
90
+ # Igual que Fiscalisto::Webhook.verify.
91
+ def verify(payload, headers, secret, tolerance: 300, now: nil)
92
+ Webhook.verify(payload, headers, secret, tolerance: tolerance, now: now)
93
+ end
94
+ end
95
+ end
data/lib/fiscalisto.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # El SDK de la API de facturación de Fiscalisto.
4
+ #
5
+ # require "fiscalisto"
6
+ #
7
+ # fiscalisto = Fiscalisto::Client.new # la llave viene de FISCALISTO_API_KEY
8
+ # invoice = fiscalisto.invoices.create(
9
+ # {
10
+ # customer: { tax_id: "ICV060329BY0", legal_name: "INMOBILIARIA CVA", tax_system: "601", zip: "33826" },
11
+ # payment_form: "03",
12
+ # items: [{ description: "Consultoría", product_key: "80101500", unit_price: 1000 }]
13
+ # },
14
+ # idempotency_key: "pedido-A-1042"
15
+ # )
16
+ # puts invoice.uuid
17
+ #
18
+ # https://fiscalisto.com/docs/api
19
+ module Fiscalisto
20
+ end
21
+
22
+ require_relative "fiscalisto/version"
23
+ require_relative "fiscalisto/errors"
24
+ require_relative "fiscalisto/error_codes"
25
+ require_relative "fiscalisto/fiscalisto_object"
26
+ require_relative "fiscalisto/list_object"
27
+ require_relative "fiscalisto/core"
28
+ require_relative "fiscalisto/webhooks"
29
+ require_relative "fiscalisto/resources"
30
+
31
+ module Fiscalisto
32
+ # El cliente de la API: Fiscalisto::Client.new("fk_test_…") o, sin llave, FISCALISTO_API_KEY.
33
+ #
34
+ # Opciones: base_url, max_retries (2), timeout (80 s), transport (otro cliente HTTP),
35
+ # api_version (la de la API, por omisión API_VERSION; nil usa la de su cuenta), language
36
+ # ("en" para los errores en inglés) y warn (cómo avisar que la llave vence pronto; Kernel.warn).
37
+ #
38
+ # Cada recurso es un método (fiscalisto.invoices, fiscalisto.customers…) y cada llamada recibe
39
+ # sus params (un Hash) y, al final, las opciones de esa llamada: idempotency_key (en lo que
40
+ # timbra o crea), timeout, max_retries y expand.
41
+ class Client < Core
42
+ include Resources
43
+
44
+ # Verificar lo que llega a su endpoint.
45
+ def webhooks
46
+ @webhooks ||= Webhooks.new
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fiscalisto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.225
5
+ platform: ruby
6
+ authors:
7
+ - Fiscalisto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-11-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Timbre facturas, pagos, notas de crédito, nómina, retenciones y lotes
14
+ (CFDI 4.0) con la API de Fiscalisto: Idempotency-Key automática, reintentos seguros,
15
+ listas que se recorren solas y webhooks verificados. Sin dependencias.'
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - lib/fiscalisto.rb
24
+ - lib/fiscalisto/core.rb
25
+ - lib/fiscalisto/error_codes.rb
26
+ - lib/fiscalisto/errors.rb
27
+ - lib/fiscalisto/fiscalisto_object.rb
28
+ - lib/fiscalisto/list_object.rb
29
+ - lib/fiscalisto/resources.rb
30
+ - lib/fiscalisto/version.rb
31
+ - lib/fiscalisto/webhooks.rb
32
+ homepage: https://fiscalisto.com/docs/api/sdk
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://fiscalisto.com/docs/api/sdk
37
+ documentation_uri: https://fiscalisto.com/docs/api
38
+ source_code_uri: https://github.com/fiscalisto/fiscalisto-ruby
39
+ changelog_uri: https://fiscalisto.com/docs/api/cambios
40
+ rubygems_mfa_required: 'true'
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.1'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.5.22
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: 'El SDK de la API de facturación de Fiscalisto: CFDI 4.0 desde Ruby.'
60
+ test_files: []