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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.coderabbit.yaml +6 -8
  3. data/.github/workflows/build.yml +6 -3
  4. data/.github/workflows/cron.yml +4 -2
  5. data/CHANGELOG.md +63 -44
  6. data/Gemfile +3 -10
  7. data/README.md +284 -325
  8. data/README.zh-TW.md +381 -0
  9. data/Rakefile +2 -51
  10. data/bin/console +3 -6
  11. data/einvoice.gemspec +17 -18
  12. data/lib/einvoice/amego/client.rb +116 -0
  13. data/lib/einvoice/amego/errors.rb +84 -0
  14. data/lib/einvoice/amego/payload.rb +270 -0
  15. data/lib/einvoice/amego/provider.rb +272 -0
  16. data/lib/einvoice/amego.rb +27 -0
  17. data/lib/einvoice/capability.rb +79 -0
  18. data/lib/einvoice/ecpay/client.rb +184 -0
  19. data/lib/einvoice/ecpay/crypto.rb +85 -0
  20. data/lib/einvoice/ecpay/errors.rb +115 -0
  21. data/lib/einvoice/ecpay/payload.rb +241 -0
  22. data/lib/einvoice/ecpay/provider.rb +371 -0
  23. data/lib/einvoice/ecpay.rb +31 -0
  24. data/lib/einvoice/error.rb +220 -0
  25. data/lib/einvoice/ezpay/client.rb +130 -0
  26. data/lib/einvoice/ezpay/crypto.rb +85 -0
  27. data/lib/einvoice/ezpay/errors.rb +72 -0
  28. data/lib/einvoice/ezpay/payload.rb +269 -0
  29. data/lib/einvoice/ezpay/provider.rb +253 -0
  30. data/lib/einvoice/ezpay.rb +19 -0
  31. data/lib/einvoice/input.rb +547 -0
  32. data/lib/einvoice/mock_provider.rb +191 -0
  33. data/lib/einvoice/mof/donation_codes.rb +157 -0
  34. data/lib/einvoice/mof.rb +16 -0
  35. data/lib/einvoice/number.rb +91 -0
  36. data/lib/einvoice/provider.rb +57 -13
  37. data/lib/einvoice/redaction.rb +54 -0
  38. data/lib/einvoice/taipei.rb +89 -0
  39. data/lib/einvoice/tax_split.rb +112 -0
  40. data/lib/einvoice/tradevan/default_api/client.rb +175 -0
  41. data/lib/einvoice/tradevan/default_api/crypto.rb +90 -0
  42. data/lib/einvoice/tradevan/default_api/errors.rb +54 -0
  43. data/lib/einvoice/tradevan/default_api/payload.rb +259 -0
  44. data/lib/einvoice/tradevan/default_api/provider.rb +353 -0
  45. data/lib/einvoice/tradevan/default_api/rules.rb +314 -0
  46. data/lib/einvoice/tradevan.rb +25 -0
  47. data/lib/einvoice/types.rb +365 -0
  48. data/lib/einvoice/version.rb +3 -1
  49. data/lib/einvoice.rb +21 -9
  50. metadata +52 -144
  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 -2025
  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 -139
  63. data/lib/einvoice/tradevan/result.rb +0 -25
  64. data/lib/einvoice/tradevan/validator/issue_data_validator.rb +0 -80
  65. data/lib/faraday/response/decode_tradevan.rb +0 -37
  66. data/lib/faraday/response/parse_xml.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ce2293f378b646e5ae9ab94d5cce8b539a7dd441d8db03472ae3c6f4bbd9735
4
- data.tar.gz: 0d8e775b71d10cb37f3892cc40bd8e46e1ce872721ec7cb1c0864807d6a5f24d
3
+ metadata.gz: 7032cdc2be7baa52d3da13599eefc949a1b1ab31554197a6e6a4c248fd125a90
4
+ data.tar.gz: 0a01df51176d85cd882b973fbf6e4bb2d902bd5dce6cdc43b737a827437c1792
5
5
  SHA512:
