Commit 9752fa1164fc73d44cad83fc636f9987a0d19a43

Authored by Theo
1 parent ee0099fd
Exists in master

new feature: 微信企业付款

Showing 2 changed files with 33 additions and 14 deletions   Show diff stats
lib/wx_pay/service.rb
... ... @@ -49,19 +49,26 @@ module WxPay
49 49  
50 50 check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
51 51  
52   - # 微信退款需要双向证书
53   - # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
54   - # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
  52 + r = invoke_remote_with_cert("#{GATEWAY_URL}/secapi/pay/refund", make_payload(params))
55 53  
56   - WxPay.extra_rest_client_options = {
57   - ssl_client_cert: WxPay.apiclient_cert.certificate,
58   - ssl_client_key: WxPay.apiclient_cert.key,
59   - verify_ssl: OpenSSL::SSL::VERIFY_NONE
60   - }
  54 + yield(r) if block_given?
61 55  
62   - r = invoke_remote "#{GATEWAY_URL}/secapi/pay/refund", make_payload(params)
  56 + r
  57 + end
63 58  
64   - yield(r) if block_given?
  59 + INVOKE_TRANSFER_REQUIRED_FIELDS = %i(partner_trade_no openid check_name amount desc spbill_create_ip)
  60 + def self.invoke_transfer params
  61 + params = {
  62 + mch_appid: WxPay.appid,
  63 + mchid: WxPay.mch_id,
  64 + nonce_str: SecureRandom.uuid.tr('-', '')
  65 + }.merge(params)
  66 +
  67 + check_required_options(params, INVOKE_TRANSFER_REQUIRED_FIELDS)
  68 +
  69 + r = invoke_remote_with_cert("#{GATEWAY_URL}/mmpaymkttransfers/promotion/transfers", make_payload(params))
  70 +
  71 + yield r if block_given?
65 72  
66 73 r
67 74 end
... ... @@ -81,14 +88,26 @@ module WxPay
81 88 "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
82 89 end
83 90  
84   - def self.invoke_remote(url, payload)
  91 + def self.invoke_remote_with_cert(url, payload)
  92 + # 微信退款、企业付款等需要双向证书
  93 + # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
  94 + # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
  95 +
  96 + invoke_remote(url, payload, {
  97 + ssl_client_cert: WxPay.apiclient_cert.certificate,
  98 + ssl_client_key: WxPay.apiclient_cert.key,
  99 + verify_ssl: OpenSSL::SSL::VERIFY_NONE
  100 + })
  101 + end
  102 +
  103 + def self.invoke_remote(url, payload, extra_rest_client_options = {})
85 104 r = RestClient::Request.execute(
86 105 {
87 106 method: :post,
88 107 url: url,
89 108 payload: payload,
90 109 headers: { content_type: 'application/xml' }
91   - }.merge(WxPay.extra_rest_client_options)
  110 + }.merge(WxPay.extra_rest_client_options).merge(extra_rest_client_options)
92 111 )
93 112  
94 113 if r
... ...
lib/wx_pay/sign.rb
... ... @@ -6,8 +6,8 @@ module WxPay
6 6 key = params.delete(:key)
7 7  
8 8 query = params.sort.map do |key, value|
9   - "#{key}=#{value}"
10   - end.join('&')
  9 + "#{key}=#{value}" if value != "" && !value.nil?
  10 + end.compact.join('&')
11 11  
12 12 Digest::MD5.hexdigest("#{query}&key=#{key || WxPay.key}").upcase
13 13 end
... ...