class WeiXinPay(object):
"""配置账号信息"""
# =======【基本信息设置】=====================================
# 微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
APPID = "xxx" #公众号appid
APPSECRET = "" #公众号appsecret
MCHID = "333" # 商户号ID,身份标识
# 异步通知url,商户根据实际开发过程设定
NOTIFY_URL = "https://shepaipai.vip/app_pay_back"
TRADE_TYPE = 'JSAPI'
APIKEY = "**********" # 商户秘钥
def __init__(self, order_id, body, total_fee, spbill_create_ip,openid):
if body == "平台-充值":
self.NOTIFY_URL = "www.baidu.com" # 异步通知url
self.params = {
'appid': self.APPID, # appid
'mch_id': self.MCHID, # 商户号
'nonce_str': self.getNonceStr(),
'body': body, # 商品描述
'out_trade_no': str(order_id), # 商户订单号
# 'total_fee': str(int(total_fee)), !!! 正式环境
'total_fee': '1',
'spbill_create_ip': spbill_create_ip, # 127.0.0.1
'trade_type': self.TRADE_TYPE, # 交易类型
'openid': openid, # trade_type为JSAPI时,openid为必填参数!
'notify_url': self.NOTIFY_URL # 微信支付结果异步通知地址
}
# 开发者调用支付统一下单API生成预交易单
self.url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' # 微信请求url
self.error = None
def getNonceStr(self, length=32):
"""生成随机字符串"""
import random
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
strs = []
for x in range(length):
strs.append(chars[random.randrange(0, len(chars))])
return "".join(strs)
def getOpenID(self, code):
'''获取 openid'''
param = {
'code': code, # 用户点击按钮跳转到微信授权页, 微信处理完后重定向到redirect_uri, 并给我们加上code=xxx的参数, 这个code就是我们需要的
'appid': self.params['appid'],
'secret': self.params['APPSECRET'],
'grant_type': self.params['JSAPI'],
}
# 通过code获取access_token
openIdUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token'
resp = requests.get(openIdUrl, params=param)
# {openid, accss_token, refresh_token, openid, scope, expires_in}
openId = json.loads(resp.text)['openid']
# return resp.text
return openId
def key_value_url(self, value, urlencode):
"""
将键值对转为 key1=value1&key2=value2
对参数按照key=value的格式,并按照参数名ASCII字典序排序
"""
slist = sorted(value)
buff = []
for k in slist:
v = quote(value[k]) if urlencode else value[k]
buff.append("{0}={1}".format(k, v))
return "&".join(buff)
def get_sign(self, params):
"""
生成sign
拼接API密钥
"""
stringA = self.key_value_url(params, False)
stringSignTemp = stringA + '&key=' + self.APIKEY # APIKEY, API密钥,需要在商户后台设置
print(stringSignTemp)
sign = (hashlib.md5(stringSignTemp.encode("utf-8")).hexdigest()).upper()
params['sign'] = sign
return self.params
def get_req_xml(self):
"""
拼接XML
"""
self.get_sign(self.params)
xml = "<xml>"
for k, v in self.params.items():
# v = v.encode('utf8')
# k = k.encode('utf8')
xml += '<' + k + '>' + v + '</' + k + '>'
xml += "</xml>"
return xml.encode("utf-8")
def get_prepay_id(self):
"""
请求获取prepay_id
"""
xml = self.get_req_xml()
print(xml)
respone = requests.post(self.url, xml, headers={'Content-Type': 'application/xml'})
content = xml_to_dict(respone.content)
if content['return_code'] == 'SUCCESS':
prepay_id = content['prepay_id']
else:
return 1
def re_finall(self):
"""得到prepay_id后再次签名,返回给终端参数
"""
self.get_prepay_id()
if self.error:
return
sign_again_params = {
'appId': self.params['appid'],
'nonceStr': self.params['nonce_str'],
'timeStamp': self.params['timestamp'],
'package': 'prepay_id=' + self.params['prepay_id'],
'signType': 'MD5',
}
self.get_sign(sign_again_params)
self.params['sign'] = sign_again_params['sign']
# # 移除其他不需要返回参数
# for i in self.params.keys():
# print(i)
# if i not in ['appid', 'mch_id', 'nonce_str', 'timestamp', 'sign', 'package', 'prepay_id']:
# self.params.pop(i)
print(self.params)
return self.params # 返回给app
在view.py文件调用支付函数
def View(request):
if request.META.get('HTTP_X_FORWARDED_FOR'):
spbill_create_ip = request.META['HTTP_X_FORWARDED_FOR']
else:
spbill_create_ip = request.META['REMOTE_ADDR']
openid = 'ottytthtg0'
params = WeiXinPay(order_id=‘234243434243243242’, body='充值', total_fee=1,
spbill_create_ip=spbill_create_ip, openid=openid).re_finall()
return JsonResponse(params)