einvoice 1.5.0 → 2.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.coderabbit.yaml +6 -8
- data/.github/workflows/build.yml +6 -3
- data/.github/workflows/cron.yml +4 -2
- data/CHANGELOG.md +63 -44
- data/Gemfile +3 -10
- data/README.md +284 -325
- data/README.zh-TW.md +381 -0
- data/Rakefile +2 -51
- data/bin/console +3 -6
- data/einvoice.gemspec +17 -18
- data/lib/einvoice/amego/client.rb +116 -0
- data/lib/einvoice/amego/errors.rb +84 -0
- data/lib/einvoice/amego/payload.rb +270 -0
- data/lib/einvoice/amego/provider.rb +272 -0
- data/lib/einvoice/amego.rb +27 -0
- data/lib/einvoice/capability.rb +79 -0
- data/lib/einvoice/ecpay/client.rb +184 -0
- data/lib/einvoice/ecpay/crypto.rb +85 -0
- data/lib/einvoice/ecpay/errors.rb +115 -0
- data/lib/einvoice/ecpay/payload.rb +241 -0
- data/lib/einvoice/ecpay/provider.rb +371 -0
- data/lib/einvoice/ecpay.rb +31 -0
- data/lib/einvoice/error.rb +220 -0
- data/lib/einvoice/ezpay/client.rb +130 -0
- data/lib/einvoice/ezpay/crypto.rb +85 -0
- data/lib/einvoice/ezpay/errors.rb +72 -0
- data/lib/einvoice/ezpay/payload.rb +269 -0
- data/lib/einvoice/ezpay/provider.rb +253 -0
- data/lib/einvoice/ezpay.rb +19 -0
- data/lib/einvoice/input.rb +547 -0
- data/lib/einvoice/mock_provider.rb +191 -0
- data/lib/einvoice/mof/donation_codes.rb +157 -0
- data/lib/einvoice/mof.rb +16 -0
- data/lib/einvoice/number.rb +91 -0
- data/lib/einvoice/provider.rb +57 -13
- data/lib/einvoice/redaction.rb +54 -0
- data/lib/einvoice/taipei.rb +89 -0
- data/lib/einvoice/tax_split.rb +112 -0
- data/lib/einvoice/tradevan/default_api/client.rb +175 -0
- data/lib/einvoice/tradevan/default_api/crypto.rb +90 -0
- data/lib/einvoice/tradevan/default_api/errors.rb +54 -0
- data/lib/einvoice/tradevan/default_api/payload.rb +259 -0
- data/lib/einvoice/tradevan/default_api/provider.rb +353 -0
- data/lib/einvoice/tradevan/default_api/rules.rb +314 -0
- data/lib/einvoice/tradevan.rb +25 -0
- data/lib/einvoice/types.rb +365 -0
- data/lib/einvoice/version.rb +3 -1
- data/lib/einvoice.rb +21 -9
- metadata +52 -144
- data/docs/Tradevan E-Invoice APIs.md +0 -201
- data/docs/neweb.md +0 -347
- data/lib/einvoice/client.rb +0 -21
- data/lib/einvoice/configuration.rb +0 -49
- data/lib/einvoice/connection.rb +0 -31
- data/lib/einvoice/donation_unit_list.json +0 -2025
- data/lib/einvoice/result.rb +0 -21
- data/lib/einvoice/tradevan/model/base.rb +0 -34
- data/lib/einvoice/tradevan/model/issue_data.rb +0 -129
- data/lib/einvoice/tradevan/model/issue_item.rb +0 -41
- data/lib/einvoice/tradevan/model/void_data.rb +0 -35
- data/lib/einvoice/tradevan/provider.rb +0 -139
- data/lib/einvoice/tradevan/result.rb +0 -25
- data/lib/einvoice/tradevan/validator/issue_data_validator.rb +0 -80
- data/lib/faraday/response/decode_tradevan.rb +0 -37
- data/lib/faraday/response/parse_xml.rb +0 -14
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Einvoice
|
|
4
|
+
# The tax of an invoice or an allowance, computed by the core from its items,
|
|
5
|
+
# +price_mode+, +tax_type+ and +tax_rate+. Adapters read the values their
|
|
6
|
+
# provider wants from here and do not compute tax themselves.
|
|
7
|
+
#
|
|
8
|
+
# The totals are whole TWD and follow the MIG: +sales_amount+ excludes tax and
|
|
9
|
+
# is +taxable_amount+ + +zero_rated_amount+ + +tax_free_amount+, and
|
|
10
|
+
# +total_amount+ is +sales_amount+ + +tax_amount+. A B2C invoice reports its
|
|
11
|
+
# AmountSummary with the tax inside the sales amount (F0401), but its
|
|
12
|
+
# breakdown still splits the tax out, for providers that ask for it.
|
|
13
|
+
TaxBreakdown = Data.define(:lines, :sales_amount, :taxable_amount, :zero_rated_amount, :tax_free_amount,
|
|
14
|
+
:tax_amount, :total_amount)
|
|
15
|
+
|
|
16
|
+
# One line of a TaxBreakdown. +net_amount+ excludes tax, +gross_amount+
|
|
17
|
+
# includes it, and +tax+ is whole TWD. +gross_unit_price+ comes from the
|
|
18
|
+
# item's +unit_price+, with tax, rounded to 7 decimal places (MIG 表 15-5).
|
|
19
|
+
# It is not derived from the amounts, so +unit price × quantity+ can differ
|
|
20
|
+
# from the amount by the rounding of the line tax. Each provider note says
|
|
21
|
+
# how much its API allows.
|
|
22
|
+
TaxBreakdown::Line = Data.define(:tax_type, :net_amount, :tax, :gross_amount, :gross_unit_price)
|
|
23
|
+
|
|
24
|
+
# Splits tax over lines by the largest remainder: each taxed line takes the
|
|
25
|
+
# whole part of its share, and the units left go to the lines with the
|
|
26
|
+
# largest fractions, later lines first on a tie. So the line taxes add up to
|
|
27
|
+
# the total tax, and each line tax is the floor or the ceiling of its share.
|
|
28
|
+
# A line tax has the sign of its amount: a discount line with a negative
|
|
29
|
+
# amount can take a negative tax, and no other line does. Lines that are not
|
|
30
|
+
# taxed have tax 0.
|
|
31
|
+
module TaxSplit
|
|
32
|
+
Line = Data.define(:amount, :tax)
|
|
33
|
+
|
|
34
|
+
module_function
|
|
35
|
+
|
|
36
|
+
# The TaxBreakdown of +items+. +types+ is the tax type of each item, in
|
|
37
|
+
# item order (see TaxType.per_item). Input has checked every value: each
|
|
38
|
+
# line has a tax type (an item of a MIXED invoice has its own), and each
|
|
39
|
+
# number is valid. The breakdown only reads the items' numbers as
|
|
40
|
+
# Rationals, because the items keep the caller's values as given.
|
|
41
|
+
def breakdown(items, types:, tax_rate:, price_mode:)
|
|
42
|
+
taxed = types.map { |type| TaxType::TAXED.include?(type) }
|
|
43
|
+
# Input has checked the rate: a taxed line has one, above 0. A line that
|
|
44
|
+
# is not taxed has rate 0. An input built with .new skips Input, and may
|
|
45
|
+
# leave the rate out when no line is taxed.
|
|
46
|
+
tax_rate ||= 0r if taxed.none?
|
|
47
|
+
rates = taxed.map { |line_taxed| line_taxed ? tax_rate : 0r }
|
|
48
|
+
inclusive = price_mode == PriceMode::TAX_INCLUSIVE
|
|
49
|
+
amounts = items.map { |item| Number.to_rational(item.amount) }
|
|
50
|
+
split = if inclusive
|
|
51
|
+
split(amounts, taxable: taxed, rate: tax_rate)
|
|
52
|
+
else
|
|
53
|
+
allocate(amounts, taxable: taxed, total_tax: (taxable_sum(amounts, taxed) * tax_rate).round)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
lines = items.each_with_index.map do |item, index|
|
|
57
|
+
price = Number.to_rational(item.unit_price)
|
|
58
|
+
gross_price = inclusive ? price : price * (1 + rates[index])
|
|
59
|
+
line = split[index]
|
|
60
|
+
TaxBreakdown::Line.new(tax_type: types[index], net_amount: Number.plain(line.amount), tax: line.tax,
|
|
61
|
+
gross_amount: Number.plain(line.amount + line.tax),
|
|
62
|
+
gross_unit_price: Number.plain(gross_price.round(Number::DECIMAL_PLACES)))
|
|
63
|
+
end
|
|
64
|
+
totals(lines, split)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The split keeps each amount exact, and the tax is whole TWD, so with
|
|
68
|
+
# tax-included items the total is still the rounded sum of the items.
|
|
69
|
+
def totals(lines, split)
|
|
70
|
+
tax = split.sum(&:tax)
|
|
71
|
+
sales = split.sum(0r, &:amount).round
|
|
72
|
+
# Sum the exact split, not the lines: net_amount may be a Float, and 0.35 +
|
|
73
|
+
# 0.15 as Floats rounds to 0, not 1.
|
|
74
|
+
untaxed = lambda do |type|
|
|
75
|
+
lines.zip(split).select { |line, _| line.tax_type == type }.sum(0r) { |_, part| part.amount }.round
|
|
76
|
+
end
|
|
77
|
+
zero = untaxed.call(TaxType::ZERO_RATED)
|
|
78
|
+
free = untaxed.call(TaxType::TAX_FREE)
|
|
79
|
+
TaxBreakdown.new(lines: lines, sales_amount: sales, taxable_amount: sales - zero - free,
|
|
80
|
+
zero_rated_amount: zero, tax_free_amount: free, tax_amount: tax, total_amount: sales + tax)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Splits the tax out of tax-included amounts. The total tax is
|
|
84
|
+
# round(taxed total × rate / (1 + rate)). Returns tax-excluded lines.
|
|
85
|
+
def split(amounts, taxable:, rate:)
|
|
86
|
+
factor = rate / (1 + rate)
|
|
87
|
+
total = (taxable_sum(amounts, taxable) * factor).round
|
|
88
|
+
taxes = distribute(amounts, taxable, total) { |amount| amount * factor }
|
|
89
|
+
amounts.zip(taxes).map { |amount, tax| Line.new(amount: amount - tax, tax: tax) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Spreads a total tax over tax-excluded amounts, in proportion to the taxed
|
|
93
|
+
# amounts. Returns the lines with their amounts unchanged.
|
|
94
|
+
def allocate(amounts, taxable:, total_tax:)
|
|
95
|
+
base = taxable_sum(amounts, taxable)
|
|
96
|
+
taxes = distribute(amounts, taxable, total_tax) { |amount| base.zero? ? 0 : amount * total_tax / base }
|
|
97
|
+
amounts.zip(taxes).map { |amount, tax| Line.new(amount: amount, tax: tax) }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def taxable_sum(amounts, taxable)
|
|
101
|
+
amounts.each_index.sum(0r) { |i| taxable[i] ? amounts[i] : 0 }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def distribute(amounts, taxable, total)
|
|
105
|
+
shares = amounts.each_index.map { |i| taxable[i] ? yield(amounts[i]) : 0r }
|
|
106
|
+
taxes = shares.map(&:floor)
|
|
107
|
+
order = amounts.each_index.select { |i| taxable[i] }.sort_by { |i| [taxes[i] - shares[i], -i] }
|
|
108
|
+
(total - taxes.sum).times { |k| taxes[order[k]] += 1 }
|
|
109
|
+
taxes
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module Einvoice
|
|
8
|
+
module Tradevan
|
|
9
|
+
module DefaultAPI
|
|
10
|
+
# Speaks the DEFAULTAPI wire protocol: encrypt the parameters into +v+,
|
|
11
|
+
# send it in the query string (also for the POST endpoints, as the 1.x
|
|
12
|
+
# client did), then read the +Success+ / +Message+ envelope and raise the
|
|
13
|
+
# matching {Einvoice::Error} subclass.
|
|
14
|
+
#
|
|
15
|
+
# Callers get the decrypted +Message+ back and never see the envelope.
|
|
16
|
+
class Client
|
|
17
|
+
PROVIDER = "tradevan"
|
|
18
|
+
PATH_PREFIX = "/DEFAULTAPI/"
|
|
19
|
+
# The envelope's +Success+ values. A body with another value was not
|
|
20
|
+
# written by DEFAULTAPI, so the HTTP status decides.
|
|
21
|
+
SUCCESS_VALUES = %w[Y N E].freeze
|
|
22
|
+
|
|
23
|
+
attr_reader :base_url, :timeout
|
|
24
|
+
|
|
25
|
+
# @param base_url [String] the Tradevan host. There is no default: the
|
|
26
|
+
# 1.x client never shipped one, and the hosts are not public.
|
|
27
|
+
def initialize(acnt:, acntp:, key1:, key2:, base_url:, timeout: 30)
|
|
28
|
+
if base_url.to_s.empty?
|
|
29
|
+
raise ValidationError.new("Tradevan base_url is required", provider: PROVIDER)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
@acnt = acnt.to_s
|
|
33
|
+
@acntp = acntp.to_s
|
|
34
|
+
@key1 = key1.to_s
|
|
35
|
+
@key2 = key2.to_s
|
|
36
|
+
# Frozen: the checks above must hold for every request after this.
|
|
37
|
+
@base_url = Redaction.base_url!(base_url, provider: PROVIDER)
|
|
38
|
+
@timeout = timeout
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def inspect
|
|
42
|
+
"#<#{self.class.name} base_url=#{@base_url.inspect} acnt=#{@acnt.inspect} " \
|
|
43
|
+
"acntp=#{Redaction::FILTERED} key1=#{Redaction::FILTERED} key2=#{Redaction::FILTERED}>"
|
|
44
|
+
end
|
|
45
|
+
alias to_s inspect
|
|
46
|
+
|
|
47
|
+
# Call +path+ (for example +"post/issue"+ or +"get/searchInvoiceDetail"+)
|
|
48
|
+
# with +params+, and return the decrypted +Message+. A path that starts
|
|
49
|
+
# with +post/+ is sent as POST, everything else as GET.
|
|
50
|
+
def request(path, params)
|
|
51
|
+
v = Crypto.encode_params(params, acnt: @acnt, acntp: @acntp, key1: @key1, key2: @key2)
|
|
52
|
+
read(*send_request(path, v))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Raise unless the inner status field (+issueStatus+, +voidStatus+) is
|
|
56
|
+
# +"Y"+. +Success: "Y"+ alone only means the envelope was read.
|
|
57
|
+
def check_status!(result, field)
|
|
58
|
+
return if result.is_a?(Hash) && result[field] == "Y"
|
|
59
|
+
|
|
60
|
+
reason = result.is_a?(Hash) ? result["failReason"].to_s : ""
|
|
61
|
+
raise_business(reason, result)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def send_request(path, v)
|
|
67
|
+
uri = URI("#{@base_url}#{PATH_PREFIX}#{path}")
|
|
68
|
+
uri.query = URI.encode_www_form(v: v)
|
|
69
|
+
|
|
70
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
71
|
+
http.use_ssl = uri.scheme == "https"
|
|
72
|
+
http.open_timeout = @timeout
|
|
73
|
+
http.read_timeout = @timeout
|
|
74
|
+
response = nil
|
|
75
|
+
http.request(build_request(path, uri)) { |res| (response = res).read_body }
|
|
76
|
+
[parse(response), response]
|
|
77
|
+
rescue *Error::NETWORK_ERRORS => e
|
|
78
|
+
raise NetworkError.for_failure(e, provider: PROVIDER, name: "Tradevan", response: response)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def build_request(path, uri)
|
|
82
|
+
request = if path.start_with?("post/")
|
|
83
|
+
Net::HTTP::Post.new(uri.request_uri).tap do |post|
|
|
84
|
+
post["Content-Type"] = "application/x-www-form-urlencoded"
|
|
85
|
+
post.body = ""
|
|
86
|
+
end
|
|
87
|
+
else
|
|
88
|
+
Net::HTTP::Get.new(uri.request_uri)
|
|
89
|
+
end
|
|
90
|
+
request["Accept"] = "application/json; charset=utf-8"
|
|
91
|
+
request
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# A body that Tradevan wrote (a JSON object with +Success+) decides the
|
|
95
|
+
# error, whatever the HTTP status. Any other body falls back to the
|
|
96
|
+
# status: HTTP 429 and 5xx are a NetworkError, so the caller can retry
|
|
97
|
+
# (docs/providers/README.md, "The unified model").
|
|
98
|
+
def parse(response)
|
|
99
|
+
body = JSON.parse(response.body.to_s)
|
|
100
|
+
return body if body.is_a?(Hash) && SUCCESS_VALUES.include?(body["Success"])
|
|
101
|
+
|
|
102
|
+
raise_for_status!(response)
|
|
103
|
+
body
|
|
104
|
+
rescue JSON::ParserError => e
|
|
105
|
+
raise_for_status!(response)
|
|
106
|
+
raise ProviderError.new("Tradevan returned a non-JSON response (HTTP #{response.code})",
|
|
107
|
+
provider: PROVIDER, raw_code: response.code, cause: e)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def raise_for_status!(response)
|
|
111
|
+
error = NetworkError.for_status(response, provider: PROVIDER)
|
|
112
|
+
raise error if error
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def read(envelope, response)
|
|
116
|
+
unless envelope.is_a?(Hash)
|
|
117
|
+
raise ProviderError.new("Tradevan returned an unexpected envelope", provider: PROVIDER,
|
|
118
|
+
raw: envelope)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
case envelope["Success"]
|
|
122
|
+
when "Y" then message(envelope, response)
|
|
123
|
+
when "N" then raise_failure(envelope, response)
|
|
124
|
+
when "E" then raise_transport(envelope, response)
|
|
125
|
+
else
|
|
126
|
+
raise ProviderError.new("Tradevan returned an unknown Success value: " \
|
|
127
|
+
"#{envelope['Success'].inspect}", provider: PROVIDER, raw: envelope)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# +Success: "N"+ carries an encrypted Message, like a success does.
|
|
132
|
+
# The decrypted +Message+. When it cannot be read, the envelope tells
|
|
133
|
+
# nothing, so HTTP 429 and 5xx decide, as for any other body.
|
|
134
|
+
def message(envelope, response)
|
|
135
|
+
Crypto.decrypt_message(envelope["Message"], @key1)
|
|
136
|
+
rescue ProviderError
|
|
137
|
+
raise_for_status!(response)
|
|
138
|
+
raise
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def raise_failure(envelope, response)
|
|
142
|
+
result = message(envelope, response)
|
|
143
|
+
reason = result.is_a?(Hash) ? result["failReason"].to_s : ""
|
|
144
|
+
raise_business(reason, result)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# +Success: "E"+ carries a plain text Message. Without readable text,
|
|
148
|
+
# the envelope tells nothing, so HTTP 429 and 5xx decide. Otherwise it
|
|
149
|
+
# is a plain ProviderError: an object such as {"password" => nil} must
|
|
150
|
+
# not reach the patterns, which would read it as a credential error.
|
|
151
|
+
def raise_transport(envelope, response)
|
|
152
|
+
message = envelope["Message"]
|
|
153
|
+
unless message.is_a?(String) && !message.strip.empty?
|
|
154
|
+
raise_for_status!(response)
|
|
155
|
+
raise ProviderError.new("Tradevan returned an error without a readable message",
|
|
156
|
+
provider: PROVIDER, raw_code: "E", raw: envelope)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
code, reason = Errors.classify_transport(message)
|
|
160
|
+
raise Einvoice::Error.for(
|
|
161
|
+
code, message, provider: PROVIDER, reason: reason, raw_code: "E", raw_message: message, raw: envelope
|
|
162
|
+
)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def raise_business(message, raw)
|
|
166
|
+
code, reason = Errors.classify(message)
|
|
167
|
+
raise Einvoice::Error.for(
|
|
168
|
+
code, message.empty? ? "Tradevan returned an error" : message,
|
|
169
|
+
provider: PROVIDER, reason: reason, raw_code: "N", raw_message: message, raw: raw
|
|
170
|
+
)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
module Einvoice
|
|
7
|
+
module Tradevan
|
|
8
|
+
module DefaultAPI
|
|
9
|
+
# The DEFAULTAPI cipher, ported from the 1.x client (which ran in
|
|
10
|
+
# production for years): AES-128-CBC with the key reused as the IV, no
|
|
11
|
+
# PKCS padding, the plain text padded with NUL bytes, and strict Base64.
|
|
12
|
+
#
|
|
13
|
+
# A request carries one parameter, +v+. Each API parameter is encrypted
|
|
14
|
+
# with +key1+, the result is wrapped with the plain +acnt+ / +acntp+ in a
|
|
15
|
+
# JSON object, and that object is encrypted with +key2+. A response
|
|
16
|
+
# +Message+ is decrypted with +key1+: the 1.x code did so, and Tradevan's
|
|
17
|
+
# own document (which says +key2+) was wrong on this point.
|
|
18
|
+
module Crypto
|
|
19
|
+
BLOCK = 16
|
|
20
|
+
|
|
21
|
+
module_function
|
|
22
|
+
|
|
23
|
+
# Encrypt +plaintext+ with +key+ and return strict Base64.
|
|
24
|
+
def encrypt(plaintext, key)
|
|
25
|
+
cipher = new_cipher(key, :encrypt)
|
|
26
|
+
[cipher.update(pad(plaintext.to_s.b)) + cipher.final].pack("m0")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Reverse of {encrypt}. Trailing NUL padding is removed.
|
|
30
|
+
def decrypt(base64, key)
|
|
31
|
+
cipher = new_cipher(key, :decrypt)
|
|
32
|
+
plain = cipher.update(base64.to_s.unpack1("m")) + cipher.final
|
|
33
|
+
plain.sub(/\0+\z/, "").force_encoding(Encoding::UTF_8)
|
|
34
|
+
rescue OpenSSL::Cipher::CipherError, ArgumentError => e
|
|
35
|
+
raise ProviderError.new("Could not decrypt the Tradevan response",
|
|
36
|
+
provider: Client::PROVIDER, cause: e)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Build the +v+ query value for one call. A Hash parameter is sent as
|
|
40
|
+
# JSON; anything else as its string form.
|
|
41
|
+
def encode_params(params, acnt:, acntp:, key1:, key2:)
|
|
42
|
+
encrypted = params.to_h { |name, value| [name.to_s, encrypt(serialize(value), key1)] }
|
|
43
|
+
envelope = { "acnt" => acnt.to_s, "acntp" => acntp.to_s }.merge(encrypted)
|
|
44
|
+
encrypt(JSON.generate(envelope), key2)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Read an encrypted response +Message+ back into a Hash or an Array. Some
|
|
48
|
+
# raw endpoints answer an Array. Anything else, such as +null+, is not a
|
|
49
|
+
# message the adapter can read.
|
|
50
|
+
def decrypt_message(message, key1)
|
|
51
|
+
result = JSON.parse(decrypt(message, key1))
|
|
52
|
+
return result if result.is_a?(Hash) || result.is_a?(Array)
|
|
53
|
+
|
|
54
|
+
raise ProviderError.new("Tradevan returned a message that is not a JSON object or array",
|
|
55
|
+
provider: Client::PROVIDER, raw: result)
|
|
56
|
+
rescue JSON::ParserError => e
|
|
57
|
+
raise ProviderError.new("Tradevan returned a malformed JSON message",
|
|
58
|
+
provider: Client::PROVIDER, cause: e)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Pad with NUL bytes to a whole block. An empty text becomes one block of
|
|
62
|
+
# NULs; a text that already fills whole blocks gets nothing.
|
|
63
|
+
def pad(bytes)
|
|
64
|
+
remainder = bytes.bytesize % BLOCK
|
|
65
|
+
return bytes if remainder.zero? && !bytes.empty?
|
|
66
|
+
|
|
67
|
+
bytes + ("\0" * (BLOCK - remainder))
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def serialize(value)
|
|
71
|
+
value.is_a?(Hash) ? JSON.generate(value) : value.to_s
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def new_cipher(key, direction)
|
|
75
|
+
size = key.to_s.bytesize
|
|
76
|
+
unless size == BLOCK
|
|
77
|
+
raise ValidationError.new("Tradevan keys must be 16 bytes (AES-128-CBC), got #{size}",
|
|
78
|
+
provider: Client::PROVIDER)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
cipher = OpenSSL::Cipher.new("aes-128-cbc").public_send(direction)
|
|
82
|
+
cipher.padding = 0
|
|
83
|
+
cipher.key = key
|
|
84
|
+
cipher.iv = key
|
|
85
|
+
cipher
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Einvoice
|
|
4
|
+
module Tradevan
|
|
5
|
+
module DefaultAPI
|
|
6
|
+
# Maps DEFAULTAPI failures onto the unified {Einvoice::Error} tree.
|
|
7
|
+
#
|
|
8
|
+
# DEFAULTAPI has no error code list. A failure is +Success: "E"+ with a
|
|
9
|
+
# plain text +Message+, or +Success: "N"+ (or an inner +issueStatus+ /
|
|
10
|
+
# +voidStatus+ of +"N"+) with a free text +failReason+. The only failure
|
|
11
|
+
# text on record is +duplicated+, and it comes from a 1.x spec stub, not
|
|
12
|
+
# from a real response. So every pattern below is a guess, kept in one
|
|
13
|
+
# place so that real texts can replace it.
|
|
14
|
+
#
|
|
15
|
+
# Unknown failures fall back to +:provider+, not +:validation+: with so few
|
|
16
|
+
# samples, an unknown failure is more likely a provider problem than bad
|
|
17
|
+
# input.
|
|
18
|
+
module Errors
|
|
19
|
+
# Whole English words only: "keyword" is not about a key.
|
|
20
|
+
CREDENTIALS = /帳號|密碼|金鑰|\b(acntp?|keys?|key[12]|password|account)\b/i
|
|
21
|
+
DUPLICATE = /duplicat|重[複覆]/i
|
|
22
|
+
# Checked before ALREADY_VOID: "已作廢,不能折讓" is a refusal, and the
|
|
23
|
+
# refusal is the part the caller can act on. 不能 must come after 作廢
|
|
24
|
+
# and before 折讓, so "發票不能作廢,因為已折讓" (a blocked void) falls
|
|
25
|
+
# through to ALLOWANCE_MADE.
|
|
26
|
+
BLOCKED_BY_VOID = /作廢.*不能.*折讓/
|
|
27
|
+
ALREADY_VOID = /已(被)?作廢/
|
|
28
|
+
ALLOWANCE_MADE = /已折讓|折讓過/
|
|
29
|
+
MISSING = /查無|不存在|not\s*found/i
|
|
30
|
+
|
|
31
|
+
module_function
|
|
32
|
+
|
|
33
|
+
# +Success: "E"+: the request did not reach the business logic.
|
|
34
|
+
def classify_transport(message)
|
|
35
|
+
return [:auth, Reason::CREDENTIALS_INVALID] if message.to_s.match?(CREDENTIALS)
|
|
36
|
+
|
|
37
|
+
[:provider, nil]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# +Success: "N"+, or an inner status of +"N"+.
|
|
41
|
+
def classify(message)
|
|
42
|
+
case message.to_s
|
|
43
|
+
when DUPLICATE then [:conflict, Reason::DUPLICATE_ORDER]
|
|
44
|
+
when BLOCKED_BY_VOID then [:conflict, Reason::ALLOWANCE_BLOCKED_BY_VOID]
|
|
45
|
+
when ALREADY_VOID then [:conflict, Reason::ALREADY_VOIDED]
|
|
46
|
+
when ALLOWANCE_MADE then [:conflict, Reason::VOID_BLOCKED_BY_ALLOWANCE]
|
|
47
|
+
when MISSING then [:not_found, nil]
|
|
48
|
+
else [:provider, nil]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|