微信退款
阿新 • • 發佈:2020-07-28
code = json_data.get('code', None)
kind = json_data.get('kind', None)
print(1111111111111111112222222222222222)
if code is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '引數缺失'}), status=200, content_type='application/json; charset=utf-8')
"""產生隨機字串,不長於32位"""
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'strs = []
for x in range(32):
strs.append(chars[random.randrange(0, len(chars))])
strs = ''.join(strs)
strs1 = []
for x in range(32):
strs1.append(chars[random.randrange(0, len(chars))])
strs1 = ''.join(strs)
stringA = "appid={}&mch_id={}&nonce_str={}&out_refund_no={}&out_trade_no={}&refund_fee={}&total_fee={}" \.format(global_settings.WX_APPID, global_settings.WX_MCH_ID, strs,
strs1, code, 1, 1)
stringSignTemp = stringA + '&key=Heibeizhidaguangdian123456789000'
print(stringSignTemp)
sign = hashlib.md5(stringSignTemp.encode('utf-8')).hexdigest().upper()
print(sign)
params = "<xml><appid>{}</appid><mch_id>{}</mch_id><nonce_str>{}</nonce_str><out_refund_no>{}</out_refund_no><out_trade_no>{}</out_trade_no><refund_fee>{}</refund_fee><total_fee>{}</total_fee><sign>{}</sign></xml>".format(global_settings.WX_APPID, global_settings.WX_MCH_ID, strs,
strs1, code, '1', '1', sign)
print(params)
import requests
cert_path = "{}/rsa/cert/apiclient_cert.pem".format(settings.BASE_DIR)
key_path = "{}/rsa/cert/apiclient_key.pem".format(settings.BASE_DIR)
res = requests.post(global_settings.WX_REFUND_URL, cert=(cert_path, key_path), data=params.encode('utf-8')) # 退款需要證書
print(res, '退款返回')
array_data = {}
root = fromstring(res.content)
for child in root:
value = child.text
array_data[child.tag] = value
print(array_data)