einvoice 1.4.0 → 2.0.0.alpha2

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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.coderabbit.yaml +12 -0
  3. data/.github/workflows/build.yml +7 -4
  4. data/CHANGELOG.md +55 -35
  5. data/Gemfile +3 -2
  6. data/README.md +230 -265
  7. data/README.zh-TW.md +322 -0
  8. data/Rakefile +2 -13
  9. data/bin/console +3 -6
  10. data/einvoice.gemspec +24 -21
  11. data/lib/einvoice/amego/client.rb +116 -0
  12. data/lib/einvoice/amego/errors.rb +84 -0
  13. data/lib/einvoice/amego/payload.rb +276 -0
  14. data/lib/einvoice/amego/provider.rb +267 -0
  15. data/lib/einvoice/amego.rb +27 -0
  16. data/lib/einvoice/capability.rb +79 -0
  17. data/lib/einvoice/ecpay/client.rb +183 -0
  18. data/lib/einvoice/ecpay/crypto.rb +85 -0
  19. data/lib/einvoice/ecpay/errors.rb +115 -0
  20. data/lib/einvoice/ecpay/payload.rb +241 -0
  21. data/lib/einvoice/ecpay/provider.rb +362 -0
  22. data/lib/einvoice/ecpay.rb +31 -0
  23. data/lib/einvoice/error.rb +220 -0
  24. data/lib/einvoice/ezpay/client.rb +130 -0
  25. data/lib/einvoice/ezpay/crypto.rb +85 -0
  26. data/lib/einvoice/ezpay/errors.rb +72 -0
  27. data/lib/einvoice/ezpay/payload.rb +269 -0
  28. data/lib/einvoice/ezpay/provider.rb +235 -0
  29. data/lib/einvoice/ezpay.rb +19 -0
  30. data/lib/einvoice/input.rb +494 -0
  31. data/lib/einvoice/mock_provider.rb +191 -0
  32. data/lib/einvoice/mof/donation_codes.rb +157 -0
  33. data/lib/einvoice/mof.rb +16 -0
  34. data/lib/einvoice/number.rb +51 -0
  35. data/lib/einvoice/provider.rb +57 -14
  36. data/lib/einvoice/redaction.rb +54 -0
  37. data/lib/einvoice/taipei.rb +89 -0
  38. data/lib/einvoice/tax_split.rb +124 -0
  39. data/lib/einvoice/tradevan/default_api/client.rb +175 -0
  40. data/lib/einvoice/tradevan/default_api/crypto.rb +90 -0
  41. data/lib/einvoice/tradevan/default_api/errors.rb +54 -0
  42. data/lib/einvoice/tradevan/default_api/payload.rb +259 -0
  43. data/lib/einvoice/tradevan/default_api/provider.rb +366 -0
  44. data/lib/einvoice/tradevan/default_api/rules.rb +314 -0
  45. data/lib/einvoice/tradevan.rb +25 -0
  46. data/lib/einvoice/types.rb +323 -0
  47. data/lib/einvoice/version.rb +3 -1
  48. data/lib/einvoice.rb +21 -10
  49. metadata +60 -176
  50. data/.github/workflows/cron.yml +0 -31
  51. data/docs/Tradevan E-Invoice APIs.md +0 -201
  52. data/docs/neweb.md +0 -347
  53. data/lib/einvoice/client.rb +0 -21
  54. data/lib/einvoice/configuration.rb +0 -49
  55. data/lib/einvoice/connection.rb +0 -31
  56. data/lib/einvoice/donation_unit_list.json +0 -2022
  57. data/lib/einvoice/result.rb +0 -21
  58. data/lib/einvoice/tradevan/model/base.rb +0 -34
  59. data/lib/einvoice/tradevan/model/issue_data.rb +0 -129
  60. data/lib/einvoice/tradevan/model/issue_item.rb +0 -41
  61. data/lib/einvoice/tradevan/model/void_data.rb +0 -35
  62. data/lib/einvoice/tradevan/provider.rb +0 -140
  63. data/lib/einvoice/tradevan/result.rb +0 -25
  64. data/lib/einvoice/tradevan/validator/issue_data_validator.rb +0 -80
  65. data/lib/einvoice/utils.rb +0 -13
  66. data/lib/faraday/response/decode_tradevan.rb +0 -38
