Module: Wechatpay::Utils

Defined in:
lib/wechatpay/utils.rb

Class Method Summary collapse

Class Method Details

.add_sign_and_generate_xml_body(options) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/wechatpay/utils.rb', line 28

def add_sign_and_generate_xml_body(options)
  with_hash(options) do
    options[:sign] = Wechatpay::Sign.md5(options)
    options = Wechatpay::Utils.cdata(options)
    Wechatpay::Utils.to_xml(options)
  end
end

.cdata(hash) ⇒ Object



4
5
6
7
8
# File 'lib/wechatpay/utils.rb', line 4

def cdata(hash)
  with_hash(hash) do
    hash.each { |k, v| hash[k] = cdata_cell(v) }
  end
end

.cdata_cell(item) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/wechatpay/utils.rb', line 10

def cdata_cell(item)
  if item.is_a? Numeric
    item
  else
    "<![CDATA[#{item}]]>"
  end
end

.nonce_strObject



48
49
50
# File 'lib/wechatpay/utils.rb', line 48

def nonce_str
  SecureRandom.urlsafe_base64(nil, false)
end

.timestampObject



44
45
46
# File 'lib/wechatpay/utils.rb', line 44

def timestamp
  Time.now.to_i
end

.to_xml(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/wechatpay/utils.rb', line 18

def to_xml(hash)
  with_hash(hash) do
    str = "<xml>\n"
    hash.each do |k, v|
      str << "<#{k}>#{v}</#{k}>\n"
    end
    str << "</xml>"
  end
end

.with_hash(hash) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/wechatpay/utils.rb', line 36

def with_hash(hash)
  if hash.is_a? Hash
    yield hash
  else
    raise ArgumentError
  end
end