einvoice 2.0.0.alpha2 → 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/.github/workflows/build.yml +1 -1
- data/.github/workflows/cron.yml +33 -0
- data/CHANGELOG.md +20 -0
- data/README.md +150 -84
- data/README.zh-TW.md +140 -81
- data/einvoice.gemspec +3 -2
- data/lib/einvoice/amego/payload.rb +4 -10
- data/lib/einvoice/amego/provider.rb +17 -12
- data/lib/einvoice/ecpay/client.rb +4 -3
- data/lib/einvoice/ecpay/payload.rb +9 -9
- data/lib/einvoice/ecpay/provider.rb +26 -17
- data/lib/einvoice/ezpay/payload.rb +3 -3
- data/lib/einvoice/ezpay/provider.rb +31 -13
- data/lib/einvoice/input.rb +66 -13
- data/lib/einvoice/mock_provider.rb +1 -1
- data/lib/einvoice/number.rb +40 -0
- data/lib/einvoice/tax_split.rb +27 -39
- data/lib/einvoice/tradevan/default_api/payload.rb +3 -3
- data/lib/einvoice/tradevan/default_api/provider.rb +11 -24
- data/lib/einvoice/types.rb +48 -6
- data/lib/einvoice/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7032cdc2be7baa52d3da13599eefc949a1b1ab31554197a6e6a4c248fd125a90
|
|
4
|
+
data.tar.gz: 0a01df51176d85cd882b973fbf6e4bb2d902bd5dce6cdc43b737a827437c1792
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f298e3f764fc1d5f38275ca03ac5f8566ca87363916c1e9419282c54286e840c4f47ce95c2a8b2204f0221ea108f3f0218453ff786ef258dbaabe0115027fbe6
|
|
7
|
+
data.tar.gz: e461e04956867a510f46d3a5381ddfe1ec19d346350fbb920a40ea39d739e4965328cd3b71da8b25c6a1d4c00c48b33f6b343f13abadd689500e9360709eaba5
|
data/.github/workflows/build.yml
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: Update donation unit list
|
|
2
|
+
on:
|
|
3
|
+
schedule:
|
|
4
|
+
- cron: 0 0 * * 0
|
|
5
|
+
workflow_dispatch:
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: write
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
with:
|
|
16
|
+
ref: 1-x-stable
|
|
17
|
+
- uses: ruby/setup-ruby@v1
|
|
18
|
+
with:
|
|
19
|
+
ruby-version: '3.3'
|
|
20
|
+
bundler-cache: true
|
|
21
|
+
- name: Update donation unit list
|
|
22
|
+
run: bundle exec rake update_donation_unit
|
|
23
|
+
- name: Commit and push changes
|
|
24
|
+
run: |
|
|
25
|
+
if git diff --quiet; then
|
|
26
|
+
echo "Donation unit list is up to date"
|
|
27
|
+
exit 0
|
|
28
|
+
fi
|
|
29
|
+
git config user.name "github-actions[bot]"
|
|
30
|
+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
|
31
|
+
git add lib/einvoice/donation_unit_list.json
|
|
32
|
+
git commit -m "Automated donation unit list update"
|
|
33
|
+
git push
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,26 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
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
|
+
|
|
9
29
|
## [2.0.0.alpha2] - 2026-11-20
|
|
10
30
|
|
|
11
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).
|
data/README.md
CHANGED
|
@@ -4,40 +4,49 @@ English | [繁體中文](README.zh-TW.md)
|
|
|
4
4
|
|
|
5
5
|
[](https://github.com/abookyun/einvoice/actions/workflows/build.yml) [](https://codecov.io/github/abookyun/einvoice)
|
|
6
6
|
|
|
7
|
-
A
|
|
7
|
+
A **Taiwan e-invoice SDK** that works with several providers (加值中心). Every provider follows the same 財政部 standard, MIG 4.1. Only the wire format differs: field names, signing and the envelope. So this gem defines the five operations once: issue (開立), void (作廢), allowance (折讓), void allowance (折讓作廢) and query (查詢). Your code calls `Einvoice::Provider` with the unified types, and each provider is an adapter behind it. To move to another provider, you start with a new constructor, not new business code.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
The providers still differ in what they can do. Each adapter says so through its capabilities, `query_statuses` and `UnsupportedError`, so you can check a provider before you switch to it.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
> **2.0 is a rewrite, and 1.x is in maintenance mode.** 1.x was a client for one provider, Tradevan, with a different API. If you depend on it, pin `~> 1.4`. It continues on the [`1-x-stable`](https://github.com/abookyun/einvoice/tree/1-x-stable) branch. 2.0 is not backward compatible. To move to 2.0, read [Upgrading from 1.x to 2.0](docs/upgrading-to-2.0.md). The [changelog](CHANGELOG.md) lists every change.
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
## Providers at a glance
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
| | ECPay 綠界 | Amego 光貿 | ezPay 簡單行動支付 | Tradevan 關貿 DEFAULTAPI |
|
|
16
|
+
| --- | --- | --- | --- | --- |
|
|
17
|
+
| Status | Checked against ECPay's public stage | Checked against Amego's public test account | Experimental: built from ezPay's manuals, not run against ezPay | Experimental: ported from the 1.x client, not run against the live API |
|
|
18
|
+
| B2B (`B2B`) | Yes | Yes | Yes | Yes |
|
|
19
|
+
| Mixed tax (`MIXED_TAX`) | Yes | Yes | B2C only | No |
|
|
20
|
+
| Special tax (特種稅額) | Yes | No | No | No |
|
|
21
|
+
| Query by order id (`QUERY_BY_ORDER_ID`) | Yes | Yes | No | No |
|
|
22
|
+
| Carrier validation (`CARRIER_VALIDATION`) | Mobile barcode only | Mobile barcode only | No | No |
|
|
23
|
+
| Foreign currency (`FOREIGN_CURRENCY`) | No | No | No | No |
|
|
24
|
+
| `query_statuses` | All | All | `:voided` | `:allowance` |
|
|
16
25
|
|
|
17
|
-
|
|
26
|
+
All four have the five operations. A "No" raises `UnsupportedError`. The [provider notes](docs/providers/README.md) compare the wire formats and the test accounts.
|
|
18
27
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
An **ezPay 簡單行動支付** adapter is experimental: it is built from ezPay's public manuals, and has not run against ezPay.
|
|
22
|
-
|
|
23
|
-
A **Tradevan 關貿 DEFAULTAPI** adapter is experimental: it is ported from the 1.x client and has not been run against the live API.
|
|
24
|
-
|
|
25
|
-
Other centers follow. Coverage and completeness for those will vary: their docs are less complete and they have no public sandbox, so an adapter built from docs alone carries some uncertainty until real production use confirms it.
|
|
28
|
+
Other providers may follow. Most have less complete docs and no public test account, so an adapter built from docs alone stays uncertain until it runs in production.
|
|
26
29
|
|
|
27
30
|
## Installation
|
|
28
31
|
|
|
29
|
-
2.0 is in
|
|
32
|
+
2.0 is in beta, and its API may still change before 2.0.0. It needs Ruby 3.3 or newer. Pin the exact version:
|
|
30
33
|
|
|
31
34
|
```ruby
|
|
32
|
-
gem "einvoice", "2.0.0.
|
|
35
|
+
gem "einvoice", "2.0.0.beta1"
|
|
33
36
|
```
|
|
34
37
|
|
|
35
|
-
To follow the latest work, install from
|
|
38
|
+
To follow the latest work, install from `main` instead:
|
|
36
39
|
|
|
37
40
|
```ruby
|
|
38
|
-
gem "einvoice", git: "https://github.com/abookyun/einvoice", branch: "
|
|
41
|
+
gem "einvoice", git: "https://github.com/abookyun/einvoice", branch: "main"
|
|
39
42
|
```
|
|
40
43
|
|
|
44
|
+
From 2.0.0 on, the gem follows [Semantic Versioning](https://semver.org/), with three limits:
|
|
45
|
+
|
|
46
|
+
- The experimental adapters, ezPay and Tradevan, may change in a minor release. Nobody has run them against the provider yet, so the first real run may need a change.
|
|
47
|
+
- `tax_breakdown` may get new fields, or change one, in a minor release. The adapters read it, and a new provider may need more from it.
|
|
48
|
+
- A new check that makes the input follow the MIG is a fix, not a breaking change. An input that breaks the MIG is a wrong filing, even when a provider takes it.
|
|
49
|
+
|
|
41
50
|
`require "einvoice"` loads the core and `MockProvider` only. Require the provider you use, and only that one is loaded:
|
|
42
51
|
|
|
43
52
|
```ruby
|
|
@@ -47,15 +56,17 @@ require "einvoice/mof" # the 財政部 lookups, if you use them
|
|
|
47
56
|
|
|
48
57
|
## Usage
|
|
49
58
|
|
|
50
|
-
|
|
59
|
+
### Issue, query and void
|
|
60
|
+
|
|
61
|
+
Depend on the `Einvoice::Provider` contract, not on one adapter. You build each provider as an instance, and there is no global configuration. So an app with many merchants holds one provider for each set of credentials.
|
|
51
62
|
|
|
52
63
|
```ruby
|
|
53
|
-
#
|
|
64
|
+
# Every adapter is an Einvoice::Provider. MockProvider runs in memory.
|
|
54
65
|
provider = Einvoice::MockProvider.new
|
|
55
66
|
|
|
56
|
-
|
|
67
|
+
issued = provider.issue(
|
|
57
68
|
order_id: "ORDER_1",
|
|
58
|
-
buyer: { email: "buyer@example.com" }, #
|
|
69
|
+
buyer: { email: "buyer@example.com" }, # no ubn, so B2C (二聯式)
|
|
59
70
|
items: [
|
|
60
71
|
{ description: "咖啡拿鐵", quantity: 1, unit_price: 100, amount: 100 }
|
|
61
72
|
],
|
|
@@ -64,39 +75,56 @@ result = provider.issue(
|
|
|
64
75
|
carrier: { type: "MEMBER" }
|
|
65
76
|
)
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
issued.invoice_number # => "MK10000000"
|
|
79
|
+
issued.status # => :issued
|
|
69
80
|
|
|
70
|
-
|
|
71
|
-
|
|
81
|
+
found = provider.query(invoice: issued.reference)
|
|
82
|
+
found.status # => :issued, or nil when the provider cannot tell
|
|
72
83
|
provider.query_statuses # => [:issued, :voided, :allowance]
|
|
73
84
|
|
|
74
|
-
provider.void(invoice:
|
|
85
|
+
provider.void(invoice: issued.reference, reason: "客戶取消")
|
|
75
86
|
```
|
|
76
87
|
|
|
77
|
-
|
|
88
|
+
Inputs take plain hashes, with string or symbol keys. An enum field takes the symbol (`:taxable`) or a wire-style string (`"TAXABLE"`). Before anything goes over the network, the core checks the input against the MIG and turns it into immutable value objects. A bad input raises `Einvoice::ValidationError`. A value object that you build with `.new` gets the same checks. [Input rules](#input-rules) lists them.
|
|
78
89
|
|
|
79
|
-
|
|
90
|
+
### References
|
|
80
91
|
|
|
81
|
-
|
|
92
|
+
`issued.reference` is an `InvoiceReference`. It holds what a provider asks for when you later void the invoice, make an allowance on it, or query it:
|
|
82
93
|
|
|
83
|
-
-
|
|
84
|
-
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
- the number, `invoice_datetime`, `random_code`, `order_id`, `total_amount` and `buyer_ubn`
|
|
95
|
+
- `tax_type` and `tax_rate`, which an allowance takes
|
|
96
|
+
|
|
97
|
+
Store it with your order, and pass it back later:
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
stored = issued.reference.to_h # save this with your order
|
|
101
|
+
provider.void(invoice: stored, reason: "客戶取消")
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- `to_h` holds JSON-safe values only, so it fits a JSON column. `invoice_datetime` is an ISO 8601 string in UTC+8.
|
|
105
|
+
- A stored time has to carry its offset. `"2026-08-01 10:00"` is refused, so that it is not read in the wrong zone. A date alone, such as `"2026-08-01"`, is that day in Taipei.
|
|
106
|
+
- You can also pass the `InvoiceReference` itself.
|
|
107
|
+
- A plain invoice number works too. A provider that needs more raises `ValidationError` and names the field.
|
|
108
|
+
- To find an invoice without its number, query `invoice: { order_id: "ORDER_1" }`. The provider has to declare `QUERY_BY_ORDER_ID`.
|
|
109
|
+
- `void_allowance` takes `allowance:`. Pass the `AllowanceReference` from `AllowanceResult#reference`.
|
|
91
110
|
|
|
92
111
|
### Amounts and tax
|
|
93
112
|
|
|
94
|
-
The core computes the tax from the items
|
|
113
|
+
The core computes the tax from the items:
|
|
114
|
+
|
|
115
|
+
- `price_mode` says if the item amounts include tax.
|
|
116
|
+
- `tax_type` is the tax type of the whole invoice. An item can have its own.
|
|
117
|
+
- `tax_rate` is the rate.
|
|
95
118
|
|
|
96
|
-
|
|
119
|
+
The result is `input.tax_breakdown`: the amount without tax, the tax and the amount with tax of each line, and the MIG totals. Adapters read it, and do not compute tax themselves. The core never changes your items.
|
|
120
|
+
|
|
121
|
+
Items with different tax types make a mixed-tax invoice (混合稅率). You do not ask for it. A provider without `MIXED_TAX` raises `UnsupportedError`.
|
|
122
|
+
|
|
123
|
+
`amount` is optional. Leave it out, and the core fills it in from the breakdown. Give it, and the core checks it against the breakdown, so your totals cannot drift from the items. An invoice follows F0401. For a consumer, the tax stays inside `sales_amount`. For a business buyer, `tax_amount` is `round(taxable sales × tax_rate)`:
|
|
97
124
|
|
|
98
125
|
```ruby
|
|
99
|
-
amount: { sales_amount:
|
|
126
|
+
amount: { sales_amount: 100, tax_amount: 0, total_amount: 100 } # 100 with tax, to a consumer (B2C)
|
|
127
|
+
amount: { sales_amount: 95, tax_amount: 5, total_amount: 100 } # 100 with tax, to a business (B2B)
|
|
100
128
|
```
|
|
101
129
|
|
|
102
130
|
An allowance follows G0401: `sales_amount` excludes tax, for a consumer too. It takes `tax_type` and `tax_rate` from the invoice reference, so pass `issued.reference`, or give them yourself. There is no default, so a zero-rated invoice is never credited with 5% tax. When the invoice has mixed tax, give every item its `tax_type`.
|
|
@@ -111,11 +139,18 @@ provider.allowance(
|
|
|
111
139
|
|
|
112
140
|
### Errors
|
|
113
141
|
|
|
114
|
-
Every failure is
|
|
142
|
+
Every failure is a subclass of `Einvoice::Error`. It carries:
|
|
143
|
+
|
|
144
|
+
- a stable `#code`
|
|
145
|
+
- the provider's own `#raw_code` and `#raw_message`
|
|
146
|
+
- an optional `#reason`, which says what to do next
|
|
147
|
+
- `#group`, which says which side failed: `:invoice` (the request breaks a 財政部 rule, or the invoice is not in the state it needs) or `:provider` (the account, the connection, or the provider's own service)
|
|
148
|
+
|
|
149
|
+
Rescue one class, or all of them:
|
|
115
150
|
|
|
116
151
|
```ruby
|
|
117
152
|
begin
|
|
118
|
-
provider.void(invoice:
|
|
153
|
+
provider.void(invoice: issued.reference, reason: "customer cancelled")
|
|
119
154
|
rescue Einvoice::ConflictError => e
|
|
120
155
|
# Only :already_voided means that the invoice is voided. Another reason,
|
|
121
156
|
# such as :void_blocked_by_allowance, means that it is not.
|
|
@@ -129,21 +164,31 @@ end
|
|
|
129
164
|
The class tells you what to do next, the same way at every provider:
|
|
130
165
|
|
|
131
166
|
- `ValidationError`: the input breaks a MIG rule, so no provider takes it. Fix the input.
|
|
132
|
-
- `UnsupportedError`: this provider does not take the input, or
|
|
133
|
-
- `NetworkError`: the request may not have reached the provider.
|
|
134
|
-
- After a timeout on `allowance`, a retry can credit twice, so check first. Amego takes your `allowance_id` and refuses a second one with `reason: :duplicate_allowance`. ECPay and ezPay assign the allowance number themselves, so the adapter cannot find an allowance that went through. Check the provider's back office before you retry. Tradevan takes `allowance_id` too, but nobody has checked whether it refuses a repeat.
|
|
135
|
-
- After a timeout on `void` or `void_allowance`, a retry is safe, because nothing can be voided twice. A `ConflictError` with `reason: :already_voided` means that the first one went through.
|
|
167
|
+
- `UnsupportedError`: this provider does not take the input, or does not have the feature. Another provider may take it.
|
|
168
|
+
- `NetworkError`: the request may not have reached the provider. You can retry it. See the table below.
|
|
136
169
|
- `reason: :rate_limited`, on any class: the provider asks you to slow down. Retry later.
|
|
137
170
|
- `ConflictError` with `reason: :retry_later`: the provider is still working on the invoice. Try again later.
|
|
138
171
|
|
|
172
|
+
After a timeout, the first request may have gone through:
|
|
173
|
+
|
|
174
|
+
| Operation | Retry? | How you know the first one went through |
|
|
175
|
+
| --- | --- | --- |
|
|
176
|
+
| `issue` | Yes, with the same `order_id` | `ConflictError` with `reason: :duplicate_order` |
|
|
177
|
+
| `void`, `void_allowance` | Yes. Nothing can be voided twice | `ConflictError` with `reason: :already_voided` |
|
|
178
|
+
| `allowance` at Amego | Yes, with the same `allowance_id` | Amego refuses a second one with `reason: :duplicate_allowance` |
|
|
179
|
+
| `allowance` at ECPay or ezPay | Check first | The provider assigns the allowance number, so the adapter cannot find it. Check the provider's back office |
|
|
180
|
+
| `allowance` at Tradevan | Check first | Tradevan takes `allowance_id`, but nobody has checked whether it refuses a repeat |
|
|
181
|
+
|
|
139
182
|
### Credentials and buyer data
|
|
140
183
|
|
|
141
|
-
- `inspect`, `to_s` and `pp` show
|
|
184
|
+
- `inspect`, `to_s` and `pp` show a result's `raw` as `[FILTERED]`, because `raw` is the provider's response and holds the buyer's data. Read `#raw` directly when you need it.
|
|
185
|
+
- A `Buyer` shows its name, email, address and phone as `[FILTERED]` too, in every input and result. Its `ubn` stays.
|
|
186
|
+
- An adapter's client shows its keys as `[FILTERED]`.
|
|
142
187
|
- `base_url` has to use https. Only localhost may use http, for tests. It must not carry a user, a password, a query or a fragment.
|
|
143
188
|
|
|
144
189
|
### Capabilities
|
|
145
190
|
|
|
146
|
-
A provider declares what it supports
|
|
191
|
+
A provider declares what it supports. Check a feature first, so that you do not find a gap only when a request fails.
|
|
147
192
|
|
|
148
193
|
```ruby
|
|
149
194
|
provider.supports?(Einvoice::Capability::FOREIGN_CURRENCY) # => true / false
|
|
@@ -151,7 +196,11 @@ provider.assert_supports!(Einvoice::Capability::B2B) # raises Unsupported
|
|
|
151
196
|
provider.query_statuses # => the statuses that query can return
|
|
152
197
|
```
|
|
153
198
|
|
|
154
|
-
|
|
199
|
+
[Providers at a glance](#providers-at-a-glance) shows what each provider declares.
|
|
200
|
+
|
|
201
|
+
## Provider notes
|
|
202
|
+
|
|
203
|
+
Everything above works the same at every provider. `issue`, `void`, `allowance`, `void_allowance` and `query` take the same unified input and return the same value types. Each section below lists only what differs.
|
|
155
204
|
|
|
156
205
|
### ECPay 綠界
|
|
157
206
|
|
|
@@ -166,24 +215,18 @@ provider = Einvoice::ECPay::Provider.new(
|
|
|
166
215
|
)
|
|
167
216
|
```
|
|
168
217
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
To try it without an account, use ECPay's published sandbox credentials (shared with every other developer, never for anything real):
|
|
218
|
+
To try it without an account, use ECPay's published test credentials. Every developer shares them, so never use them for anything real:
|
|
172
219
|
|
|
173
220
|
```ruby
|
|
174
221
|
provider = Einvoice::ECPay::Provider.new(**Einvoice::ECPay::SANDBOX)
|
|
175
222
|
```
|
|
176
223
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
- **Paper vs. electronic is derived.** An invoice with a carrier or a donation is electronic (`Print=0`); anything else prints, and ECPay then requires a buyer name, an address, and an email or a phone.
|
|
180
|
-
- **Mixed tax rates are derived too.** Items that disagree on `tax_type` produce a 混合稅率 (TaxType 9) invoice; you don't ask for it explicitly.
|
|
224
|
+
- **The adapter decides paper or electronic.** An invoice with a carrier or a donation is electronic (`Print=0`). Anything else prints, and ECPay then requires a buyer name, an address, and an email or a phone.
|
|
181
225
|
- **Void, allowance and a query by number need the invoice's issue date.** It comes from `invoice.invoice_datetime`. If the reference has an `order_id` instead, a query goes by order id, and a void or an allowance looks the date up first. A failed lookup raises `ProviderError` with `reason: :lookup_failed`. A plain number is not enough.
|
|
182
226
|
- **Void allowance needs the invoice number**, so pass the `AllowanceReference`, not only the allowance number.
|
|
183
|
-
- **
|
|
184
|
-
- **
|
|
185
|
-
-
|
|
186
|
-
- **Anything else**: ECPay has ~25 further B2C endpoints (延遲開立, 字軌設定, 列印, 通知 …). Reach them through `provider.raw(path, payload)`, which applies the envelope, encryption and error mapping for you.
|
|
227
|
+
- **Carrier validation**: `validate_carrier({ type: "MOBILE_BARCODE", code: "/ABC1234" })` asks ECPay whether a mobile barcode is registered. `validate_love_code("168001")` and `love_code_organ_name("168001")` check a 愛心碼. These ask ECPay, so they need this adapter and its credentials. `Einvoice::MOF::DonationCodes` ([below](#財政部-lookups)) checks a 愛心碼 at 財政部 and needs neither.
|
|
228
|
+
- **Anything else**: ECPay has about 25 more B2C endpoints, such as 延遲開立, 字軌設定, 列印 and 通知. Reach them through `provider.raw(path, payload)`. It applies the envelope, the encryption and the error mapping for you.
|
|
229
|
+
- The details and the open questions are in [`docs/providers/ecpay/b2c.md`](docs/providers/ecpay/b2c.md).
|
|
187
230
|
|
|
188
231
|
### Amego 光貿
|
|
189
232
|
|
|
@@ -196,15 +239,19 @@ provider = Einvoice::Amego::Provider.new(
|
|
|
196
239
|
)
|
|
197
240
|
```
|
|
198
241
|
|
|
199
|
-
To try it without an account, use Amego's published test account
|
|
242
|
+
To try it without an account, use Amego's published test account. Every developer shares it, so never use it for anything real:
|
|
243
|
+
|
|
244
|
+
```ruby
|
|
245
|
+
provider = Einvoice::Amego::Provider.new(**Einvoice::Amego::SANDBOX)
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Amego has one host for test and production. The account decides which one you use.
|
|
200
249
|
|
|
201
250
|
- **Amego picks the invoice number** (自動配號).
|
|
202
251
|
- **A void is queued.** Amego accepts it at once, but a query right after it can still report the invoice as issued.
|
|
203
|
-
- **An allowance needs the original invoice's buyer and date.** The reference has the date and the 統一編號, but not the buyer's name, so the adapter queries the invoice for it.
|
|
204
|
-
- **The allowance number is yours.** `allowance_id`
|
|
205
|
-
- **
|
|
206
|
-
- **Not supported**: special tax rates and foreign currency. `tax_rate` must be 0 or 0.05.
|
|
207
|
-
- **Carrier validation**: `validate_carrier({ type: "MOBILE_BARCODE", code: "/ABC1234" })` asks Amego whether a mobile barcode is registered. Amego can check a mobile barcode only. For 愛心碼, use `Einvoice::MOF::DonationCodes` (below).
|
|
252
|
+
- **An allowance needs the original invoice's buyer and date.** The reference has the date and the 統一編號, but not the buyer's name, so the adapter queries the invoice for it. To skip the query, pass `provider_options: { buyer_name: }`, for example for an invoice older than 180 days. The allowance date is today (Asia/Taipei).
|
|
253
|
+
- **The allowance number is yours.** The adapter sends `allowance_id` as Amego's `AllowanceNumber`: at most 16 characters, and unique in the account.
|
|
254
|
+
- **Carrier validation**: `validate_carrier({ type: "MOBILE_BARCODE", code: "/ABC1234" })` asks Amego whether a mobile barcode is registered. For a 愛心碼, use `Einvoice::MOF::DonationCodes` ([below](#財政部-lookups)).
|
|
208
255
|
- **Anything else**: reach the other endpoints through `provider.raw(path, payload)`. Section 3 of the note lists them.
|
|
209
256
|
- The details and the open questions are in [`docs/providers/amego/api.md`](docs/providers/amego/api.md).
|
|
210
257
|
|
|
@@ -222,11 +269,10 @@ provider = Einvoice::EzPay::Provider.new(
|
|
|
222
269
|
|
|
223
270
|
- **Experimental.** ezPay has no shared test account, and nobody has run this adapter against ezPay. Its encryption and `CheckCode` match the samples in ezPay's manual byte for byte. Test it against your own test shop before you rely on it.
|
|
224
271
|
- **A query needs the random code.** The reference from `issue` or `query` has it.
|
|
225
|
-
- **An allowance needs the invoice's order id.** The reference has it. Without it, the adapter queries the invoice by number first. ezPay assigns the allowance number, so
|
|
226
|
-
- **A query cannot tell an allowance.** ezPay only says if an invoice is voided. An invoice that is not voided reads back with status `nil`.
|
|
272
|
+
- **An allowance needs the invoice's order id.** The reference has it. Without it, the adapter queries the invoice by number first. ezPay assigns the allowance number, so the adapter does not send `allowance_id`. The allowance date is today (Asia/Taipei).
|
|
273
|
+
- **A query cannot tell an allowance.** ezPay only says if an invoice is voided. An invoice that is not voided reads back with status `nil`.
|
|
227
274
|
- **A void waits for the upload.** ezPay uploads the day's invoices the next morning, and refuses a void before that.
|
|
228
275
|
- **Prices follow the category.** ezPay wants B2C prices with tax and B2B prices without. The adapter takes them from the core's breakdown, so any `price_mode` works. Item counts and unit prices must be whole numbers.
|
|
229
|
-
- **Not supported**: query by order id, special tax rates, foreign currency, mixed tax on a B2B invoice, and the carrier check APIs. `tax_rate` must be 0 or 0.05.
|
|
230
276
|
- **Anything else**: `provider.raw(path, version, fields)`, for example `provider.raw("/Api/invoice_touch_issue", "1.0", { ... })`.
|
|
231
277
|
- The details and the open questions are in [`docs/providers/ezpay/invoice.md`](docs/providers/ezpay/invoice.md).
|
|
232
278
|
|
|
@@ -247,17 +293,32 @@ provider = Einvoice::Tradevan::DefaultAPI::Provider.new(
|
|
|
247
293
|
```
|
|
248
294
|
|
|
249
295
|
- **Experimental.** This adapter is ported from the 1.x client, its validators and its README examples. Tradevan does not publish its API docs, and nobody has run this adapter against the live API. Test it against your own account before you rely on it. The 1.x client stays on `1-x-stable`.
|
|
250
|
-
- **Void and allowance need the original `saleIdentifier`.** DEFAULTAPI identifies an invoice by it. The adapter takes it from `provider_options: { sale_identifier: }`
|
|
296
|
+
- **Void and allowance need the original `saleIdentifier`.** DEFAULTAPI identifies an invoice by it. The adapter takes it from `provider_options: { sale_identifier: }` first. Use this for an invoice that the 1.x client issued. Otherwise it builds the identifier from the reference's order id, or else queries the invoice by number. An allowance also needs the invoice date, from the reference or the same query. The reference from `issue` has all of these.
|
|
251
297
|
- **An allowance needs the tax type and rate** before any lookup. They come from the reference of `issue` or `query`. With only an invoice number, pass `tax_type:` and, for taxable items, `tax_rate:` in the allowance input.
|
|
252
|
-
- **Query status is often `nil`.**
|
|
253
|
-
- **Prices include tax on the wire.** The adapter takes them from the core's breakdown, so any `price_mode` works.
|
|
254
|
-
- **Not supported**: query by order id, mixed tax, the special tax rate, and foreign currency.
|
|
298
|
+
- **Query status is often `nil`.** Nobody knows what DEFAULTAPI's status codes mean, so the adapter does not guess. `status` is `:allowance` when the invoice has an allowance, and `nil` otherwise. The raw code is in `result.raw["status"]`.
|
|
299
|
+
- **Prices include tax on the wire.** The adapter takes them from the core's breakdown, so any `price_mode` works. When the allowance response has no date, the allowance date is today (Asia/Taipei).
|
|
255
300
|
- **Anything else**: reach the other DEFAULTAPI endpoints through `provider.raw(path, params)`, for example `provider.raw("get/getInvoiceMarkInfo", { ... })`.
|
|
256
301
|
- The details and the open questions are in [`docs/providers/tradevan/defaultapi.md`](docs/providers/tradevan/defaultapi.md).
|
|
257
302
|
|
|
303
|
+
## Input rules
|
|
304
|
+
|
|
305
|
+
The core checks these rules before any request, so every provider gives the same answer. A broken rule raises `ValidationError`.
|
|
306
|
+
|
|
307
|
+
- A buyer `ubn` is 8 digits, a company's 統一編號. A B2B invoice needs one, and an explicit `category` has to agree with it.
|
|
308
|
+
- Totals are whole numbers and not negative. Item quantity, price and amount are numbers with at most 7 decimal places. An invoice item may be negative, as a discount line. An allowance item may not (MIG 4.1), and its amount is whole TWD (MIG G0401).
|
|
309
|
+
- A number can be an `Integer`, a `BigDecimal`, a `Rational`, a `Float` or a numeric string such as `"100"` or `"0.05"`. A `Float` is read through its shortest form, so `0.1` stays `0.1`.
|
|
310
|
+
- A mobile barcode is `/` and 7 characters. A citizen certificate is 2 letters and 14 digits. These formats come from the providers' docs, because the MIG lists only the carrier types. A 愛心碼 is 3 to 7 digits. The MIG also takes the recipient's 統一編號 in that field. But at the time of sale, the buyer gives a 愛心碼, so the core takes only that form.
|
|
311
|
+
- `tax_rate` is a decimal, such as `0.05`, and has to be a MIG rate. On an issue, the lines decide its default, not the header: `0.05` when a line is taxable, else `0`. A special-tax line needs a rate. A taxable line takes the general rate `0.05` only (營業稅法第 10 條). The other rates are for special tax.
|
|
312
|
+
- A zero-rated invoice needs `customs_clearance_mark`, `"1"` or `"2"` (MIG CustomsClearanceMark). It also needs `zero_tax_rate_reason`, `"71"` to `"79"` (MIG ZeroTaxRateReason).
|
|
313
|
+
- The amounts agree with the items ([Amounts and tax](#amounts-and-tax)).
|
|
314
|
+
- The MIG field lengths: `remark` 200, buyer `name` 60, `address` 100, `email` 400, `phone` 26, carrier `code` 400, item `description` 500, `unit` 6 and `remark` 120. A consumer's name must not be `0` to `0000`, and a carrier code must not start or end with a space. A provider with a shorter limit of its own raises `UnsupportedError`.
|
|
315
|
+
- A void reason is at most 20 characters. A void allowance reason defaults to `作廢折讓`.
|
|
316
|
+
|
|
317
|
+
The [unified model](docs/providers/README.md#the-unified-model) explains each rule and its source.
|
|
318
|
+
|
|
258
319
|
## 財政部 lookups
|
|
259
320
|
|
|
260
|
-
|
|
321
|
+
These clients use 財政部's own public services. They issue nothing and need no credentials, so they work with any provider, or before you pick one.
|
|
261
322
|
|
|
262
323
|
```ruby
|
|
263
324
|
require "einvoice/mof"
|
|
@@ -269,14 +330,14 @@ codes.lookup("2718")
|
|
|
269
330
|
# name="社團法人台北市喜願協會", short_name="喜願協會",
|
|
270
331
|
# ubn="92000392", city="臺北市">
|
|
271
332
|
|
|
272
|
-
codes.exist?("105") # => false
|
|
333
|
+
codes.exist?("105") # => false: well-formed, but not registered
|
|
273
334
|
codes.for_ubn("92000392") # => every 愛心碼 that organisation holds
|
|
274
335
|
codes.all # => the whole dataset (~2,000, five requests)
|
|
275
336
|
```
|
|
276
337
|
|
|
277
|
-
|
|
338
|
+
`Input` does **not** call this client, on purpose. `Input` checks locally, and issuing an invoice must never depend on a third party being reachable. `Input` checks the shape of a 愛心碼. Whether the code is *registered* is a question you ask when it suits you: at order time, or once at boot through `#all`, to build your own set for offline checks.
|
|
278
339
|
|
|
279
|
-
The dataset needs no credentials today.
|
|
340
|
+
The dataset needs no credentials today. But its published OpenAPI declares `api_key` and `oauth2` schemes. So a 401 or 403 raises `Einvoice::AuthError` that says so, and not a parse failure.
|
|
280
341
|
|
|
281
342
|
## Development
|
|
282
343
|
|
|
@@ -286,21 +347,26 @@ bundle exec rspec # run the specs
|
|
|
286
347
|
bundle exec rake # default task: rspec
|
|
287
348
|
```
|
|
288
349
|
|
|
289
|
-
The shared examples in `spec/support/shared_examples/` are the executable contract. Every adapter runs `"an invoice provider"`, and every adapter's client runs `"a provider client"
|
|
350
|
+
The shared examples in `spec/support/shared_examples/` are the executable contract. Every adapter runs `"an invoice provider"`, and every adapter's client runs `"a provider client"`. So a new adapter proves that it follows the unified model by including them. A new adapter also gets its own entry file, `lib/einvoice/<name>.rb`, that requires `einvoice`. `lib/einvoice.rb` does not load it, and `spec/spec_helper.rb` does.
|
|
351
|
+
|
|
352
|
+
To write a new adapter, start with the provider notes in [`docs/providers/`](docs/providers/README.md). Read them in this order:
|
|
290
353
|
|
|
291
|
-
|
|
354
|
+
1. the 財政部 MIG note, the standard that every provider follows
|
|
355
|
+
2. the ECPay note, the first adapter, to see how a note maps to code
|
|
356
|
+
3. the note for your provider
|
|
357
|
+
4. the shared examples above
|
|
292
358
|
|
|
293
|
-
The ECPay adapter is tested in three layers
|
|
359
|
+
The ECPay adapter is tested in three layers. Two of them run offline in CI:
|
|
294
360
|
|
|
295
|
-
1. **The contract, against a wire-format fake.** The shared examples run through an in-process fake that speaks the real protocol: the AES envelope, PHP url-encoding and ECPay's own RtnCodes. So a request only
|
|
296
|
-
2. **Recorded real responses (VCR).** `spec/einvoice/ecpay/recorded_spec.rb` replays cassettes
|
|
297
|
-
3. **The live API, opt-in.**
|
|
361
|
+
1. **The contract, against a wire-format fake.** The shared examples run through an in-process fake that speaks the real protocol: the AES envelope, PHP url-encoding and ECPay's own RtnCodes. So a request passes only if the encryption and the field mapping are correct. The fake keeps state, so the contract can run issue, allowance and void in order.
|
|
362
|
+
2. **Recorded real responses (VCR).** `spec/einvoice/ecpay/recorded_spec.rb` replays cassettes from ECPay's stage host. So the field names, value types and error codes under test are ECPay's own, not our reading of the docs. To record again, raise the epoch in that spec, then run with `VCR_RECORD=1`. Cassettes only ever hold sandbox traffic.
|
|
363
|
+
3. **The live API, opt-in.** It keeps the other two honest:
|
|
298
364
|
|
|
299
365
|
```bash
|
|
300
366
|
ECPAY_LIVE=1 bundle exec rspec spec/einvoice/ecpay/live_spec.rb
|
|
301
367
|
```
|
|
302
368
|
|
|
303
|
-
It
|
|
369
|
+
It uses the public sandbox credentials by default, so it needs no setup. To use your own stage account, set `ECPAY_MERCHANT_ID`, `ECPAY_HASH_KEY` and `ECPAY_HASH_IV`. CI does not run it, because it needs the network and a shared sandbox.
|
|
304
370
|
|
|
305
371
|
Amego has a live spec too. It runs against Amego's public test account:
|
|
306
372
|
|