python flask 微信支付下单及支付结果回调处理代码笔记

2024-06-21 22:36

本文主要是介绍python flask 微信支付下单及支付结果回调处理代码笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

因为微信支付下单的时候不允许同一个订单重复下单,所以在下单之前 对订单号加了三位随机字符串 在回调时 进行截取操作

@order_api.route('/user/wx_xcx_pay/notify', methods=['POST'])
def new_notify():headers = request.headers# print('headers', headers)wxpay = wx_pay_func()result = wxpay.callback(headers, request.data)if result and result.get('event_type') == 'TRANSACTION.SUCCESS':resp = result.get('resource')out_trade_no = resp.get('out_trade_no')  # 本系统订单号transaction_id = resp.get('transaction_id')  # 微信支付订单号first_character = out_trade_no[0]  # 获取开头字母 确定支付类型order_number = out_trade_no[:-3]  # 获取实际订单号success_time = resp.get('success_time')  # 支付完成时间payer = resp.get('payer')  # 付款人信息amount = resp.get('amount').get('total')  # 金额 单位分payment_amount = Decimal(amount) / Decimal(100)  # 实际支付金额 单位元# 上门取 或者 上门洗if first_character in ['X', 'Q']:return order_pay_callback(order_number, amount, success_time, transaction_id, resp)# 充值订单elif first_character == 'C':return recharge_pay_callback(order_number, transaction_id, payment_amount)# 补差价订单elif first_character == 'B':return price_difference_payment_callback(order_number, transaction_id, payment_amount)else:return reg_func(200, None, '支付失败')def load_private_key(file_name):# 获取当前文件的目录current_dir = os.path.dirname(__file__)# 构建完整的文件路径file_path = os.path.join(current_dir, file_name)try:# 安全地打开文件with open(file_path, 'r') as file:private_key = file.read()return private_keyexcept FileNotFoundError:print(f"Error: The file {file_name} was not found in the directory {current_dir}.")return Noneexcept Exception as e:print(f"An error occurred: {e}")return None# 初始化
def wx_pay_func():mch_id = get_config('wx_xcx_pay_mid')appid = get_config('wx_xcx_appid')APIV3_KEY = get_config('wx_xcx_pay_secret')# 日志记录器,记录web请求和回调细节logging.basicConfig(filename=os.path.join(os.getcwd(), 'demo.log'), level=logging.DEBUG, filemode='a',format='%(asctime)s - %(process)s - %(levelname)s: %(message)s')LOGGER = logging.getLogger("demo")# 代理设置,None或者{"https": "http://10.10.1.10:1080"},详细格式参见https://requests.readthedocs.io/en/latest/user/advanced/#proxiesPROXY = None# 接入模式:False=直连商户模式,True=服务商模式PARTNER_MODE = FalseCERT_DIR = None# 请求超时时间配置TIMEOUT = (10, 30)  # 建立连接最大超时时间是10s,读取响应的最大超时时间是30sPRIVATE_KEY = load_private_key('apiclient_key.pem')CERT_SERIAL_NO = get_config('CERT_SERIAL_NO')api_url = get_config('api_domain')NOTIFY_URL = f'https://{api_url}/user/wx_xcx_pay/notify'print('NOTIFY_URL', NOTIFY_URL)wxpay = WeChatPay(wechatpay_type=WeChatPayType.NATIVE,mchid=mch_id,private_key=PRIVATE_KEY,cert_serial_no=CERT_SERIAL_NO,apiv3_key=APIV3_KEY,appid=appid,notify_url=NOTIFY_URL,cert_dir=CERT_DIR,logger=LOGGER,partner_mode=PARTNER_MODE,proxy=PROXY,timeout=TIMEOUT)return wxpaydef generate_random_string():# 字符集合,包括大写字母、小写字母和数字characters = string.ascii_letters + string.digits# 从字符集合中随机选择3个字符random_string = ''.join(random.choices(characters, k=3))return random_stringdef xcx_pay(openid, amount, order_number):wxpay = wx_pay_func()str1 = generate_random_string()out_trade_no = order_number + str1description = out_trade_nopayer = {'openid': openid}code, message = wxpay.pay(description=description,out_trade_no=out_trade_no,amount={'total': amount},pay_type=WeChatPayType.MINIPROG,payer=payer)result = json.loads(message)if code in range(200, 300):APPID = get_config('wx_xcx_appid')prepay_id = result.get('prepay_id')timestamp = str(int(time.time()))noncestr = str(uuid.uuid4()).replace('-', '')package = 'prepay_id=' + prepay_idsign = wxpay.sign(data=[APPID, timestamp, noncestr, package])signtype = 'RSA'result = {'appId': APPID,'timeStamp': timestamp,'nonceStr': noncestr,'package': 'prepay_id=%s' % prepay_id,'signType': signtype,'paySign': sign}return reg_func(200, result, '微信支付下单成功')else:return reg_func(500, result.get('code'), '微信支付下单失败')

代码解释

这段Python代码实现了一个处理微信支付通知的API端点。以下是详细解释:

路由定义与请求处理函数

@order_api.route('/user/wx_xcx_pay/notify', methods=['POST'])
def new_notify():

这段代码定义了一个Flask路由,当客户端以POST请求访问/user/wx_xcx_pay/notify时,会调用new_notify函数。

获取请求头和初始化微信支付对象

    headers = request.headerswxpay = wx_pay_func()

