记录一次用python升级微信支付的代金券的接口

2024-05-09 01:58

本文主要是介绍记录一次用python升级微信支付的代金券的接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近打开微信支付的后台,做代金券页面竟然要升级接口

官方给了一堆文档,一如既往读的头疼

于是微信开发者社区看到一篇文章

借鉴一下,来这里mark一下

首先你要先安装python不会的话去参照这个链接安装一下

https://www.liaoxuefeng.com/wiki/1016959663602400/1016959856222624

我就是参照这个安装的,安装的3.x版本

安装成功后看一下版本

 出现这个就是安装成功

然后接下来打开微信支付的后台

提前准备好

1.微信支付商户号

2.api秘钥

好了接下来

准备开始

随便新建一个.py的文件,在这里我是命名为1.py

接下来随便用个编辑器打开,我这里用的hubider你用vscdoe都行,用记事本都ok

复制的微信社区一个大佬的代码

复制之前先替换一下里面对应内容

import time
from xml.dom import minidomimport hashlib
from heapq import heappush, heappop
from collections import OrderedDictimport requestsSandBox_Url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'
MicroPay_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay"
UnifiedOrder_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"
OrderQuery_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"
ReFund_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refund"
RefundQuery_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery"
DownloadBill_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill"
nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"if __name__ == '__main__':mch_id = "这里放微信商户号"wxpay_key = "这里放秘钥"def get_sign_key(mch_id, key):template = "<xml><mch_id><![CDATA[{0}]]></mch_id>" \"<nonce_str><![CDATA[{1}]]></nonce_str>" \"<sign><![CDATA[{2}]]></sign></xml>"nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"encrypted_str = "mch_id=" + mch_id + "&nonce_str=" + nonce_str + "&key=" + keym = hashlib.md5()m.update(encrypted_str.encode('utf-8'))sign_key_request_data = template.format(mch_id, nonce_str, m.hexdigest().upper())result = requests.post(SandBox_Url, sign_key_request_data)dom = minidom.parseString(result.content)root = dom.documentElementsandbox_signkey = ''if root.getElementsByTagName("return_code")[0].childNodes[0].nodeValue == "FAIL":retmsg = root.getElementsByTagName("return_msg")[0].childNodes[0].nodeValueraise RuntimeError("请求出了点小错误:" + retmsg)else:sandbox_signkey = root.getElementsByTagName("sandbox_signkey")[0].childNodes[0].nodeValueprint("亲,这就是你的沙箱密钥了哦: \n" + sandbox_signkey)return sandbox_signkeydef to_tree_map(param_map):keys = param_map.keys()heap = []for item in keys:heappush(heap, item)sort = []while heap:sort.append(heappop(heap))res_map = OrderedDict()for key in sort:res_map[key] = param_map.get(key)return res_mapdef build_xml(param, wxpay_key):tree_map = to_tree_map(param)encrypted_str = ""for k in tree_map:encrypted_str += "{}={}&".format(k, tree_map[k])encrypted_str = encrypted_str + "key=" + wxpay_keym = hashlib.md5()m.update(encrypted_str.encode('utf-8'))sign = m.hexdigest().upper()param.update(sign=sign)complete_tree_map = to_tree_map(param)xml = "<xml>"for k in complete_tree_map:xml += "<{}><![CDATA[{}]]></{}>".format(k, complete_tree_map[k], k)xml += "</xml>"return xmldef request_handler(url, xml, desc):result = requests.post(url, xml)print(desc + "我才不是请求结果呢:\n" + result.content.decode("utf-8"))def upgrade(mch_id, wxpay_key):if mch_id == "":raise RuntimeError("出差错了哦,亲,你的商户号在哪呢?不填写商户号亲亲是要给空气去验收吗?")if wxpay_key == "":raise RuntimeError("出差错了哦,亲,你不填写商户密钥怎么继续呢,是用爱吗?")key = get_sign_key(mch_id, wxpay_key)nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"out_trade_no = round(time.time())MicroPay_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id,'nonce_str': nonce_str, 'body': "check",'out_trade_no': out_trade_no, 'total_fee': "501", 'spbill_create_ip': "8.8.8.8",'auth_code': "120061098828009406", }MicroPay_xml = build_xml(MicroPay_param, key)request_handler(MicroPay_Url, MicroPay_xml, "亲,用例编号1001刷卡正常支付有结果了,快来看呀 \n")time.sleep(1)OrderQuery_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id, 'nonce_str': nonce_str,'out_trade_no': out_trade_no,}OrderQuery_xml = build_xml(OrderQuery_param, key)request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1001刷卡正常支付查询出结果了,快来看呀 \n")time.sleep(1)out_trade_no_2nd = round(time.time())print("我是1002下单的订单号:",+ out_trade_no_2nd)MicroPay_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id,'nonce_str': nonce_str, 'body': "check",'out_trade_no': out_trade_no_2nd, 'total_fee': "502", 'spbill_create_ip': "8.8.8.8",'auth_code': "120061098828009406",}MicroPay_xml = build_xml(MicroPay_param, key)request_handler(MicroPay_Url, MicroPay_xml, "亲,用例编号1002刷卡正常支付结果来了,你还抓紧不来看 \n")time.sleep(1)OrderQuery_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id, 'nonce_str': nonce_str,'out_trade_no': out_trade_no_2nd,}OrderQuery_xml = build_xml(OrderQuery_param, key)request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1002刷卡正常支付查询结果,结果好像有点不太对呢 \n")time.sleep(1)ReFund_param = {'appid': "wxd678efh567hg6787", 'mch_id': mch_id, 'nonce_str':nonce_str,'out_refund_no': out_trade_no,'total_fee': "502",'refund_fee': "501",  'out_trade_no': out_trade_no_2nd,}ReFund_xml = build_xml(ReFund_param, key)request_handler(ReFund_Url, ReFund_xml, "亲,下面展示的是用例编号1002刷卡支付退款的结果,你猜对不对 \n")time.sleep(1)RefundQuery_param = {'appid': "wxd678efh567hg6787", 'mch_id': mch_id,'nonce_str':nonce_str,'out_trade_no': out_trade_no_2nd, }RefundQuery_xml = build_xml(RefundQuery_param, key)request_handler(RefundQuery_Url, RefundQuery_xml, "亲,用例编号1002刷卡支付退款查询结果返回中,加载不出来长按电源键或Ait+F4重试哦 \n")time.sleep(1)nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"out_trade_no = round(time.time())UnifiedOrder_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id,'nonce_str': nonce_str, 'body': "check",'out_trade_no': out_trade_no, 'total_fee': "551", 'notify_url':"https://www.weixin.qq.com/wxpay/pay.php",'spbill_create_ip': "8.8.8.8",'trade_type': "JSAPI", }UnifiedOrder_xml = build_xml(UnifiedOrder_param, key)request_handler(UnifiedOrder_Url, UnifiedOrder_xml, "亲,用例编号1003-公众号/APP/扫码正常支付有结果了,快来看呀 \n")time.sleep(1)OrderQuery_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id, 'nonce_str': nonce_str,'out_trade_no': out_trade_no,}OrderQuery_xml = build_xml(OrderQuery_param, key)request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1003-公众号/APP/扫码正常支付查询出结果了,快来看呀 \n")time.sleep(1)out_trade_no_2nd = round(time.time() * 1000)print("我是1002下单的订单号:",+ out_trade_no_2nd)UnifiedOrder_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id,'nonce_str': nonce_str, 'body': "check",'out_trade_no': out_trade_no_2nd, 'total_fee': "552", 'notify_url':"https://www.weixin.qq.com/wxpay/pay.php",'spbill_create_ip': "8.8.8.8",'trade_type': "JSAPI", }UnifiedOrder_xml = build_xml(UnifiedOrder_param, key)request_handler(UnifiedOrder_Url, UnifiedOrder_xml, "亲,用例编号1004-公众号/APP/扫码支付退款结果来了,你还抓紧不来看 \n")time.sleep(1)OrderQuery_param = {'appid': "wxd678efh567hg6787",'mch_id': mch_id, 'nonce_str': nonce_str,'out_trade_no': out_trade_no_2nd,}OrderQuery_xml = build_xml(OrderQuery_param, key)request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1004-公众号/APP/扫码支付退款查询结果,结果好像有点不太对呢 \n")time.sleep(1)ReFund_param = {'appid': "wxd678efh567hg6787", 'mch_id': mch_id, 'nonce_str':nonce_str,'out_refund_no': out_trade_no,'total_fee': "552",'refund_fee': "551",  'out_trade_no': out_trade_no_2nd,}ReFund_xml = build_xml(ReFund_param, key)request_handler(ReFund_Url, ReFund_xml, "亲,下面展示的是用例编号1004-公众号/APP/扫码支付退款的结果,你猜对不对 \n")time.sleep(1)RefundQuery_param = {'appid': "wxd678efh567hg6787", 'mch_id': mch_id,'nonce_str':nonce_str,'out_trade_no': out_trade_no_2nd, }RefundQuery_xml = build_xml(RefundQuery_param, key)request_handler(RefundQuery_Url, RefundQuery_xml, "亲,用例编号1004-公众号/APP/扫码支付退款查询结果返回中,加载不出来长按电源键或Ait+F4重试哦 \n")time.sleep(1)DownloadBill_param = {'appid': "wxd678efh567hg6787", 'mch_id': mch_id, 'nonce_str': nonce_str,'bill_date': "2021-04-01", 'bill_type': "ALL"}DownloadBill_xml = build_xml(DownloadBill_param, key)request_handler(DownloadBill_Url, DownloadBill_xml, "亲,你要下载交易的对账单来了,加载中······,加载不出来长按电源键或Ait+F4重试哦 \n")upgrade(mch_id, wxpay_key) 

大佬的文章等会放在底部

然后再这个文件夹下打开cmd

python 1.py运行

报错,是对应的包没有安装

py的包不是npm是pip命令

 

安装一下这个包

然后重新 python 1.py

多运行两次,本地沙箱环境

然后去这个链接查看升级成功没有

https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=15_6&index=4

然后再返回微信支付代金券查看接口就升级成功

 

 

参考微信社区大佬链接

https://developers.weixin.qq.com/community/develop/article/doc/0002e82b060c3028230c915f150813

 

这篇关于记录一次用python升级微信支付的代金券的接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/972074

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核