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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.coderabbit.yaml +12 -0
  3. data/.github/workflows/build.yml +7 -4
  4. data/CHANGELOG.md +55 -35
  5. data/Gemfile +3 -2
  6. data/README.md +230 -265
  7. data/README.zh-TW.md +322 -0
  8. data/Rakefile +2 -13
  9. data/bin/console +3 -6
  10. data/einvoice.gemspec +24 -21
  11. data/lib/einvoice/amego/client.rb +116 -0
  12. data/lib/einvoice/amego/errors.rb +84 -0
  13. data/lib/einvoice/amego/payload.rb +276 -0
  14. data/lib/einvoice/amego/provider.rb +267 -0
  15. data/lib/einvoice/amego.rb +27 -0
  16. data/lib/einvoice/capability.rb +79 -0
  17. data/lib/einvoice/ecpay/client.rb +183 -0
  18. data/lib/einvoice/ecpay/crypto.rb +85 -0
  19. data/lib/einvoice/ecpay/errors.rb +115 -0
  20. data/lib/einvoice/ecpay/payload.rb +241 -0
  21. data/lib/einvoice/ecpay/provider.rb +362 -0
  22. data/lib/einvoice/ecpay.rb +31 -0
  23. data/lib/einvoice/error.rb +220 -0
  24. data/lib/einvoice/ezpay/client.rb +130 -0
  25. data/lib/einvoice/ezpay/crypto.rb +85 -0
  26. data/lib/einvoice/ezpay/errors.rb +72 -0
  27. data/lib/einvoice/ezpay/payload.rb +269 -0
  28. data/lib/einvoice/ezpay/provider.rb +235 -0
  29. data/lib/einvoice/ezpay.rb +19 -0
  30. data/lib/einvoice/input.rb +494 -0
  31. data/lib/einvoice/mock_provider.rb +191 -0
  32. data/lib/einvoice/mof/donation_codes.rb +157 -0
  33. data/lib/einvoice/mof.rb +16 -0
  34. data/lib/einvoice/number.rb +51 -0
  35. data/lib/einvoice/provider.rb +57 -14
  36. data/lib/einvoice/redaction.rb +54 -0
  37. data/lib/einvoice/taipei.rb +89 -0
  38. data/lib/einvoice/tax_split.rb +124 -0
  39. data/lib/einvoice/tradevan/default_api/client.rb +175 -0
  40. data/lib/einvoice/tradevan/default_api/crypto.rb +90 -0
  41. data/lib/einvoice/tradevan/default_api/errors.rb +54 -0
  42. data/lib/einvoice/tradevan/default_api/payload.rb +259 -0
  43. data/lib/einvoice/tradevan/default_api/provider.rb +366 -0
  44. data/lib/einvoice/tradevan/default_api/rules.rb +314 -0
  45. data/lib/einvoice/tradevan.rb +25 -0
  46. data/lib/einvoice/types.rb +323 -0
  47. data/lib/einvoice/version.rb +3 -1
  48. data/lib/einvoice.rb +21 -10
  49. metadata +60 -176
  50. data/.github/workflows/cron.yml +0 -31
  51. data/docs/Tradevan E-Invoice APIs.md +0 -201
  52. data/docs/neweb.md +0 -347
  53. data/lib/einvoice/client.rb +0 -21
  54. data/lib/einvoice/configuration.rb +0 -49
  55. data/lib/einvoice/connection.rb +0 -31
  56. data/lib/einvoice/donation_unit_list.json +0 -2022
  57. data/lib/einvoice/result.rb +0 -21
  58. data/lib/einvoice/tradevan/model/base.rb +0 -34
  59. data/lib/einvoice/tradevan/model/issue_data.rb +0 -129
  60. data/lib/einvoice/tradevan/model/issue_item.rb +0 -41
  61. data/lib/einvoice/tradevan/model/void_data.rb +0 -35
  62. data/lib/einvoice/tradevan/provider.rb +0 -140
  63. data/lib/einvoice/tradevan/result.rb +0 -25
  64. data/lib/einvoice/tradevan/validator/issue_data_validator.rb +0 -80
  65. data/lib/einvoice/utils.rb +0 -13
  66. data/lib/faraday/response/decode_tradevan.rb +0 -38