首先获取请求的头信息,然后调用wx_pay_func函数初始化微信支付对象。

处理微信支付回调

    result = wxpay.callback(headers, request.data)

使用微信支付对象的callback方法处理回调数据,返回结果存储在result变量中。

判断支付是否成功并提取相关信息

    if result and result.get('event_type') == 'TRANSACTION.SUCCESS':resp = result.get('resource')out_trade_no = resp.get('out_trade_no')transaction_id = resp.get('transaction_id')first_character = out_trade_no[0]order_number = out_trade_no[:-3]success_time = resp.get('success_time')payer = resp.get('payer')amount = resp.get('amount').get('total')payment_amount = Decimal(amount) / Decimal(100)

检查支付结果是否成功,若成功,则从回调结果中提取相关信息,包括系统订单号、微信支付订单号、支付类型标识、实际订单号、支付完成时间、付款人信息以及支付金额。

根据支付类型调用不同的处理函数

        if first_character in ['X', 'Q']:return order_pay_callback(order_number, amount, success_time, transaction_id, resp)elif first_character == 'C':return recharge_pay_callback(order_number, transaction_id, payment_amount)elif first_character == 'B':return price_difference_payment_callback(order_number, transaction_id, payment_amount)else:return reg_func(200, None, '支付失败')

根据订单号的第一个字符判断支付类型,并调用相应的处理函数。

初始化微信支付对象函数

def wx_pay_func():mch_id = get_config('wx_xcx_pay_mid')appid = get_config('wx_xcx_appid')APIV3_KEY = get_config('wx_xcx_pay_secret')logging.basicConfig(filename=os.path.join(os.getcwd(), 'demo.log'), level=logging.DEBUG, filemode='a', format='%(asctime)s - %(process)s - %(levelname)s: %(message)s')LOGGER = logging.getLogger("demo")PROXY = NonePARTNER_MODE = FalseCERT_DIR = NoneTIMEOUT = (10, 30)PRIVATE_KEY = load_private_key('apiclient_key.pem')CERT_SERIAL_NO = get_config('CERT_SERIAL_NO')api_url = get_config('api_domain')NOTIFY_URL = f'https://{api_url}/user/wx_xcx_pay/notify'wxpay = WeChatPay(wechatpay_type=WeChatPayType.NATIVE,mchid=mch_id,private_key=PRIVATE_KEY,cert_serial_no=CERT_SERIAL_NO,apiv3_key=APIV3_KEY,appid=appid,notify_url=NOTIFY_URL,cert_dir=CERT_DIR,logger=LOGGER,partner_mode=PARTNER_MODE,proxy=PROXY,timeout=TIMEOUT)return wxpay

该函数初始化并返回一个微信支付对象,包含商户号、应用ID、API密钥等必要的配置,以及日志记录器和请求超时设置等。

函数解释

生成随机字符串函数
def generate_random_string():# 字符集合,包括大写字母、小写字母和数字characters = string.ascii_letters + string.digits# 从字符集合中随机选择3个字符random_string = ''.join(random.choices(characters, k=3))return random_string
  • 功能:生成一个包含大写字母、小写字母和数字的随机字符串,长度为3。
  • 步骤
    • 定义字符集合,包括所有字母和数字。
    • 随机选择3个字符,并组合成一个字符串返回。
小程序支付函数
def xcx_pay(openid, amount, order_number):wxpay = wx_pay_func()str1 = generate_random_string()out_trade_no = order_number + str1description = out_trade_nopayer = {'openid': openid}code, message = wxpay.pay(description=description,out_trade_no=out_trade_no,amount={'total': amount},pay_type=WeChatPayType.MINIPROG,payer=payer)result = json.loads(message)if code in range(200, 300):APPID = get_config('wx_xcx_appid')prepay_id = result.get('prepay_id')timestamp = str(int(time.time()))noncestr = str(uuid.uuid4()).replace('-', '')package = 'prepay_id=' + prepay_idsign = wxpay.sign(data=[APPID, timestamp, noncestr, package])signtype = 'RSA'result = {'appId': APPID,'timeStamp': timestamp,'nonceStr': noncestr,'package': 'prepay_id=%s' % prepay_id,'signType': signtype,'paySign': sign}return reg_func(200, result, '微信支付下单成功')else:return reg_func(500, result.get('code'), '微信支付下单失败')
  • 功能:为微信小程序生成支付订单,并返回支付参数给前端。

  • 参数

    • openid:用户的微信OpenID。
    • amount:支付金额(单位为分)。
    • order_number:订单号。
  • 步骤

    1. 初始化微信支付对象。
    2. 生成随机字符串并附加到订单号后面,形成新的商户订单号(out_trade_no)。
    3. 调用微信支付对象的pay方法,传入支付描述、商户订单号、支付金额、支付类型和付款人信息。
    4. 解析支付请求的返回结果。
    5. 如果请求成功(状态码在200到299之间):
      • 获取应用ID和预支付交易会话标识(prepay_id)。
      • 生成当前时间戳、随机字符串和支付签名。
      • 构建支付参数并返回成功信息。
    6. 如果请求失败,返回错误信息。

总结

  1. generate_random_string 函数用于生成一个长度为3的随机字符串,以增强订单号的唯一性。
  2. xcx_pay 函数用于处理微信小程序支付请求,生成支付订单并返回支付参数给前端。

中文解释由chatgpt协助整理

这篇关于python flask 微信支付下单及支付结果回调处理代码笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B