Python实现微信电脑版微信支付收款监听及支付回调通知

摘要

为什么要监听收款?那是因为现在还有人在使用微信的收款码、商业码、赞赏码实现免签支付,这类实现方法的最终方案还是监听收款结果。

技术原理

通过Python实时解析微信电脑版控件的文本内容来获取信息。不需要Hook和抓包,也不是走任何的协议,就是非常简单的界面信息获取和解析。

如何使用

  1. 登录电脑版微信;
  2. 找到微信支付公众号;
  3. 双击,让微信支付公众号单独显示,如下图;
  4. WxPayPcNotify.py修改你的接收通知的Url;
  5. cmd运行WxPayPcNotify.py即可开启监听。

在这里插入图片描述

接收支付结果通知

WxPayPcNotify.py监听到收款通知后,会向你服务器POST三个参数:

amount:收款金额
sender:微信昵称
timestamp:到账时间

nitify.php示例

<?php
	
	// 收款金额
	$amount = trim($_POST['amount']);

	// 微信昵称
	$sender = trim($_POST['sender']);

	// 到账时间
	$timestamp = trim($_POST['timestamp']);

	// 编写你的逻辑

?>

代码

WxPayPcNotify.py

import re
import time
import uiautomation as automation
import requests

last_matched_info = None

def explore_control(control, depth, target_depth):
    global last_matched_info
    try:
        name = control.Name
        if name:
            if depth == target_depth:
                # 匹配收款金额信息
                match = re.search(r'收款金额¥([\d.]+)', name)
                if match:
                    global amount
                    amount = match.group(1)
                    last_matched_info = f"收款金额: ¥{amount}, "

                # 匹配来自、到账时间信息
                match = re.search(r'来自(.+?)到账时间(.+?)备注', name)
                if match:
                    global sender
                    sender = match.group(1)
                    global timestamp
                    timestamp = match.group(2)
                    last_matched_info += f"来自: {sender}, 到账时间: {timestamp}"
                return
        # 递归处理子控件
        for child in control.GetChildren():
            explore_control(child, depth + 4, target_depth)
    except Exception as e:
        print(f"发生错误: {str(e)}")

def process_wechat_window(wechat_window, prev_info):
    global last_matched_info
    if wechat_window.Exists(0):
        explore_control(wechat_window, 0, 60)
        if last_matched_info and last_matched_info != prev_info:
            print(last_matched_info)
            print("-----------------------------------------------------------------")
            print("持续监听中...")
            print("-----------------------------------------------------------------")
            prev_info = last_matched_info
            
            # 向服务器发送请求
            send_http_request(last_matched_info,amount,sender,timestamp)

    else:
        print("无法获取到窗口,请保持微信支付窗口显示...")
    return prev_info

def send_http_request(info,amount,sender,timestamp):

    # 接收通知的Url
    server_url = 'https://www.yourdomain.com/notify.php'
    try:
        # 将金额、来自、到账时间POST给服务器
        response = requests.post(server_url, data={'amount': amount,'sender': sender,'timestamp': timestamp})
        # 通知成功
        # print("通知成功")
    except Exception as e:
        # 通知失败
        print(f"通知服务器失败...: {str(e)}")

def main():
    global last_matched_info
    prev_info = None
    try:
        # 获取微信窗口
        wechat_window = automation.WindowControl(searchDepth=1, ClassName='ChatWnd')
        prev_info = process_wechat_window(wechat_window, prev_info)
    except Exception as e:
        print(f"发生错误: {str(e)}")

    while True:
        try:
            # 持续监听微信窗口
            wechat_window = automation.WindowControl(searchDepth=1, ClassName='ChatWnd')
            prev_info = process_wechat_window(wechat_window, prev_info)
        except Exception as e:
            print(f"发生错误: {str(e)}")

        time.sleep(2)

if __name__ == "__main__":
    print("-----------------------------------------------------------------")
    print("欢迎使用liKeYun_WxPayPcNotify微信电脑版收款监控脚本...")
    print("-----------------------------------------------------------------")
    main()

作者

TANKING

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TANKING-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值