2026-03-05 01:48:10 +08:00
|
|
|
import { NextRequest } from 'next/server';
|
|
|
|
|
import { handlePaymentNotify } from '@/lib/order/service';
|
2026-03-07 04:15:54 +08:00
|
|
|
import { paymentRegistry } from '@/lib/payment';
|
|
|
|
|
import type { PaymentType } from '@/lib/payment';
|
2026-03-07 00:07:56 +08:00
|
|
|
import { getEnv } from '@/lib/config';
|
2026-03-07 04:15:54 +08:00
|
|
|
import { extractHeaders } from '@/lib/utils/api';
|
2026-03-05 01:48:10 +08:00
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
try {
|
2026-03-07 00:07:56 +08:00
|
|
|
// 官方支付宝未配置时,直接返回成功(避免旧回调重试产生错误日志)
|
|
|
|
|
const env = getEnv();
|
|
|
|
|
if (!env.ALIPAY_APP_ID || !env.ALIPAY_PRIVATE_KEY) {
|
|
|
|
|
return new Response('success', { headers: { 'Content-Type': 'text/plain' } });
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 04:15:54 +08:00
|
|
|
const provider = paymentRegistry.getProvider('alipay_direct' as PaymentType);
|
2026-03-05 01:48:10 +08:00
|
|
|
const rawBody = await request.text();
|
2026-03-07 04:15:54 +08:00
|
|
|
const headers = extractHeaders(request);
|
2026-03-05 01:48:10 +08:00
|
|
|
|
2026-03-07 04:15:54 +08:00
|
|
|
const notification = await provider.verifyNotification(rawBody, headers);
|
|
|
|
|
if (!notification) {
|
|
|
|
|
return new Response('success', { headers: { 'Content-Type': 'text/plain' } });
|
|
|
|
|
}
|
|
|
|
|
const success = await handlePaymentNotify(notification, provider.name);
|
2026-03-05 01:48:10 +08:00
|
|
|
return new Response(success ? 'success' : 'fail', {
|
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Alipay notify error:', error);
|
|
|
|
|
return new Response('fail', {
|
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|