python对接微信支付_python3接入微信企业支付实现小程序提现

最近发现某些小程序有了提现功能,原来小程序是不支持提现的,所以当初实现方法是打算让用户去关注公众号,再从公众号提现,当然前提要公众号跟小程序使用同一的unionid来标记唯一用户,既然现在支持小程序直接提现了,那就不再使用关注公众号这种影响用户体验的方法了

小程序提现使用的是企业付款接口(这什么烂文档,这个接口印象中已经出现很久了,因为没有在小程序开发中就没太留意,产生了小程序不能做提现的错觉,虽然也不可能单独出现在小程序文档中,你好歹也提提吧),首先申请商户号,现在使用企业付款接口也需要开通条件

开通商户号并达到开通条件后,就是接口的使用了,无非就是一个http请求罢了,根据之前做小程序支付调用统一下单接口的经验,这里也理所当然的使用python的http请求库requests,这里先附上统一下单接口的代码(请注意使用的库)

#python 3.5.1

from xml.etree.ElementTree import Element, tostring

import requests,hashlib,random

url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'

data = {

'appid': APPID,#申请商户号后提供

'mch_id': MCH_ID,#申请商户号后提供

'nonce_str': get_nonce_str(),

'body': '测试',

'out_trade_no': get_out_trade_no(),

'total_fee': 1,

'spbill_create_ip': get_host(), #服务器ip

'notify_url': 'http://www.weixin.qq.com/wxpay/pay.php',#暂时没用到,微信以http请求方式将支付结果发到这个请求地址上

'trade_type': 'JSAPI',

'openid': openid,

}

# 签名算法

data['sign'] = wx_sign(data)

xml_data = bytes.decode(tostring(dict_to_xml_data(data)))

response = requests.post(url=url, data=xml_data)

----------------------------------------------------------------

# 签名算法

def wx_sign(proto_data):

'''根据数组的key排序,并拼接加密'''

a = ''

for key in (sorted(proto_data.keys())):

if a == '':

a = key + '=' + str(proto_data[key])

else:

a = a + '&' + key + '=' + str(proto_data[key])

a = a + '&key=' + KEY

# md5加密

a = hashlib.md5(a.encode('utf-8')).hexdigest().upper()

return a

def dict_to_xml_data(data):

# dict to xml

elem = Element('xml')

for key, value in data.items():

child = Element(key)

child.text = str(value)

elem.append(child)

return elem

def get_out_trade_no():

'''商户订单号'''

return time.strftime("%Y%m%d%H%M%S", time.localtime()) + str(uuid.uuid1())[:8]

def get_nonce_str():

'''随机字符串'''

return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(32))

理所当然的企业付款接口的写法,requests.post()有一个cert的参数,就是微信提供的私钥和公钥证书,据说requests还不支持p12的证书

url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'

data = {

'mch_appid': APPID,

'mchid': MCH_ID,

'nonce_str': get_nonce_str(),

'partner_trade_no': get_out_trade_no(),

'check_name': 'NO_CHECK',

'amount': 100,

'spbill_create_ip': get_host(),

'desc': u'测试',

'openid': openid,

}

data['sign'] = wx_sign(data)

xml_data = bytes.decode(tostring(dict_to_xml_data(data)))

#logger.debug('xml_data=======' + xml_data)

response = requests.post(url=url, data=xml_data,cert=('/home/pem/apiclient_cert.pem','/home/pem/apiclient_key.pem'))

运行后报【参数错误:描述信息存在非UTF8字符】,指的就是我desc写的内容有问题罗,尝试着把desc内容改成英文字母和数字,运行成功在手机微信的微信支付上可以看到入账信息,但是我这里的入账详情就是要写中文啊

把发送前的xml打印出来看一下发现在xml中desc的内容为测试,接着把xml_data = bytes.decode(tostring(dict_to_xml_data(data)))改成xml_data = bytes.decode(tostring(dict_to_xml_data(data),encoding='utf8'))思路是将body换成utf8编码再发送,报了下面的错误

File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 1127, in _send_request

body = body.encode('iso-8859-1')

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 375-376: ordinal not in range(256)

意思是说requests库发出请求body需以iso-8859来解码,根据错误信息跳到requests源码,body默认的编码为iso 8895-1,没有找到发送请求时改变编码的方式(如果发现requests能修改的小伙伴希望告知),只有修改response返回的编码方式

# RFC 2616 Section 3.7.1 says that text default has a

# default charset of iso-8859-1.

body = body.encode('iso-8859-1')

关键时刻只能曲线救国了,决定换一个http的请求库,在使用了urllib3发现有一样的问题,原来requests库就是在urllib3上封装的,最后使用urllib成功实现了,使用urllib库的棘手地方在于发送请求时带上证书,实现代码如下

url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'

data = {

'mch_appid': APPID,

'mchid': MCH_ID,

'nonce_str': get_nonce_str(),

'partner_trade_no': get_out_trade_no(),

'check_name': 'NO_CHECK',

'amount': 100,

'spbill_create_ip': get_host(),

'desc': u'测试',

'openid': openid,

}

# 签名算法

data['sign'] = wx_sign(data)

xml_data = tostring(dict_to_xml_data(data), encoding='utf8') #这个地方加了utf8编码,并且返回byte数据

from urllib import request as res

import ssl

context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

context.load_cert_chain(certfile='/home/pem/apiclient_cert.pem', keyfile='/home/pem/apiclient_key.pem')

response = res.urlopen(url=url, data=xml_data, context=context)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值