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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +75 -0
- data/lib/fiscalisto/core.rb +363 -0
- data/lib/fiscalisto/error_codes.rb +662 -0
- data/lib/fiscalisto/errors.rb +103 -0
- data/lib/fiscalisto/fiscalisto_object.rb +100 -0
- data/lib/fiscalisto/list_object.rb +63 -0
- data/lib/fiscalisto/resources.rb +12577 -0
- data/lib/fiscalisto/version.rb +7 -0
- data/lib/fiscalisto/webhooks.rb +95 -0
- data/lib/fiscalisto.rb +49 -0
- metadata +60 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fiscalisto
|
|
4
|
+
# Un error de la API (o de la conexión, con type "connection_error"). +code+ es estable: decida
|
|
5
|
+
# con él (Fiscalisto::ErrorCode); el mensaje es para mostrarlo a una persona. Cada +type+ tiene
|
|
6
|
+
# su clase (InvalidRequestError, RateLimitError…), así que también puede rescatar por clase.
|
|
7
|
+
#
|
|
8
|
+
# begin
|
|
9
|
+
# fiscalisto.invoices.create(params)
|
|
10
|
+
# rescue Fiscalisto::InvoiceError => e
|
|
11
|
+
# puts e.code, e.sat_code, e.request_log_url
|
|
12
|
+
# end
|
|
13
|
+
class Error < StandardError
|
|
14
|
+
# invalid_request_error, authentication_error, permission_error, not_found_error,
|
|
15
|
+
# idempotency_error, rate_limit_error, invoice_error, balance_error, api_error,
|
|
16
|
+
# connection_error.
|
|
17
|
+
attr_reader :type
|
|
18
|
+
attr_reader :code
|
|
19
|
+
# El campo que hay que corregir (items.0.unit_price).
|
|
20
|
+
attr_reader :param
|
|
21
|
+
# El código del SAT o del PAC cuando ellos lo rechazaron (CFDI40145).
|
|
22
|
+
attr_reader :sat_code
|
|
23
|
+
# El comprobante rechazado (inv_…), cuando lo hay.
|
|
24
|
+
attr_reader :invoice
|
|
25
|
+
attr_reader :doc_url
|
|
26
|
+
# Búsquelo en Desarrolladores › Solicitudes o mándelo a soporte.
|
|
27
|
+
attr_reader :request_id
|
|
28
|
+
# La solicitud en Desarrolladores › Solicitudes, para abrirla en el navegador.
|
|
29
|
+
attr_reader :request_log_url
|
|
30
|
+
# La Idempotency-Key que se mandó: repita con ella para saber si se timbró.
|
|
31
|
+
attr_reader :idempotency_key
|
|
32
|
+
# El código HTTP (nil: la solicitud no llegó).
|
|
33
|
+
attr_reader :status
|
|
34
|
+
# El objeto error tal como llegó.
|
|
35
|
+
attr_reader :raw
|
|
36
|
+
|
|
37
|
+
# La clase de cada type.
|
|
38
|
+
def self.for(raw, status = nil, request_id = nil, idempotency_key = nil)
|
|
39
|
+
klass = TYPES.fetch(raw.is_a?(Hash) ? raw["type"] : nil, Error)
|
|
40
|
+
klass.new(raw, status, request_id, idempotency_key)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def initialize(raw = {}, status = nil, request_id = nil, idempotency_key = nil)
|
|
44
|
+
raw = raw.is_a?(Hash) ? raw.transform_keys(&:to_s) : {}
|
|
45
|
+
text = ->(k) { raw[k].is_a?(String) ? raw[k] : nil }
|
|
46
|
+
@raw = raw
|
|
47
|
+
@type = text.call("type") || "api_error"
|
|
48
|
+
@code = text.call("code") || "unknown"
|
|
49
|
+
@param = text.call("param")
|
|
50
|
+
@sat_code = text.call("sat_code")
|
|
51
|
+
@invoice = text.call("invoice")
|
|
52
|
+
@doc_url = text.call("doc_url")
|
|
53
|
+
@request_id = text.call("request_id") || request_id
|
|
54
|
+
@request_log_url = text.call("request_log_url")
|
|
55
|
+
@idempotency_key = text.call("idempotency_key") || idempotency_key
|
|
56
|
+
@status = status
|
|
57
|
+
super(text.call("message") || "Error de la API de Fiscalisto")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def inspect
|
|
61
|
+
"#<#{self.class.name} code=#{code.inspect} status=#{status.inspect} message=#{message.inspect}>"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# 400: falta un campo o no es válido (param dice cuál).
|
|
66
|
+
class InvalidRequestError < Error; end
|
|
67
|
+
# 401: la llave no sirve, venció o no llegó.
|
|
68
|
+
class AuthenticationError < Error; end
|
|
69
|
+
# 403: la llave no puede hacer eso.
|
|
70
|
+
class PermissionError < Error; end
|
|
71
|
+
# 404: no existe (o no es de esta llave).
|
|
72
|
+
class NotFoundError < Error; end
|
|
73
|
+
# 409/422: la misma Idempotency-Key con otro cuerpo, o una que sigue en curso.
|
|
74
|
+
class IdempotencyError < Error; end
|
|
75
|
+
# 429: demasiadas solicitudes (el SDK ya esperó y reintentó).
|
|
76
|
+
class RateLimitError < Error; end
|
|
77
|
+
# 402/422: el SAT o el PAC no aceptó el comprobante (sat_code dice por qué).
|
|
78
|
+
class InvoiceError < Error; end
|
|
79
|
+
# 402: no quedan timbres.
|
|
80
|
+
class BalanceError < Error; end
|
|
81
|
+
# 5xx: falló de nuestro lado (o lo que contestó no era la API).
|
|
82
|
+
class APIError < Error; end
|
|
83
|
+
# La API no contestó: la red, un proxy o el tiempo de espera.
|
|
84
|
+
class APIConnectionError < Error; end
|
|
85
|
+
|
|
86
|
+
class Error
|
|
87
|
+
TYPES = {
|
|
88
|
+
"invalid_request_error" => InvalidRequestError,
|
|
89
|
+
"authentication_error" => AuthenticationError,
|
|
90
|
+
"permission_error" => PermissionError,
|
|
91
|
+
"not_found_error" => NotFoundError,
|
|
92
|
+
"idempotency_error" => IdempotencyError,
|
|
93
|
+
"rate_limit_error" => RateLimitError,
|
|
94
|
+
"invoice_error" => InvoiceError,
|
|
95
|
+
"balance_error" => BalanceError,
|
|
96
|
+
"api_error" => APIError,
|
|
97
|
+
"connection_error" => APIConnectionError
|
|
98
|
+
}.freeze
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# El webhook no es de Fiscalisto: falta un encabezado, la hora o la firma no cuadran.
|
|
102
|
+
class WebhookSignatureError < StandardError; end
|
|
103
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fiscalisto
|
|
4
|
+
# Un objeto de la API: léalo como método (+invoice.uuid+) o como Hash (+invoice[:uuid]+,
|
|
5
|
+
# +invoice["uuid"]+). Los objetos de adentro también lo son, y un campo expandido
|
|
6
|
+
# (expand: ["issuer"]) trae aquí el objeto en lugar de su id. Un campo que se llama como un
|
|
7
|
+
# método de Ruby (+class+, +hash+, +method+…) se lee con [].
|
|
8
|
+
# (Sin Enumerable a propósito: count, first, max… son nombres de campos.)
|
|
9
|
+
class FiscalistoObject
|
|
10
|
+
# El JSON de la API como FiscalistoObject (y sus listas y objetos de adentro).
|
|
11
|
+
def self.construct(value)
|
|
12
|
+
case value
|
|
13
|
+
when Hash then new(value)
|
|
14
|
+
when Array then value.map { |v| construct(v) }
|
|
15
|
+
else value
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(values = {})
|
|
20
|
+
@values = values.to_h { |k, v| [k.to_s, self.class.construct(v)] }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def [](key)
|
|
24
|
+
@values[key.to_s]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def []=(key, value)
|
|
28
|
+
@values[key.to_s] = self.class.construct(value)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def key?(key)
|
|
32
|
+
@values.key?(key.to_s)
|
|
33
|
+
end
|
|
34
|
+
alias include? key?
|
|
35
|
+
|
|
36
|
+
def keys
|
|
37
|
+
@values.keys
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def dig(key, *rest)
|
|
41
|
+
value = self[key]
|
|
42
|
+
rest.empty? || value.nil? ? value : value.dig(*rest)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def fetch(key, *default, &block)
|
|
46
|
+
@values.fetch(key.to_s, *default, &block)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def each(&block)
|
|
50
|
+
@values.each(&block)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Como Hash, con llaves String (y sus objetos de adentro también).
|
|
54
|
+
def to_h
|
|
55
|
+
@values.transform_values { |v| self.class.plain(v) }
|
|
56
|
+
end
|
|
57
|
+
alias to_hash to_h
|
|
58
|
+
|
|
59
|
+
def to_json(*args)
|
|
60
|
+
require "json"
|
|
61
|
+
to_h.to_json(*args)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def ==(other)
|
|
65
|
+
other.is_a?(FiscalistoObject) ? to_h == other.to_h : to_h == other
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def inspect
|
|
69
|
+
id = @values["id"] ? " id=#{@values["id"].inspect}" : ""
|
|
70
|
+
"#<#{self.class.name}[#{@values["object"] || "object"}]#{id} #{to_h.inspect}>"
|
|
71
|
+
end
|
|
72
|
+
alias to_s inspect
|
|
73
|
+
|
|
74
|
+
def method_missing(name, *args)
|
|
75
|
+
key = name.to_s
|
|
76
|
+
if key.end_with?("=") && args.length == 1
|
|
77
|
+
self[key.chomp("=")] = args.first
|
|
78
|
+
elsif args.empty? && @values.key?(key)
|
|
79
|
+
@values[key]
|
|
80
|
+
elsif args.empty? && key.end_with?("?") && @values.key?(key.chomp("?"))
|
|
81
|
+
!!@values[key.chomp("?")]
|
|
82
|
+
else
|
|
83
|
+
super
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def respond_to_missing?(name, include_private = false)
|
|
88
|
+
key = name.to_s.chomp("=").chomp("?")
|
|
89
|
+
@values.key?(key) || super
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def self.plain(value)
|
|
93
|
+
case value
|
|
94
|
+
when FiscalistoObject then value.to_h
|
|
95
|
+
when Array then value.map { |v| plain(v) }
|
|
96
|
+
else value
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fiscalisto
|
|
4
|
+
# Una lista: +data+ y +has_more+ son la primera página; +each+ (y todo Enumerable: map, select,
|
|
5
|
+
# first(50), to_a…) recorre todos los objetos, página por página, pidiendo la siguiente solo
|
|
6
|
+
# cuando hace falta.
|
|
7
|
+
#
|
|
8
|
+
# fiscalisto.invoices.list(status: "valid").each { |invoice| puts invoice.uuid }
|
|
9
|
+
class ListObject
|
|
10
|
+
include Enumerable
|
|
11
|
+
|
|
12
|
+
# page: lambda(cursor) que contesta una página (Hash de la API); forward: con
|
|
13
|
+
# ending_before las páginas van hacia lo más nuevo.
|
|
14
|
+
def initialize(page, forward: false)
|
|
15
|
+
@page = page
|
|
16
|
+
@forward = forward
|
|
17
|
+
@first = nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# La primera página como la contesta la API (object, data, has_more, url).
|
|
21
|
+
def first_page
|
|
22
|
+
@first ||= @page.call(nil)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def data
|
|
26
|
+
first_page["data"]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def has_more
|
|
30
|
+
first_page["has_more"] ? true : false
|
|
31
|
+
end
|
|
32
|
+
alias has_more? has_more
|
|
33
|
+
|
|
34
|
+
def url
|
|
35
|
+
first_page["url"]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# lista[:data], lista["has_more"]: la primera página.
|
|
39
|
+
def [](key)
|
|
40
|
+
first_page[key]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def each(&block)
|
|
44
|
+
return enum_for(:each) unless block
|
|
45
|
+
|
|
46
|
+
page = first_page
|
|
47
|
+
loop do
|
|
48
|
+
items = page["data"] || []
|
|
49
|
+
(@forward ? items.reverse : items).each(&block)
|
|
50
|
+
break if !page["has_more"] || items.empty?
|
|
51
|
+
|
|
52
|
+
page = @page.call((@forward ? items.first : items.last)["id"])
|
|
53
|
+
end
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
# El nombre de Stripe.
|
|
57
|
+
alias auto_paging_each each
|
|
58
|
+
|
|
59
|
+
def inspect
|
|
60
|
+
"#<#{self.class.name} url=#{url.inspect} has_more=#{has_more}>"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|