@@ -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], 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, :itemList])
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,140 +0,0 @@
1
- require 'base64'
2
- require 'openssl'
3
-
4
- require "einvoice/utils"
5
-
6
- require "einvoice/tradevan/model/base"
7
- require "einvoice/tradevan/model/issue_data"
8
- require "einvoice/tradevan/model/issue_item"
9
- require "einvoice/tradevan/model/void_data"
10
-
11
- require "einvoice/tradevan/result"
12
-
13
- module Einvoice
14
- module Tradevan
15
- class Provider < Einvoice::Provider
16
- def issue(payload, options = {})
17
- issue_data = Einvoice::Tradevan::Model::IssueData.new
18
- issue_data.from_json(payload.to_json)
19
-
20
- if issue_data.valid?
21
- response = connection.post do |request|
22
- request.url endpoint_url || endpoint + "/DEFAULTAPI/post/issue"
23
- request.params[:v] = encrypted_params(issueData: issue_data.payload)
24
- end.body
25
-
26
- Einvoice::Tradevan::Result.new(response)
27
- else
28
- Einvoice::Tradevan::Result.new(issue_data.errors)
29
- end
30
- end
31
-
32
- def cancel(payload, options = {})
33
- void_data = Einvoice::Tradevan::Model::VoidData.new
34
- void_data.from_json(payload.to_json)
35
-
36
- if void_data.valid?
37
- response = connection.post do |request|
38
- request.url endpoint_url || endpoint + "/DEFAULTAPI/post/cancel"
39
- request.params[:v] = encrypted_params(voidData: void_data.payload)
40
- end.body
41
-
42
- Einvoice::Tradevan::Result.new(response)
43
- else
44
- Einvoice::Tradevan::Result.new(void_data.errors)
45
- end
46
- end
47
-
48
- def search_invoice_by_member_id(payload, options = {})
49
- response = connection.get do |request|
50
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/searchInvoiceByMemberId"
51
- request.params[:v] = encrypted_params(payload)
52
- end.body
53
-
54
- Einvoice::Tradevan::Result.new(response)
55
- end
56
-
57
- def search_invoice_detail(invoice_number)
58
- response = connection.get do |request|
59
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/searchInvoiceDetail"
60
- request.params[:v] = encrypted_params(invoiceNumber: invoice_number)
61
- end.body
62
-
63
- Einvoice::Tradevan::Result.new(response)
64
- end
65
-
66
- def search_invoice_by_transaction_number(payload, options = {})
67
- response = connection.get do |request|
68
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/searchInvoiceInfoByTransactionnumber"
69
- request.params[:v] = encrypted_params(payload.slice(:companyUn, :orgId, :transactionNumber))
70
- end.body
71
-
72
- Einvoice::Tradevan::Result.new(response)
73
- end
74
-
75
- def send_card_info_to_cust(payload, options = {})
76
- response = connection.get do |request|
77
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/sendCardInfotoCust"
78
- request.params[:v] = encrypted_params(payload)
79
- end.body
80
-
81
- Einvoice::Tradevan::Result.new(response)
82
- end
83
-
84
- def get_invoice_mark_info(payload, options = {})
85
- response = connection.get do |request|
86
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/getInvoiceMarkInfo"
87
- request.params[:v] = encrypted_params(payload)
88
- end.body
89
-
90
- Einvoice::Tradevan::Result.new(response)
91
- end
92
-
93
- def get_donate_unit_list(companyUn, options = {})
94
- response = connection.get do |request|
95
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/getDonateUnitList"
96
- request.params[:v] = encrypted_params(companyUn: companyUn)
97
- end.body
98
-
99
- Einvoice::Tradevan::Result.new(response)
100
- end
101
-
102
- def get_invoice_content(payload, options = {})
103
- response = connection.get do |request|
104
- request.url endpoint_url || endpoint + "/DEFAULTAPI/get/getInvoiceContent"
105
- request.params[:v] = encrypted_params(payload)
106
- end.body
107
-
108
- Einvoice::Tradevan::Result.new(response)
109
- end
110
-
111
- private
112
-
113
- def encrypted_params(params)
114
- encrypted_params = params.dup
115
- encrypted_params.each do |key, value|
116
- value = value.is_a?(Hash) ? value.to_json : value.to_s
117
- encrypted_params[key] = encrypt(encryption_keys[:key1], value)
118
- end
119
-
120
- v = { acnt: client_id, acntp: client_secret }.merge(encrypted_params).to_json
121
- encrypt(encryption_keys[:key2], v)
122
- end
123
-
124
- def encrypt(key, content)
125
- cipher = OpenSSL::Cipher::AES.new(128, :CBC)
126
- cipher.encrypt
127
- cipher.key = key
128
- cipher.iv = key
129
- cipher.padding = 0
130
-
131
- # padding with "\u0000"
132
- q, m = content.bytesize.divmod(cipher.block_size)
133
- content_bytes_with_padding = content.bytes.fill(0, content.bytesize..(cipher.block_size * (q + 1) - 1))
134
- content = content_bytes_with_padding.pack('C*').force_encoding('utf-8') if m!= 0 || q == 0
135
-
136
- Base64.strict_encode64(cipher.update(content) + cipher.final)
137
- end
138
- end
139
- end
140
- 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,13 +0,0 @@
1
- require "gyoku"
2
-
3
- module Einvoice
4
- module Utils
5
- def camelize(hash)
6
- hash.deep_transform_keys { |k| k.to_s.camelize }
7
- end
8
-
9
- def encode_xml(hash)
10
- ::Gyoku.xml(hash, key_converter: :none)
11
- end
12
- end
13
- end
@@ -1,38 +0,0 @@
1
- require 'faraday'
2
-
3
- module Faraday
4
- class Response::DecodeTradevan < Faraday::Middleware
5
- dependency do
6
- require 'base64' unless defined?(::Base64)
7
- require 'openssl' unless defined?(::OpenSSL)
8
- end
9
-
10
- def initialize(app, key)
11
- super(app)
12
- @key = key
13
- end
14
-
15
- def call(env)
16
- @app.call(env).on_complete do |env|
17
- if env[:body] && env[:body]['Success'] != 'E'
18
- env[:body]['Message'] = decrypt(@key, env[:body]['Message'])
19
- end
20
- end
21
- end
22
-
23
- private
24
-
25
- def decrypt(key, content)
26
- cipher = OpenSSL::Cipher::AES.new(128, :CBC)
27
- cipher.decrypt
28
- cipher.key = key
29
- cipher.iv = key
30
- cipher.padding = 0
31
-
32
- decrypted = cipher.update(Base64.decode64(content)) + cipher.final
33
- JSON.parse(decrypted.strip)
34
- end
35
- end
36
- end
37
-
38
- Faraday::Response.register_middleware decode_tradevan: Faraday::Response::DecodeTradevan