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.
- checksums.yaml +4 -4
- data/.coderabbit.yaml +12 -0
- data/.github/workflows/build.yml +7 -4
- data/CHANGELOG.md +55 -35
- data/Gemfile +3 -2
- data/README.md +230 -265
- data/README.zh-TW.md +322 -0
- data/Rakefile +2 -13
- data/bin/console +3 -6
- data/einvoice.gemspec +24 -21
- data/lib/einvoice/amego/client.rb +116 -0
- data/lib/einvoice/amego/errors.rb +84 -0
- data/lib/einvoice/amego/payload.rb +276 -0
- data/lib/einvoice/amego/provider.rb +267 -0
- data/lib/einvoice/amego.rb +27 -0
- data/lib/einvoice/capability.rb +79 -0
- data/lib/einvoice/ecpay/client.rb +183 -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 +362 -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 +235 -0
- data/lib/einvoice/ezpay.rb +19 -0
- data/lib/einvoice/input.rb +494 -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 +51 -0
- data/lib/einvoice/provider.rb +57 -14
- data/lib/einvoice/redaction.rb +54 -0
- data/lib/einvoice/taipei.rb +89 -0
- data/lib/einvoice/tax_split.rb +124 -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 +366 -0
- data/lib/einvoice/tradevan/default_api/rules.rb +314 -0
- data/lib/einvoice/tradevan.rb +25 -0
- data/lib/einvoice/types.rb +323 -0
- data/lib/einvoice/version.rb +3 -1
- data/lib/einvoice.rb +21 -10
- metadata +60 -176
- data/.github/workflows/cron.yml +0 -31
- 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 -2022
- 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 -140
- data/lib/einvoice/tradevan/result.rb +0 -25
- data/lib/einvoice/tradevan/validator/issue_data_validator.rb +0 -80
- data/lib/einvoice/utils.rb +0 -13
- data/lib/faraday/response/decode_tradevan.rb +0 -38
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
module Einvoice
|
|
6
|
+
module EzPay
|
|
7
|
+
# {Einvoice::Provider} over ezPay 簡單行動支付's e-invoice API (the 標準版
|
|
8
|
+
# manual, EZP_INVI 1.2.2).
|
|
9
|
+
#
|
|
10
|
+
# **Experimental.** It is built from docs/providers/ezpay/invoice.md alone.
|
|
11
|
+
# ezPay has no shared test account, and nobody has run this adapter against
|
|
12
|
+
# ezPay yet. Test it against your own test shop first.
|
|
13
|
+
#
|
|
14
|
+
# ezPay needs facts that a plain invoice number does not carry. The
|
|
15
|
+
# reference from #issue or #query has them:
|
|
16
|
+
#
|
|
17
|
+
# - A query needs the number with +random_code+. ezPay can also find an
|
|
18
|
+
# invoice by order id, but only with its total, so the adapter does not
|
|
19
|
+
# declare QUERY_BY_ORDER_ID.
|
|
20
|
+
# - An allowance needs the invoice's +order_id+, and +buyer_ubn+ to pick
|
|
21
|
+
# prices with or without tax. Without an order id, the adapter queries
|
|
22
|
+
# the invoice by number first.
|
|
23
|
+
#
|
|
24
|
+
# An invoice can only be voided after ezPay uploads it, which happens the
|
|
25
|
+
# next day.
|
|
26
|
+
class Provider < ::Einvoice::Provider
|
|
27
|
+
CAPABILITIES = Set[
|
|
28
|
+
Capability::ISSUE,
|
|
29
|
+
Capability::VOID,
|
|
30
|
+
Capability::ALLOWANCE,
|
|
31
|
+
Capability::VOID_ALLOWANCE,
|
|
32
|
+
Capability::QUERY,
|
|
33
|
+
Capability::B2B,
|
|
34
|
+
Capability::MIXED_TAX
|
|
35
|
+
].freeze
|
|
36
|
+
|
|
37
|
+
VERSION = { issue: "1.5", allowance: "1.3", query: "1.3", other: "1.0" }.freeze
|
|
38
|
+
# InvoiceStatus 1 is "not voided", with or without an allowance, so it
|
|
39
|
+
# has no unified status (docs, section 4.4).
|
|
40
|
+
STATUS = { "2" => InvoiceStatus::VOIDED }.freeze
|
|
41
|
+
# InvoiceStatus 1 is "not voided", with or without an allowance (4.4).
|
|
42
|
+
QUERY_STATUSES = STATUS.values.freeze
|
|
43
|
+
|
|
44
|
+
attr_reader :capabilities, :client
|
|
45
|
+
|
|
46
|
+
def initialize(merchant_id:, hash_key:, hash_iv:, mode: ProviderMode::TEST, base_url: nil, timeout: 30)
|
|
47
|
+
super()
|
|
48
|
+
@client = Client.new(merchant_id: merchant_id, hash_key: hash_key, hash_iv: hash_iv, mode: mode,
|
|
49
|
+
base_url: base_url, timeout: timeout)
|
|
50
|
+
@capabilities = CAPABILITIES
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def name
|
|
54
|
+
Client::PROVIDER
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def query_statuses
|
|
58
|
+
QUERY_STATUSES
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Call any ezPay path with raw fields, for example
|
|
62
|
+
#
|
|
63
|
+
# provider.raw("/Api/invoice_touch_issue", "1.0", { InvoiceTransNo: "…", … })
|
|
64
|
+
#
|
|
65
|
+
# The encryption and the error mapping are applied as usual. Brace the
|
|
66
|
+
# Hash: an unbraced one is read as keyword arguments.
|
|
67
|
+
def raw(path, version, fields)
|
|
68
|
+
@client.request(path, version, fields)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# 開立發票 (+invoice_issue+, issued now).
|
|
72
|
+
def issue(input)
|
|
73
|
+
parsed = Input.issue(input, provider: name)
|
|
74
|
+
assert_currency_supported!(parsed.currency)
|
|
75
|
+
result = @client.request("/Api/invoice_issue", VERSION[:issue], Payload.issue(parsed), operation: :issue)
|
|
76
|
+
@client.verify_check_code!(result)
|
|
77
|
+
|
|
78
|
+
IssueInvoiceResult.new(
|
|
79
|
+
invoice_number: result["InvoiceNumber"].to_s,
|
|
80
|
+
invoice_datetime: taipei_time(result["CreateTime"]),
|
|
81
|
+
random_code: result["RandomNum"].to_s,
|
|
82
|
+
order_id: result["MerchantOrderNo"].to_s,
|
|
83
|
+
total_amount: result["TotalAmt"].to_i,
|
|
84
|
+
buyer_ubn: parsed.buyer&.ubn,
|
|
85
|
+
tax_type: TaxType.of(parsed.items, parsed.tax_type),
|
|
86
|
+
tax_rate: parsed.tax_rate,
|
|
87
|
+
status: InvoiceStatus::ISSUED,
|
|
88
|
+
raw: result
|
|
89
|
+
)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# 作廢發票 (+invoice_invalid+). Only after the next day's upload.
|
|
93
|
+
def void(input)
|
|
94
|
+
parsed = Input.void(input, provider: name)
|
|
95
|
+
result = @client.request("/Api/invoice_invalid", VERSION[:other], Payload.void(parsed), operation: :void)
|
|
96
|
+
VoidInvoiceResult.new(invoice_number: result.fetch("InvoiceNumber", parsed.invoice.number).to_s,
|
|
97
|
+
status: InvoiceStatus::VOIDED, raw: result)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# 開立折讓 (+allowance_issue+, confirmed now). ezPay assigns the
|
|
101
|
+
# allowance number, so +allowance_id+ is not sent. The result has no date.
|
|
102
|
+
# The allowance is confirmed at once, so its date is today in Taipei.
|
|
103
|
+
def allowance(input)
|
|
104
|
+
parsed = Input.allowance(input, provider: name)
|
|
105
|
+
data = Payload.allowance(parsed, original_invoice(parsed.invoice))
|
|
106
|
+
result = @client.request("/Api/allowance_issue", VERSION[:allowance], data, operation: :allowance)
|
|
107
|
+
AllowanceResult.new(
|
|
108
|
+
allowance_number: result["AllowanceNo"].to_s,
|
|
109
|
+
invoice_number: result.fetch("InvoiceNumber", parsed.invoice.number).to_s,
|
|
110
|
+
allowance_date: Taipei.today,
|
|
111
|
+
total_amount: result["AllowanceAmt"].to_i,
|
|
112
|
+
raw: result
|
|
113
|
+
)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# 作廢折讓 (+allowanceInvalid+).
|
|
117
|
+
def void_allowance(input)
|
|
118
|
+
parsed = Input.void_allowance(input, provider: name)
|
|
119
|
+
result = @client.request("/Api/allowanceInvalid", VERSION[:other], Payload.void_allowance(parsed),
|
|
120
|
+
operation: :void_allowance)
|
|
121
|
+
VoidAllowanceResult.new(allowance_number: result.fetch("AllowanceNo", parsed.allowance.allowance_number).to_s,
|
|
122
|
+
raw: result)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# 查詢發票 (+invoice_search+). See the class comment for the second key.
|
|
126
|
+
#
|
|
127
|
+
# The result has no allowance data, so +status+ is never +ALLOWANCE+.
|
|
128
|
+
def query(input)
|
|
129
|
+
reference = Input.query(input, provider: name).invoice
|
|
130
|
+
assert_supports!(Capability::QUERY_BY_ORDER_ID) if reference.number.nil?
|
|
131
|
+
search(reference)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
private
|
|
135
|
+
|
|
136
|
+
def search(reference)
|
|
137
|
+
result = @client.request("/Api/invoice_search", VERSION[:query], Payload.query(reference), operation: :query)
|
|
138
|
+
@client.verify_check_code!(result)
|
|
139
|
+
build_query_result(result)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# The invoice's order id, and whether its buyer is a business (docs,
|
|
143
|
+
# section 4.1). From the reference when it has the order id, else from a
|
|
144
|
+
# query by number. A failed query is the provider's error, not the
|
|
145
|
+
# invoice's: a NotFoundError would read as "the invoice of this call does
|
|
146
|
+
# not exist".
|
|
147
|
+
def original_invoice(reference)
|
|
148
|
+
return { order_id: reference.order_id, business: !reference.buyer_ubn.nil? } if reference.order_id
|
|
149
|
+
unless Payload.queryable?(reference)
|
|
150
|
+
raise ValidationError.new("an ezPay allowance needs invoice.order_id, or invoice.random_code to look it up",
|
|
151
|
+
provider: name)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
found = begin
|
|
155
|
+
search(reference)
|
|
156
|
+
rescue Error => e
|
|
157
|
+
raise ProviderError.new("allowance needs the invoice's order id; looking it up failed: #{e.message}",
|
|
158
|
+
provider: name, reason: Reason::LOOKUP_FAILED, cause: e)
|
|
159
|
+
end
|
|
160
|
+
if found.order_id.nil?
|
|
161
|
+
raise ProviderError.new("allowance needs the invoice's order id; the invoice found has none",
|
|
162
|
+
provider: name, reason: Reason::LOOKUP_FAILED)
|
|
163
|
+
end
|
|
164
|
+
{ order_id: found.order_id, business: !found.buyer.ubn.nil? }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def build_query_result(result)
|
|
168
|
+
QueryInvoiceResult.new(
|
|
169
|
+
invoice_number: result["InvoiceNumber"].to_s,
|
|
170
|
+
invoice_datetime: taipei_time(result["CreateTime"]),
|
|
171
|
+
random_code: result["RandomNum"].to_s,
|
|
172
|
+
order_id: presence(result["MerchantOrderNo"]),
|
|
173
|
+
status: STATUS[result["InvoiceStatus"].to_s],
|
|
174
|
+
amount: AmountSummary.new(sales_amount: result["Amt"].to_i, tax_amount: result["TaxAmt"].to_i,
|
|
175
|
+
total_amount: result["TotalAmt"].to_i),
|
|
176
|
+
buyer: Buyer.new(name: presence(result["BuyerName"]), ubn: presence(result["BuyerUBN"]),
|
|
177
|
+
email: presence(result["BuyerEmail"]), address: presence(result["BuyerAddress"]),
|
|
178
|
+
phone: presence(result["BuyerPhone"])),
|
|
179
|
+
items: item_detail(result["ItemDetail"]).map { |row| build_item(row) },
|
|
180
|
+
tax_type: TAX_TYPE[result["TaxType"].to_s],
|
|
181
|
+
tax_rate: query_rate(result["TaxRate"]),
|
|
182
|
+
raw: result
|
|
183
|
+
)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# ezPay TaxType → unified 課稅別. 9 is mixed.
|
|
187
|
+
TAX_TYPE = Payload::TAX_TYPE.invert.merge(Payload::MIXED_TAX_CODE => TaxType::MIXED).freeze
|
|
188
|
+
|
|
189
|
+
# The query returns the rate as a fraction, for example "0.05000". A rate
|
|
190
|
+
# that is not a MIG rate reads as nil.
|
|
191
|
+
def query_rate(reported)
|
|
192
|
+
Input.mig_rate!(reported, "TaxRate", name)
|
|
193
|
+
rescue ValidationError
|
|
194
|
+
nil
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# +ItemDetail+ is a JSON string inside +Result+.
|
|
198
|
+
def item_detail(value)
|
|
199
|
+
rows = value.is_a?(String) && !value.strip.empty? ? JSON.parse(value) : value
|
|
200
|
+
Array(rows).select { |row| row.is_a?(Hash) }
|
|
201
|
+
rescue JSON::ParserError => e
|
|
202
|
+
raise ProviderError.new("ezPay returned an ItemDetail that is not JSON", provider: name, raw: value,
|
|
203
|
+
cause: e)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def build_item(row)
|
|
207
|
+
InvoiceItem.new(
|
|
208
|
+
description: row["ItemName"].to_s,
|
|
209
|
+
quantity: numeric(row["ItemCount"]),
|
|
210
|
+
unit_price: numeric(row["ItemPrice"]),
|
|
211
|
+
amount: numeric(row["ItemAmount"]),
|
|
212
|
+
unit: presence(row["ItemWord"]),
|
|
213
|
+
tax_type: Payload::TAX_TYPE.invert[row["ItemTaxType"].to_s],
|
|
214
|
+
remark: nil
|
|
215
|
+
)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def numeric(value)
|
|
219
|
+
rational = value.to_s.to_r
|
|
220
|
+
rational.denominator == 1 ? rational.to_i : rational.to_f
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def presence(value)
|
|
224
|
+
string = value.to_s
|
|
225
|
+
string.empty? ? nil : string
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# "2015-11-03 17:58:36" (UTC+8) → Time, or nil.
|
|
229
|
+
def taipei_time(value)
|
|
230
|
+
match = /\A(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\z/.match(value.to_s)
|
|
231
|
+
match && Time.new(*match.captures.map(&:to_i), "+08:00")
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "einvoice"
|
|
4
|
+
require "einvoice/ezpay/crypto"
|
|
5
|
+
require "einvoice/ezpay/errors"
|
|
6
|
+
require "einvoice/ezpay/client"
|
|
7
|
+
require "einvoice/ezpay/payload"
|
|
8
|
+
require "einvoice/ezpay/provider"
|
|
9
|
+
|
|
10
|
+
module Einvoice
|
|
11
|
+
# Adapter for ezPay 簡單行動支付's e-invoice API. Experimental: see
|
|
12
|
+
# {Provider}.
|
|
13
|
+
#
|
|
14
|
+
# The wire format is a form POST whose +PostData_+ is a PHP query string,
|
|
15
|
+
# encrypted with AES-256-CBC and hex encoded. None of that reaches
|
|
16
|
+
# application code: {Provider} exposes only the unified model.
|
|
17
|
+
module EzPay
|
|
18
|
+
end
|
|
19
|
+
end
|