module Qtpay::Service

Constants

CREATE_ORDER_REQUIRED_PARAMS
CREATE_PRE_ORDER_REQUIRED_PARAMS
CREATE_SIMPLE_ORDER_REQUIRED_PARAMS
GET_ORDER_REQUIRED_PARAMS
GET_USER_TOKEN_REQUIRED_PARAMS
MICROPAY_ORDER_REQUIRED_PARAMS
REFUND_ORDER_REQUIRED_PARAMS

Public Class Methods

check_required_params(params, names) click to toggle source
# 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 = {}) click to toggle source

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

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

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

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

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

# 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 = {}) click to toggle source
# 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 ={}) click to toggle source

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

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

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)

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source

params ====

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

other params are same as create_order

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

params ====

(required) order_id

(optional) amt: amount

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/qtpay/service.rb, line 174
def self.sign_params(params, options = {})
  params.merge(
      'sign' => Qtpay::Sign.generate(params, options)
  )
end