einvoice 1.5.0 → 2.0.0.beta1
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 +4 -4
- data/.coderabbit.yaml +6 -8
- data/.github/workflows/build.yml +6 -3
- data/.github/workflows/cron.yml +4 -2
- data/CHANGELOG.md +63 -44
- data/Gemfile +3 -10
- data/README.md +284 -325
- data/README.zh-TW.md +381 -0
- data/Rakefile +2 -51
- data/bin/console +3 -6
- data/einvoice.gemspec +17 -18
- data/lib/einvoice/amego/client.rb +116 -0
- data/lib/einvoice/amego/errors.rb +84 -0
- data/lib/einvoice/amego/payload.rb +270 -0
- data/lib/einvoice/amego/provider.rb +272 -0
- data/lib/einvoice/amego.rb +27 -0
- data/lib/einvoice/capability.rb +79 -0
- data/lib/einvoice/ecpay/client.rb +184 -0
- data/lib/einvoice/ecpay/crypto.rb +85 -0
- data/lib/einvoice/ecpay/errors.rb +115 -0
- data/lib/einvoice/ecpay/payload.rb +241 -0
- data/lib/einvoice/ecpay/provider.rb +371 -0
- data/lib/einvoice/ecpay.rb +31 -0
- data/lib/einvoice/error.rb +220 -0
- data/lib/einvoice/ezpay/client.rb +130 -0
- data/lib/einvoice/ezpay/crypto.rb +85 -0
- data/lib/einvoice/ezpay/errors.rb +72 -0
- data/lib/einvoice/ezpay/payload.rb +269 -0
- data/lib/einvoice/ezpay/provider.rb +253 -0
- data/lib/einvoice/ezpay.rb +19 -0
- data/lib/einvoice/input.rb +547 -0
- data/lib/einvoice/mock_provider.rb +191 -0
- data/lib/einvoice/mof/donation_codes.rb +157 -0
- data/lib/einvoice/mof.rb +16 -0
- data/lib/einvoice/number.rb +91 -0
- data/lib/einvoice/provider.rb +57 -13
- data/lib/einvoice/redaction.rb +54 -0
- data/lib/einvoice/taipei.rb +89 -0
- data/lib/einvoice/tax_split.rb +112 -0
- data/lib/einvoice/tradevan/default_api/client.rb +175 -0
- data/lib/einvoice/tradevan/default_api/crypto.rb +90 -0
- data/lib/einvoice/tradevan/default_api/errors.rb +54 -0
- data/lib/einvoice/tradevan/default_api/payload.rb +259 -0
- data/lib/einvoice/tradevan/default_api/provider.rb +353 -0
- data/lib/einvoice/tradevan/default_api/rules.rb +314 -0
- data/lib/einvoice/tradevan.rb +25 -0
- data/lib/einvoice/types.rb +365 -0
- data/lib/einvoice/version.rb +3 -1
- data/lib/einvoice.rb +21 -9
- metadata +52 -144
- data/docs/Tradevan E-Invoice APIs.md +0 -201
- data/docs/neweb.md +0 -347
- data/lib/einvoice/client.rb +0 -21
- data/lib/einvoice/configuration.rb +0 -49
- data/lib/einvoice/connection.rb +0 -31
- data/lib/einvoice/donation_unit_list.json +0 -2025
- data/lib/einvoice/result.rb +0 -21
- data/lib/einvoice/tradevan/model/base.rb +0 -34
- data/lib/einvoice/tradevan/model/issue_data.rb +0 -129
- data/lib/einvoice/tradevan/model/issue_item.rb +0 -41
- data/lib/einvoice/tradevan/model/void_data.rb +0 -35
- data/lib/einvoice/tradevan/provider.rb +0 -139
- data/lib/einvoice/tradevan/result.rb +0 -25
- data/lib/einvoice/tradevan/validator/issue_data_validator.rb +0 -80
- data/lib/faraday/response/decode_tradevan.rb +0 -37
- data/lib/faraday/response/parse_xml.rb +0 -14
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Einvoice
|
|
4
|
+
module Tradevan
|
|
5
|
+
module DefaultAPI
|
|
6
|
+
# The +issueData+ and +voidData+ field rules, ported from the 1.x
|
|
7
|
+
# validators (+IssueData+, +IssueItem+, +VoidData+ and
|
|
8
|
+
# +issue_data_validator.rb+). Those rules are the only record of the
|
|
9
|
+
# DEFAULTAPI schema, and they ran in production, so they are kept as they
|
|
10
|
+
# were. Two changes:
|
|
11
|
+
#
|
|
12
|
+
# - The 1.x client also checked +donationUnit+ against a bundled list of
|
|
13
|
+
# donation codes. 2.0 has no bundled list. {Einvoice::Input} checks the
|
|
14
|
+
# format, and {Einvoice::MOF::DonationCodes} checks the registry.
|
|
15
|
+
# - A key outside the schema is an error here. Tradevan rejects the whole
|
|
16
|
+
# request when it sees one (PR #38), so it is better to fail before the
|
|
17
|
+
# call.
|
|
18
|
+
#
|
|
19
|
+
# Each check returns a Hash of field name => messages. An empty Hash means
|
|
20
|
+
# the data is valid.
|
|
21
|
+
module Rules
|
|
22
|
+
# A whole decimal string. String#to_r would read "100oops" as 100.
|
|
23
|
+
DECIMAL = /\A-?\d+(\.\d+)?\z/
|
|
24
|
+
|
|
25
|
+
ISSUE_TYPES = %w[I R G H A].freeze
|
|
26
|
+
VOID_TYPES = %w[C I A].freeze
|
|
27
|
+
YES_NO = %w[Y N].freeze
|
|
28
|
+
DATE = /\A\d{8}\z/
|
|
29
|
+
TIME = /\A\d{2}:\d{2}:\d{2}\z/
|
|
30
|
+
|
|
31
|
+
ISSUE_FIELDS = %w[
|
|
32
|
+
companyUn orgId orgUn type saleIdentifier allowanceIdentifier transactionNumber
|
|
33
|
+
transactionDate transactionTime total transactionSource transactionTarget
|
|
34
|
+
invoiceNumber allowanceNumber allowanceDate allowanceExclusiveAmount allowanceTax
|
|
35
|
+
allowancePaperReturned allowanceInclusiveAmount paperPrintMode invoiceAlarmMode
|
|
36
|
+
invoicePaperReturned donate donationUnit carrierType carrierId carrierIdHidden
|
|
37
|
+
buyerUn buyerTitle receiverName receiverAddrZip receiverAddrRoad receiverEmail
|
|
38
|
+
receiverMobile allowaDeclaration idViewId memberId checkNumber invoiceDate
|
|
39
|
+
invoiceTime texclusiveAmount oeclusiveAmount zexclusiveAmount tax mainRemark
|
|
40
|
+
invoiceType itemList
|
|
41
|
+
].freeze
|
|
42
|
+
|
|
43
|
+
ITEM_FIELDS = %w[
|
|
44
|
+
saleIdentifier serialNumber invoiceNumber invoiceDate invoiceTime productCode
|
|
45
|
+
productName qty price tax itemExclude itemTotal taxType description
|
|
46
|
+
].freeze
|
|
47
|
+
|
|
48
|
+
# Fields that carry the caller's input, with a DEFAULTAPI length limit of
|
|
49
|
+
# Tradevan's own: stricter than the MIG's (productName 300 against 500,
|
|
50
|
+
# receiverEmail 80 against 400, receiverMobile 15 against 26, carrierId
|
|
51
|
+
# 64 against 400), or with no MIG field (the order and allowance ids).
|
|
52
|
+
# The core refuses a value over the MIG's limit first, so a value here
|
|
53
|
+
# is over Tradevan's limit only.
|
|
54
|
+
OWN_LIMITS = %w[
|
|
55
|
+
transactionNumber saleIdentifier allowanceIdentifier productName
|
|
56
|
+
receiverEmail receiverMobile carrierId carrierIdHidden
|
|
57
|
+
].freeze
|
|
58
|
+
TOO_LONG = /\Amust be \d+ characters or fewer\z/
|
|
59
|
+
|
|
60
|
+
VOID_FIELDS = %w[
|
|
61
|
+
type saleIdentifier invoiceNumber invoicePaperReturned allowanceNumber
|
|
62
|
+
allowancePaperReturned companyUn
|
|
63
|
+
].freeze
|
|
64
|
+
|
|
65
|
+
module_function
|
|
66
|
+
|
|
67
|
+
# Errors for an +issueData+ Hash (string or symbol keys).
|
|
68
|
+
def issue_errors(data)
|
|
69
|
+
data = stringify(data)
|
|
70
|
+
check = Check.new(data)
|
|
71
|
+
type = data["type"].to_s
|
|
72
|
+
|
|
73
|
+
check.unknown_keys(ISSUE_FIELDS)
|
|
74
|
+
issue_common(check, data)
|
|
75
|
+
issue_by_type(check, data, type)
|
|
76
|
+
issue_items(check, data, type)
|
|
77
|
+
check.errors
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Errors for one +itemList+ entry.
|
|
81
|
+
def item_errors(item)
|
|
82
|
+
item = stringify(item)
|
|
83
|
+
check = Check.new(item)
|
|
84
|
+
check.unknown_keys(ITEM_FIELDS)
|
|
85
|
+
check.required("saleIdentifier", max: 100)
|
|
86
|
+
check.required("serialNumber", is: 4, format: /\A\d+\z/)
|
|
87
|
+
check.optional("invoiceNumber", is: 10)
|
|
88
|
+
check.optional("invoiceDate", is: 8, format: /\A\d+\z/)
|
|
89
|
+
check.optional("invoiceTime", is: 8, format: TIME)
|
|
90
|
+
check.optional("productCode", max: 30)
|
|
91
|
+
check.required("productName", max: 300)
|
|
92
|
+
check.required("qty", max: 20)
|
|
93
|
+
check.required("price", max: 20)
|
|
94
|
+
%w[tax itemExclude itemTotal].each { |field| check.optional(field, max: 20) }
|
|
95
|
+
check.optional("taxType", in: %w[T O Z])
|
|
96
|
+
check.optional("description", max: 300)
|
|
97
|
+
check.errors
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Errors for a +voidData+ Hash.
|
|
101
|
+
def void_errors(data)
|
|
102
|
+
data = stringify(data)
|
|
103
|
+
check = Check.new(data)
|
|
104
|
+
type = data["type"].to_s
|
|
105
|
+
|
|
106
|
+
check.unknown_keys(VOID_FIELDS)
|
|
107
|
+
check.required("type", in: VOID_TYPES)
|
|
108
|
+
if %w[C I].include?(type)
|
|
109
|
+
check.required("saleIdentifier", max: 100)
|
|
110
|
+
check.required("invoiceNumber", is: 10)
|
|
111
|
+
check.required("invoicePaperReturned", in: YES_NO)
|
|
112
|
+
end
|
|
113
|
+
if type == "A"
|
|
114
|
+
check.required("companyUn", is: 8)
|
|
115
|
+
check.required("allowanceNumber", is: 16)
|
|
116
|
+
check.required("allowancePaperReturned", in: YES_NO)
|
|
117
|
+
end
|
|
118
|
+
check.errors
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Raise an error listing every problem, or return +nil+. When every
|
|
122
|
+
# problem is a length over one of Tradevan's own limits, the MIG allows
|
|
123
|
+
# the input, so it is an UnsupportedError (docs/providers/README.md,
|
|
124
|
+
# "The unified model"). Anything else is a ValidationError.
|
|
125
|
+
def validate!(errors, what)
|
|
126
|
+
return if errors.empty?
|
|
127
|
+
|
|
128
|
+
details = errors.flat_map { |field, messages| messages.map { |m| "#{field} #{m}" } }
|
|
129
|
+
if errors.all? { |field, messages| own_limit?(field, messages) }
|
|
130
|
+
raise UnsupportedError.new("Tradevan DEFAULTAPI does not take this #{what} — #{details.join('; ')}",
|
|
131
|
+
provider: Client::PROVIDER, raw_message: details.join("; "))
|
|
132
|
+
end
|
|
133
|
+
raise ValidationError.new("Invalid Tradevan #{what} — #{details.join('; ')}",
|
|
134
|
+
provider: Client::PROVIDER, raw_message: details.join("; "))
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def own_limit?(field, messages)
|
|
138
|
+
OWN_LIMITS.include?(field.sub(/\AitemList\[\d+\]\./, "")) && messages.all?(TOO_LONG)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def issue_common(check, data)
|
|
142
|
+
check.required("companyUn", is: 8)
|
|
143
|
+
check.required("orgId", is: 5)
|
|
144
|
+
check.optional("orgUn", is: 8)
|
|
145
|
+
check.required("type", in: ISSUE_TYPES)
|
|
146
|
+
check.required("transactionNumber", max: 50)
|
|
147
|
+
check.required("transactionDate", format: DATE)
|
|
148
|
+
check.optional("transactionSource", max: 50)
|
|
149
|
+
check.optional("transactionTarget", max: 50)
|
|
150
|
+
check.required("paperPrintMode", in: %w[0 1 2 3 4])
|
|
151
|
+
check.required("invoiceAlarmMode", in: %w[0 1 2 3 4 5 6])
|
|
152
|
+
check.optional("idViewId", max: 10)
|
|
153
|
+
check.optional("memberId", max: 50)
|
|
154
|
+
|
|
155
|
+
type = data["type"].to_s
|
|
156
|
+
print_mode = data["paperPrintMode"].to_s.to_i
|
|
157
|
+
no_buyer = blank?(data["buyerUn"])
|
|
158
|
+
|
|
159
|
+
if type == "A" || (no_buyer && print_mode != 0)
|
|
160
|
+
check.required("invoicePaperReturned", in: YES_NO)
|
|
161
|
+
end
|
|
162
|
+
if print_mode.zero? && data["donate"] == "N" && type != "A"
|
|
163
|
+
check.required("carrierType", is: 6)
|
|
164
|
+
end
|
|
165
|
+
return if %w[A H].include?(type)
|
|
166
|
+
|
|
167
|
+
check.optional("buyerUn", is: 8)
|
|
168
|
+
check.optional("buyerTitle", max: 60)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def issue_by_type(check, data, type)
|
|
172
|
+
prefix = "#{data['companyUn']}_#{data['orgId']}_"
|
|
173
|
+
print_mode = data["paperPrintMode"].to_s.to_i
|
|
174
|
+
|
|
175
|
+
if %w[I R G H].include?(type)
|
|
176
|
+
check.required("saleIdentifier", max: 100, prefix: prefix)
|
|
177
|
+
end
|
|
178
|
+
if %w[A H].include?(type)
|
|
179
|
+
check.required("allowanceIdentifier", max: 100, prefix: prefix)
|
|
180
|
+
check.required("allowanceExclusiveAmount", max: 20)
|
|
181
|
+
check.required("allowanceTax", max: 8)
|
|
182
|
+
check.required("allowancePaperReturned", in: YES_NO)
|
|
183
|
+
check.required("allowanceInclusiveAmount", max: 20)
|
|
184
|
+
end
|
|
185
|
+
check.required("invoiceNumber", is: 10) if %w[R G H].include?(type)
|
|
186
|
+
if type == "H"
|
|
187
|
+
check.required("allowanceNumber", is: 16, prefix: data["orgId"].to_s)
|
|
188
|
+
check.required("allowanceDate", format: DATE)
|
|
189
|
+
end
|
|
190
|
+
check.optional("allowaDeclaration", format: /\A\d{6}\z/) if type == "A"
|
|
191
|
+
|
|
192
|
+
if %w[I R G].include?(type)
|
|
193
|
+
check.required("transactionTime", format: TIME)
|
|
194
|
+
check.required("total", max: 20)
|
|
195
|
+
check.total_matches_items
|
|
196
|
+
if blank?(data["buyerUn"]) && print_mode.zero?
|
|
197
|
+
check.required("donate", is: 1)
|
|
198
|
+
end
|
|
199
|
+
check.required("donationUnit", max: 10) if data["donate"] == "Y"
|
|
200
|
+
if print_mode.zero? && data["donate"] == "N"
|
|
201
|
+
check.required("carrierId", max: 64)
|
|
202
|
+
check.required("carrierIdHidden", max: 64)
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
if %w[I R G A].include?(type)
|
|
206
|
+
check.optional("receiverName", max: 30)
|
|
207
|
+
check.optional("receiverAddrZip", max: 6)
|
|
208
|
+
check.optional("receiverAddrRoad", max: 100)
|
|
209
|
+
check.optional("receiverEmail", max: 80)
|
|
210
|
+
check.optional("receiverMobile", max: 15)
|
|
211
|
+
end
|
|
212
|
+
check.optional("tax", max: 20) if %w[I G].include?(type)
|
|
213
|
+
issue_type_g(check) if type == "G"
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def issue_type_g(check)
|
|
217
|
+
check.optional("checkNumber", is: 4)
|
|
218
|
+
check.optional("invoiceDate", is: 8)
|
|
219
|
+
check.optional("invoiceTime", is: 8)
|
|
220
|
+
%w[texclusiveAmount oeclusiveAmount zexclusiveAmount].each do |field|
|
|
221
|
+
check.optional(field, max: 20)
|
|
222
|
+
end
|
|
223
|
+
check.optional("mainRemark", max: 300)
|
|
224
|
+
check.optional("invoiceType", in: %w[07 08])
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def issue_items(check, data, type)
|
|
228
|
+
items = data["itemList"]
|
|
229
|
+
unless items.is_a?(Array) && !items.empty?
|
|
230
|
+
check.add("itemList", "is required")
|
|
231
|
+
return
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
items = items.map { |item| stringify(item) }
|
|
235
|
+
items.each_with_index do |item, index|
|
|
236
|
+
item_errors(item).each do |field, messages|
|
|
237
|
+
messages.each { |message| check.add("itemList[#{index}].#{field}", message) }
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
needed = %w[itemTotal taxType]
|
|
242
|
+
needed += %w[invoiceNumber invoiceDate itemExclude] if %w[A H].include?(type)
|
|
243
|
+
needed.each do |field|
|
|
244
|
+
check.add("itemList", "needs #{field} on every item") if items.any? { |i| blank?(i[field]) }
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def stringify(hash)
|
|
249
|
+
return {} unless hash.is_a?(Hash)
|
|
250
|
+
|
|
251
|
+
hash.to_h { |key, value| [key.to_s, value] }
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def blank?(value)
|
|
255
|
+
value.nil? || value.to_s.strip.empty?
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Collects messages for one Hash. Lengths count characters, as the 1.x
|
|
259
|
+
# validators did.
|
|
260
|
+
class Check
|
|
261
|
+
attr_reader :errors
|
|
262
|
+
|
|
263
|
+
def initialize(data)
|
|
264
|
+
@data = data
|
|
265
|
+
@errors = {}
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def add(field, message)
|
|
269
|
+
(@errors[field] ||= []) << message
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def unknown_keys(allowed)
|
|
273
|
+
(@data.keys - allowed).each { |key| add(key, "is not a DEFAULTAPI field") }
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def required(field, **rule)
|
|
277
|
+
return add(field, "is required") if Rules.blank?(@data[field])
|
|
278
|
+
|
|
279
|
+
apply(field, **rule)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def optional(field, **rule)
|
|
283
|
+
apply(field, **rule) unless Rules.blank?(@data[field])
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def total_matches_items
|
|
287
|
+
items = Array(@data["itemList"]).map { |item| Rules.stringify(item) }
|
|
288
|
+
# Only whole decimal strings: String#to_r would read "100oops" as 100.
|
|
289
|
+
values = [@data["total"], *items.map { |item| item["itemTotal"] }].map(&:to_s)
|
|
290
|
+
return add("total", "and every itemTotal must be decimal numbers") unless values.all?(DECIMAL)
|
|
291
|
+
|
|
292
|
+
# Compared as decimals: "0.5" + "0.5" is "1".
|
|
293
|
+
sum = items.sum { |item| item["itemTotal"].to_s.to_r }
|
|
294
|
+
return if @data["total"].to_s.to_r == sum
|
|
295
|
+
|
|
296
|
+
add("total", "must equal the sum of itemTotal (#{Number.plain(sum)})")
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
private
|
|
300
|
+
|
|
301
|
+
def apply(field, is: nil, max: nil, in: nil, format: nil, prefix: nil)
|
|
302
|
+
value = @data[field].to_s
|
|
303
|
+
choices = binding.local_variable_get(:in)
|
|
304
|
+
add(field, "must be #{is} characters") if is && value.length != is
|
|
305
|
+
add(field, "must be #{max} characters or fewer") if max && value.length > max
|
|
306
|
+
add(field, "must be one of #{choices.join(', ')}") if choices && !choices.include?(value)
|
|
307
|
+
add(field, "has an invalid format") if format && !value.match?(format)
|
|
308
|
+
add(field, "must start with #{prefix}") if prefix && !value.start_with?(prefix)
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "einvoice"
|
|
4
|
+
require "einvoice/tradevan/default_api/crypto"
|
|
5
|
+
require "einvoice/tradevan/default_api/errors"
|
|
6
|
+
require "einvoice/tradevan/default_api/client"
|
|
7
|
+
require "einvoice/tradevan/default_api/rules"
|
|
8
|
+
require "einvoice/tradevan/default_api/payload"
|
|
9
|
+
require "einvoice/tradevan/default_api/provider"
|
|
10
|
+
|
|
11
|
+
module Einvoice
|
|
12
|
+
# Adapters for Tradevan 關貿網路. Each Tradevan API version has its own
|
|
13
|
+
# namespace, so a newer one can sit next to the old one:
|
|
14
|
+
#
|
|
15
|
+
# provider = Einvoice::Tradevan::DefaultAPI::Provider.new(...)
|
|
16
|
+
#
|
|
17
|
+
# There is deliberately no shorter alias. An alias that later points to a newer
|
|
18
|
+
# API would move users to it without them noticing.
|
|
19
|
+
module Tradevan
|
|
20
|
+
# Tradevan's DEFAULTAPI (paths under +/DEFAULTAPI/+), the API the 1.x client
|
|
21
|
+
# wraps. Experimental: see {Provider}.
|
|
22
|
+
module DefaultAPI
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|