Python實現微信電腦版微信支付收款監聽及支付回撥通知

TANKING發表於2024-03-04

摘要

為什麼要監聽收款?那是因為現在還有人在使用微信的收款碼、商業碼、讚賞碼實現免籤支付,這類實現方法的最終方案還是監聽收款結果。

技術原理

透過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

相關文章