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
data/lib/einvoice/result.rb
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
module Einvoice
|
|
2
|
-
class Result
|
|
3
|
-
attr_reader :response
|
|
4
|
-
|
|
5
|
-
def initialize(response = nil)
|
|
6
|
-
@response = response
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def errors
|
|
10
|
-
raise NotImplementedError, 'You must initialize one of Einvoice::Response subclasses then use it.'
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def successful?
|
|
14
|
-
raise NotImplementedError, 'You must initialize one of Einvoice::Response subclasses then use it.'
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def data
|
|
18
|
-
raise NotImplementedError, 'You must initialize one of Einvoice::Response subclasses then use it.'
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require "active_model"
|
|
2
|
-
|
|
3
|
-
require "einvoice/tradevan/validator/issue_data_validator"
|
|
4
|
-
|
|
5
|
-
module Einvoice
|
|
6
|
-
module Tradevan
|
|
7
|
-
module Model
|
|
8
|
-
class Base
|
|
9
|
-
include ActiveModel::Model
|
|
10
|
-
include ActiveModel::Validations
|
|
11
|
-
include ActiveModel::Serialization
|
|
12
|
-
include ActiveModel::Serializers::JSON
|
|
13
|
-
|
|
14
|
-
include Einvoice::Tradevan::Validator
|
|
15
|
-
|
|
16
|
-
def attributes=(hash)
|
|
17
|
-
@itemList ||= []
|
|
18
|
-
hash.each do |key, value|
|
|
19
|
-
case key.to_sym
|
|
20
|
-
when :itemList
|
|
21
|
-
value.each { |v| @itemList << IssueItem.new(v) }
|
|
22
|
-
else
|
|
23
|
-
send("#{key}=", value)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def attributes
|
|
29
|
-
instance_values
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
module Einvoice
|
|
2
|
-
module Tradevan
|
|
3
|
-
module Model
|
|
4
|
-
class IssueData < Base
|
|
5
|
-
VALID_OPTIONS_KEYS = [
|
|
6
|
-
:companyUn,
|
|
7
|
-
:orgId,
|
|
8
|
-
:orgUn,
|
|
9
|
-
:type,
|
|
10
|
-
:saleIdentifier,
|
|
11
|
-
:allowanceIdentifier,
|
|
12
|
-
:transactionNumber,
|
|
13
|
-
:transactionDate,
|
|
14
|
-
:transactionTime,
|
|
15
|
-
:total,
|
|
16
|
-
:transactionSource,
|
|
17
|
-
:transactionTarget,
|
|
18
|
-
:invoiceNumber,
|
|
19
|
-
:allowanceNumber,
|
|
20
|
-
:allowanceDate,
|
|
21
|
-
:allowanceExclusiveAmount,
|
|
22
|
-
:allowanceTax,
|
|
23
|
-
:allowancePaperReturned,
|
|
24
|
-
:allowanceInclusiveAmount,
|
|
25
|
-
:paperPrintMode,
|
|
26
|
-
:invoiceAlarmMode,
|
|
27
|
-
:invoicePaperReturned,
|
|
28
|
-
:donate,
|
|
29
|
-
:donationUnit,
|
|
30
|
-
:carrierType,
|
|
31
|
-
:carrierId,
|
|
32
|
-
:carrierIdHidden,
|
|
33
|
-
:buyerUn,
|
|
34
|
-
:buyerTitle,
|
|
35
|
-
:receiverName,
|
|
36
|
-
:receiverAddrZip,
|
|
37
|
-
:receiverAddrRoad,
|
|
38
|
-
:receiverEmail,
|
|
39
|
-
:receiverMobile,
|
|
40
|
-
:allowaDeclaration,
|
|
41
|
-
:idViewId,
|
|
42
|
-
:memberId,
|
|
43
|
-
:checkNumber,
|
|
44
|
-
:invoiceDate,
|
|
45
|
-
:invoiceTime,
|
|
46
|
-
:texclusiveAmount,
|
|
47
|
-
:oeclusiveAmount,
|
|
48
|
-
:zexclusiveAmount,
|
|
49
|
-
:tax,
|
|
50
|
-
:mainRemark,
|
|
51
|
-
:invoiceType,
|
|
52
|
-
:itemList
|
|
53
|
-
].freeze
|
|
54
|
-
|
|
55
|
-
attr_accessor(*VALID_OPTIONS_KEYS)
|
|
56
|
-
|
|
57
|
-
validates :companyUn, presence: true, length: { is: 8 }
|
|
58
|
-
validates :orgId, presence: true, length: { is: 5 }
|
|
59
|
-
validates :orgUn, length: { is: 8 }, allow_blank: true
|
|
60
|
-
validates :type, presence: true, length: { is: 1 }, inclusion: { in: %w(I R G H A) }
|
|
61
|
-
validates :transactionNumber, presence: true, length: { maximum: 50 }
|
|
62
|
-
validates :transactionDate, presence: true, length: { is: 8 }, format: { with: /\A\d{8}\Z/ }
|
|
63
|
-
validates :transactionSource, length: { maximum: 50 } # Public Affair Firm presence: true
|
|
64
|
-
validates :transactionTarget, length: { maximum: 50 } # Public Affair Firm presence: true
|
|
65
|
-
validates :paperPrintMode, presence: true, length: { is: 1 }, inclusion: { in: %w(0 1 2 3 4) }
|
|
66
|
-
validates :invoiceAlarmMode, presence: true, length: { is: 1 }, inclusion: { in: %w(0 1 2 3 4 5 6) }
|
|
67
|
-
validates :invoicePaperReturned, presence: true, length: { maximum: 1 }, inclusion: { in: %w(Y N) }, invoicePaperReturned: true, if: proc { self.type == 'A' || (self.buyerUn.blank? && self.paperPrintMode.to_i != 0) }
|
|
68
|
-
validates :carrierType, presence: true, length: { is: 6 }, if: proc { self.paperPrintMode.to_i == 0 && self.donate == 'N' && self.type != 'A' }
|
|
69
|
-
validates :buyerUn, length: { is: 8 }, allow_blank: true, unless: proc { %w(A H).include?(self.type) }
|
|
70
|
-
validates :buyerTitle, length: { maximum: 60 }, allow_blank: true, unless: proc { %w(A H).include?(self.type) }
|
|
71
|
-
validates :idViewId, allow_blank: true, length: { maximum: 10 }
|
|
72
|
-
validates :memberId, allow_blank: true, length: { maximum: 50 }
|
|
73
|
-
validates :itemList, presence: true, itemList: true
|
|
74
|
-
|
|
75
|
-
# Type I R G H
|
|
76
|
-
validates :saleIdentifier, presence: true, length: { maximum: 100 }, saleIdentifier: true, if: proc { %w(I R G H).include?(self.type) }
|
|
77
|
-
|
|
78
|
-
# Type A H
|
|
79
|
-
validates :allowanceIdentifier, presence: true, length: { maximum: 100 }, allowanceIdentifier: true, if: proc { %w(A H).include?(self.type) }
|
|
80
|
-
validates :allowanceExclusiveAmount, presence: true, length: { maximum: 20 }, if: proc { %w(A H).include?(self.type) }
|
|
81
|
-
validates :allowanceTax, presence: true, length: { maximum: 8 }, if: proc { %w(A H).include?(self.type) }
|
|
82
|
-
validates :allowancePaperReturned, presence: true, length: { is: 1 }, inclusion: { in: %w(Y N) }, if: proc { %w(A H).include?(self.type) }
|
|
83
|
-
validates :allowanceInclusiveAmount, presence: true, length: { maximum: 20 }, if: proc { %w(A H).include?(self.type) }
|
|
84
|
-
|
|
85
|
-
# Type R G H
|
|
86
|
-
validates :invoiceNumber, presence: true, length: { is: 10 }, if: proc { %w(R G H).include?(self.type) }
|
|
87
|
-
|
|
88
|
-
# Type H
|
|
89
|
-
validates :allowanceNumber, presence: true, length: { is: 16 }, allowanceNumber: true, if: proc { self.type == 'H' }
|
|
90
|
-
validates :allowanceDate, presence: true, length: { is: 8 }, format: { with: /\A\d{8}\Z/ }, if: proc { self.type == 'H' }
|
|
91
|
-
|
|
92
|
-
# Type A
|
|
93
|
-
validates :allowaDeclaration, allow_blank: true, length: { is: 6 }, format: { with: /\A\d{6}\Z/ }, if: proc { self.type == 'A' }
|
|
94
|
-
|
|
95
|
-
# Type I R G
|
|
96
|
-
validates :transactionTime, presence: true, length: { is: 8 }, format: { with: /\A\d{2}\:\d{2}\:\d{2}\Z/ }, if: proc { %w(I R G).include?(self.type) }
|
|
97
|
-
validates :total, presence: true, length: { maximum: 20 }, total: true, if: proc { %w(I R G).include?(self.type) }
|
|
98
|
-
validates :donate, presence: true, length: { is: 1 }, if: proc { %w(I R G).include?(self.type) && self.buyerUn.blank? && self.paperPrintMode.to_i == 0 }
|
|
99
|
-
validates :donationUnit, presence: true, length: { maximum: 10 }, donationUnit: true, if: proc { %w(I R G).include?(self.type) && self.donate == 'Y' }
|
|
100
|
-
validates :carrierId, presence: true, length: { maximum: 64 }, if: proc { %w(I R G).include?(self.type) && self.paperPrintMode.to_i == 0 && self.donate == 'N' }
|
|
101
|
-
validates :carrierIdHidden, presence: true, length: { maximum: 64 }, if: proc { %w(I R G).include?(self.type) && self.paperPrintMode.to_i == 0 && self.donate == 'N' }
|
|
102
|
-
|
|
103
|
-
# Type I R G A
|
|
104
|
-
validates :receiverName, allow_blank: true, length: { maximum: 30 }, if: proc { %w(I R G A).include?(self.type) }
|
|
105
|
-
validates :receiverAddrZip, allow_blank: true, length: { maximum: 6 }, if: proc { %w(I R G A).include?(self.type) }
|
|
106
|
-
validates :receiverAddrRoad, allow_blank: true, length: { maximum: 100 }, if: proc { %w(I R G A).include?(self.type) }
|
|
107
|
-
validates :receiverEmail, allow_blank: true, length: { maximum: 80 }, if: proc { %w(I R G A).include?(self.type) }
|
|
108
|
-
validates :receiverMobile, allow_blank: true, length: { maximum: 15 }, if: proc { %w(I R G A).include?(self.type) }
|
|
109
|
-
|
|
110
|
-
# Type I G
|
|
111
|
-
validates :tax, allow_blank: true, length: { maximum: 20 }, if: proc { %w(I G).include?(self.type) }
|
|
112
|
-
|
|
113
|
-
# Type G
|
|
114
|
-
validates :checkNumber, allow_blank: true, length: { is: 4 }, if: proc { self.type == 'G' }
|
|
115
|
-
validates :invoiceDate, allow_blank: true, length: { is: 8 }, if: proc { self.type == 'G' }
|
|
116
|
-
validates :invoiceTime, allow_blank: true, length: { is: 8 }, if: proc { self.type == 'G' }
|
|
117
|
-
validates :texclusiveAmount, allow_blank: true, length: { maximum: 20 }, if: proc { self.type == 'G' }
|
|
118
|
-
validates :oeclusiveAmount, allow_blank: true, length: { maximum: 20 }, if: proc { self.type == 'G' }
|
|
119
|
-
validates :zexclusiveAmount, allow_blank: true, length: { maximum: 20 }, if: proc { self.type == 'G' }
|
|
120
|
-
validates :mainRemark, allow_blank: true, length: { maximum: 300 }, if: proc { self.type == 'G' }
|
|
121
|
-
validates :invoiceType, allow_blank: true, length: { is: 2 }, inclusion: { in: %w(07 08) }, if: proc { self.type == 'G' }
|
|
122
|
-
|
|
123
|
-
def payload
|
|
124
|
-
serializable_hash(except: [:errors, :validation_context, :context_for_validation], include: [:itemList])
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
module Einvoice
|
|
2
|
-
module Tradevan
|
|
3
|
-
module Model
|
|
4
|
-
class IssueItem < Base
|
|
5
|
-
VALID_OPTIONS_KEYS = [
|
|
6
|
-
:saleIdentifier, # Same as invoice.saleIdentifier
|
|
7
|
-
:serialNumber,
|
|
8
|
-
:invoiceNumber,
|
|
9
|
-
:invoiceDate,
|
|
10
|
-
:invoiceTime,
|
|
11
|
-
:productCode,
|
|
12
|
-
:productName,
|
|
13
|
-
:qty,
|
|
14
|
-
:price,
|
|
15
|
-
:tax,
|
|
16
|
-
:itemExclude,
|
|
17
|
-
:itemTotal,
|
|
18
|
-
:taxType,
|
|
19
|
-
:description
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
attr_accessor(*VALID_OPTIONS_KEYS)
|
|
23
|
-
|
|
24
|
-
validates :saleIdentifier, presence: true, length: { maximum: 100 }
|
|
25
|
-
validates :serialNumber, presence: true, length: { is: 4 }, numericality: true
|
|
26
|
-
validates :invoiceNumber, allow_blank: true, length: { is: 10 }
|
|
27
|
-
validates :invoiceDate, allow_blank: true, length: { is: 8 }, numericality: true
|
|
28
|
-
validates :invoiceTime, allow_blank: true, length: { is: 8 }, format: { with: /\A\d{2}\:\d{2}\:\d{2}\Z/ }
|
|
29
|
-
validates :productCode, allow_blank: true, length: { maximum: 30 }
|
|
30
|
-
validates :productName, presence: true, length: { maximum: 300 }
|
|
31
|
-
validates :qty, presence: true, length: { maximum: 20 }
|
|
32
|
-
validates :price, presence: true, length: { maximum: 20 }
|
|
33
|
-
validates :tax, allow_blank: true, length: { maximum: 20 }
|
|
34
|
-
validates :itemExclude, allow_blank: true, length: { maximum: 20 }
|
|
35
|
-
validates :itemTotal, allow_blank: true, length: { maximum: 20 }
|
|
36
|
-
validates :taxType, allow_blank: true, length: { is: 1 }, inclusion: { in: %w(T O Z) }
|
|
37
|
-
validates :description, allow_blank: true, length: { maximum: 300 }
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
module Einvoice
|
|
2
|
-
module Tradevan
|
|
3
|
-
module Model
|
|
4
|
-
class VoidData < Base
|
|
5
|
-
VALID_OPTIONS_KEYS = [
|
|
6
|
-
:type,
|
|
7
|
-
:saleIdentifier,
|
|
8
|
-
:invoiceNumber,
|
|
9
|
-
:invoicePaperReturned,
|
|
10
|
-
:allowanceNumber,
|
|
11
|
-
:allowancePaperReturned,
|
|
12
|
-
:companyUn
|
|
13
|
-
].freeze
|
|
14
|
-
|
|
15
|
-
attr_accessor(*VALID_OPTIONS_KEYS)
|
|
16
|
-
|
|
17
|
-
validates :type, presence: true, length: { is: 1 }, inclusion: { in: %w(C I A) }
|
|
18
|
-
|
|
19
|
-
# Type C I
|
|
20
|
-
validates :saleIdentifier, presence: true, length: { maximum: 100 }, if: proc { %w(C I).include?(self.type) }
|
|
21
|
-
validates :invoiceNumber, presence: true, length: { is: 10 }, if: proc { %w(C I).include?(self.type) }
|
|
22
|
-
validates :invoicePaperReturned, presence: true, length: { maximum: 1 }, inclusion: { in: %w(Y N) }, if: proc { %w(C I).include?(self.type) }
|
|
23
|
-
|
|
24
|
-
# Type A
|
|
25
|
-
validates :companyUn, presence: true, length: { is: 8 }, if: proc { self.type == 'A' }
|
|
26
|
-
validates :allowanceNumber, presence: true, length: { is: 16 }, if: proc { self.type == 'A' }
|
|
27
|
-
validates :allowancePaperReturned, presence: true, length: { is: 1 }, inclusion: { in: %w(Y N) }, if: proc { self.type == 'A' }
|
|
28
|
-
|
|
29
|
-
def payload
|
|
30
|
-
serializable_hash(except: [:errors, :validation_context, :context_for_validation, :itemList])
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
require 'base64'
|
|
2
|
-
require 'openssl'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require "einvoice/tradevan/model/base"
|
|
6
|
-
require "einvoice/tradevan/model/issue_data"
|
|
7
|
-
require "einvoice/tradevan/model/issue_item"
|
|
8
|
-
require "einvoice/tradevan/model/void_data"
|
|
9
|
-
|
|
10
|
-
require "einvoice/tradevan/result"
|
|
11
|
-
|
|
12
|
-
module Einvoice
|
|
13
|
-
module Tradevan
|
|
14
|
-
class Provider < Einvoice::Provider
|
|
15
|
-
def issue(payload, options = {})
|
|
16
|
-
issue_data = Einvoice::Tradevan::Model::IssueData.new
|
|
17
|
-
issue_data.from_json(payload.to_json)
|
|
18
|
-
|
|
19
|
-
if issue_data.valid?
|
|
20
|
-
response = connection.post do |request|
|
|
21
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/post/issue"
|
|
22
|
-
request.params[:v] = encrypted_params(issueData: issue_data.payload)
|
|
23
|
-
end.body
|
|
24
|
-
|
|
25
|
-
Einvoice::Tradevan::Result.new(response)
|
|
26
|
-
else
|
|
27
|
-
Einvoice::Tradevan::Result.new(issue_data.errors)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def cancel(payload, options = {})
|
|
32
|
-
void_data = Einvoice::Tradevan::Model::VoidData.new
|
|
33
|
-
void_data.from_json(payload.to_json)
|
|
34
|
-
|
|
35
|
-
if void_data.valid?
|
|
36
|
-
response = connection.post do |request|
|
|
37
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/post/cancel"
|
|
38
|
-
request.params[:v] = encrypted_params(voidData: void_data.payload)
|
|
39
|
-
end.body
|
|
40
|
-
|
|
41
|
-
Einvoice::Tradevan::Result.new(response)
|
|
42
|
-
else
|
|
43
|
-
Einvoice::Tradevan::Result.new(void_data.errors)
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def search_invoice_by_member_id(payload, options = {})
|
|
48
|
-
response = connection.get do |request|
|
|
49
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/searchInvoiceByMemberId"
|
|
50
|
-
request.params[:v] = encrypted_params(payload)
|
|
51
|
-
end.body
|
|
52
|
-
|
|
53
|
-
Einvoice::Tradevan::Result.new(response)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def search_invoice_detail(invoice_number)
|
|
57
|
-
response = connection.get do |request|
|
|
58
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/searchInvoiceDetail"
|
|
59
|
-
request.params[:v] = encrypted_params(invoiceNumber: invoice_number)
|
|
60
|
-
end.body
|
|
61
|
-
|
|
62
|
-
Einvoice::Tradevan::Result.new(response)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def search_invoice_by_transaction_number(payload, options = {})
|
|
66
|
-
response = connection.get do |request|
|
|
67
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/searchInvoiceInfoByTransactionnumber"
|
|
68
|
-
request.params[:v] = encrypted_params(payload.slice(:companyUn, :orgId, :transactionNumber))
|
|
69
|
-
end.body
|
|
70
|
-
|
|
71
|
-
Einvoice::Tradevan::Result.new(response)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def send_card_info_to_cust(payload, options = {})
|
|
75
|
-
response = connection.get do |request|
|
|
76
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/sendCardInfotoCust"
|
|
77
|
-
request.params[:v] = encrypted_params(payload)
|
|
78
|
-
end.body
|
|
79
|
-
|
|
80
|
-
Einvoice::Tradevan::Result.new(response)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def get_invoice_mark_info(payload, options = {})
|
|
84
|
-
response = connection.get do |request|
|
|
85
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/getInvoiceMarkInfo"
|
|
86
|
-
request.params[:v] = encrypted_params(payload)
|
|
87
|
-
end.body
|
|
88
|
-
|
|
89
|
-
Einvoice::Tradevan::Result.new(response)
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def get_donate_unit_list(companyUn, options = {})
|
|
93
|
-
response = connection.get do |request|
|
|
94
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/getDonateUnitList"
|
|
95
|
-
request.params[:v] = encrypted_params(companyUn: companyUn)
|
|
96
|
-
end.body
|
|
97
|
-
|
|
98
|
-
Einvoice::Tradevan::Result.new(response)
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def get_invoice_content(payload, options = {})
|
|
102
|
-
response = connection.get do |request|
|
|
103
|
-
request.url endpoint_url || endpoint + "/DEFAULTAPI/get/getInvoiceContent"
|
|
104
|
-
request.params[:v] = encrypted_params(payload)
|
|
105
|
-
end.body
|
|
106
|
-
|
|
107
|
-
Einvoice::Tradevan::Result.new(response)
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
private
|
|
111
|
-
|
|
112
|
-
def encrypted_params(params)
|
|
113
|
-
encrypted_params = params.dup
|
|
114
|
-
encrypted_params.each do |key, value|
|
|
115
|
-
value = value.is_a?(Hash) ? value.to_json : value.to_s
|
|
116
|
-
encrypted_params[key] = encrypt(encryption_keys[:key1], value)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
v = { acnt: client_id, acntp: client_secret }.merge(encrypted_params).to_json
|
|
120
|
-
encrypt(encryption_keys[:key2], v)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def encrypt(key, content)
|
|
124
|
-
cipher = OpenSSL::Cipher::AES.new(128, :CBC)
|
|
125
|
-
cipher.encrypt
|
|
126
|
-
cipher.key = key
|
|
127
|
-
cipher.iv = key
|
|
128
|
-
cipher.padding = 0
|
|
129
|
-
|
|
130
|
-
# padding with "\u0000"
|
|
131
|
-
q, m = content.bytesize.divmod(cipher.block_size)
|
|
132
|
-
content_bytes_with_padding = content.bytes.fill(0, content.bytesize..(cipher.block_size * (q + 1) - 1))
|
|
133
|
-
content = content_bytes_with_padding.pack('C*').force_encoding('utf-8') if m!= 0 || q == 0
|
|
134
|
-
|
|
135
|
-
Base64.strict_encode64(cipher.update(content) + cipher.final)
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
end
|
|
139
|
-
end
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
module Einvoice
|
|
2
|
-
module Tradevan
|
|
3
|
-
class Result < Einvoice::Result
|
|
4
|
-
def errors
|
|
5
|
-
if response.is_a? ActiveModel::Errors
|
|
6
|
-
response.full_messages.join('; ')
|
|
7
|
-
else
|
|
8
|
-
response && response["Message"]
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def successful?
|
|
13
|
-
response && !response.is_a?(ActiveModel::Errors) && response["Success"] == 'Y'
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def data
|
|
17
|
-
if response.is_a? ActiveModel::Errors
|
|
18
|
-
nil
|
|
19
|
-
else
|
|
20
|
-
response && response["Message"]
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
module Einvoice
|
|
2
|
-
module Tradevan
|
|
3
|
-
module Validator
|
|
4
|
-
class SaleIdentifierValidator < ActiveModel::EachValidator
|
|
5
|
-
def validate_each(record, attribute, value)
|
|
6
|
-
unless record.saleIdentifier =~ Regexp.new("\\A#{record.companyUn}_#{record.orgId}_")
|
|
7
|
-
record.errors.add attribute, options[:message] || :invalid
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
class AllowanceIdentifierValidator < ActiveModel::EachValidator
|
|
13
|
-
def validate_each(record, attribute, value)
|
|
14
|
-
unless record.allowanceIdentifier =~ Regexp.new("\\A#{record.companyUn}_#{record.orgId}_")
|
|
15
|
-
record.errors.add attribute, options[:message] || :invalid
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
class TotalValidator < ActiveModel::EachValidator
|
|
21
|
-
def validate_each(record, attribute, value)
|
|
22
|
-
if value.to_i != record.itemList.map(&:itemTotal).map(&:to_i).sum
|
|
23
|
-
record.errors.add attribute, options[:message] || :invalid
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
class AllowanceNumberValidator < ActiveModel::EachValidator
|
|
29
|
-
def validate_each(record, attribute, value)
|
|
30
|
-
unless record.allowanceNumber =~ Regexp.new("\\A#{record.orgId}")
|
|
31
|
-
record.errors.add attribute, options[:message] || :invalid
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
class InvoicePaperReturnedValidator < ActiveModel::EachValidator
|
|
37
|
-
def validate_each(record, attribute, value)
|
|
38
|
-
if value.blank? && record.buyerUn.blank? && record.paperPrintMode != '0'
|
|
39
|
-
record.errors.add attribute, options[:message] || :invalid
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
class DonationUnitValidator < ActiveModel::EachValidator
|
|
45
|
-
def validate_each(record, attribute, value)
|
|
46
|
-
donation_unit_list_file = [File.expand_path('../../../', __FILE__), "/donation_unit_list.json"].join
|
|
47
|
-
units = JSON.parse(File.read(donation_unit_list_file))
|
|
48
|
-
|
|
49
|
-
unless units[value]
|
|
50
|
-
record.errors.add attribute, options[:message] || :invalid
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
class ItemListValidator < ActiveModel::EachValidator
|
|
56
|
-
def validate_each(record, attribtue, value)
|
|
57
|
-
if record.itemList.map(&:itemTotal).map(&:blank?).reduce(&:|)
|
|
58
|
-
record.errors.add :itemList, options[:message] || :invalid
|
|
59
|
-
elsif record.itemList.map(&:taxType).map(&:blank?).reduce(&:|)
|
|
60
|
-
record.errors.add :itemList, options[:message] || :invalid
|
|
61
|
-
else
|
|
62
|
-
# none
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
if %w(A H).include?(record.type)
|
|
66
|
-
if record.itemList.map(&:invoiceNumber).map(&:blank?).reduce(&:|)
|
|
67
|
-
record.errors.add :itemList, options[:message] || :invalid
|
|
68
|
-
elsif record.itemList.map(&:invoiceDate).map(&:blank?).reduce(&:|)
|
|
69
|
-
record.errors.add :itemList, options[:message] || :invalid
|
|
70
|
-
elsif record.itemList.map(&:itemExclude).map(&:blank?).reduce(&:|)
|
|
71
|
-
record.errors.add :itemList, options[:message] || :invalid
|
|
72
|
-
else
|
|
73
|
-
# none
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
require 'base64'
|
|
2
|
-
require 'json'
|
|
3
|
-
require 'openssl'
|
|
4
|
-
|
|
5
|
-
require 'faraday'
|
|
6
|
-
|
|
7
|
-
module Faraday
|
|
8
|
-
class Response::DecodeTradevan < Faraday::Middleware
|
|
9
|
-
def initialize(app, key)
|
|
10
|
-
super(app)
|
|
11
|
-
@key = key
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def call(env)
|
|
15
|
-
@app.call(env).on_complete do |env|
|
|
16
|
-
if env[:body] && env[:body]['Success'] != 'E'
|
|
17
|
-
env[:body]['Message'] = decrypt(@key, env[:body]['Message'])
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
private
|
|
23
|
-
|
|
24
|
-
def decrypt(key, content)
|
|
25
|
-
cipher = OpenSSL::Cipher::AES.new(128, :CBC)
|
|
26
|
-
cipher.decrypt
|
|
27
|
-
cipher.key = key
|
|
28
|
-
cipher.iv = key
|
|
29
|
-
cipher.padding = 0
|
|
30
|
-
|
|
31
|
-
decrypted = cipher.update(Base64.decode64(content)) + cipher.final
|
|
32
|
-
JSON.parse(decrypted.strip)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
Faraday::Response.register_middleware decode_tradevan: Faraday::Response::DecodeTradevan
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
require 'faraday'
|
|
2
|
-
require 'multi_xml'
|
|
3
|
-
|
|
4
|
-
module Faraday
|
|
5
|
-
class Response::ParseXml < Faraday::Middleware
|
|
6
|
-
def on_complete(env)
|
|
7
|
-
return unless env[:body].is_a?(String) && !env[:body].strip.empty?
|
|
8
|
-
|
|
9
|
-
env[:body] = ::MultiXml.parse(env[:body])
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
Faraday::Response.register_middleware xml: Faraday::Response::ParseXml
|