diff --git a/ecpay.gemspec b/ecpay.gemspec deleted file mode 100644 index b82b5dd..0000000 --- a/ecpay.gemspec +++ /dev/null @@ -1,25 +0,0 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'ecpay_invoice/version' - -Gem::Specification.new do |spec| - spec.name = "ecpay_invoice" - spec.version = ECpayInvoice::VERSION - spec.authors = ["Ying Wu"] - spec.email = ["ying.wu@ecpay.com.tw"] - - spec.summary = "綠界電子發票串接用SDK" - spec.description = "" - spec.homepage = "" - spec.license = "MIT" - - spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - spec.bindir = "bin" - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] - - spec.add_development_dependency "bundler", "~> 1.12" - spec.add_development_dependency "rake", "~> 11.1" - spec.add_development_dependency "rspec", "~> 3.4" -end diff --git a/ecpay_invoice-2.0.0/Gemfile b/ecpay_invoice-2.0.0/Gemfile deleted file mode 100644 index 0023fb0..0000000 --- a/ecpay_invoice-2.0.0/Gemfile +++ /dev/null @@ -1,4 +0,0 @@ -source 'https://rubygems.org' - -# Specify your gem's dependencies in ecpay.gemspec -gemspec diff --git a/ecpay_invoice-2.0.0/Rakefile b/ecpay_invoice-2.0.0/Rakefile deleted file mode 100644 index b7e9ed5..0000000 --- a/ecpay_invoice-2.0.0/Rakefile +++ /dev/null @@ -1,6 +0,0 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" - -RSpec::Core::RakeTask.new(:spec) - -task :default => :spec diff --git a/ecpay_invoice-2.0.0/conf/invoice_conf.xml b/ecpay_invoice-2.0.0/conf/invoice_conf.xml deleted file mode 100644 index 87e7327..0000000 --- a/ecpay_invoice-2.0.0/conf/invoice_conf.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - Test - Stage_Account - N - - - - - - - - - 2000132 - ejCk326UnaZWKisg - q9jcZX8Ib9LM8wYk - - - - - - - - diff --git a/ecpay_invoice-2.0.0/ecpay.gemspec b/ecpay_invoice-2.0.0/ecpay.gemspec deleted file mode 100644 index c355bd6..0000000 --- a/ecpay_invoice-2.0.0/ecpay.gemspec +++ /dev/null @@ -1,25 +0,0 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'ecpay_invoice/version' - -Gem::Specification.new do |spec| - spec.name = "ecpay_invoice" - spec.version = ECpayInvoice::VERSION - spec.authors = ["Ying Wu"] - spec.email = ["ying.wu@ecpay.com.tw"] - - spec.summary = "綠界電子發票串接用SDK" - spec.description = "" - spec.homepage = "" - spec.license = "MIT" - - spec.files = Dir["Rakefile", "ecpay.gemspec", "Gemfile", "{lib,conf}/**/*"] - spec.bindir = "bin" - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] - - spec.add_development_dependency "bundler", "~> 1.12" - spec.add_development_dependency "rake", "~> 11.1" - spec.add_development_dependency "rspec", "~> 3.4" -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice.rb deleted file mode 100644 index b4d6022..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "ecpay_invoice/version" -require "ecpay_invoice/invoice_client" -require "ecpay_invoice/query_client" -require "ecpay_invoice/notify_client" - -module ECpayInvoice -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/ECpayInvoice.xml b/ecpay_invoice-2.0.0/lib/ecpay_invoice/ECpayInvoice.xml deleted file mode 100644 index b96aea9..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/ECpayInvoice.xml +++ /dev/null @@ -1,565 +0,0 @@ - - - - - https://einvoice.ecpay.com.tw/Invoice/Issue - https://einvoice-stage.ecpay.com.tw/Invoice/Issue - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^.{1,30}$ - - - ^([\w_]{1,20}|)$ - - - ^(\d{8}|)$ - - - ^.{0,60}$ - - - ^.{0,200}$ - - - ^(\d{10,20}|)$ - - - ^.{0,200}$ - - - - - - - - - - - - - - - - ^([Xx]\d{2,6}|\d{3,7}|)$ - - - - - - - - - - - - - - - - - GE - - 1 - - - ^.{0,4096}$ - - - ^.{0,4096}$ - - - ^[\d\|]{0,4096}$ - - - ^.{0,4096}$ - - - ^{0,4096}$ - - - ^([1-3\|]{0,4096}|)$ - - - ^{0,4096}$ - - - ^.{0,4096}$ - - - - - - - - - - - - - - https://einvoice.ecpay.com.tw/Invoice/DelayIssue - https://einvoice-stage.ecpay.com.tw/Invoice/DelayIssue - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^.{1,30}$ - - - ^([\w_]{1,20}|)$ - - - ^(\d{8}|)$ - - - ^.{0,60}$ - - - ^.{0,200}$ - - - ^(\d{10,20}|)$ - - - ^.{0,200}$ - - - - - - - - - - - - - - - - ^([Xx]\d{2,6}|\d{3,7}|)$ - - - - - - - - - - - - - - - - - GE - - 1 - - - ^.{0,4096}$ - - - ^.{0,4096}$$ - - - ^[\d\|]{0,4096}$ - - - ^.{0,4096}$ - - - ^{0,4096}$ - - - ^.{0,4096}$ - - - ^{0,4096}$ - - - - - - - - - - - ^\d{1,15}$ - - - ^.{1,30}$ - - - ^[2]$ - - - ^ECPAY$ - - - ^.{0,200}$ - - - - - - https://einvoice.ecpay.com.tw/Invoice/TriggerIssue - https://einvoice-stage.ecpay.com.tw/Invoice/TriggerIssue - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^.{1,30}$ - - - ^[2]$ - - - - - - https://einvoice.ecpay.com.tw/Invoice/Allowance - https://einvoice-stage.ecpay.com.tw/Invoice/Allowance - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^[A-Z]{2}\d{8}$ - - - - - - - - - ^.{0,60}$ - - - ^.{0,100}$ - - - ^(\d{10,20}|)$ - - - GE - - 1 - - - ^.{0,4096}$ - - - ^[\d\|]{0,4096}$ - - - ^.{0,4096}$ - - - ^{0,4096}$ - - - ^.{0,4096}$$ - - - ^{0,4096}$ - - - - - - https://einvoice.ecpay.com.tw/Invoice/IssueInvalid - https://einvoice-stage.ecpay.com.tw/Invoice/IssueInvalid - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^[A-Z]{2}\d{8}$ - - - ^.{0,4096}$ - - - - - - https://einvoice.ecpay.com.tw/Invoice/AllowanceInvalid - https://einvoice-stage.ecpay.com.tw/Invoice/AllowanceInvalid - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^[A-Z]{2}\d{8}$ - - - ^\d{16}$ - - - ^.{0,4096}$ - - - - - - https://einvoice.ecpay.com.tw/Query/Issue - https://einvoice-stage.ecpay.com.tw/Query/Issue - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^.{0,30}$ - - - - - - https://einvoice.ecpay.com.tw/Query/Allowance - https://einvoice-stage.ecpay.com.tw/Query/Allowance - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^[A-Z]{2}\d{8}$ - - - ^\d{16}$ - - - - - - https://einvoice.ecpay.com.tw/Query/IssueInvalid - https://einvoice-stage.ecpay.com.tw/Query/IssueInvalid - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^.{0,30}$ - - - - - - https://einvoice.ecpay.com.tw/Query/AllowanceInvalid - https://einvoice-stage.ecpay.com.tw/Query/AllowanceInvalid - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^[A-Z]{2}\d{8}$ - - - ^\d{16}$ - - - - - - https://einvoice.ecpay.com.tw/Query/CheckMobileBarCode - https://einvoice-stage.ecpay.com.tw/Query/CheckMobileBarCode - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^\/[A-Za-z0-9\ \-\.\+]{7}$ - - - - - - https://einvoice.ecpay.com.tw/Query/CheckLoveCode - https://einvoice-stage.ecpay.com.tw/Query/CheckLoveCode - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^([Xx]\d{2,6}|\d{3,7})$ - - - - - - https://einvoice.ecpay.com.tw/Notify/InvoiceNotify - https://einvoice-stage.ecpay.com.tw/Notify/InvoiceNotify - - - - - - - - - - ^\d{10}$ - - - ^\d{7,10}$ - - - ^[A-Z]{2}\d{8}$ - - - - - ^(\d{10,20}|)$ - - - ^.{0,80}$ - - - - - - - - - - - - - - - - - - - - - diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/core_ext/hash.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/core_ext/hash.rb deleted file mode 100644 index 88a30fd..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/core_ext/hash.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Hash - def stringify_keys - result = self.class.new - orig_key_len = keys.length() - each_key do |key| - result[key.to_s] = self[key] - end - unless result.keys.length() == orig_key_len - raise "Duplicate key name during convertion, there might be symbol & string key with the same name." - end - return result - end -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/core_ext/string.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/core_ext/string.rb deleted file mode 100644 index d66a6f5..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/core_ext/string.rb +++ /dev/null @@ -1,5 +0,0 @@ -class String - def hashify - Hash[self.force_encoding("UTF-8").split("&").map! { |i| i.split("=") }] - end -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/error.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/error.rb deleted file mode 100644 index 4f91cad..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/error.rb +++ /dev/null @@ -1,8 +0,0 @@ -module ECpayErrorDefinition - # Generic ECpay exception class. - class ECpayError < StandardError; end - class ECpayMissingOption < ECpayError; end - class ECpayInvalidMode < ECpayError; end - class ECpayInvalidParam < ECpayError; end - class ECpayInvoiceRuleViolate < ECpayError; end -end \ No newline at end of file diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/helper.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/helper.rb deleted file mode 100644 index 445ec20..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/helper.rb +++ /dev/null @@ -1,164 +0,0 @@ -require 'digest' -require 'uri' -require 'cgi' -require 'net/http' -require 'nokogiri' -require 'date' - -class APIHelper - conf = File.join(File.dirname(__FILE__), '..', '..', 'conf', 'invoice_conf.xml') - @@conf_xml = Nokogiri::XML(File.open(conf)) - - def initialize - active_merc_info = @@conf_xml.xpath('/Conf/MercProfile').text - @op_mode = @@conf_xml.xpath('/Conf/OperatingMode').text - @contractor_stat = @@conf_xml.xpath('/Conf/IsProjectContractor').text - merc_info = @@conf_xml.xpath("/Conf/MerchantInfo/MInfo[@name=\"#{active_merc_info}\"]") - @ignore_payment = [] - @@conf_xml.xpath('/Conf/IgnorePayment//Method').each {|t| @ignore_payment.push(t.text)} - if merc_info != [] - @merc_id = merc_info[0].xpath('./MerchantID').text.freeze - @hkey = merc_info[0].xpath('./HashKey').text.freeze - @hiv = merc_info[0].xpath('./HashIV').text.freeze - - else - raise "Specified merchant setting name (#{active_merc_info}) not found." - end - end - - def get_mercid() - return @merc_id - end - - def get_op_mode() - return @op_mode - end - - def get_ignore_pay() - return @ignore_payment - end - - def get_curr_unixtime() - return Time.now.to_i - end - - def is_contractor?() - if @contractor_stat == 'N' - return false - elsif @contractor_stat == 'Y' - return true - else - raise "Unknown [IsProjectContractor] configuration." - end - end - - def urlencode_dot_net(raw_data, case_tr:'DOWN') - if raw_data.is_a?(String) - encoded_data = CGI.escape(raw_data) - case case_tr - when 'KEEP' - # Do nothing - when 'UP' - encoded_data.upcase! - when 'DOWN' - encoded_data.downcase! - end - # Process encoding difference between .NET & CGI - encoded_data.gsub!('%21', '!') - encoded_data.gsub!('%2a', '*') - encoded_data.gsub!('%28', '(') - encoded_data.gsub!('%29', ')') - return encoded_data - else - raise "Data recieved is not a string." - end - end - - def encode_special_param!(params, target_arr) - if params.is_a?(Hash) - target_arr.each do |n| - if params.keys.include?(n) - val = self.urlencode_dot_net(params[n]) - params[n] = val - end - end - end - end - - - - def gen_chk_mac_value(params, mode: 1) - if params.is_a?(Hash) - # raise exception if param contains CheckMacValue, HashKey, HashIV - sec = ['CheckMacValue', 'HashKey', 'HashIV'] - sec.each do |pa| - if params.keys.include?(pa) - raise "Parameters shouldn't contain #{pa}" - end - end - - raw = params.sort_by{|key,val|key.downcase}.map!{|key,val| "#{key}=#{val}"}.join('&') - raw = self.urlencode_dot_net(["HashKey=#{@hkey}", raw, "HashIV=#{@hiv}"].join("&"), case_tr: 'DOWN') - p raw - - - case mode - when 0 - chksum = Digest::MD5.hexdigest(raw) - when 1 - chksum = Digest::SHA256.hexdigest(raw) - else - raise "Unexpected hash mode." - end - return chksum.upcase! - else - raise "Data recieved is not a Hash." - end - end - - - def http_request(method:, url:, payload:) - - target_url = URI.parse(url) - - case method - when 'GET' - target_url.query = URI.encode_www_form(payload) - res = Net::HTTP.get_response(target_url) - when 'POST' - res = Net::HTTP.post_form(target_url, payload) - else - raise ArgumentError, "Only GET & POST method are avaliable." - end - return res.body - # if res == Net::HTTPOK - # return res - # else - # raise "#{res.message}, #{res}" - # end - - # when Net::HTTPClientError, Net::HTTPInternalServerError - # raise Net::HTTPError.new(http_response.message, http_response) - # else - # raise Net::HTTPError.new("Unexpected HTTP response.", http_response) - # end - end - - - def gen_html_post_form(act:, id:, parameters:, input_typ:'hidden', submit: true) - f = Nokogiri::HTML::Builder.new do |doc| - doc.form(method: 'post', action: act, id: id) { - parameters.map{|key,val| - doc.input(type: input_typ, name: key, id: key, value: val) - } - if submit == true - doc.script(type: 'text/javascript').text("document.getElementById(\"#{id}\").submit();") - end - } - end - return f.to_html - end - - - -end \ No newline at end of file diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/invoice_client.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/invoice_client.rb deleted file mode 100644 index c949bdb..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/invoice_client.rb +++ /dev/null @@ -1,166 +0,0 @@ -require "digest" -require "uri" -require "net/http" -require "net/https" -require "json" -require "cgi" -require "securerandom" -require "ecpay_invoice/helper" -require "ecpay_invoice/verification" -require "ecpay_invoice/error" -require "ecpay_invoice/core_ext/hash" -require "ecpay_invoice/core_ext/string" -# require "../../../gem/lib/ecpay_invoice/helper" -# require "../../../gem/lib/ecpay_invoice/verification" -# require "../../../gem/lib/ecpay_invoice/error" -# require "../../../gem/lib/ecpay_invoice/core_ext/hash" -# require "../../../gem/lib/ecpay_invoice/core_ext/string" - -module ECpayInvoice - - class InvoiceClientECPay - include ECpayErrorDefinition - - def initialize - @helper = APIHelper.new - end - - def ecpay_invoice_issue(param) - invoice_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - param['CarruerNum'] = param['CarruerNum'].to_s.gsub('+', ' ') - p param['TimeStamp'] - res = invoice_pos_proc!(params: param, apiname: 'InvoiceIssue') - return res - end - - def ecpay_invoice_delay(param) - invoice_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - param['CarruerNum'] = param['CarruerNum'].to_s.gsub('+', ' ') - param['PayType'] = '2' - param['PayAct'] = 'ECPAY' - p param['TimeStamp'] - res = invoice_pos_proc!(params: param, apiname: 'InvoiceDelayIssue') - return res - end - - def ecpay_invoice_trigger(param) - invoice_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - param['PayType'] = '2' - p param['TimeStamp'] - res = invoice_pos_proc!(params: param, apiname: 'InvoiceTriggerIssue') - return res - end - - def ecpay_invoice_allowance(param) - invoice_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = invoice_pos_proc!(params: param, apiname: 'InvoiceAllowance') - return res - end - - def ecpay_invoice_issue_invalid(param) - invoice_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = invoice_pos_proc!(params: param, apiname: 'InvoiceIssueInvalid') - return res - end - - def ecpay_invoice_allowance_invalid(param) - invoice_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = invoice_pos_proc!(params: param, apiname: 'InvoiceAllowanceInvalid') - return res - end - - ### Private method definition start ### - private - - def get_curr_unix_time() - return Time.now.to_i - end - - def invoice_base_proc!(params:) - if params.is_a?(Hash) - # Transform param key to string - params.stringify_keys() - - params['MerchantID'] = @helper.get_mercid - - #gem_uid = SecureRandom::hex - #p gem_uid.to_s[0, 30] - #params['RelateNumber'] = gem_uid.to_s[0, 30] - - else - raise ECpayInvalidParam, "Recieved parameter object must be a Hash" - end - end - - def invoice_pos_proc!(params:, apiname:) - verify_query_api = ECpayInvoice::InvoiceParamVerify.new(apiname) - if apiname == 'InvoiceIssue' - exclusive_list = ['InvoiceRemark', 'ItemName', 'ItemRemark', 'ItemWord'] - verify_query_api.verify_inv_issue_param(params) - elsif apiname == 'InvoiceDelayIssue' - exclusive_list = ['InvoiceRemark', 'ItemName', 'ItemRemark', 'ItemWord'] - verify_query_api.verify_inv_delay_param(params) - elsif apiname == 'InvoiceTriggerIssue' - exclusive_list = [] - verify_query_api.verify_inv_trigger_param(params) - elsif apiname == 'InvoiceAllowance' - exclusive_list = ['ItemName', 'ItemWord'] - verify_query_api.verify_inv_allowance_param(params) - elsif apiname == 'InvoiceIssueInvalid' - exclusive_list = ['Reason'] - verify_query_api.verify_inv_issue_invalid_param(params) - elsif apiname == 'InvoiceAllowanceInvalid' - exclusive_list = ['Reason'] - verify_query_api.verify_inv_allowance_invalid_param(params) - end - - #encode special param - sp_param = verify_query_api.get_special_encode_param(apiname) - @helper.encode_special_param!(params, sp_param) - - exclusive_ele = {} - for param in exclusive_list - exclusive_ele[param] = params[param] - params.delete(param) - end - - # Insert chkmacval - chkmac = @helper.gen_chk_mac_value(params, mode: 0) - params['CheckMacValue'] = chkmac - - for param in exclusive_list - params[param] = exclusive_ele[param] - end - - sp_param.each do |key| - params[key] = CGI::unescape(params[key]) - end - - # gen post html - api_url = verify_query_api.get_svc_url(apiname, @helper.get_op_mode) - #post from server - resp = @helper.http_request(method: 'POST', url: api_url, payload: params) - - # return post response - return resp - end - - ### Private method definition end ### - - end -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/notify_client.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/notify_client.rb deleted file mode 100644 index 6fc3293..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/notify_client.rb +++ /dev/null @@ -1,79 +0,0 @@ -require "digest" -require "uri" -require "net/http" -require "net/https" -require "json" -require "date" -require "cgi" -require "ecpay_invoice/helper" -require "ecpay_invoice/verification" -require "ecpay_invoice/error" -require "ecpay_invoice/core_ext/hash" -require "ecpay_invoice/core_ext/string" -# require "../../../gem/lib/ecpay_invoice/helper" -# require "../../../gem/lib/ecpay_invoice/verification" -# require "../../../gem/lib/ecpay_invoice/error" -# require "../../../gem/lib/ecpay_invoice/core_ext/hash" -# require "../../../gem/lib/ecpay_invoice/core_ext/string" - -module ECpayInvoice - - class NotifyClientECPay - - def initialize - @helper = APIHelper.new - end - - def ecpay_invoice_notify(param) - notify_base_proc!(params: param) - unix_time = get_curr_unix_time() - 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = notify_pos_proc!(params: param, apiname: 'InvoiceNotify') - return res - end - - ### Private method definition start ### - private - - def get_curr_unix_time() - return Time.now.to_i - end - - def notify_base_proc!(params:) - if params.is_a?(Hash) - # Transform param key to string - params.stringify_keys() - - params['MerchantID'] = @helper.get_mercid - - else - raise ECpayInvalidParam, "Recieved parameter object must be a Hash" - end - end - - def notify_pos_proc!(params:, apiname:) - verify_query_api = ECpayInvoice::NotifyParamVerify.new(apiname) - verify_query_api.verify_notify_param(params) - #encode special param - sp_param = verify_query_api.get_special_encode_param(apiname) - @helper.encode_special_param!(params, sp_param) - - # Insert chkmacval - chkmac = @helper.gen_chk_mac_value(params, mode: 0) - params['CheckMacValue'] = chkmac - params['NotifyMail'] = CGI::unescape(params['NotifyMail']) - p params - # gen post html - api_url = verify_query_api.get_svc_url(apiname, @helper.get_op_mode) - #post from server - resp = @helper.http_request(method: 'POST', url: api_url, payload: params) - - # return post response - return resp - end - - ### Private method definition end ### - - end -end \ No newline at end of file diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/query_client.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/query_client.rb deleted file mode 100644 index 92e30df..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/query_client.rb +++ /dev/null @@ -1,120 +0,0 @@ -require "digest" -require "uri" -require "net/http" -require "net/https" -require "json" -require "date" -require "ecpay_invoice/helper" -require "ecpay_invoice/verification" -require "ecpay_invoice/error" -require "ecpay_invoice/core_ext/hash" -require "ecpay_invoice/core_ext/string" -# require "../../../gem/lib/ecpay_invoice/helper" -# require "../../../gem/lib/ecpay_invoice/verification" -# require "../../../gem/lib/ecpay_invoice/error" -# require "../../../gem/lib/ecpay_invoice/core_ext/hash" -# require "../../../gem/lib/ecpay_invoice/core_ext/string" - -module ECpayInvoice - - class QueryClientECPay - - def initialize - @helper = APIHelper.new - end - - def ecpay_query_invoice_issue(param) - query_base_proc!(params: param) - unix_time = get_curr_unix_time() + 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = query_pos_proc!(params: param, apiname: 'QueryIssue') - return res - end - - def ecpay_query_invoice_allowance(param) - query_base_proc!(params: param) - unix_time = get_curr_unix_time() + 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = query_pos_proc!(params: param, apiname: 'QueryAllowance') - return res - end - - def ecpay_query_invoice_issue_invalid(param) - query_base_proc!(params: param) - unix_time = get_curr_unix_time() + 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = query_pos_proc!(params: param, apiname: 'QueryIssueInvalid') - return res - end - - def ecpay_query_invoice_allowance_invalid(param) - query_base_proc!(params: param) - unix_time = get_curr_unix_time() + 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = query_pos_proc!(params: param, apiname: 'QueryAllowanceInvalid') - return res - end - - def ecpay_query_check_mob_barcode(param) - query_base_proc!(params: param) - unix_time = get_curr_unix_time() + 120 - param['TimeStamp'] = unix_time.to_s - param['BarCode'] = param['BarCode'].to_s.gsub('+', ' ') - p param['BarCode'] - p param['TimeStamp'] - res = query_pos_proc!(params: param, apiname: 'CheckMobileBarCode') - return res - end - - def query_check_love_code(param) - query_base_proc!(params: param) - unix_time = get_curr_unix_time() + 120 - param['TimeStamp'] = unix_time.to_s - p param['TimeStamp'] - res = query_pos_proc!(params: param, apiname: 'CheckLoveCode') - return res - end - - ### Private method definition start ### - private - - def get_curr_unix_time() - return Time.now.to_i - end - - def query_base_proc!(params:) - if params.is_a?(Hash) - # Transform param key to string - params.stringify_keys() - - params['MerchantID'] = @helper.get_mercid - else - raise ECpayInvalidParam, "Recieved parameter object must be a Hash" - end - end - - def query_pos_proc!(params:, apiname:) - verify_query_api = ECpayInvoice::QueryParamVerify.new(apiname) - verify_query_api.verify_query_param(params) - #encode special param - - # Insert chkmacval - chkmac = @helper.gen_chk_mac_value(params, mode: 0) - params['CheckMacValue'] = chkmac - p params - # gen post html - api_url = verify_query_api.get_svc_url(apiname, @helper.get_op_mode) - #post from server - resp = @helper.http_request(method: 'POST', url: api_url, payload: params) - - # return post response - return resp - end - ### Private method definition end ### - - end -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/verification.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/verification.rb deleted file mode 100644 index e145e4b..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/verification.rb +++ /dev/null @@ -1,1048 +0,0 @@ - -require "ecpay_invoice/error" -# require "../../../gem/lib/ecpay_invoice/error" -require "nokogiri" -require 'date' -module ECpayInvoice - class InvoiceVerifyBase - include ECpayErrorDefinition - @@param_xml = Nokogiri::XML(File.open(File.join(File.dirname(__FILE__), 'ECpayInvoice.xml'))) - - def get_svc_url(apiname, mode) - url = @@param_xml.xpath("/ecpayInvoice/#{apiname}/ServiceAddress/url[@type=\"#{mode}\"]").text - return url - end - - def get_special_encode_param(apiname) - ret = [] - node = @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param[@urlencode=\"1\"]") - node.each {|elem| ret.push(elem.attributes['name'].value)} - return ret - end - - def get_basic_params(apiname) - basic_param = [] - @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters/param[@require=\"1\"]").each do |elem| - basic_param.push(elem.attributes['name'].value) - end - return basic_param - end - - def get_cond_param(apiname) - aio_sw_param = [] - conditional_param = {} - @@param_xml.xpath("/ecpayInvoice/#{apiname}/Config/switchparam/n").each do |elem| - aio_sw_param.push(elem.text) - end - aio_sw_param.each do |pname| - opt_param = {} - node = @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param[@name=\"#{pname}\"]") - node.xpath('./condparam').each do |elem| - opt = elem.attributes['owner'].value - params = [] - elem.xpath('./param[@require="1"]').each do |pa| - params.push(pa.attributes['name'].value) - end - opt_param[opt] = params - end - conditional_param[pname] = opt_param - end - return conditional_param - end - - def get_param_type(apiname) - param_type = {} - @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param").each do |elem| - param_type[elem.attributes['name'].value] = elem.attributes['type'].value - end - return param_type - end - - def get_opt_param_pattern(apiname) - pattern = {} - node = @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param[@type=\"Opt\"]") - node.each do |param_elem| - opt_elems = param_elem.xpath('./option') - opt = [] - opt_elems.each{|oe|opt.push(oe.text)} - pattern[param_elem.attributes['name'].value] = opt - end - return pattern - end - - def get_int_param_pattern(apiname) - pattern = {} - node = @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param[@type=\"Int\"]") - node.each do |param_elem| - mode = param_elem.xpath('./mode').text - mx = param_elem.xpath('./maximum').text - mn = param_elem.xpath('./minimal').text - a = [] - [mode, mx, mn].each{|f|a.push(f)} - pattern[param_elem.attributes['name'].value] = a - end - return pattern - end - - def get_str_param_pattern(apiname) - pattern = {} - node = @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param[@type=\"String\"]") - node.each do |param_elem| - p_name = param_elem.attributes['name'].value - pat_elems = param_elem.xpath('./pattern') - # if pat_elems.length > 1 - # raise "Only 1 pattern tag is allowed for each parameter (#{p_name}) " - # elsif pat_elems.length = 0 - # raise "No pattern tag found for parameter (#{p_name}) " - # end - pat = pat_elems.text - pattern[p_name] = pat - end - return pattern - end - - def get_depopt_param_pattern(apiname) - pattern = {} - - node = @@param_xml.xpath("/ecpayInvoice/#{apiname}/Parameters//param[@type=\"DepOpt\"]") - node.each do |param_elem| - parent_n_opts = {} - sub_opts = {} - p_name = param_elem.attributes['name'].value - parent_name = param_elem.attributes['main'].value - param_elem.xpath('./mainoption').each do |elem| - k = elem.attributes['name'].value - opt = [] - elem.element_children.each{|c|opt.push(c.text)} - sub_opts[k] = opt - end - parent_n_opts[parent_name] = sub_opts - pattern[p_name] = parent_n_opts - end - return pattern - end - - def get_all_pattern(apiname) - res = {} - res['Type_idx'] = self.get_param_type(apiname) - res['Int'] = self.get_int_param_pattern(apiname) - res['String'] = self.get_str_param_pattern(apiname) - res['Opt'] = self.get_opt_param_pattern(apiname) - res['DepOpt'] = self.get_depopt_param_pattern(apiname) - return res - end - - def verify_param_by_pattern(params, pattern) - type_index = pattern['Type_idx'] - params.keys.each do |p_name| - p_type = type_index[p_name] - patt_container = pattern[p_type] - case - when p_type == 'String' - regex_patt = patt_container[p_name] - mat = /#{regex_patt}/.match(params[p_name]) - if mat.nil? - raise ECpayInvalidParam, "Wrong format of param #{p_name} or length exceeded." - end - when p_type == 'Opt' - aval_opt = patt_container[p_name] - mat = aval_opt.include?(params[p_name]) - if mat == false - raise ECpayInvalidParam, "Unexpected option of param #{p_name} (#{params[p_name]}). Avaliable option: (#{aval_opt})." - end - when p_type == 'Int' - criteria = patt_container[p_name] - mode = criteria[0] - max = criteria[1].to_i - min = criteria[2].to_i - val = params[p_name].to_i - case - when mode == 'BETWEEN' - if val < min or val > max - raise ECpayInvalidParam, "Value of #{p_name} should be between #{min} and #{max} ." - end - when mode == 'GE' - if val < min - raise ECpayInvalidParam, "Value of #{p_name} should be greater than or equal to #{min}." - end - when mode == 'LE' - if val > max - raise ECpayInvalidParam, "Value of #{p_name} should be less than or equal to #{max}." - end - when mode == 'EXCLUDE' - if val >= min and val <= max - raise ECpayInvalidParam, "Value of #{p_name} can NOT be between #{min} and #{max} .." - end - else - raise "Unexpected integer verification mode for parameter #{p_name}: #{mode}. " - end - when p_type == 'DepOpt' - dep_opt = patt_container[p_name] - parent_param = dep_opt.keys()[0] - all_dep_opt = dep_opt[parent_param] - parent_val = params[parent_param] - aval_opt = all_dep_opt[parent_val] - if aval_opt.nil? and pattern['Opt'][parent_param].include?(parent_val) == false - raise ECpayInvalidParam, "Cannot find avaliable option of [#{p_name}] by related param [#{parent_param}](Value: #{parent_val})." - elsif aval_opt.is_a?(Array) - unless aval_opt.include?(params[p_name]) - raise ECpayInvalidParam, "Unexpected option of param #{p_name} (#{params[p_name]}). Avaliable option: (#{aval_opt})." - end - end - - else - raise "Unexpected type (#{p_type}) for parameter #{p_name}. " - - end - end - end - end - - class InvoiceParamVerify < InvoiceVerifyBase - include ECpayErrorDefinition - def initialize(apiname) - @inv_basic_param = self.get_basic_params(apiname).freeze - @inv_conditional_param = self.get_cond_param(apiname).freeze - @all_param_pattern = self.get_all_pattern(apiname).freeze - end - - def verify_inv_issue_param(params) - if params.is_a?(Hash) - #發票所有參數預設要全帶 - - if params.has_value?(nil) - raise ECpayInvalidParam, %Q{Parameter value cannot be nil} - end - #1. 比對欄位是否缺乏 - param_diff = @inv_basic_param - params.keys() - unless param_diff == [] - raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}} - end - - #2. 比對特殊欄位值相依需求 - #b 列印註記[Print]為 1 => CustomerName, CustomerAddr - if params['Print'].to_s == '1' - if params['CustomerName'].to_s.empty? or params['CustomerAddr'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerName] and [CustomerAddr] can not be empty when [Print] is 1." - end - unless params['CarruerType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerType] is not empty." - end - unless params['CarruerNum'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerNum] is not empty." - end - end - - #c CustomerPhone和CustomerEmail至少一個有值 - if params['CustomerPhone'].to_s.empty? and params['CustomerEmail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerPhone] and [CustomerEmail] can not both be empty." - end - - #d [TaxType]為 2 => ClearanceMark = 必須為 1 or 2,ItemTaxType 必須為空 - if params['TaxType'].to_s == '2' - if !params['ItemRemark'].to_s.empty? - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark'] - else - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - end - @vat_params_list = ['ItemCount', 'ItemAmount'] - unless ['1', '2'].include?(params['ClearanceMark'].to_s) - raise ECpayInvoiceRuleViolate, "[ClearanceMark] has to be 1 or 2 when [TaxType] is 2." - end - unless params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 2." - end - # 當[TaxType]為2時為零稅率,vat為0時商品單價為免稅,不須再加稅 - # 若vat為1時商品單價為含稅,須再退稅 - if params['vat'].to_s == '0' - @tax_fee = 1 - elsif params['vat'].to_s == '1' - @tax_fee = 1.05 - end - #d.1 [TaxType]為 1 => ItemTaxType, ClearanceMark 必須為空 - elsif params['TaxType'].to_s == '1' - if !params['ItemRemark'].to_s.empty? - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark'] - else - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - end - @vat_params_list = ['ItemCount', 'ItemAmount'] - unless params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 1." - end - unless params['ClearanceMark'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 1." - end - # 當[TaxType]為1時為應稅,vat為0時商品單價為免稅,須再加稅 - # 若vat為1時商品單價為含稅,不須再加稅 - if params['vat'].to_s == '0' - @tax_fee = 1.05 - elsif params['vat'].to_s == '1' - @tax_fee = 1 - end - #d.2 [TaxType]為 3 => ItemTaxType, ClearanceMark 必須為空 - elsif params['TaxType'].to_s == '3' - if !params['ItemRemark'].to_s.empty? - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark'] - else - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - end - @vat_params_list = ['ItemCount', 'ItemAmount'] - unless params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 3." - end - unless params['ClearanceMark'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 3." - end - # 當[TaxType]為3時為免稅,vat為0時商品單價為免稅,不須再加稅 - # 若vat為1時商品單價為含稅,須再退稅 - if params['vat'].to_s == '0' - @tax_fee = 1 - elsif params['vat'].to_s == '1' - @tax_fee = 1.05 - end - #d.3 [TaxType]為 9 => ItemTaxType 必須為兩項商品(含)以上,且不可為空 - elsif params['TaxType'].to_s == '9' - if !params['ItemRemark'].to_s.empty? - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark', 'ItemTaxType'] - else - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemTaxType'] - end - @vat_params_list = ['ItemCount', 'ItemAmount', 'ItemTaxType'] - unless params['ItemTaxType'].include?('|') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '|'." - end - if params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be empty when [TaxType] is 9." - end - # 當[ItmeTaxType]含2選項的話[ClearanceMark]須為1或2 - if params['ItemTaxType'].include?('2') - unless ['1', '2'].include?(params['ClearanceMark'].to_s) - raise ECpayInvoiceRuleViolate, "[ClearanceMark] has to be 1 or 2 when [ItemTaxType] has 2." - end - end - end - - #e 統一編號[CustomerIdentifier]有值時 => CarruerType != 1, 2 or 3, *Donation = 0, print = 1 - unless params['CustomerIdentifier'].to_s.empty? - if ['1', '2', '3'].include?(params['CarruerType'].to_s) - raise ECpayInvoiceRuleViolate, "[CarruerType] Cannot be 1, 2 or 3 when [CustomerIdentifier] is given." - end - unless params['Donation'].to_s == '0' and params['Print'].to_s == '1' - raise ECpayInvoiceRuleViolate, "[Print] must be 1 and [Donation] must be 0 when [CustomerIdentifier] is given." - end - end - - # [CarruerType]為'' or 1 時 => CarruerNum = '', [CarruerType]為 2, CarruerNum = 固定長度為 16 且格式為 2 碼大小寫字母加上 14 碼數字。 [CarruerType]為 3 ,帶固定長度為 8 且格式為 1 碼斜線「/」加上由 7 碼數字及大小寫字母組成 - if ['', '1'].include?(params['CarruerType'].to_s) - unless params['CarruerNum'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CarruerNum] must be empty when [CarruerType] is empty or 1." - end - elsif params['CarruerType'].to_s == '2' - unless params['CustomerID'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 2." - end - if /[A-Za-z]{2}[0-9]{14}/.match(params['CarruerNum']).nil? - raise ECpayInvoiceRuleViolate, "[CarruerNum] must be 2 alphabets and 14 numbers when [CarruerType] is 2." - end - elsif params['CarruerType'].to_s == '3' - unless params['CustomerID'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 3." - end - if /^\/[A-Za-z0-9\s+-.]{7}$/.match(params['CarruerNum']).nil? - raise ECpayInvoiceRuleViolate, "[CarruerNum] must start with '/' followed by 7 alphabet and number characters when [CarruerType] is 3." - end - else - raise ECpayInvoiceRuleViolate, "Unexpected value in [CarruerType]." - end - - # Donation = 1 => LoveCode不能為空, print = 0 - if params['Donation'].to_s == '1' - if params['LoveCode'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[LoveCode] cannot be empty when [Donation] is 1." - end - unless params['Print'].to_s == '0' - raise ECpayInvoiceRuleViolate, "[Print] must be 0 when [Donation] is 1." - end - # Donation = 0 => LoveCode不能有值 - elsif params['Donation'].to_s == '0' - unless params['LoveCode'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[LoveCode] must be empty when [Donation] is 0." - end - end - - # [vat]為0時 => ItemPrice = 未稅, ItemAmount = (ItemPrice * ItemCount) + (ItemPrice * ItemCount * tax(5%)) - # 未稅加稅單一商品時直接四捨五入帶入ItemAmount,且ItemAmount等於SalesAmount - # 未稅加稅多樣商品時先算稅金加總帶入ItemAmount,且ItemAmount全部金額加總後帶入SalesAmount後四捨五入 - vat_params = @vat_params_list - # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對 - if params['vat'].to_s == '0' - if !params['ItemPrice'].include?('|') - unless params['ItemAmount'].to_f == (params['ItemPrice'].to_f * params['ItemCount'].to_i * @tax_fee).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_f}) times [ItemCount] (#{params['ItemCount'].to_i}) '*' tax (#{@tax_fee}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_f})} - end - # 驗證單筆商品合計是否等於發票金額 - unless params['SalesAmount'].to_i == (params['ItemAmount'].to_f).round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [SalesAmount] (#{params['SalesAmount'].to_i})} - end - - elsif params['ItemPrice'].include?('|') - vat_cnt = params['ItemPrice'].split('|').length - vat_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless vat_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})} - end - end - vat_amount_arr = params['ItemAmount'].split('|') - vat_price_arr = params['ItemPrice'].split('|') - vat_count_arr = params['ItemCount'].split('|') - (1..vat_cnt).each do |index| - if @vat_params_list.length == 3 - vat_tax_arr = params['ItemTaxType'].split('|') - if vat_tax_arr[index - 1].to_s == '1' - @tax_fee = 1.05 - elsif vat_tax_arr[index - 1].to_s == '2' or vat_tax_arr[index - 1].to_s == '3' - @tax_fee = 1 - else - raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be (#{vat_tax_arr[index - 1]}). Avaliable option: (1, 2, 3)." - end - end - unless vat_amount_arr[index - 1].to_f == (vat_price_arr[index - 1].to_f * vat_count_arr[index - 1].to_i * @tax_fee).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1].to_f}) times [ItemCount] (#{vat_count_arr[index - 1].to_f}) '*' tax(#{@tax_fee}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_f})} - end - #Verify ItemAmount subtotal equal SalesAmount - chk_amount_subtotal = 0 - vat_amount_arr.each do |val| - chk_amount_subtotal += val.to_f - end - unless params['SalesAmount'].to_i == (chk_amount_subtotal.to_f).round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [SalesAmount] (#{params['SalesAmount'].to_i})} - end - end - end - end - - # [vat]為1時 => ItemPrice = 含稅, ItemAmount = ItemPrice * ItemCount - # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對 - # 含稅扣稅單一商品時直接四捨五入帶入ItemAmount,且ItemAmount等於SalesAmount - # 含稅扣稅多樣商品時先算稅金加總四捨五入後帶入ItemAmount,且ItemAmount全部金額加總後等於SalesAmount - if params['vat'].to_s == '1' - if !params['ItemPrice'].include?('|') - - unless params['ItemAmount'].to_f == (params['ItemPrice'].to_f * params['ItemCount'].to_i / @tax_fee).round(2) - - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_f}) times [ItemCount] (#{params['ItemCount'].to_f}) '/' tax (#{@tax_fee}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_f})} - end - # 驗證單筆商品合計是否等於發票金額 - unless params['SalesAmount'].to_i == (params['ItemAmount'].to_f).round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [SalesAmount] (#{params['SalesAmount'].to_i})} - end - elsif params['ItemPrice'].include?('|') - vat_cnt = params['ItemPrice'].split('|').length - vat_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless vat_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})} - end - end - vat_amount_arr = params['ItemAmount'].split('|') - vat_price_arr = params['ItemPrice'].split('|') - vat_count_arr = params['ItemCount'].split('|') - (1..vat_cnt).each do |index| - if @vat_params_list.length == 3 - vat_tax_arr = params['ItemTaxType'].split('|') - if vat_tax_arr[index - 1].to_s == '1' - @tax_fee = 1 - elsif vat_tax_arr[index - 1].to_s == '2' or vat_tax_arr[index - 1].to_s == '3' - @tax_fee = 1.05 - else - raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be (#{vat_tax_arr[index - 1]}). Avaliable option: (1, 2, 3)." - end - end - unless vat_amount_arr[index - 1].to_f == (vat_price_arr[index - 1].to_f * vat_count_arr[index - 1].to_i / @tax_fee).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1]}) times [ItemCount] (#{vat_count_arr[index - 1]}) '/' tax(#{@tax_fee}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_i})} - end - #Verify ItemAmount subtotal equal SalesAmount - chk_amount_subtotal = 0 - vat_amount_arr.each do |val| - chk_amount_subtotal += val.to_f - end - unless params['SalesAmount'].to_i == (chk_amount_subtotal.to_f).round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [SalesAmount] (#{params['SalesAmount'].to_i})} - end - end - end - end - - #3. 比對商品名稱,數量,單位,價格,tax,合計,備註項目數量是否一致,欄位是否為空 - if params['ItemName'].to_s.empty? or params['ItemWord'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemName] or [ItemWord] cannot be empty" - end - - # ItemTaxType and ItemRemark會因為TaxType and ItemRemark is not empty 新增至@item_params_list - item_params = @item_params_list - #商品名稱含有管線 => 認為是多樣商品 *ItemName, *ItemCount ,*ItemWord, *ItemPrice, *ItemAmount, *ItemTaxType, *ItemRemark逐一用管線分割,計算數量後與第一個比對 - if params['ItemName'].include?('|') - item_cnt = params['ItemName'].split('|').length - item_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless item_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{item_cnt})} - end - end - # 課稅類別[TaxType] = 9 時 => ItemTaxType 能含有1,2 3(and at least contains one 1 and other) - if params['TaxType'].to_s == '9' - item_tax = params['ItemTaxType'].split('|') - p item_tax - aval_tax_type = ['1', '2', '3'] - vio_tax_t = (item_tax - aval_tax_type) - unless vio_tax_t == [] - raise ECpayInvoiceRuleViolate, "Ilegal [ItemTaxType]: #{vio_tax_t}" - end - unless item_tax.include?('1') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '1'." - end - if item_cnt >= 2 - if !item_tax.include?('2') and !item_tax.include?('3') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] cannot be all 1 when [TaxType] is 9." - end - end - if item_tax.include?('2') and item_tax.include?('3') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] cannot contain 2 and 3 at the same time." - end - end - else - #沒有管線 => 逐一檢查@item_params_list的欄位有無管線 - item_params.each do |param_name| - if params[param_name].include?('|') - raise "Item info [#{param_name}] contains pipeline delimiter but there's only one item in param [ItemName]" - end - end - end - - #4 比對所有欄位Pattern - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - end - - def verify_inv_delay_param(params) - if params.is_a?(Hash) - #發票所有參數預設要全帶 - - if params.has_value?(nil) - raise ECpayInvalidParam, %Q{Parameter value cannot be nil} - end - #1. 比對欄位是否缺乏 - param_diff = @inv_basic_param - params.keys() - unless param_diff == [] - raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}} - end - - #2. 比對特殊欄位值相依需求 - #b 列印註記[Print]為 1 => CustomerName, CustomerAddr - if params['Print'].to_s == '1' - if params['CustomerName'].to_s.empty? or params['CustomerAddr'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerName] and [CustomerAddr] can not be empty when [Print] is 1." - end - unless params['CarruerType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerType] is not empty." - end - unless params['CarruerNum'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerNum] is not empty." - end - end - - #c CustomerPhone和CustomerEmail至少一個有值 - if params['CustomerPhone'].to_s.empty? and params['CustomerEmail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerPhone] and [CustomerEmail] can not both be empty." - end - - #d [TaxType]為 2 => ClearanceMark = 必須為 1 or 2,ItemTaxType 必須為空 - if params['TaxType'].to_s == '2' - @tax_fee = 1.05 - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - @vat_params_list = ['ItemCount', 'ItemAmount'] - unless ['1', '2'].include?(params['ClearanceMark'].to_s) - raise ECpayInvoiceRuleViolate, "[ClearanceMark] has to be 1 or 2 when [TaxType] is 2." - end - unless params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 2." - end - #d.1 [TaxType]為 1 => ItemTaxType, ClearanceMark 必須為空 - elsif params['TaxType'].to_s == '1' - @tax_fee = 1 - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - @vat_params_list = ['ItemCount', 'ItemAmount'] - unless params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 1." - end - unless params['ClearanceMark'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 1." - end - #d.2 [TaxType]為 3 => ItemTaxType, ClearanceMark 必須為空 - elsif params['TaxType'].to_s == '3' - @tax_fee = 1.05 - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - @vat_params_list = ['ItemCount', 'ItemAmount'] - unless params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 3." - end - unless params['ClearanceMark'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 3." - end - #d.3 [TaxType]為 9 => ItemTaxType 必須為兩項商品(含)以上,且不可為空 - elsif params['TaxType'].to_s == '9' - @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemTaxType'] - @vat_params_list = ['ItemCount', 'ItemAmount', 'ItemTaxType'] - unless params['ItemTaxType'].include?('|') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '|'." - end - if params['ItemTaxType'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be empty when [TaxType] is 9." - end - end - - #e 統一編號[CustomerIdentifier]有值時 => CarruerType != 1, 2 or 3, *Donation = 0, print = 1 - unless params['CustomerIdentifier'].to_s.empty? - if ['1', '2', '3'].include?(params['CarruerType'].to_s) - raise ECpayInvoiceRuleViolate, "[CarruerType] Cannot be 1, 2 or 3 when [CustomerIdentifier] is given." - end - unless params['Donation'].to_s == '0' and params['Print'].to_s == '1' - raise ECpayInvoiceRuleViolate, "[Print] must be 1 and [Donation] must be 0 when [CustomerIdentifier] is given." - end - end - - # DelayFlag Rules When [DelayFlag] is '1' the [DelayDay] range be between 1 and 15 - # When [DelayFlag] is '2' the [DelayDay] range be between 0 and 15 - if params['DelayFlag'].to_s == '1' - if params['DelayDay'].to_i > 15 or params['DelayDay'].to_i < 1 - raise ECpayInvoiceRuleViolate, "[DelayDay] must be between 1 and 15 when [DelayFlag] is '1'." - end - elsif params['DelayFlag'].to_s == '2' - if params['DelayDay'].to_i > 15 or params['DelayDay'].to_i < 0 - raise ECpayInvoiceRuleViolate, "[DelayDay] must be between 0 and 15 when [DelayFlag] is '2'." - end - end - - # [CarruerType]為'' or 1 時 => CarruerNum = '', [CarruerType]為 2, CarruerNum = 固定長度為 16 且格式為 2 碼大小寫字母加上 14 碼數字。 [CarruerType]為 3 ,帶固定長度為 8 且格式為 1 碼斜線「/」加上由 7 碼數字及大小寫字母組成 - if ['', '1'].include?(params['CarruerType'].to_s) - unless params['CarruerNum'].to_s == '' - raise ECpayInvoiceRuleViolate, "[CarruerNum] must be empty when [CarruerType] is empty or 1." - end - elsif params['CarruerType'].to_s == '2' - unless params['CustomerID'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 3." - end - if /[A-Za-z]{2}[0-9]{14}/.match(params['CarruerNum']).nil? - raise ECpayInvoiceRuleViolate, "[CarruerNum] must be 2 alphabets and 14 numbers when [CarruerType] is 2." - end - elsif params['CarruerType'].to_s == '3' - unless params['CustomerID'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 3." - end - if /^\/[A-Za-z0-9\s+-.]{7}$/.match(params['CarruerNum']).nil? - raise ECpayInvoiceRuleViolate, "[CarruerNum] must start with '/' followed by 7 alphabet and number characters when [CarruerType] is 3." - end - else - raise ECpayInvoiceRuleViolate, "Unexpected value in [CarruerType]." - end - - # Donation = 1 => LoveCode不能為空, print = 0 - if params['Donation'].to_s == '1' - if params['LoveCode'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[LoveCode] cannot be empty when [Donation] is 1." - end - unless params['Print'].to_s == '0' - raise ECpayInvoiceRuleViolate, "[Print] must be 0 when [Donation] is 1." - end - # Donation = 0 => LoveCode不能有值 - elsif params['Donation'].to_s == '0' - unless params['LoveCode'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[LoveCode] must be empty when [Donation] is 0." - end - end - - vat_params = @vat_params_list - # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對 - if !params['ItemPrice'].include?('|') - unless params['ItemAmount'].to_f == (params['ItemPrice'].to_f * params['ItemCount'].to_i / @tax_fee).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_i}) times [ItemCount] (#{params['ItemCount'].to_i}) '/' tax (#{@tax_fee}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_i})} - end - # 驗證單筆商品合計是否等於發票金額 - unless params['SalesAmount'].to_i == (params['ItemAmount'].to_f).round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [SalesAmount] (#{params['SalesAmount'].to_i})} - end - elsif params['ItemPrice'].include?('|') - vat_cnt = params['ItemPrice'].split('|').length - vat_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless vat_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})} - end - end - vat_amount_arr = params['ItemAmount'].split('|') - vat_price_arr = params['ItemPrice'].split('|') - vat_count_arr = params['ItemCount'].split('|') - (1..vat_cnt).each do |index| - if @vat_params_list.length == 3 - vat_tax_arr = params['ItemTaxType'].split('|') - if vat_tax_arr[index - 1].to_s == '1' - @tax_fee = 1 - elsif vat_tax_arr[index - 1].to_s == '2' or vat_tax_arr[index - 1].to_s == '3' - @tax_fee = 1.05 - else - raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be (#{vat_tax_arr[index - 1]}). Avaliable option: (1, 2, 3)." - end - end - unless vat_amount_arr[index - 1].to_f == (vat_price_arr[index - 1].to_f * vat_count_arr[index - 1].to_f / @tax_fee).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1]}) times [ItemCount] (#{vat_count_arr[index - 1]}) '/' tax(#{@tax_fee}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_i})} - end - #Verify ItemAmount subtotal equal SalesAmount - chk_amount_subtotal = 0 - vat_amount_arr.each do |val| - chk_amount_subtotal += val.to_f - end - unless params['SalesAmount'].to_i == chk_amount_subtotal.round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [SalesAmount] (#{params['SalesAmount'].to_i})} - end - end - end - - #3. 比對商品名稱,數量,單位,價格,tax,合計,備註項目數量是否一致,欄位是否為空 - if params['ItemName'].to_s.empty? or params['ItemWord'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemName] or [ItemWord] cannot be empty" - end - item_params = @item_params_list - #商品名稱含有管線 => 認為是多樣商品 *ItemName, *ItemCount ,*ItemWord, *ItemPrice, *ItemAmount逐一用管線分割,計算數量後與第一個比對 - if params['ItemName'].include?('|') - item_cnt = params['ItemName'].split('|').length - item_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless item_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{item_cnt})} - end - end - # 課稅類別[TaxType] = 9 時 => ItemTaxType 能含有1,2 3(and at least contains one 1 and other) - if params['TaxType'].to_s == '9' - item_tax = params['ItemTaxType'].split('|') - p item_tax - aval_tax_type = ['1', '2', '3'] - vio_tax_t = (item_tax - aval_tax_type) - unless vio_tax_t == [] - raise ECpayInvoiceRuleViolate, "Ilegal [ItemTaxType]: #{vio_tax_t}" - end - unless item_tax.include?('1') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '1'." - end - if !item_tax.include?('2') and !item_tax.include?('3') - raise ECpayInvoiceRuleViolate, "[ItemTaxType] cannot be all 1 when [TaxType] is 9." - end - end - else - #沒有管線 => 逐一檢查後6項有無管線 - item_params.each do |param_name| - if params[param_name].include?('|') - raise "Item info [#{param_name}] contains pipeline delimiter but there's only one item in param [ItemName]" - end - end - end - - #4 比對所有欄位Pattern - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - end - - def verify_inv_trigger_param(params) - if params.is_a?(Hash) - param_diff = @inv_basic_param - params.keys - unless param_diff == [] - raise ECpayInvalidParam, "Lack required param #{param_diff}" - end - - #Verify Value pattern of each param - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - - end - - def verify_inv_allowance_param(params) - if params.is_a?(Hash) - #發票所有參數預設要全帶 - - if params.has_value?(nil) - raise ECpayInvalidParam, %Q{Parameter value cannot be nil} - end - #1. 比對欄位是否缺乏 - param_diff = @inv_basic_param - params.keys() - unless param_diff == [] - raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}} - end - - #2. 比對特殊欄位值相依需求 - #NotifyPhone和NotifyMail至少一個有值 - if params['AllowanceNotify'].to_s == 'S' - if params['NotifyPhone'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[NotifyPhone] cannot be empty." - end - elsif params['AllowanceNotify'].to_s == 'E' - if params['NotifyMail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[NotifyMail] cannot be empty." - end - elsif params['AllowanceNotify'].to_s == 'A' - if params['NotifyPhone'].to_s.empty? or params['NotifyMail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[NotifyPhone] and [NotifyMail] can not be empty." - end - end - - vat_params = ['ItemCount', 'ItemAmount'] - # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對 - # 驗證單筆ItemAmount = (ItemPrice * ItemCount) - if !params['ItemPrice'].include?('|') - unless params['ItemAmount'].to_f == (params['ItemPrice'].to_f * params['ItemCount'].to_i).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_i}) times [ItemCount] (#{params['ItemCount'].to_i}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_i})} - end - # 驗證單筆商品合計是否等於發票金額 - unless params['AllowanceAmount'].to_i == (params['ItemAmount'].to_f).round - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [AllowanceAmount] (#{params['AllowanceAmount'].to_i})} - end - elsif params['ItemPrice'].include?('|') - vat_cnt = params['ItemPrice'].split('|').length - vat_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless vat_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})} - end - end - vat_amount_arr = params['ItemAmount'].split('|') - vat_price_arr = params['ItemPrice'].split('|') - vat_count_arr = params['ItemCount'].split('|') - (1..vat_cnt).each do |index| - unless vat_amount_arr[index - 1].to_f == (vat_price_arr[index - 1].to_f * vat_count_arr[index - 1].to_f).round(2) - raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1]}) times [ItemCount] (#{vat_count_arr[index - 1]}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_i})} - end - #Verify ItemAmount subtotal equal SalesAmount - chk_amount_subtotal = 0 - vat_amount_arr.each do |val| - chk_amount_subtotal += val.to_f - end - unless params['AllowanceAmount'].to_i == (chk_amount_subtotal.to_f).round(0) - raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [AllowanceAmount] (#{params['AllowanceAmount'].to_i})} - end - end - end - - #3. 比對商品名稱,數量,單位,價格,tax,合計,備註項目數量是否一致,欄位是否為空 - if params['ItemName'].to_s.empty? or params['ItemWord'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[ItemName] or [ItemWord] cannot be empty" - end - item_params = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount'] - #商品名稱含有管線 => 認為是多樣商品 *ItemName, *ItemCount ,*ItemWord, *ItemPrice, *ItemAmount逐一用管線分割,計算數量後與第一個比對 - if params['ItemName'].include?('|') - item_cnt = params['ItemName'].split('|').length - item_params.each do |param_name| - # Check if there's empty value. - unless /(\|\||^\||\|$)/.match(params[param_name]).nil? - raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value." - end - p_cnt = params[param_name].split('|').length - unless item_cnt == p_cnt - raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{item_cnt})} - end - end - # ItemTaxType 能含有1,2 3(and at least contains one 1 and other) - if params['ItemTaxType'].include?('|') - item_tax = params['ItemTaxType'].split('|') - aval_tax_type = ['1', '3'] - vio_tax_t = (item_tax - aval_tax_type) - unless vio_tax_t == [] - raise ECpayInvoiceRuleViolate, "Ilegal [ItemTaxType]: #{vio_tax_t}" - end - end - else - #沒有管線 => 逐一檢查後6項有無管線 - item_params.each do |param_name| - if params[param_name].include?('|') - raise "Item info [#{param_name}] contains pipeline delimiter but there's only one item in param [ItemName]" - end - end - end - - #4 比對所有欄位Pattern - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - end - - def verify_inv_issue_invalid_param(params) - if params.is_a?(Hash) - param_diff = @inv_basic_param - params.keys - unless param_diff == [] - raise ECpayInvalidParam, "Lack required param #{param_diff}" - end - - #Verify Value pattern of each param - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - - end - - def verify_inv_allowance_invalid_param(params) - if params.is_a?(Hash) - param_diff = @inv_basic_param - params.keys - unless param_diff == [] - raise ECpayInvalidParam, "Lack required param #{param_diff}" - end - - #Verify Value pattern of each param - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - - end - end - - class QueryParamVerify < InvoiceVerifyBase - include ECpayErrorDefinition - def initialize(apiname) - @inv_basic_param = self.get_basic_params(apiname).freeze - @inv_conditional_param = self.get_cond_param(apiname).freeze - @all_param_pattern = self.get_all_pattern(apiname).freeze - end - - def verify_query_param(params) - if params.is_a?(Hash) - param_diff = @inv_basic_param - params.keys - unless param_diff == [] - raise ECpayInvalidParam, "Lack required param #{param_diff}" - end - - #Verify Value pattern of each param - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - - end - end - - class NotifyParamVerify < InvoiceVerifyBase - include ECpayErrorDefinition - def initialize(apiname) - @inv_basic_param = self.get_basic_params(apiname).freeze - @inv_conditional_param = self.get_cond_param(apiname).freeze - @all_param_pattern = self.get_all_pattern(apiname).freeze - end - - def verify_notify_param(params) - if params.is_a?(Hash) - #發送發票通知預設參數要全帶 - - if params.has_value?(nil) - raise ECpayInvalidParam, %Q{Parameter value cannot be nil} - end - #1. 比對欄位是否缺乏 - param_diff = @inv_basic_param - params.keys() - unless param_diff == [] - raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}} - end - - #2. 比對特殊欄位值相依需求 - #a Phone和NotifyMail至少一個有值 - if params['Phone'].to_s.empty? and params['NotifyMail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Phone] and [NotifyMail] can not both be empty." - end - - #b [Notify] is S [Phone] can not be empty or [Notify] is E [NotifyMail] can not be empty - # If [Notify] is A [Phone] and [NotifyMail] can not both be empty - if params['Notify'].to_s == 'S' - if params['Phone'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Phone] can not be empty when [Notify] is 'S'." - end - elsif params['Notify'].to_s == 'E' - if params['NotifyMail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[NotifyMail] can not be empty when [Notify] is 'E'." - end - elsif params['Notify'].to_s == 'A' - if params['Phone'].to_s.empty? or params['NotifyMail'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[Phone] and [NotifyMail] can not be empty when [Notify] is 'A'." - end - else - raise ECpayInvoiceRuleViolate, "Unexpected value in [Notify]." - end - - #c [InvoiceTag] is I,II,A,AI,AW [InvoiceNo] can not be empty or [InvoiceTag] is A,AI [AllowanceNo] can not be empty - if params['InvoiceTag'].to_s == 'I' or params['InvoiceTag'].to_s == 'II' or params['InvoiceTag'].to_s == 'AW' - if params['InvoiceNo'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[InvoiceNo] can not be empty." - end - elsif params['InvoiceTag'].to_s == 'A' or params['InvoiceTag'].to_s == 'AI' - if /^\d{16}$/.match(params['AllowanceNo']).nil? - raise ECpayInvoiceRuleViolate, "[AllowanceNo] must followed by 16 number characters when [InvoiceTag] is 'A' or 'AI'." - end - if params['InvoiceNo'].to_s.empty? - if params['AllowanceNo'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[InvoiceNo] and [AllowanceNo] can not be empty when [Notify] is 'A' or 'AI'." - end - raise ECpayInvoiceRuleViolate, "[InvoiceNo] can not be empty." - end - unless params['InvoiceNo'].to_s.empty? - if params['AllowanceNo'].to_s.empty? - raise ECpayInvoiceRuleViolate, "[AllowanceNo] can not be empty." - end - end - else - raise ECpayInvoiceRuleViolate, "Unexpected value in [InvoiceTag]." - end - - #Verify Value pattern of each param - self.verify_param_by_pattern(params, @all_param_pattern) - - else - raise TypeError, "Recieved argument is not a hash" - end - - end - end -end diff --git a/ecpay_invoice-2.0.0/lib/ecpay_invoice/version.rb b/ecpay_invoice-2.0.0/lib/ecpay_invoice/version.rb deleted file mode 100644 index 47aebb5..0000000 --- a/ecpay_invoice-2.0.0/lib/ecpay_invoice/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module ECpayInvoice - VERSION = '2.0.0' -end diff --git a/ecpay_invoice-2.0.2.gem b/ecpay_invoice-2.0.2.gem deleted file mode 100644 index 475bdcf..0000000 Binary files a/ecpay_invoice-2.0.2.gem and /dev/null differ diff --git a/ecpay_invoice-2.0.3.gem b/ecpay_invoice-2.0.3.gem new file mode 100644 index 0000000..0e3fc6c Binary files /dev/null and b/ecpay_invoice-2.0.3.gem differ