@@ -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
@@ -0,0 +1,323 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Einvoice
4
+ # Provider-agnostic domain model for Taiwan e-invoices (財政部 MIG 4.0).
5
+ #
6
+ # Every adapter maps these unified types to/from its own wire format; business
7
+ # code depends ONLY on these so switching providers never touches application
8
+ # logic. All value objects are immutable ({Data}) — use +#with+ to derive a
9
+ # changed copy, and pattern-match via +deconstruct_keys+.
10
+ #
11
+ # Money convention: the statutory amount fields are integers in New Taiwan
12
+ # Dollars (a MIG invariant — even cross-border invoices are filed in TWD). For
13
+ # a foreign-currency sale, set +currency+ (ISO 4217) + +exchange_rate+ to
14
+ # annotate the original transaction; the amount fields stay TWD.
15
+
16
+ # 課稅別 — maps to MIG TaxType.
17
+ module TaxType
18
+ TAXABLE = :taxable # 應稅 (5% business tax)
19
+ ZERO_RATED = :zero_rated # 零稅率 (exports etc.)
20
+ TAX_FREE = :tax_free # 免稅
21
+ SPECIAL = :special # 特種稅額
22
+ ALL = [TAXABLE, ZERO_RATED, TAX_FREE, SPECIAL].freeze
23
+ # Not a MIG tax type, and an issue input does not take it. It describes an
24
+ # existing invoice whose items have different tax types, on a reference or
25
+ # an allowance. Each allowance item then has to carry its own tax_type.
26
+ MIXED = :mixed
27
+ # Taxes that add tax to a line. ZERO_RATED and TAX_FREE lines have tax 0.
28
+ TAXED = [TAXABLE, SPECIAL].freeze
29
+
30
+ module_function
31
+
32
+ # The tax type of a whole invoice: the one its items share, else MIXED.
33
+ def of(items, invoice_tax_type)
34
+ kinds = items.map { |item| item.tax_type || invoice_tax_type }.uniq
35
+ kinds.size > 1 ? MIXED : kinds.first || invoice_tax_type
36
+ end
37
+ end
38
+
39
+ # Invoice category. Derived from whether the buyer has a 統一編號:
40
+ # B2B = triplicate (三聯式), B2C = duplicate (二聯式).
41
+ module InvoiceCategory
42
+ B2B = :b2b
43
+ B2C = :b2c
44
+ ALL = [B2B, B2C].freeze
45
+ end
46
+
47
+ # Whether item/line amounts already include the 5% business tax.
48
+ module PriceMode
49
+ TAX_INCLUSIVE = :tax_inclusive
50
+ TAX_EXCLUSIVE = :tax_exclusive
51
+ ALL = [TAX_INCLUSIVE, TAX_EXCLUSIVE].freeze
52
+ end
53
+
54
+ # 載具類別.
55
+ module CarrierType
56
+ MOBILE_BARCODE = :mobile_barcode # 手機條碼 — /ABC1234 (MIG 3J0002)
57
+ CITIZEN_CERTIFICATE = :citizen_certificate # 自然人憑證條碼 — 16 chars (MIG CQ0001)
58
+ MEMBER = :member # 會員/通用載具 issued by the center
59
+ ALL = [MOBILE_BARCODE, CITIZEN_CERTIFICATE, MEMBER].freeze
60
+ end
61
+
62
+ # Lifecycle status of an invoice as reported by a provider.
63
+ module InvoiceStatus
64
+ ISSUED = :issued
65
+ VOIDED = :voided
66
+ ALLOWANCE = :allowance # has at least one active allowance (折讓)
67
+ ALL = [ISSUED, VOIDED, ALLOWANCE].freeze
68
+ end
69
+
70
+ module ProviderMode
71
+ TEST = :test
72
+ PRODUCTION = :production
73
+ ALL = [TEST, PRODUCTION].freeze
74
+ end
75
+
76
+ # 買受人. A ubn (統一編號, 8 digits) implies a B2B / triplicate invoice.
77
+ Buyer = Data.define(:name, :ubn, :email, :address, :phone) do
78
+ def initialize(name: nil, ubn: nil, email: nil, address: nil, phone: nil)
79
+ super
80
+ end
81
+
82
+ # Presence of a 統一編號 implies a business (B2B) buyer.
83
+ def business?
84
+ !(ubn.nil? || ubn.to_s.empty?)
85
+ end
86
+
87
+ # Name, email, address and phone are personal data, so inspect, to_s and pp
88
+ # show them as [FILTERED], here and inside every input or result. The ubn is
89
+ # a company's public number and stays. Read the fields directly when needed.
90
+ def inspect
91
+ fields = to_h.map do |key, value|
92
+ "#{key}=#{key == :ubn || value.nil? ? value.inspect : Redaction::FILTERED}"
93
+ end
94
+ "#<data #{self.class.name} #{fields.join(', ')}>"
95
+ end
96
+ alias_method :to_s, :inspect
97
+
98
+ def pretty_print(printer)
99
+ printer.text(inspect)
100
+ end
101
+ end
102
+
103
+ # 載具. +code+ shape depends on +type+ (mobile barcode "/"+7, citizen cert 16
104
+ # chars, member often omitted — provider links by email/member id).
105
+ Carrier = Data.define(:type, :code) do
106
+ def initialize(type:, code: nil)
107
+ super
108
+ end
109
+ end
110
+
111
+ # 捐贈 — donating the invoice to a charity by 愛心碼.
112
+ Donation = Data.define(:npoban)
113
+
114
+ # 愛心碼 are 3–7 digits (MIG 捐贈碼). The single definition of that shape:
115
+ # {Input} enforces it on the way in, and both the adapters and
116
+ # {MOF::DonationCodes} refer to it rather than restating the pattern.
117
+ #
118
+ # Assigned here rather than inside a +Data.define+ block, where a constant
119
+ # would land in +Einvoice+ instead of on this class — the block is evaluated
120
+ # with the enclosing lexical scope, so +Donation::CODE_FORMAT+ would not exist.
121
+ Donation::CODE_FORMAT = /\A\d{3,7}\z/
122
+
123
+ # A single invoice line. +tax_type+ is required only on mixed-tax invoices.
124
+ # +unit_price+ and +amount+ include tax or not as the input's +price_mode+
125
+ # says. The core never changes them: the tax of each line is in the input's
126
+ # #tax_breakdown.
127
+ InvoiceItem = Data.define(:description, :quantity, :unit_price, :amount, :unit, :tax_type, :remark) do
128
+ def initialize(description:, quantity:, unit_price:, amount:, unit: nil, tax_type: nil, remark: nil)
129
+ super
130
+ end
131
+ end
132
+
133
+ # Monetary summary for the whole invoice (integer TWD).
134
+ # total_amount = sales_amount + tax_amount.
135
+ AmountSummary = Data.define(:sales_amount, :tax_amount, :total_amount)
136
+
137
+ # --- Issue (開立) ---
138
+
139
+ # +customs_clearance_mark+ (+"1"+ or +"2"+) and +zero_tax_rate_reason+
140
+ # (+"71"+ to +"79"+) are the MIG's CustomsClearanceMark and
141
+ # ZeroTaxRateReason, for an invoice with a zero-rated line.
142
+ IssueInvoiceInput = Data.define(
143
+ :order_id, :buyer, :items, :amount, :tax_type, :price_mode, :tax_rate, :category,
144
+ :carrier, :donation, :remark, :currency, :exchange_rate, :customs_clearance_mark,
145
+ :zero_tax_rate_reason, :provider_options
146
+ ) do
147
+ def initialize(order_id:, buyer:, items:, tax_type:, price_mode:, amount: nil, tax_rate: nil,
148
+ category: nil, carrier: nil, donation: nil, remark: nil, currency: nil,
149
+ exchange_rate: nil, customs_clearance_mark: nil, zero_tax_rate_reason: nil,
150
+ provider_options: nil)
151
+ super
152
+ end
153
+
154
+ # True when a line is zero rated, by its own tax type or the invoice's.
155
+ def zero_rated?
156
+ items.any? { |item| (item.tax_type || tax_type) == TaxType::ZERO_RATED }
157
+ end
158
+
159
+ # Explicit +category+, else derived from the buyer's 統一編號.
160
+ def resolved_category
161
+ category || (buyer&.business? ? InvoiceCategory::B2B : InvoiceCategory::B2C)
162
+ end
163
+
164
+ # The tax of every line and the MIG totals, computed by the core. See
165
+ # {TaxBreakdown}.
166
+ def tax_breakdown(provider: nil)
167
+ TaxSplit.breakdown(items, tax_type: tax_type, tax_rate: tax_rate, price_mode: price_mode, provider: provider)
168
+ end
169
+ end
170
+
171
+ # --- References ---
172
+
173
+ # The facts about an existing invoice that providers ask for when they void,
174
+ # credit or query it. Results of issue and query return one as #reference, so
175
+ # a caller passes it on and does not have to know what each provider needs.
176
+ # Every field is optional. An adapter that needs a missing field may query the
177
+ # provider for it, or raises ValidationError naming the field.
178
+ #
179
+ # +invoice_datetime+ is a Time in UTC+8. +buyer_ubn+ is nil for a consumer.
180
+ # +tax_type+ and +tax_rate+ are the invoice's, so an allowance can take them
181
+ # from here. +tax_type+ is MIXED when the items differ.
182
+ #
183
+ # A reference is stored between calls, often for days. #to_h is that stored
184
+ # form: JSON-safe values only, with +invoice_datetime+ as an ISO 8601 string in
185
+ # UTC+8. Pass the Hash back as +invoice:+, as it is or after a JSON round
186
+ # trip, and Input reads the same reference.
187
+ InvoiceReference = Data.define(:number, :invoice_datetime, :random_code, :order_id, :total_amount,
188
+ :buyer_ubn, :tax_type, :tax_rate) do
189
+ def initialize(number: nil, invoice_datetime: nil, random_code: nil, order_id: nil,
190
+ total_amount: nil, buyer_ubn: nil, tax_type: nil, tax_rate: nil)
191
+ super
192
+ end
193
+
194
+ def to_h
195
+ super.merge(invoice_datetime: invoice_datetime && Taipei.iso8601(invoice_datetime),
196
+ tax_type: tax_type&.to_s, tax_rate: tax_rate && Number.plain(tax_rate.to_r)).compact
197
+ end
198
+ end
199
+
200
+ # The facts about an existing allowance. +allowance_date+ is a Date. #to_h is
201
+ # the stored form, as for InvoiceReference, with the date as YYYY-MM-DD.
202
+ AllowanceReference = Data.define(:allowance_number, :allowance_date, :invoice_number) do
203
+ def initialize(allowance_number:, allowance_date: nil, invoice_number: nil)
204
+ super
205
+ end
206
+
207
+ def to_h
208
+ super.merge(allowance_date: allowance_date&.strftime("%Y-%m-%d")).compact
209
+ end
210
+ end
211
+
212
+ # --- Issue results ---
213
+
214
+ # +invoice_datetime+ is a Time in UTC+8 (MIG InvoiceDate and InvoiceTime).
215
+ # +buyer_ubn+ is nil for a consumer. +tax_type+ is TaxType.of the input, so
216
+ # MIXED for a mixed-tax invoice, and +tax_rate+ is the input's rate.
217
+ IssueInvoiceResult = Data.define(
218
+ :invoice_number, :invoice_datetime, :random_code, :order_id, :total_amount, :buyer_ubn, :tax_type,
219
+ :tax_rate, :status, :raw
220
+ ) do
221
+ include Redaction::Raw
222
+
223
+ def reference
224
+ InvoiceReference.new(number: invoice_number, invoice_datetime: invoice_datetime,
225
+ random_code: random_code, order_id: order_id,
226
+ total_amount: total_amount, buyer_ubn: buyer_ubn,
227
+ tax_type: tax_type, tax_rate: tax_rate)
228
+ end
229
+ end
230
+
231
+ # --- Void (作廢) ---
232
+
233
+ # +invoice+ is an InvoiceReference (Input also takes a plain number).
234
+ VoidInvoiceInput = Data.define(:invoice, :reason, :provider_options) do
235
+ def initialize(invoice:, reason:, provider_options: nil)
236
+ super
237
+ end
238
+ end
239
+
240
+ # +cancel_datetime+ is a Time in UTC+8 (MIG CancelDate and CancelTime), or nil
241
+ # when the provider does not return it.
242
+ VoidInvoiceResult = Data.define(:invoice_number, :status, :cancel_datetime, :raw) do
243
+ include Redaction::Raw
244
+
245
+ def initialize(invoice_number:, status:, raw:, cancel_datetime: nil)
246
+ super
247
+ end
248
+ end
249
+
250
+ # --- Allowance (折讓) and its cancellation (折讓作廢) ---
251
+
252
+ # +price_mode+ says if the item amounts include tax. +tax_type+ and
253
+ # +tax_rate+ come from the input, else from the invoice reference. With a
254
+ # MIXED tax type, every item carries its own tax_type.
255
+ #
256
+ # +amount+ follows G0401: +sales_amount+ excludes tax. Input checks it
257
+ # against #tax_breakdown, which holds the tax of each line.
258
+ AllowanceInput = Data.define(:invoice, :allowance_id, :items, :amount, :price_mode, :tax_type, :tax_rate,
259
+ :provider_options) do
260
+ def initialize(invoice:, allowance_id:, items:, price_mode:, tax_type:, tax_rate:, amount: nil,
261
+ provider_options: nil)
262
+ super
263
+ end
264
+
265
+ def tax_breakdown(provider: nil)
266
+ TaxSplit.breakdown(items, tax_type: tax_type, tax_rate: tax_rate, price_mode: price_mode, provider: provider)
267
+ end
268
+ end
269
+
270
+ # +allowance_date+ is a Date (MIG AllowanceDate has no time).
271
+ AllowanceResult = Data.define(:allowance_number, :invoice_number, :allowance_date, :total_amount, :raw) do
272
+ include Redaction::Raw
273
+
274
+ def reference
275
+ AllowanceReference.new(allowance_number: allowance_number, allowance_date: allowance_date,
276
+ invoice_number: invoice_number)
277
+ end
278
+ end
279
+
280
+ # +allowance+ is an AllowanceReference (Input also takes a plain number).
281
+ VoidAllowanceInput = Data.define(:allowance, :reason, :provider_options) do
282
+ def initialize(allowance:, reason: nil, provider_options: nil)
283
+ super
284
+ end
285
+ end
286
+
287
+ VoidAllowanceResult = Data.define(:allowance_number, :cancel_datetime, :raw) do
288
+ include Redaction::Raw
289
+
290
+ def initialize(allowance_number:, raw:, cancel_datetime: nil)
291
+ super
292
+ end
293
+ end
294
+
295
+ # --- Query (查詢) ---
296
+
297
+ # +invoice+ is an InvoiceReference with a number or an order id. A reference
298
+ # with only an order id (and what the provider needs with it) finds an invoice
299
+ # without its number. That needs QUERY_BY_ORDER_ID.
300
+ QueryInvoiceInput = Data.define(:invoice, :provider_options) do
301
+ def initialize(invoice:, provider_options: nil)
302
+ super
303
+ end
304
+ end
305
+
306
+ # +status+ is nil when the provider's status has no InvoiceStatus, for
307
+ # example a cancelled (註銷) invoice.
308
+ #
309
+ # +tax_type+ and +tax_rate+ are nil when the provider does not say.
310
+ QueryInvoiceResult = Data.define(
311
+ :invoice_number, :invoice_datetime, :random_code, :order_id, :status, :amount, :buyer, :items, :tax_type,
312
+ :tax_rate, :raw
313
+ ) do
314
+ include Redaction::Raw
315
+
316
+ def reference
317
+ InvoiceReference.new(number: invoice_number, invoice_datetime: invoice_datetime,
318
+ random_code: random_code, order_id: order_id,
319
+ total_amount: amount&.total_amount, buyer_ubn: buyer&.ubn,
320
+ tax_type: tax_type, tax_rate: tax_rate)
321
+ end
322
+ end
323
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Einvoice
2
- VERSION = "1.4.0"
4
+ VERSION = "2.0.0.alpha2"
3
5
  end
