module WxPay::Service

Constants

GATEWAY_URL
GENERATE_APP_PAY_REQ_REQUIRED_FIELDS
INVOKE_GROUPREDPACK_FIELDS

发送裂变红包 api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack

INVOKE_QUERY_REDPACK_FIELDS

查询红包 api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo

INVOKE_REDPACK_FIELDS

发送红包 api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack

INVOKE_REFUND_REQUIRED_FIELDS
INVOKE_UNIFIEDORDER_REQUIRED_FIELDS

Public Class Methods

close_order(params) { |r| ... } click to toggle source

out_trade_no

# File lib/wx_pay/service.rb, line 47
def self.close_order(params)
  params = {
    appid: WxPay.appid,
    mch_id: WxPay.mch_id
  }.merge(params)
  r = invoke_remote("#{GATEWAY_URL}/pay/closeorder", params)
  yield(r) if block_given?
  r
end
generate_app_pay_req(params) click to toggle source
# File lib/wx_pay/service.rb, line 21
def self.generate_app_pay_req(params)
  params = {
    appid: WxPay.appid,
    partnerid: WxPay.mch_id,
    package: 'Sign=WXPay',
    timestamp: Time.now.to_i.to_s
  }.merge(params)
  check_required_options(params, GENERATE_APP_PAY_REQ_REQUIRED_FIELDS)
  params[:sign] = WxPay::Sign.generate(params)
  params
end
invoke_refund(params) { |r| ... } click to toggle source
# File lib/wx_pay/service.rb, line 34
def self.invoke_refund(params)
  params = {
    appid: WxPay.appid,
    mch_id: WxPay.mch_id,
    op_user_id: WxPay.mch_id
  }.merge(params)
  check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
  r = invoke_remote("#{GATEWAY_URL}/secapi/pay/refund", params, true)
  yield(r) if block_given?
  r
end
invoke_unifiedorder(params) { |r| ... } click to toggle source
# File lib/wx_pay/service.rb, line 9
def self.invoke_unifiedorder(params)
  params = {
    appid: WxPay.appid,
    mch_id: WxPay.mch_id
  }.merge(params)
  check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
  r = invoke_remote("#{GATEWAY_URL}/pay/unifiedorder", params)
  yield r if block_given?
  r
end
query_order(params) { |r| ... } click to toggle source

查询订单 transaction_id 与 out_trade_no 二选一

# File lib/wx_pay/service.rb, line 59
def self.query_order(params)
  params = {
    appid: WxPay.appid,
    mch_id: WxPay.mch_id
  }.merge(params)
  r = invoke_remote("#{GATEWAY_URL}/pay/orderquery", params)
  yield(r) if block_given?
  r
end
query_redpack(params) { |r| ... } click to toggle source
# File lib/wx_pay/service.rb, line 82
def self.query_redpack(params)
  check_required_options(params, INVOKE_QUERY_REDPACK_FIELDS)
  r = invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/gethbinfo", params, true)
  yield(r) if block_given?
  r
end
send_groupredpack(params) { |r| ... } click to toggle source
# File lib/wx_pay/service.rb, line 92
def self.send_groupredpack(params)
  check_required_options(params, INVOKE_GROUPREDPACK_FIELDS)
  r = invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/sendgroupredpack", params, true)
  yield(r) if block_given?
  r
end
send_redpack(params) { |r| ... } click to toggle source
# File lib/wx_pay/service.rb, line 72
def self.send_redpack(params)
  check_required_options(params, INVOKE_REDPACK_FIELDS)
  r = invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/sendredpack", params, true)
  yield(r) if block_given?
  r
end

Private Class Methods

apply_apiclient_cert(params) click to toggle source

微信退款需要双向证书 pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3

# File lib/wx_pay/service.rb, line 135
def self.apply_apiclient_cert(params)
  apiclient_cert = WxPay.apiclient_cert(params.delete(:cert_path), params[:mch_id])
  WxPay.extra_rest_client_options = {
    ssl_client_cert: apiclient_cert.certificate,
    ssl_client_key: apiclient_cert.key,
    verify_ssl: OpenSSL::SSL::VERIFY_NONE
  }
end
check_required_options(options, names) click to toggle source
# File lib/wx_pay/service.rb, line 101
def self.check_required_options(options, names)
  names.each do |name|
    warn("WxPay Warn: missing required option: #{name}") unless options.has_key?(name)
  end
end
invoke_remote(url, params, need_cert=false) click to toggle source
# File lib/wx_pay/service.rb, line 113
def self.invoke_remote(url, params, need_cert=false)
  apply_apiclient_cert(params) if need_cert
  params.merge!(nonce_str: SecureRandom.hex)
  payload = make_payload(params)
  r = RestClient::Request.execute(
    {
      method: :post,
      url: url,
      payload: payload,
      headers: { content_type: 'application/xml' }
    }.merge(WxPay.extra_rest_client_options)
  )
  if r
    WxPay::Result.new Hash.from_xml(r)
  else
    nil
  end
end
make_payload(params) click to toggle source
# File lib/wx_pay/service.rb, line 107
def self.make_payload(params)
  sign = WxPay::Sign.generate(params)
  params.delete(:key) if params[:key]
  "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
end