From 10065a090522ae4538f722c4a1d3008791ddd8bc Mon Sep 17 00:00:00 2001 From: qinmingyuan <279378275@qq.com> Date: Sun, 10 Nov 2024 22:16:51 +0800 Subject: [PATCH] use md5 --- app/apis/wx_pay/api/mch.rb | 2 +- app/apis/wx_pay/api/partner.rb | 4 ++-- app/apis/wx_pay/sign/hmac.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/apis/wx_pay/api/mch.rb b/app/apis/wx_pay/api/mch.rb index ad5afdca..ee1cd32c 100644 --- a/app/apis/wx_pay/api/mch.rb +++ b/app/apis/wx_pay/api/mch.rb @@ -81,7 +81,7 @@ def pay_micropay(out_trade_no:, auth_code:, body:, total_fee:, spbill_create_ip: opts.merge! sign: WxPay::Sign::Hmac.generate(opts, key: @mch.key) r = @client.with_options(origin: BASE, debug: Rails.logger.broadcasts[0].instance_values['logdev'].dev, debug_level: 2) - .post('pay/micropay', body: opts.to_xml(root: 'xml', skip_types: true, skip_instruct: true, dasherize: false)) + .post('/pay/micropay', body: opts.to_xml(root: 'xml', skip_types: true, skip_instruct: true, dasherize: false)) Hash.from_xml(r.to_s)['xml'] end diff --git a/app/apis/wx_pay/api/partner.rb b/app/apis/wx_pay/api/partner.rb index 4e493196..e3a7dfc5 100644 --- a/app/apis/wx_pay/api/partner.rb +++ b/app/apis/wx_pay/api/partner.rb @@ -79,10 +79,10 @@ def pay_micropay(out_trade_no:, auth_code:, body:, total_fee:, spbill_create_ip: auth_code: auth_code, **v2_common_payee_params } - opts.merge! sign: WxPay::Sign::Hmac.generate(opts, key: @mch.key) + opts.merge! sign: WxPay::Sign::Hmac.generate_md5(opts, key: @mch.key) r = @client.with_options(origin: BASE, debug: Rails.logger.broadcasts[0].instance_values['logdev'].dev, debug_level: 2) - .post('pay/micropay', body: opts.to_xml(root: 'xml', skip_types: true, skip_instruct: true, dasherize: false)) + .post('/pay/micropay', body: opts.to_xml(root: 'xml', skip_types: true, skip_instruct: true, dasherize: false)) Hash.from_xml(r.to_s)['xml'] end diff --git a/app/apis/wx_pay/sign/hmac.rb b/app/apis/wx_pay/sign/hmac.rb index 780be70c..fee2ad4b 100644 --- a/app/apis/wx_pay/sign/hmac.rb +++ b/app/apis/wx_pay/sign/hmac.rb @@ -13,5 +13,14 @@ def generate(params, key:) OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, string_sign_temp).upcase end + def generate_md5(params, key:) + query = params.compact_blank.sort.map.each do |k, v| + "#{k}=#{v}" + end.join('&') + string_sign_temp = "#{query}&key=#{key}" + + Digest::MD5.hexdigest(string_sign_temp).upcase + end + end end