data/lib/einvoice.rb CHANGED
@@ -1,15 +1,26 @@
1
- require "active_support"
1
+ # frozen_string_literal: true
2
2
 
3
- require "einvoice/client"
4
- require "einvoice/configuration"
5
- require "einvoice/connection"
6
- require "einvoice/provider"
7
- require "einvoice/result"
8
- require "einvoice/utils"
9
3
  require "einvoice/version"
4
+ require "einvoice/error"
5
+ require "einvoice/redaction"
6
+ require "einvoice/taipei"
7
+ require "einvoice/number"
8
+ require "einvoice/tax_split"
9
+ require "einvoice/capability"
10
+ require "einvoice/types"
11
+ require "einvoice/input"
12
+ require "einvoice/provider"
13
+ require "einvoice/mock_provider"
10
14
 
11
- require "einvoice/tradevan/provider"
12
-
15
+ # Provider-agnostic Taiwan e-invoice SDK (財政部 MIG 4.1).
16
+ #
17
+ # The core models the five operations once: issue (開立), void (作廢),
18
+ # allowance (折讓), void-allowance (折讓作廢) and query (查詢). It has a unified
19
+ # value model and an {Einvoice::Provider} contract. Each value-added center is a
20
+ # thin adapter that maps the unified model to its wire format and back.
21
+ #
22
+ # This file loads the core only. The adapters and the MOF clients load on
23
+ # demand, so an app loads only what it uses: require "einvoice/ecpay",
24
+ # "einvoice/amego", "einvoice/ezpay", "einvoice/tradevan" or "einvoice/mof".
13
25
  module Einvoice
14
- extend Configuration
15
26
  end