class NkuhtDonateMain include Mongoid::Document include Mongoid::Timestamps include OrbitModel::Impression # encoding: utf-8 field :name field :donors_service_unit field :tel field :email field :donation_way, :default => 1 field :cheque_number field :donation_amount field :donation_use_other field :is_alumni, :type => Boolean, :default => true field :graduation_year field :has_receipt, :type => Boolean, :default => true field :receipt_title, :default => 1 field :receipt_title_other field :receipt_number field :receipt_address, :default => 1 field :receipt_address_other field :donate_public, :type => Boolean, :default => true field :note field :create_user_id field :update_user_id field :donation_serial_date, :type => String, :default => Date.current #新增日 field :donation_serial_time, :type => String, :default => Time.current #新增日時分 field :donation_serial_number #流水號 field :donation_payment_number #銷帳編號 & 訂單編號 field :donation_payment_status, :type => Boolean, :default => false #付款狀態 field :donation_payment_status_msg #付款失敗訊息代碼 field :donation_write_off_status, :type => Boolean, :default => false #銷帳狀態 belongs_to :nkuht_donate_unit belongs_to :nkuht_donate_department belongs_to :nkuht_donate_use validates_numericality_of :donation_amount, :only_integer => true #10335+年月日(8)+流水號(1~999) def self.get_payment_number @number = '10335' + Date.current.strftime("%Y%m%d") + get_serial_number end def self.get_serial_number number = "%03d" % ( NkuhtDonateMain.where(:donation_serial_date=>Date.current).count + 1 ) # return number.rjust(3, '0') end def get_atm_mac_number #年月日時分秒+流水號4碼 == 傳送序號 #傳送序號+特店代碼+訂單編號+銷帳編號+金額 sendseqno = self.donation_serial_time.to_datetime.strftime("%Y%m%d%H%M%S") + "%04d" % (self.donation_serial_number) message = Digest::SHA1.hexdigest("#{sendseqno}T00774955421#{self.donation_payment_number}#{self.donation_payment_number}#{self.donation_amount}") key = 'S8G46IY64UHZZBSKGZFI6D7E' iv = '00000000' message = [message+iv].pack('H*') # Encrypt plaintext using Triple DES cipher = OpenSSL::Cipher::Cipher.new("des-ede3-cbc") cipher.padding = 0 cipher.key = key cipher.iv = iv cipher.encrypt # Call this before setting key or iv ciphertext = cipher.update(message) # ciphertext << cipher.final # Base64-encode the ciphertext encodedCipherText = Base64.strict_encode64(ciphertext) # encodedCipherText = Base64.strict_encode64(ciphertext) return encodedCipherText end def get_market_number #’A’:1, ’B’:2, ’C’:3, ’D’:4, ’E’:5, ’F’:6, ’G’:7, ’H’:8, ’I’:9 # 第一段(9碼) 繳費期限yymmdd (6碼) + 代收項目6HC (3碼) @date = self.donation_serial_date.to_date + 14 #到期日 @pdate = (@date.strftime("%Y").to_i - 1911 ).to_s[-2..-1] + @date.strftime("%m%d") @ckno1 = @pdate + '683' @no1 = @pdate + '6HC' # 第二段(16碼) 交易序號由業者自行訂定(前5碼銀行編 貴校為10335,且16碼必須全為數字) @no2 = self.donation_payment_number # 第三段(15碼) 應繳日期 mmdd (4碼) + 檢碼(2碼) + 應繳金額(9碼) @ckno3 = @pdate[-4..-1].to_s + ("%09d" % self.donation_amount) @key1 = NkuhtDonateMain.get_market_number_key1(@ckno1, @no2, @ckno3) @key2 = NkuhtDonateMain.get_market_number_key2(@ckno1, @no2, @ckno3) @no3 = @pdate[-4..-1].to_s + @key1 + @key2 + ("%09d" % self.donation_amount) return [@no1,@no2,@no3] end def self.get_market_number_key1(code1,code2,code3) @n1 = [] (0..8).each{|a| @n1 << code1[a].to_i if a % 2 == 0 } @n2 = [] (0..15).each{|a| @n2 << code2[a].to_i if a % 2 == 0 } @n3 = [] (0..12).each{|a| @n3 << code3[a].to_i if a % 2 == 0 } @key1 = (( @n1.sum + @n2.sum + @n3.sum ) % 11).to_s if @key1 == '0' return 'A' elsif @key1 == '10' return 'B' else return @key1 end end def self.get_market_number_key2(code1,code2,code3) @n1 = [] (0..8).each{|a| @n1 << code1[a].to_i if a % 2 != 0 } @n2 = [] (0..15).each{|a| @n2 << code2[a].to_i if a % 2 != 0 } @n3 = [] (0..12).each{|a| @n3 << code3[a].to_i if a % 2 != 0 } @key2 = (( @n1.sum + @n2.sum + @n3.sum ) % 11).to_s if @key2 == '0' return 'X' elsif @key2 == '10' return 'Y' else return @key2 end end # 轉大寫數字 def self.change_cash( cash ) cNum = ["零","壹","貳","叁","肆","伍","陸","柒","捌","玖","-","-","萬","仟","佰","拾","億","仟","佰","拾","萬","仟","佰","拾",""] i = 0 sNum = "" sTemp = "" result = "" tmp = cash.to_i return '零' if tmp == 0 raise '整數部分加二位小數長度不能大於15' if tmp.to_s.size > 15 sNum = tmp.to_s.rjust(15, ' ') for i in 0..14 stemp = sNum.slice(i, 1) if stemp == ' ' next else result += " " + cNum[stemp.to_i] + " " + cNum[i + 10] end end return result end end