/** * WechatPayment Component * ───────────────────────────────────────────────────────────────────────────── * Handles WeChat Pay Native (QR code) for PC and H5 for mobile browsers. * * Flow: * 1. User enters CNY amount * 2. Component calls payment.createWechatOrder mutation * 3. PC (NATIVE): displays QR code for user to scan with WeChat * Mobile (H5): redirects to WeChat H5 payment page * 4. Polls payment.queryWechatOrder every 5 seconds * 5. On success (dbStatus === "distributed"), shows success message * * Note: JSAPI pay (inside WeChat browser) requires openid — not implemented here. * For WeChat browser users, the H5 pay type is used as fallback. */ import { useState, useEffect } from "react"; import { trpc } from "@/lib/trpc"; import { toast } from "sonner"; const XIC_PRICE_USD = 0.02; const CNY_USD_RATE = 0.138; function calcXicFromCny(cny: number): number { const usd = cny * CNY_USD_RATE; return Math.floor(usd / XIC_PRICE_USD); } // Simple QR code display using a public QR API (no external dependency needed) // In production, use a proper QR library like qrcode.react function QRCodeDisplay({ url, size = 200 }: { url: string; size?: number }) { // Use Google Charts QR API as fallback (works without npm package) // TODO: Replace with qrcode.react for production (npm install qrcode.react) const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?size=${size}x${size}&data=${encodeURIComponent(url)}&bgcolor=0a0a0f&color=f0b429&margin=10`; return (
Scan with WeChat to pay
{xicAmount.toLocaleString()} {" "} XIC tokens are being distributed to your address.
Scan QR Code with WeChat
Amount: ¥{cnyAmount.toFixed(2)} CNY
You will receive: {xicAmount.toLocaleString()} XIC
Checking payment status every 5 seconds...
Order ID: {orderId}
Redirected to WeChat Pay
Complete the payment in WeChat, then return here.
Order ID: {orderId}
• PC: Scan QR code with WeChat app
• Mobile: Redirects to WeChat H5 payment
• XIC tokens distributed within 1–5 minutes after confirmation