Module: Qtpay::Service

Defined in:
lib/qtpay/service.rb

Constant Summary collapse

GET_USER_TOKEN_REQUIRED_PARAMS =
%w( out_user )
CREATE_PRE_ORDER_REQUIRED_PARAMS =
%w( total_amt out_sn )
CREATE_ORDER_REQUIRED_PARAMS =
%w( token order_token total_amt pay_type pay_source goods_name )
MICROPAY_ORDER_REQUIRED_PARAMS =
%w( token order_token total_amt pay_type pay_source goods_name auth_code )
CREATE_SIMPLE_ORDER_REQUIRED_PARAMS =
%w( out_sn total_amt goods_name token )
GET_ORDER_REQUIRED_PARAMS =
%w(
REFUND_ORDER_REQUIRED_PARAMS =
%w(order_id)

Class Method Summary collapse

Class Method Details

.check_required_params(params, names) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/qtpay/service.rb', line 180

def self.check_required_params(params, names)
  return if !Qtpay.debug_mode?

  names.each do |name|
    warn("Qtpay Warn: missing required option: #{name}") unless params.has_key?(name)
  end
end

.create_order(params, options = {}) ⇒ Object

params ====

(required) token: auth token created in get_user_token order_token: order token created in create_pre_order total_amt: total payment amount in cents pay_type: payment type (1: alipay, 2: wechat) pay_source: payment source (4: scan code) goods_name: name of goods

(optional) pay_amt: payment amount balance_amt coupon_amt coupon_code point_amt point_num goods_info mobile openid: openid when creating wechat qrcode limit_pay



57
58
59
# File 'lib/qtpay/service.rb', line 57

def self.create_order(params, options = {})
  make_request(:post, create_order_url(params, options))
end

.create_order_url(params, options = {}) ⇒ Object



122
123
124
125
126
# File 'lib/qtpay/service.rb', line 122

def self.create_order_url(params, options = {})
  params = handle_params(params, CREATE_ORDER_REQUIRED_PARAMS, options)

  request_uri('/order/v1/create', params, options)
end

.create_pre_order(params, options = {}) ⇒ Object

params ====

(required) total_amt: total payment amount in cents out_sn: order serial number, must be uniq in all requests

(optional) mchnt_id: merchant id out_mchnt: developer defined merchant id token: auth token created in get_user_token qrcode: openid: openid when creating qrcode expire_time: order expires time, format YYYY-mm-dd HH:MM:SS



32
33
34
# File 'lib/qtpay/service.rb', line 32

def self.create_pre_order(params, options = {})
  make_request(:post, create_pre_order_url(params, options))
end

.create_pre_order_url(params, options = {}) ⇒ Object



115
116
117
118
119
# File 'lib/qtpay/service.rb', line 115

def self.create_pre_order_url(params, options = {})
  params = handle_params(params, CREATE_PRE_ORDER_REQUIRED_PARAMS, options)

  request_uri('/order/v1/pre_create', params, options)
end

.create_simple_order(params, options = {}) ⇒ Object

params ====

(required) total_amt: total payment amount in cents out_sn: order serial number, must be uniq in all requests goods_name: name of goods token: auth token created in get_user_token

(optional) expire_time: order expires time, format YYYY-mm-dd HH:MM:SS goods_info



82
83
84
# File 'lib/qtpay/service.rb', line 82

def self.create_simple_order(params, options = {})
  make_request(:post, create_simple_order_url(params, options))
end

.create_simple_order_url(params, options = {}) ⇒ Object



136
137
138
139
140
# File 'lib/qtpay/service.rb', line 136

def self.create_simple_order_url(params, options = {})
  params = handle_params(params, CREATE_SIMPLE_ORDER_REQUIRED_PARAMS, options)

  request_uri('/order/v1/simple_create', params, options)
end

.get_order(params, options = {}) ⇒ Object

params ====

(optional) token: auth token created in get_user_token, required when caller is app or h5 order_id: either order_id or out_sn is required out_sn: either order_id or out_sn is required



92
93
94
# File 'lib/qtpay/service.rb', line 92

def self.get_order(params, options ={})
  make_request(:get, get_order_url(params, options))
end

.get_order_url(params, options = {}) ⇒ Object



144
145
146
147
148
# File 'lib/qtpay/service.rb', line 144

def self.get_order_url(params, options = {})
  params = handle_params(params, GET_ORDER_REQUIRED_PARAMS, options)

  request_uri('/order/v1/query', params, options)
end

.get_user_token(params, options = {}) ⇒ Object

params ====

(required) out_user: customer uniq id

(optional) mobile: customer mobile number weixin_openid: WeiXin open id mchnt_id: merchant id out_mchnt: developer defined merchant id expires: token expires time in seconds (default 86400)



15
16
17
# File 'lib/qtpay/service.rb', line 15

def self.get_user_token(params, options = {})
  make_request(:get, get_user_token_url(params, options))
end

.get_user_token_url(params, options = {}) ⇒ Object



108
109
110
111
112
# File 'lib/qtpay/service.rb', line 108

def self.get_user_token_url(params, options = {})
  params = handle_params(params, GET_USER_TOKEN_REQUIRED_PARAMS, options)

  request_uri('/auth/v1/token', params, options)
end

.handle_params(params, required_params, options = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/qtpay/service.rb', line 157

def self.handle_params(params, required_params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, required_params)

  {
      'caller' => 'server',
      'app_code' => options[:app_code] || Qtpay.app_code,
  }.merge(params)
end

.make_request(request_type, url) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/qtpay/service.rb', line 188

def self.make_request(request_type, url)
  request_type = case request_type
    when :get
      :get_response
    when :post
      :post_form
    else
      request_type
  end

  if request_type == :get_response
    res = Net::HTTP.send(request_type, url)
  else
    res = Net::HTTP.send(request_type, url, {})
  end
  if res.respond_to?(:body)
    JSON.parse(res.body)
  else # unknown error
    res
  end

end

.micropay_order(params, options = {}) ⇒ Object

params ====

(required) auth_code: customer’s authentication code being scanned

other params are same as create_order



67
68
69
# File 'lib/qtpay/service.rb', line 67

def self.micropay_order(params, options = {})
  make_request(:post, micropay_order_url(params, options))
end

.micropay_order_url(params, options = {}) ⇒ Object



129
130
131
132
133
# File 'lib/qtpay/service.rb', line 129

def self.micropay_order_url(params, options = {})
  params = handle_params(params, MICROPAY_ORDER_REQUIRED_PARAMS, options)

  request_uri('/order/v1/micropay', params, options)
end

.refund_order(params, options = {}) ⇒ Object

params ====

(required) order_id

(optional) amt: amount



103
104
105
# File 'lib/qtpay/service.rb', line 103

def self.refund_order(params, options = {})
  make_request(:post, refund_order_url(params, options))
end

.refund_order_url(params, options = {}) ⇒ Object



151
152
153
154
155
# File 'lib/qtpay/service.rb', line 151

def self.refund_order_url(params, options = {})
  params = handle_params(params, REFUND_ORDER_REQUIRED_PARAMS, options)

  request_uri('/order/v1/refund', params, options)
end

.request_uri(path, params, options = {}) ⇒ Object



168
169
170
171
172
# File 'lib/qtpay/service.rb', line 168

def self.request_uri(path, params, options = {})
  uri = URI("#{Qtpay.gateway_url}#{path}")
  uri.query = URI.encode_www_form(sign_params(params, options))
  uri
end

.sign_params(params, options = {}) ⇒ Object



174
175
176
177
178
# File 'lib/qtpay/service.rb', line 174

def self.sign_params(params, options = {})
  params.merge(
      'sign' => Qtpay::Sign.generate(params, options)
  )
end