from django.test import TestCase # Create your tests here. # !/usr/bin/env python # -*- coding:utf-8 -*- """ 退款 根据订单号进行退款(需要证书才能操作) 文档https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_4&index=4 """ import uuid import random import hashlib import requests from xml.etree import ElementTree as ET def md5(string): ha = hashlib.md5() ha.update(string.encode(‘utf-8‘)) return ha.hexdigest() def refund(trade_no, out_refund_no, total_fee, refund_fee): """ 订单号 :param trade_no: 创建订单时自动生成的订单号 :param out_refund_no: 商户退款单号 :param total_fee: 订单金额 :param refund_fee: 退款金额 :return: """ info = { ‘appid‘: ‘wx55cca0b94f723dc7‘, ‘mch_id‘: ‘1526049051‘, ‘out_trade_no‘: trade_no, ‘nonce_str‘: "".join([chr(random.randint(65, 90)) for _ in range(12)]), ‘sign_type‘: "MD5", ‘out_refund_no‘: out_refund_no, ‘total_fee‘: total_fee, ‘refund_fee‘: refund_fee } key = "2SzCvaKgYExuItWBfYAqJFs72uUleD14" string = "&".join(["{0}={1}".format(k, info[k]) for k in sorted(info)] + ["{0}={1}".format("key", key, ), ]) info[‘sign‘] = md5(string).upper() xml = "<xml>{0}</xml>".format("".join(["<{0}>{1}</{0}>".format(k, v) for k, v in info.items()])) # 这里换成自己的 key = "xx/xxx/client_key.pem" cert = "xxx/xxx/xx/client_cert.pem" res = requests.post( url=‘https://api.mch.weixin.qq.com/secapi/pay/refund‘, data=xml.encode(‘utf-8‘), headers={ ‘Accept-Language‘: ‘zh-CN,zh;q=0.9‘ }, cert=(cert, key), verify=True ) """ <xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx2421b1c4370ec43b]]></appid> <mch_id><![CDATA[10000100]]></mch_id> <nonce_str><![CDATA[NfsMFbUFpdbEhPXP]]></nonce_str> <sign><![CDATA[B7274EB9F8925EB93100DD2085FA56C0]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id> <out_trade_no><![CDATA[1415757673]]></out_trade_no> <out_refund_no><![CDATA[1415701182]]></out_refund_no> <refund_id><![CDATA[2008450740201411110000174436]]></refund_id> <refund_fee>1</refund_fee> </xml> """ # 返回的数据 找到 success root = ET.XML(res.content.decode(‘utf-8‘)) response = {child.tag: child.text for child in root} if response[‘return_code‘] == ‘SUCCESS‘: return True if __name__ == ‘__main__‘: out_refund_no = md5(str(uuid.uuid4())) print(out_refund_no) # refund(订单号 原订单号 交了多少钱 退多少钱) refund(‘8ccdbbd652d9ad12b82cf2b021669cb9‘, out_refund_no, 0.1, 0.1)