Module: EricWeixin::Pay
- Defined in:
- lib/eric_weixin/modules/pay.rb
Class Method Summary collapse
- .generate_prepay_id(options) ⇒ Object
-
.generate_sign(options, api_key) ⇒ Object
产生签名.
-
.gethbinfo(options) ⇒ Object
根据红包单号,获取红包基础数据。 mch_billno mch_id appid mch_key.
-
.sendredpack(options) ⇒ Object
发红包,直接调用。 #参数 wxappid re_openid total_amount wishing client_ip act_name remark mch_id send_name total_num mch_key.
Class Method Details
.generate_prepay_id(options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/eric_weixin/modules/pay.rb', line 4 def self.generate_prepay_id required_field = %i(appid mch_id openid attach body out_trade_no total_fee spbill_create_ip notify_url trade_type) = {} required_field.each do |p| [p] = [p] end [:nonce_str] = SecureRandom.uuid.tr('-', '') public_account = ::EricWeixin::PublicAccount.find_by_weixin_app_id([:appid]) sign = generate_sign , public_account.mch_key = [:sign] = sign pay_load = "<xml>#{.map { |k, v| "<#{k.to_s}>#{v.to_s}</#{k.to_s}>" }.join}</xml>" require 'rest-client' response = RestClient.post 'https://api.mch.weixin.qq.com/pay/unifiedorder', pay_load pp "**********************下单结果 *********************************" pp response.force_encoding("UTF-8") result = Hash.from_xml(response.force_encoding("UTF-8")) return result['xml']['prepay_id'] if result['xml']['return_code'] == 'SUCCESS' nil end |
.generate_sign(options, api_key) ⇒ Object
产生签名
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/eric_weixin/modules/pay.rb', line 28 def self.generate_sign , api_key pp "**************** 签名参数 *********************" pp pp "**************** api_key *********************" pp api_key query = .sort.map do |k,v| "#{k.to_s}=#{v.to_s}" end.join('&') Digest::MD5.hexdigest("#{query}&key=#{api_key}").upcase end |
.gethbinfo(options) ⇒ Object
根据红包单号,获取红包基础数据。 mch_billno mch_id appid mch_key
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/eric_weixin/modules/pay.rb', line 90 def self.gethbinfo BusinessException.raise '请提供商户订单号。' if [:mch_billno].blank? BusinessException.raise '请提供商户号。' if [:mch_id].blank? BusinessException.raise '请提供公众账号ID。' if [:appid].blank? BusinessException.raise '请提供mch_key。' if [:mch_key].blank? [:nonce_str] = SecureRandom.uuid.tr('-', '') [:bill_type] = 'MCHT' api_key = [:mch_key] .delete(:mch_key) sign = generate_sign , api_key # sign = generate_sign options, options[:mch_key] [:sign] = sign pay_load = "<xml>#{.map { |k, v| "<#{k.to_s}>#{v.to_s}</#{k.to_s}>" }.join}</xml>" require 'rest-client' ca_path = Rails.root.join("ca/").to_s Dir.mkdir ca_path unless Dir.exist? ca_path BusinessException.raise '请下载证书' unless File.exist?(ca_path+"apiclient_cert.pem") && File.exist?(ca_path+"apiclient_key.pem") response = RestClient::Request.execute(method: :post, url: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo', ssl_client_cert: OpenSSL::X509::Certificate.new(File.read(ca_path+"apiclient_cert.pem")), ssl_client_key: OpenSSL::PKey::RSA.new(File.read(ca_path+"apiclient_key.pem"), "passphrase, if any"), ssl_ciphers: 'AESGCM:!aNULL', payload: pay_load) # 分析查询红包结果 pp "********************** 红包查询 结果 ******************************" pp response.force_encoding("UTF-8") result = Hash.from_xml(response.force_encoding("UTF-8")) result['xml'] end |
.sendredpack(options) ⇒ Object
发红包,直接调用。 #参数 wxappid re_openid total_amount wishing client_ip act_name remark mch_id send_name total_num mch_key
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/eric_weixin/modules/pay.rb', line 53 def self.sendredpack required_field = %i(wxappid re_openid total_amount wishing client_ip act_name remark mch_id send_name total_num mch_key) required_field.each do |p| BusinessException.raise "缺少参数:#{p.to_s},且值不可以为空白字符串。" if [p.to_sym].blank? end [:nonce_str] = SecureRandom.uuid.tr('-', '') [:mch_billno] = [:mch_id] + Time.now.strftime("%Y%m%d") + Time.now.strftime("%H%M%S") + EricTools.generate_random_string(4,1).to_s # 生成签名 api_key = [:mch_key] .delete(:mch_key) sign = generate_sign , api_key [:sign] = sign # 生成xml数据包 pay_load = "<xml>#{.map { |k, v| "<#{k.to_s}>#{v.to_s}</#{k.to_s}>" }.join}</xml>" require 'rest-client' ca_path = Rails.root.join("ca/").to_s Dir.mkdir ca_path unless Dir.exist? ca_path BusinessException.raise '请下载证书' unless File.exist?(ca_path+"apiclient_cert.pem") && File.exist?(ca_path+"apiclient_key.pem") response = RestClient::Request.execute(method: :post, url: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack', ssl_client_cert: OpenSSL::X509::Certificate.new(File.read(ca_path+"apiclient_cert.pem")), ssl_client_key: OpenSSL::PKey::RSA.new(File.read(ca_path+"apiclient_key.pem"), "passphrase, if any"), ssl_ciphers: 'AESGCM:!aNULL', payload: pay_load) # 分析请求结果 pp "********************** 发红包 请求结果 ******************************" pp response.force_encoding("UTF-8") result = Hash.from_xml(response.force_encoding("UTF-8")) result['xml'] end |