Commit 6603caf7c9913e3171a0eca8b6bb71a3785998f6

Authored by Jun Jiang
2 parents 992064fa f305e18b
Exists in master

Merge pull request #17 from bbtfr/master

微信企业付款
Showing 2 changed files with 34 additions and 15 deletions   Show diff stats
lib/wx_pay/service.rb
... ... @@ -38,7 +38,7 @@ module WxPay
38 38 params
39 39 end
40 40  
41   - INVOKE_REFUND_REQUIRED_FIELDS = %i(transaction_id out_trade_no out_refund_no total_fee refund_fee)
  41 + INVOKE_REFUND_REQUIRED_FIELDS = %i(out_refund_no total_fee refund_fee)
42 42 def self.invoke_refund(params)
43 43 params = {
44 44 appid: WxPay.appid,
... ... @@ -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
... ... @@ -141,14 +148,26 @@ module WxPay
141 148 "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
142 149 end
143 150  
144   - def self.invoke_remote(url, payload)
  151 + def self.invoke_remote_with_cert(url, payload)
  152 + # 微信退款、企业付款等需要双向证书
  153 + # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
  154 + # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
  155 +
  156 + invoke_remote(url, payload, {
  157 + ssl_client_cert: WxPay.apiclient_cert.certificate,
  158 + ssl_client_key: WxPay.apiclient_cert.key,
  159 + verify_ssl: OpenSSL::SSL::VERIFY_NONE
  160 + })
  161 + end
  162 +
  163 + def self.invoke_remote(url, payload, extra_rest_client_options = {})
145 164 r = RestClient::Request.execute(
146 165 {
147 166 method: :post,
148 167 url: url,
149 168 payload: payload,
150 169 headers: { content_type: 'application/xml' }
151   - }.merge(WxPay.extra_rest_client_options)
  170 + }.merge(WxPay.extra_rest_client_options).merge(extra_rest_client_options)
152 171 )
153 172  
154 173 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
... ...