6
- metadata.gz: 233c9f4194869b5e61420c7b255f768befe0f09cb3f952f1c300c7d9b8e417d98053461f1b794cf5f7a9a7c4b4a428b38b99db0005e77429d9686bf3afad1bda
7
- data.tar.gz: 11f5ba7e0132eb0dc9eb4d0fc16cd48252b950b3c189f2c45efa4757bfbc43936c3c97155692d2e3b989bf038e528e20f5ee023031817fed9ab0a3a7bbb564d7
6
+ metadata.gz: f298e3f764fc1d5f38275ca03ac5f8566ca87363916c1e9419282c54286e840c4f47ce95c2a8b2204f0221ea108f3f0218453ff786ef258dbaabe0115027fbe6
7
+ data.tar.gz: e461e04956867a510f46d3a5381ddfe1ec19d346350fbb920a40ea39d739e4965328cd3b71da8b25c6a1d4c00c48b33f6b343f13abadd689500e9360709eaba5
data/.coderabbit.yaml CHANGED
@@ -4,11 +4,9 @@ reviews:
4
4
  auto_review:
5
5
  enabled: true
6
6
  drafts: false
7
- path_filters:
8
- - "!lib/einvoice/donation_unit_list.json"
9
- path_instructions:
10
- - path: "lib/einvoice/tradevan/**"
11
- instructions: >
12
- This is legacy 1.x code kept for backward compatibility.
13
- Do not suggest large-scale refactors or architecture changes here.
14
- Focus only on correctness bugs and security issues.
7
+ base_branches:
8
+ - "2\\.0\\.0-alpha"
9
+ # This repository comments where code needs explaining, not on every method.
10
+ pre_merge_checks:
11
+ docstrings:
12
+ mode: "off"
@@ -2,9 +2,12 @@ name: build
2
2
 
3
3
  on:
4
4
  push:
5
- branches: [ 1-x-stable ]
5
+ branches: [ main, 2.0.0-alpha ]
6
6
  pull_request:
7
- branches: [ 1-x-stable ]
7
+ # Branch filters match the *base* of a pull request. Without 2.0.0-alpha here
8
+ # the 2.0 integration branch and everything stacked onto it would merge with
9
+ # no CI at all.
10
+ branches: [ main, 2.0.0-alpha ]
8
11
 
9
12
  permissions:
10
13
  contents: read
@@ -13,7 +16,7 @@ jobs:
13
16
  test:
14
17
  strategy:
15
18
  matrix:
16
- ruby-version: ['3.1', '3.2', '3.3', '3.4']
19
+ ruby-version: ['3.3', '3.4', '4.0']
17
20
  runs-on: ubuntu-latest
18
21
  steps:
19
22
  - uses: actions/checkout@v4
@@ -1,7 +1,7 @@
1
- name: Daily update
1
+ name: Update donation unit list
2
2
  on:
3
3
  schedule:
4
- - cron: 0 0 * * *
4
+ - cron: 0 0 * * 0
5
5
  workflow_dispatch:
6
6
 
7
7
  permissions:
@@ -12,6 +12,8 @@ jobs:
12
12
  runs-on: ubuntu-latest
13
13
  steps:
14
14
  - uses: actions/checkout@v4
15
+ with:
16
+ ref: 1-x-stable
15
17
  - uses: ruby/setup-ruby@v1
16
18
  with:
17
19
  ruby-version: '3.3'
