import { Order, PayTrade } from '../models/Order';\nimport { PayTrade as PayTradeModel } from '../models/PayTrade';\nimport AlipaySDK from 'alipay-sdk';\nimport WeChatPay from 'wechatpay-node-v3';\nimport Stripe from 'stripe';\nimport * as Paypal from '@paypal/checkout-server-sdk';\nimport Web3 from 'web3';\n\nclass AlipayAdapter {\n  constructor() {\n    this.sdk = new AlipaySDK({\n      appId: process.env.ALIPAY_APP_ID,\n      privateKey: process.env.ALIPAY_PRIVATE_KEY,\n      alipayPublicKey: process.env.ALIPAY_PUBLIC_KEY,\n      gateway: 'https://openapi.alipay.com/gateway.do',\n    });\n  }\n\n  async pay(order) {\n    const result = await this.sdk.exec('alipay.trade.app.pay', {\n      bizContent: {\n        subject: `Payment for order ${order._id}`,\n        out_trade_no: order._id.toString(),\n        total_amount: order.amount.toString(),\n        product_code: 'QUICK_MSECURITY_PAY',\n      },\n    });\n\n    const trade = new PayTradeModel({ orderId: order._id, gateway: 'alipay', response: result });\n    await trade.save();\n\n    return result;\n  }\n}\n\nclass WechatAdapter {\n  constructor() {\n    this.pay = new WeChatPay({\n      appid: process.env.WECHAT_APPID,\n      mchid: process.env.WECHAT_MCHID,\n      private_key: process.env.WECHAT_PRIVATE_KEY,\n      serial_no: process.env.WECHAT_SERIAL_NO,\n      apiv3_key: process.env.WECHAT_APIV3_KEY,\n    });\n  }\n\n  async pay(order) {\n    const params = {\n      out_trade_no: order._id.toString(),\n      appid: process.env.WECHAT_APPID,\n      description: `Payment for order ${order._id}`,\n      amount: { total: Math.floor(order.amount * 100) },\n      payer: { openid: 'user_openid' } // 需要替换为实际openid\n    };\n    const result = await this.pay.jsapi(params);\n\n    const trade = new PayTradeModel({ orderId: order._id, gateway: 'wechat', response: result });\n    await trade.save();\n\n    return result;\n  }\n}\n\nclass PaypalAdapter {\n  constructor() {\n    const environment = new Paypal.core.SandboxEnvironment(process.env.PAYPAL_CLIENT_ID, process.env.PAYPAL_CLIENT_SECRET);\n    this.client = new Paypal.core.PayPalHttpClient(environment);\n  }\n\n  async pay(order) {\n    const request = new Paypal.orders.OrdersCreateRequest();\n    request.requestBody({\n      intent: 'CAPTURE',\n      purchase_units: [{\n        amount: {\n          currency_code: order.currency,\n          value: order.amount.toString(),\n        }\n      }]\n    });\n    const response = await this.client.execute(request);\n\n    const trade = new PayTradeModel({ orderId: order._id, gateway: 'paypal', response: response.result });\n    await trade.save();\n\n    return response.result;\n  }\n}\n\nclass StripeAdapter {\n  constructor() {\n    this.stripe = new Stripe(process.env.STRIPE_SECRET_KEY);\n  }\n\n  async pay(order) {\n    const paymentIntent = await this.stripe.paymentIntents.create({\n      amount: Math.floor(order.amount * 100),\n      currency: order.currency,\n      metadata: { order_id: order._id.toString() },\n    });\n\n    const trade = new PayTradeModel({ orderId: order._id, gateway: 'stripe', response: paymentIntent });\n    await trade.save();\n\n    return paymentIntent;\n  }\n}\n\nclass UsdtAdapter {\n  async pay(order) {\n    // 实现USDT支付逻辑，使用web3\n    // 示例: 生成支付地址或交易\n    const web3 = new Web3(process.env.WEB3_PROVIDER || 'https://mainnet.infura.io/v3/YOUR_INFURA_KEY');\n    // TODO: 实现USDT ERC20转账或支付请求\n    const result = { paymentAddress: 'generated_usdt_address', amount: order.amount };\n\n    const trade = new PayTradeModel({ orderId: order._id, gateway: 'usdt', response: result });\n    await trade.save();\n\n    return result;\n  }\n}\n\nclass PaymentService {\n  async createOrder(data) {\n    const order = new Order(data);\n    await order.save();\n    return order;\n  }\n\n  async pay(orderId, gateway) {\n    const order = await Order.findById(orderId);\n    if (!order) throw new Error('Order not found');\n\n    let adapter;\n    switch (gateway) {\n      case 'alipay':\n        adapter = new AlipayAdapter();\n        break;\n      case 'wechat':\n        adapter = new WechatAdapter();\n        break;\n      case 'paypal':\n        adapter = new PaypalAdapter();\n        break;\n      case 'stripe':\n        adapter = new StripeAdapter();\n        break;\n      case 'usdt':\n        adapter = new UsdtAdapter();\n        break;\n      default:\n        throw new Error('Unsupported gateway');\n    }\n\n    const result = await adapter.pay(order);\n\n    order.status = 'paid'; // 根据实际结果更新\n    await order.save();\n\n    return result;\n  }\n}\n\nexport const paymentService = new PaymentService();