#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
隐私支付二维码生成器

生成微信扫码支付二维码，隐藏商户和商品信息
"""

import asyncio
import time
import qrcode
from pay import create_payment

async def create_private_payment():
    """创建隐私支付二维码"""
    print("微信扫码支付 - 隐私模式")
    print("=" * 40)
    
    # 配置信息
    appid = 'YSMcd16b45d'
    appsecret = '899850e778e8d2b53e4c4a4e88695688'
    
    # 生成订单号
    order_id = f"pay_{int(time.time())}"
    
    print(f"订单号: {order_id}")
    print(f"金额: 0.01元")
    print()
    print("正在生成支付二维码...")
    
    try:
        # 创建扫码支付订单 - 使用简化的描述
        pay_url = await create_payment(
            appid=appid,
            appsecret=appsecret,
            order_id=order_id,
            description="支付",  # 简化商品描述
            amount=1,  # 1分钱
            notify_url="http://localhost/notify",  # 简化回调地址
            nopay_url="http://localhost/cancel",
            callback_url="http://localhost/success", 
            pay_type=2  # 微信扫码支付
        )
        
        if pay_url:
            print("支付二维码生成成功!")
            print()
            
            # 生成二维码
            qr = qrcode.QRCode(
                version=1,
                error_correction=qrcode.constants.ERROR_CORRECT_L,
                box_size=8,  # 稍微小一点的二维码
                border=2,
            )
            qr.add_data(pay_url)
            qr.make(fit=True)
            
            # 创建二维码图片
            qr_img = qr.make_image(fill_color="black", back_color="white")
            
            # 保存二维码
            qr_filename = f"private_qr_{order_id}.png"
            qr_img.save(qr_filename)
            
            print(f"二维码已生成: {qr_filename}")
            print()
            print("使用方法:")
            print("1. 用微信扫描生成的二维码")
            print("2. 完成0.01元支付")
            print("3. 支付页面将显示最小化的商户信息")
            print()
            print("隐私设置:")
            print("- 商品描述: 仅显示'支付'")
            print("- 商户信息: 使用平台默认设置")
            print("- 订单金额: 0.01元")
            
            return order_id, pay_url, qr_filename
            
        else:
            print("支付订单创建失败")
            return None, None, None
            
    except Exception as e:
        print(f"创建支付时出错: {str(e)}")
        return None, None, None

async def create_custom_payment():
    """创建自定义支付二维码"""
    print("\n" + "=" * 40)
    print("自定义支付设置")
    print("=" * 40)
    
    # 让用户自定义设置
    description_options = {
        "1": "支付",
        "2": "订单支付", 
        "3": "在线支付",
        "4": "服务费用",
        "5": "充值"
    }
    
    print("选择商品描述:")
    for key, value in description_options.items():
        print(f"{key}. {value}")
    
    # 由于是演示，我们使用默认选项
    choice = "1"  # 默认选择"支付"
    description = description_options.get(choice, "支付")
    
    # 配置信息
    appid = 'YSMcd16b45d' 
    appsecret = '899850e778e8d2b53e4c4a4e88695688'
    
    # 生成订单号
    order_id = f"custom_{int(time.time())}"
    
    print(f"\n订单设置:")
    print(f"订单号: {order_id}")
    print(f"描述: {description}")
    print(f"金额: 0.01元")
    print("\n正在生成支付二维码...")
    
    try:
        # 创建支付订单
        pay_url = await create_payment(
            appid=appid,
            appsecret=appsecret, 
            order_id=order_id,
            description=description,
            amount=1,
            notify_url="http://localhost/notify",
            nopay_url="http://localhost/cancel",
            callback_url="http://localhost/success",
            pay_type=2
        )
        
        if pay_url:
            # 生成二维码
            qr = qrcode.QRCode(version=1, box_size=8, border=2)
            qr.add_data(pay_url)
            qr.make(fit=True)
            qr_img = qr.make_image(fill_color="black", back_color="white")
            
            # 保存二维码
            qr_filename = f"custom_qr_{order_id}.png"
            qr_img.save(qr_filename)
            
            print(f"自定义支付二维码已生成: {qr_filename}")
            return order_id, pay_url, qr_filename
            
        else:
            print("支付订单创建失败")
            return None, None, None
            
    except Exception as e:
        print(f"创建支付时出错: {str(e)}")
        return None, None, None

async def batch_create_payments():
    """批量创建不同类型的支付二维码"""
    print("批量生成支付二维码")
    print("=" * 40)
    
    payment_types = [
        ("支付", "simple"),
        ("订单", "order"), 
        ("充值", "recharge"),
        ("服务", "service")
    ]
    
    appid = 'YSMcd16b45d'
    appsecret = '899850e778e8d2b53e4c4a4e88695688'
    
    created_qrs = []
    
    for desc, type_name in payment_types:
        order_id = f"{type_name}_{int(time.time())}"
        
        try:
            pay_url = await create_payment(
                appid=appid,
                appsecret=appsecret,
                order_id=order_id,
                description=desc,
                amount=1,
                notify_url="http://localhost/notify",
                nopay_url="http://localhost/cancel", 
                callback_url="http://localhost/success",
                pay_type=2
            )
            
            if pay_url:
                # 生成二维码
                qr = qrcode.QRCode(version=1, box_size=6, border=2)
                qr.add_data(pay_url)
                qr.make(fit=True)
                qr_img = qr.make_image(fill_color="black", back_color="white")
                
                qr_filename = f"{type_name}_qr_{order_id}.png"
                qr_img.save(qr_filename)
                
                created_qrs.append({
                    'order_id': order_id,
                    'description': desc,
                    'filename': qr_filename,
                    'url': pay_url
                })
                
                print(f"✓ {desc} - {qr_filename}")
                
        except Exception as e:
            print(f"✗ 创建{desc}支付失败: {str(e)}")
    
    print(f"\n成功生成 {len(created_qrs)} 个支付二维码")
    return created_qrs

async def main():
    """主函数"""
    print("微信支付二维码生成器 - 隐私版")
    print("支持隐藏商户信息和自定义商品描述")
    print()
    
    # 选择模式
    print("选择生成模式:")
    print("1. 隐私模式 - 最小化信息显示")
    print("2. 自定义模式 - 自定义商品描述") 
    print("3. 批量模式 - 生成多种类型二维码")
    
    # 默认使用隐私模式进行演示
    mode = "1"
    
    if mode == "1":
        await create_private_payment()
    elif mode == "2":
        await create_custom_payment()
    elif mode == "3":
        await batch_create_payments()
    
    print("\n" + "=" * 40)
    print("提示:")
    print("- 所有二维码都已保存到当前目录")
    print("- 使用微信扫描任意二维码进行支付测试")
    print("- 支付金额均为0.01元")
    print("- 商户信息已最小化显示")

if __name__ == "__main__":
    asyncio.run(main())