Class: Qpay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/qpay/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/qpay/client.rb', line 6

def initialize(options = {})
  @config = Config.new
  @config.appid = options[:appid]
  @config.mch_id = options[:mch_id]
  @config.app_key = options[:app_key]
  @config.api_key = options[:api_key]
  @config.cert_type = options[:cert_type]
  @config.cert_file = options[:cert_file]
  @config.cert_password = options[:cert_password]
  @config.adapter = options[:adapter] || :patron
end

Instance Method Details

#apiObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/qpay/client.rb', line 22

def api
  @api ||= case config.adapter
             when :httparty
               require 'httparty'
               c = Class.new do
                 include ::HTTParty
                 base_uri 'https://qpay.qq.com/cgi-bin'
                 disable_rails_query_string_format
                 default_timeout 10
                 headers 'Content-Type' => 'application/xml'

                 def self.post(url, body, format:)
                   super(url, body: body, format: format).parsed_response
                 end
               end

               c.default_options.delete(:p12)
               c.default_options.delete(:p12_password)
               c.default_options.delete(:pem)
               c.default_options.delete(:pem_password)

               # set cert option
               if @config.cert_type && @config.cert_file && File.exist?(@config.cert_file)
                 c..public_send(@config.cert_type.to_sym, File.read(@config.cert_file), @config.cert_password)
               end
               c
             when :patron
               require 'patron'
               Class.new do
                 def self.post(url, body, format:)
                   s = Patron::Session.new(base_url: 'https://qpay.qq.com/cgi-bin',
                                           timeout: 10,
                                           headers: {'Content-Type' => 'application/xml'},
                                           force_ipv4: true)
                   body = s.post(url, body).body
                   case format
                     when :xml
                       MultiXml.parse(body)
                     when :csv
                       require 'csv'
                       CSV.parse(body)
                   end
                 end
               end
           end
end

#closeorder(out_trade_no, more_params = {}) ⇒ Object

以下情况需要调用关单接口:

* 

注意:订单生成后不能马上调用关单接口,最短调用时间间隔为5分钟。



100
101
102
103
104
# File 'lib/qpay/client.rb', line 100

def closeorder(out_trade_no, more_params = {})
  params = more_params.merge(out_trade_no: out_trade_no)

  api.post('/pay/qpay_close_order.cgi', request_params(params), format: :xml)
end

#configObject



18
19
20
# File 'lib/qpay/client.rb', line 18

def config
  @config.dup
end

#downloadbill(bill_date, more_params = {}) ⇒ Object

商户可以通过该接口下载历史交易清单。

注意:

1


137
138
139
140
141
142
# File 'lib/qpay/client.rb', line 137

def downloadbill(bill_date, more_params = {})
  params = more_params.merge(bill_date: Time.parse(bill_date.to_s).strftime('%Y%m%d'))

  # csv data
  api.post("/sp_download/qpay_mch_statement_down.cgi", request_params(params), format: :csv)
end

#orderquery(out_trade_no, more_params = {}) ⇒ Object

该接口提供所有手Q钱包订单的查询,商户可以通过该接口主动查询订单状态,完成下一步的业务逻辑。

需要调用查询接口的情况:

* 


89
90
91
92
93
# File 'lib/qpay/client.rb', line 89

def orderquery(out_trade_no, more_params = {})
  params = more_params.merge(out_trade_no: out_trade_no)

  api.post('/pay/qpay_order_query.cgi', request_params(params), format: :xml)
end

#refund(out_trade_no, out_refund_no, total_fee, refund_fee, more_params = {}) ⇒ Object

当交易发生之后一段时间内,由于买家或者卖家的原因需要退款时,卖家可以通过退款接口将支付款退还给买家,QQ钱包将在收到退款请求并且验证成功之后,按照退款规则将支付款按原路退到买家帐号上。

注意:

1.


112
113
114
115
116
117
118
119
120
121
# File 'lib/qpay/client.rb', line 112

def refund(out_trade_no, out_refund_no, total_fee, refund_fee, more_params = {})
  params = more_params.merge(out_trade_no: out_trade_no,
                             out_refund_no: out_refund_no,
                             total_fee: total_fee,
                             refund_fee: refund_fee)
  params[:refund_fee_type] ||= 'CNY'
  params[:op_user_id] ||= Qpay.config.mch_id

  api.post('/pay/qpay_refund.cgi', request_params(params), format: :xml)
end

#refundquery(out_trade_no, more_params = {}) ⇒ Object

提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,用余额支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。



124
125
126
127
128
# File 'lib/qpay/client.rb', line 124

def refundquery(out_trade_no, more_params = {})
  params = more_params.merge(out_trade_no: out_trade_no)

  api.post('/pay/qpay_refund_query.cgi', request_params(params), format: :xml)
end

#unifiedorder(body, out_trade_no, total_fee, spbill_create_ip, notify_url, more_params = {}) ⇒ Object

除被扫支付场景以外,商户系统先调用该接口在QQ钱包服务后台生成预支付交易单,返回正确的预支付交易回话标识后再按具体的场景生成交易串调起支付。



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qpay/client.rb', line 70

def unifiedorder(body, out_trade_no, total_fee, spbill_create_ip, notify_url, more_params = {})
  params = more_params.merge(body: body,
                             out_trade_no: out_trade_no,
                             total_fee: total_fee,
                             spbill_create_ip: spbill_create_ip,
                             notify_url: notify_url)
  params[:fee_type] ||= 'CNY'
  params[:time_start] ||= Time.now.strftime('%Y%m%d%H%M%S')
  params[:trade_type] ||= 'APP'

  api.post('/pay/qpay_unified_order.cgi', request_params(params), format: :xml)
end