Module: Shouqianba::Webapi

Defined in:
lib/shouqianba/webapi.rb,
lib/shouqianba/webapi/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

ShouqianbaHost =
"https://api.shouqianba.com"
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.activate(vendor_sn, vendor_key, app_id, code, device_id, options = {}) ⇒ Object

you code goes here



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shouqianba/webapi.rb', line 20

def self.activate(vendor_sn, vendor_key, app_id, code, device_id, options={})
  params = {
    app_id: app_id,
    code:   code,
    device_id: device_id
  }.merge({
    name: options[:name],
    os_info: options[:os_info],
    sdk_version: options[:sdk_version]
    })

  self.post_method "/terminal/activate", vendor_sn, vendor_key, {}, params do |resp|
    yield(resp)
  end
end

.cancel(terminal_sn, terminal_key, options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/shouqianba/webapi.rb', line 113

def self.cancel(terminal_sn, terminal_key, options={})
  params = {
    terminal_sn: terminal_sn
    }.merge({
      sn: options[:sn],
      client_sn: options[:client_sn]
    })
  self.post_method "/upay/v2/cancel", terminal_sn, terminal_key, {}, params do |resp|
    yield(resp)
  end
end

.check_errcode(data) ⇒ Object



183
184
185
186
187
188
# File 'lib/shouqianba/webapi.rb', line 183

def self.check_errcode(data)
  Rails.logger.info data
  if data[:result_code].present? && data[:result_code] != "200"
    raise Shouqianba::Webapi::Error.new(data[:result_code], data[:error_message], data)
  end
end

.checkin(terminal_sn, terminal_key, device_id, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shouqianba/webapi.rb', line 36

def self.checkin(terminal_sn, terminal_key, device_id, options={})
  params = {
    terminal_sn: terminal_sn,
    device_id: device_id
    }.merge({
      os_info: options[:os_info],
      sdk_version: options[:sdk_version]
    })
  self.post_method "/terminal/checkin", terminal_sn, terminal_key, {}, params do |resp|
    yield(resp)
  end
end

.get_sign(body, vendor_or_terminal_key) ⇒ Object



163
164
165
# File 'lib/shouqianba/webapi.rb', line 163

def self.get_sign(body, vendor_or_terminal_key)
  Digest::MD5.hexdigest(body+ vendor_or_terminal_key)
end

.get_wrap2_sign(params, terminal_key) ⇒ Object



158
159
160
# File 'lib/shouqianba/webapi.rb', line 158

def self.get_wrap2_sign(params, terminal_key)
  Digest::MD5.hexdigest("#{params.to_query}&#{terminal_key}").upcase
end

.pay(terminal_sn, terminal_key, client_sn, total_amount, dynamic_id, subject, operator, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shouqianba/webapi.rb', line 49

def self.pay(terminal_sn, terminal_key, client_sn, total_amount, dynamic_id, subject, operator, options={})
  params = {
    terminal_sn: terminal_sn,
    client_sn: client_sn,
    total_amount: total_amount,
    dynamic_id: dynamic_id,
    subject: subject,
    operator: operator,
    }.merge({
      payway: options[:payway],
      description: options[:description],
      longitude: options[:longitude],
      latitude: options[:latitude],
      device_id: options[:device_id],
      extended: options[:extended],
      reflect: options[:reflect],
      notify_url: options[:notify_url]
    })
  self.post_method "/upay/v2/pay", terminal_sn, terminal_key, {}, params do |resp|
    yield(resp)
  end
end

.post_method(url, vendor_or_terminal_sn, vendor_or_terminal_key, params = {}, body = {}) {|| ... } ⇒ Object

如果body是字符串,则直接作为请求的内容。如果body是其他类型,则会在内部调用to_json

Yields:

  • ()


168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/shouqianba/webapi.rb', line 168

def self.post_method(url, vendor_or_terminal_sn, vendor_or_terminal_key, params={}, body={})
  uri = URI.parse("#{ShouqianbaHost}#{url}?#{params.to_query}")
  https = Net::HTTP.new(uri.host,uri.port)
  https.open_timeout = 60
  https.read_timeout = 60
  https.use_ssl = true
  body = body.is_a?(String) ? body : JSON.generate(body)
  req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json', 'Authorization' => "#{vendor_or_terminal_sn} #{self.get_sign(body, vendor_or_terminal_key)}"})
  req.body = body
  response = https.request(req)
  data = ActiveSupport::JSON.decode(response.body).to_options
  check_errcode(data)
  yield(data[:biz_response])
end

.precreate(terminal_sn, terminal_key, client_sn, total_amount, payway, subject, operator, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shouqianba/webapi.rb', line 73

def self.precreate(terminal_sn, terminal_key, client_sn, total_amount, payway, subject, operator, options={})
  params = {
    terminal_sn: terminal_sn,
    client_sn: client_sn,
    total_amount: total_amount,
    payway: payway,
    subject: subject,
    operator: operator
    }.merge({
      sub_payway: options[:sub_payway],
      payer_uid: options[:payer_uid],
      description: options[:description],
      longitude: options[:longitude],
      latitude: options[:latitude],
      extended: options[:extended],
      reflect: options[:reflect],
      notify_url: options[:notify_url]
    })
  self.post_method "/upay/v2/precreate", terminal_sn, terminal_key, {}, params do |resp|
    yield(resp)
  end
end

.query(terminal_sn, terminal_key, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/shouqianba/webapi.rb', line 125

def self.query(terminal_sn, terminal_key, options={})
  params = {
    terminal_sn: terminal_sn
  }.merge({
    sn: options[:sn],
    client_sn: options[:client_sn]
  })
  self.post_method "/upay/v2/query", terminal_sn, terminal_key, {}, params do |resp|
    yield(resp)
  end
end

.refund(terminal_sn, terminal_key, refund_request_no, operator, refund_amount, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/shouqianba/webapi.rb', line 96

def self.refund(terminal_sn, terminal_key, refund_request_no, operator, refund_amount, options={})
  params = {
    terminal_sn: terminal_sn,
    refund_request_no: refund_request_no,
    operator: operator,
    refund_amount: refund_amount
    }.merge({
      sn: options[:sn],
      client_sn: options[:client_sn],
      client_tsn: options[:client_tsn],

    })
  self.post_method "/upay/v2/refund", terminal_sn, terminal_key, {}, params do |resp|
    yield(resp)
  end
end

.wap2_url(terminal_sn, terminal_key, client_sn, total_amount, subject, operator, return_url, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/shouqianba/webapi.rb', line 137

def self.wap2_url(terminal_sn, terminal_key, client_sn, total_amount, subject, operator, return_url, options={})
  params = {
    terminal_sn: terminal_sn,
    client_sn: client_sn,
    total_amount: total_amount,
    subject: subject,
    operator: operator,
    return_url: return_url
  }.merge({
    payway: options[:payway],
    description: options[:description],
    longitude: options[:longitude],
    latitude: options[:latitude],
    extended: options[:extended],
    reflect: options[:reflect],
    notify_url: options[:notify_url]
    })
  params[:sign] = get_wrap2_sign(params, terminal_key)
  "https://m.wosai.cn/qr/gateway?#{params.to_query}"
end