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 )
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



140
141
142
143
144
145
146
# File 'lib/qtpay/service.rb', line 140

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



97
98
99
100
101
# File 'lib/qtpay/service.rb', line 97

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



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

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

.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



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

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

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



104
105
106
107
108
# File 'lib/qtpay/service.rb', line 104

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



83
84
85
86
87
# File 'lib/qtpay/service.rb', line 83

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



117
118
119
120
121
122
123
124
125
# File 'lib/qtpay/service.rb', line 117

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/qtpay/service.rb', line 148

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

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

params ====

(required) order_id

(optional) amt: amount



78
79
80
# File 'lib/qtpay/service.rb', line 78

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

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



111
112
113
114
115
# File 'lib/qtpay/service.rb', line 111

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



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

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



134
135
136
137
138
# File 'lib/qtpay/service.rb', line 134

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