data/CHANGELOG.md CHANGED
@@ -2,24 +2,66 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/).
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [2.0.0.beta1] - 2026-12-02
10
+
11
+ 2.0 moves to `main`. The API may still change before 2.0.0. From 2.0.0 on, the gem follows Semantic Versioning, with the limits that the README lists.
12
+
13
+ ### Changed
14
+ - **Ruby 3.3 or newer is required.** Ruby 3.2 reached its end of life in March 2026. 2.0.0.alpha2 still runs on 3.2.
15
+ - **An allowance item amount has to be whole TWD.** MIG G0401 wants each item amount without tax as an integer, and the line tax is an integer too. The core used to take 7 decimal places here, as on an invoice, and split 10.5 into 9.5 and a tax of 1. Quantity and unit price may still have decimals.
16
+ - **A taxable line takes the general rate 0.05 only.** The law sets the general rate at 5% to 10% (營業稅法第 10 條), and 0.05 is the only MIG rate in that range. The other MIG rates are for special tax. Every adapter refused another rate already, but the core and `MockProvider` took it.
17
+ - **A zero-rated invoice needs `zero_tax_rate_reason`.** The MIG requires it with `customs_clearance_mark` (F0401). The core did not ask for it, because ECPay can take it from its back office. The reason goes to the MOF in the end, so the caller now gives it at every provider.
18
+ - **A bad number in a provider's response raises `ProviderError`.** The adapters read the numbers in a response with the parser that reads the input. Before, a value such as `"abc"` could read as `0`, and a total of `99.6` as `99`, with no error. A missing item number now reads as `nil`, not `0`. ECPay and Amego need every total, because their live responses always have them. ezPay and Tradevan read a missing total as `0`, because no live response shows them. An ezPay query also checks that its totals add up.
19
+ - **An Amego query reads the tax type from `tax_type`.** The adapter worked the type out from the items, and read it as `nil` when an item had no type. An item without a type now takes the invoice's type, unless the invoice is `MIXED`.
20
+ - **A queried ezPay item takes the invoice's tax type.** ezPay sends `ItemTaxType` on a mixed-tax invoice only, so the other items read back with no tax type. They now take the invoice's `TaxType`.
21
+
22
+ ### Removed
23
+ - **`TaxBreakdown::Line#net_unit_price`.** No adapter used it. The adapters that need a price without tax (Amego allowances, ezPay) take the line amount over the quantity instead, because the unit price can miss the amount by the rounding of the line tax.
24
+ - **The `provider:` keyword of `#tax_breakdown`.** The core checks the input before it computes the breakdown, so the breakdown raises no error and has no use for a provider name. Call `input.tax_breakdown` with no arguments.
25
+
26
+ ### Fixed
27
+ - **An ECPay query reads the tax type from `IIS_Tax_Type`.** The adapter worked the type out from the items instead. An item without `ItemTaxType` then made a taxable invoice read as `MIXED`, or with no tax type at all. An item without a type now takes the invoice's type, unless the invoice is `MIXED`.
28
+
29
+ ## [2.0.0.alpha2] - 2026-11-20
30
+
31
+ 2.0 is a rewrite. 1.x was a client for one provider, Tradevan. 2.0 has one interface for several providers, so application code does not depend on one provider. **2.0 is not backward compatible.** 1.x stays in maintenance on `1-x-stable`. Pin `~> 1.4` if you depend on it. To move to 2.0, read [Upgrading from 1.x to 2.0](docs/upgrading-to-2.0.md).
32
+
33
+ ### Added
34
+ - **One interface for every provider.** `Einvoice::Provider` has the five operations: `issue`, `void`, `allowance`, `void_allowance` and `query`. They take plain hashes or value objects, and return value types (`Data.define`).
35
+ - **Load only the provider you use.** `require "einvoice"` loads the core. Each adapter loads with its own `require`, such as `require "einvoice/ecpay"`, and so do the 財政部 lookups (`require "einvoice/mof"`). 2.0 has no runtime dependencies.
36
+ - **Checks before the call.** Input is checked locally, and a bad request raises `Einvoice::ValidationError` before anything goes over the network. The core checks the MIG rules that hold at every provider, so every provider answers them alike: for example the 統一編號, the 愛心碼 (3 to 7 digits), carrier codes, the MIG tax rates and the MIG field lengths. The README lists them.
37
+ - **The core computes tax.** For an issue and an allowance, the core computes the tax of every line and the MIG totals from the items, `price_mode`, `tax_type` and `tax_rate`, as `#tax_breakdown`. Adapters do not compute tax, so the same input gets the same tax at every provider. `amount` is optional: without it, the core fills it in. A given `amount` is checked against the breakdown, by F0401 for an invoice and G0401 for an allowance.
38
+ - **References.** Issue, query and allowance results return a `#reference` that holds what a provider asks for later. Pass it to `void`, `allowance` and `query`, and you do not need to know each provider's needs. `reference.to_h` is JSON-safe, so you can store it with your order. A time without an offset is refused when it is read back. An allowance takes its tax type and rate from the reference, with no default, so a zero rated invoice is not credited with tax. An allowance item may not be negative (MIG 4.1). An invoice item may be, as a discount line.
39
+ - **MIG names and types.** Dates are `invoice_datetime` (a `Time` in UTC+8), `allowance_date` (a `Date`) and `cancel_datetime`. A zero rated invoice takes `customs_clearance_mark` and `zero_tax_rate_reason`.
40
+ - **Errors you can act on.** Every failure is an `Einvoice::Error` with a stable `#code`. `#group` says which side failed, the invoice or the provider, and `#reason` says what to do next. For example, `:already_voided` on a void is safe to treat as done, but `:allowance_blocked_by_void` means that no credit was made. A `ValidationError` means that no provider takes the input. An `UnsupportedError` means that this provider does not take it, but another one may. A `NetworkError` can be retried. `#raw_code` and `#raw_message` keep the provider's own answer.
41
+ - **Capabilities.** Each provider declares the optional features it has, such as B2B, mixed tax, query by order id and carrier validation (`#supports?`, `#validate_carrier`). A missing feature raises `UnsupportedError`. `#query_statuses` lists the statuses that `query` can return, because some providers cannot tell every state.
42
+ - **`Einvoice::MockProvider`**, an in-memory provider for testing your application. It follows the same contract and the same invoice states as a real adapter.
43
+ - **ECPay (綠界)**, the first adapter. Its live spec runs against ECPay's public stage, and its specs replay responses recorded there.
44
+ - **Amego (光貿).** Its live spec runs against Amego's public test account.
45
+ - **ezPay (簡單行動支付), experimental.** Built from ezPay's manuals, and checked against their samples. Nobody has run it against ezPay, so test it against your own test shop first.
46
+ - **Tradevan (關貿) DEFAULTAPI, experimental.** Ported from the 1.x client. Nobody has run it against the live API, so test it against your own account first.
47
+ - **`Einvoice::MOF::DonationCodes`**, which looks up a 愛心碼 in 財政部's open data, in place of the list that 1.x shipped.
48
+ - **Data kept out of logs.** `inspect`, `to_s` and `pp` show a result's raw response, a buyer's personal data and a client's credentials as `[FILTERED]`. `base_url` must use https (localhost may use http), and must not carry a user, a password, a query or a fragment.
49
+
50
+ Each provider's note in `docs/providers/` gives its mapping, its limits and what was verified, and how sure we are of each.
51
+
52
+ ### Removed
53
+ - The 1.x Tradevan layer (`Client`, `Configuration`, `Connection`, `Result` and the Tradevan models). The unified core and the adapters replace it. Tradevan DEFAULTAPI is back as an adapter.
54
+ - `lib/einvoice/donation_unit_list.json` and the scheduled workflow that refreshed it. `Einvoice::MOF::DonationCodes` looks the codes up live.
7
55
 
