Class: JwAlipay::Helper

Inherits:
Object
  • Object
show all
Includes:
Sign
Defined in:
lib/jw_alipay/helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sign

#_md5_sign, #_query_string, #_sign_string, #_sort_params, #sign, #verify_sign

Constructor Details

#initialize(options = {}) ⇒ Helper

Returns a new instance of Helper.



7
8
9
10
11
12
13
# File 'lib/jw_alipay/helper.rb', line 7

def initialize(options = {})
  @fields = {}
  @datum = {}
  @xml_root = options[:xml_root]
  add_field('partner', PARTNER)
  add_field('_input_charset', 'utf-8')
end

Instance Attribute Details

#datum ⇒ Object (readonly)

Returns the value of attribute datum.



5
6
7
# File 'lib/jw_alipay/helper.rb', line 5

def datum
  @datum
end

#fields ⇒ Object (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/jw_alipay/helper.rb', line 5

def fields
  @fields
end

#xml_root ⇒ Object (readonly)

Returns the value of attribute xml_root.



5
6
7
# File 'lib/jw_alipay/helper.rb', line 5

def xml_root
  @xml_root
end

Instance Method Details

#add_field(name, value) ⇒ Object



15
16
17
18
# File 'lib/jw_alipay/helper.rb', line 15

def add_field(name, value)
  return if name.blank? || value.blank?
  self.fields[name.to_s] = value.to_s
end

#add_fields(params) ⇒ Object



20
21
22
23
24
# File 'lib/jw_alipay/helper.rb', line 20

def add_fields(params)
  (params || {}).each do |k, v|
    add_field(k, v)
  end
end

#add_xml_field(name, value) ⇒ Object



26
27
28
# File 'lib/jw_alipay/helper.rb', line 26

def add_xml_field(name, value)
  self.datum[name.to_s] = value.to_s
end

#add_xml_fields(params) ⇒ Object



30
31
32
33
34
# File 'lib/jw_alipay/helper.rb', line 30

def add_xml_fields(params)
  (params || {}).each do |k, v|
    add_xml_field(k, v)
  end
end

#query_string ⇒ Object



41
42
43
# File 'lib/jw_alipay/helper.rb', line 41

def query_string
  sign(self.fields)
end

#xml_string ⇒ Object



36
37
38
39
# File 'lib/jw_alipay/helper.rb', line 36

def xml_string
  xml = self.datum.to_xml(:skip_instruct => true, :root => self.xml_root, :skip_types => true)
  xml.gsub("-", "_").gsub("\n", "").gsub(" ", "")
end