2

摘要

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

技术原理

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

如何使用

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

2345_image_file_copy_2.jpg

接收支付结果通知

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


TANKING
4.8k 声望509 粉丝

热爱分享,热爱创作,热爱研究。