8
56
  ## [1.5.0] - 2026-09-11
9
57
 
10
58
  ### Fixed
11
- - Tradevan `IssueData#payload` and `VoidData#payload` now exclude
12
- `context_for_validation`. On Rails 8 this internal ivar was sent as a
13
- business field once `valid?` had run, and Tradevan rejected the whole
14
- request. Thanks @dlackty.
59
+ - Tradevan `IssueData#payload` and `VoidData#payload` now exclude `context_for_validation`. On Rails 8 this internal ivar was sent as a business field once `valid?` had run, and Tradevan rejected the whole request. Thanks @dlackty.
15
60
 
16
61
  ### Changed
17
62
  - **Ruby 3.1 or newer is now required.** CI covers 3.1 through 3.4.
18
- - **Faraday 2 is now required** (`~> 2.1`); `faraday_middleware` is gone,
19
- replaced by a built-in `Faraday::Response::ParseXml`. If you pin
20
- `faraday` to 1.x, stay on 1.4.0.
21
- - `Einvoice::Utils` and the `gyoku` dependency have been removed. Nothing
22
- in the gem called them.
63
+ - **Faraday 2 is now required** (`~> 2.1`); `faraday_middleware` is gone, replaced by a built-in `Faraday::Response::ParseXml`. If you pin `faraday` to 1.x, stay on 1.4.0.
64
+ - `Einvoice::Utils` and the `gyoku` dependency have been removed. Nothing in the gem called them.
23
65
 
24
66
  ### Internal
25
67
  - Added request-level Tradevan provider specs.
@@ -29,46 +71,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
29
71
  ## [1.4.0] - 2026-08-08
30
72
 
31
73
  ### Changed
32
- - **TLS certificate verification is now enabled by default.** Previously every
33
- request was sent with `ssl: { verify: false }`, regardless of configuration.
34
- If you were relying on that behavior (e.g. against an endpoint with an
35
- incomplete certificate chain), set `config.ssl_verify = false` or pass
36
- `ssl_verify: false` to `Provider.new` to restore the old behavior.
37
- - `Client` now delegates to the provider via `public_send` instead of `send`,
38
- and implements `respond_to_missing?`. Private provider methods (e.g.
39
- `encrypt`, `connection`) are no longer reachable through `Client`.
74
+ - **TLS certificate verification is now enabled by default.** Previously every request was sent with `ssl: { verify: false }`, regardless of configuration. If you were relying on that behavior (e.g. against an endpoint with an incomplete certificate chain), set `config.ssl_verify = false` or pass `ssl_verify: false` to `Provider.new` to restore the old behavior.
75
+ - `Client` now delegates to the provider via `public_send` instead of `send`, and implements `respond_to_missing?`. Private provider methods (e.g. `encrypt`, `connection`) are no longer reachable through `Client`.
40
76
 
