Module: CmbPay::Util

Defined in:
lib/cmb_pay/util.rb

Constant Summary collapse

MILLISECONDS_SINCE_UNIX_EPOCH =
Time.new(2000, 1, 1).to_i * 1000

Class Method Summary collapse

Class Method Details

.binary_to_hex(s) ⇒ Object



9
10
11
# File 'lib/cmb_pay/util.rb', line 9

def self.binary_to_hex(s)
  s.unpack('H*')[0].upcase
end

.cmb_bill_no(bill_no) ⇒ Object

招行定单号,6位或10位长数字,由商户系统生成,一天内不能重复



20
21
22
# File 'lib/cmb_pay/util.rb', line 20

def self.cmb_bill_no(bill_no)
  format('%010d', bill_no.to_i % 10_000_000_000)
end

.cmb_timestamp(t: nil) ⇒ Object



13
14
15
16
17
# File 'lib/cmb_pay/util.rb', line 13

def self.cmb_timestamp(t: nil)
  return t if t.is_a?(Integer)
  t = Time.now if t.nil?
  t.to_i * 1000 + (t.usec / 1000) - MILLISECONDS_SINCE_UNIX_EPOCH
end

.hex_to_binary(str) ⇒ Object



5
6
7
# File 'lib/cmb_pay/util.rb', line 5

def self.hex_to_binary(str)
  [str].pack('H*')
end