41
77
  ### Fixed
42
- - `TotalValidator` comparison was inverted and compared a `String` against an
43
- `Integer`, so it never actually validated anything. It now correctly fails
44
- when `total` doesn't match the sum of item totals.
45
- - `AllowanceNumberValidator` and `IssueItem#invoiceTime`'s format regexp had
46
- broken escape sequences (`\A` and `\d` collapsing to literal characters in a
47
- double-quoted string), causing them to reject all valid input.
48
- - `ItemListValidator` pushed directly onto `record.errors[:itemList]`, which
49
- is deprecated in Rails 6.1 and removed in 7.0. It now uses `errors.add` and
50
- actually registers errors again.
51
- - Tradevan response decoding used `JSON.load`, which is unsafe for untrusted
52
- input. Switched to `JSON.parse`.
53
- - `lib/einvoice/donation_unit_list.json` was stale since 2023-12 (the
54
- scheduled update workflow had been silently broken), causing
55
- `DonationUnitValidator` to reject currently valid donation codes and accept
56
- retired ones. The list has been refreshed (~340 entries changed).
78
+ - `TotalValidator` comparison was inverted and compared a `String` against an `Integer`, so it never actually validated anything. It now correctly fails when `total` doesn't match the sum of item totals.
79
+ - `AllowanceNumberValidator` and `IssueItem#invoiceTime`'s format regexp had broken escape sequences (`\A` and `\d` collapsing to literal characters in a double-quoted string), causing them to reject all valid input.
80
+ - `ItemListValidator` pushed directly onto `record.errors[:itemList]`, which is deprecated in Rails 6.1 and removed in 7.0. It now uses `errors.add` and actually registers errors again.
81
+ - Tradevan response decoding used `JSON.load`, which is unsafe for untrusted input. Switched to `JSON.parse`.
82
+ - `lib/einvoice/donation_unit_list.json` was stale since 2023-12 (the scheduled update workflow had been silently broken), causing `DonationUnitValidator` to reject currently valid donation codes and accept retired ones. The list has been refreshed (~340 entries changed).
57
83
 
58
84
  ### Added
59
- - Specs for `Client` delegation, `Configuration#ssl_verify`,
60
- `Provider#connection` SSL behavior, the validator fixes above, and the
61
- previously-untested `decode_tradevan` middleware.
85
+ - Specs for `Client` delegation, `Configuration#ssl_verify`, `Provider#connection` SSL behavior, the validator fixes above, and the previously-untested `decode_tradevan` middleware.
62
86
 
63
87
  ### Internal
64
88
  - CI coverage reporting migrated from Code Climate (discontinued) to Codecov.
65
- - The scheduled `cron.yml` workflow that refreshes the donation unit list
66
- has been repaired — it depended on two archived GitHub Actions
67
- (`actions/setup-ruby`, `mikeal/publish-to-github-action`) and hadn't run
68
- successfully since 2023-12.
89
+ - The scheduled `cron.yml` workflow that refreshes the donation unit list has been repaired — it depended on two archived GitHub Actions (`actions/setup-ruby`, `mikeal/publish-to-github-action`) and hadn't run successfully since 2023-12.
69
90
 
70
91
  ---
71
92
 
72
- Versions prior to 1.4.0 were not tracked in this file. See the
73
- [release history](https://github.com/abookyun/einvoice/releases) and
74
- [commit log](https://github.com/abookyun/einvoice/commits/main/) for earlier changes.
93
+ Versions prior to 1.4.0 were not tracked in this file. See the [release history](https://github.com/abookyun/einvoice/releases) and [commit log](https://github.com/abookyun/einvoice/commits/main/) for earlier changes.
data/Gemfile CHANGED
@@ -1,16 +1,9 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in einvoice.gemspec
4
6
  gemspec
5
7
 
6
- # Remove this pin once we drop Ruby 3.1 support. json 3.0 dropped quirks_mode,
7
- # which activesupport 7.2.3.2 still passes; 7.2.3.2 is the newest activesupport
8
- # that works on Ruby 3.1.
9
- gem "json", "< 3.0"
10
-
11
- # Used by the update_donation_unit rake task; csv leaves the default gems in Ruby 3.4.
12
- gem "csv", require: false, group: :development
13
-
14
- gem "ostruct", require: false, group: :development
15
8
  gem "simplecov", require: false, group: :test
16
9
  gem "simplecov-lcov", require: false, group: :test