Files
makemd/server/src/services/PaymentRiskService.ts

51 lines
2.2 KiB
TypeScript
Raw Normal View History

import db from '../config/database';
import { logger } from '../utils/logger';
import { DecisionExplainabilityEngine } from '../core/ai/DecisionExplainabilityEngine';
/**
* [BIZ_OPS_153] (Gateway)
* @description 退 PayPal/Stripe
*/
export class PaymentRiskService {
/**
* (BIZ_OPS_153)
*/
static async monitorPaymentGatewayRisk(tenantId: string, gatewayId: string): Promise<any> {
logger.info(`[PaymentRisk] Monitoring gateway risk for Tenant: ${tenantId}, Gateway: ${gatewayId}`);
try {
// 1. 获取近 7 天的取消率 (模拟)
const cancellationRate = 0.15; // 15% 取消率
const threshold = 0.10;
// 2. 识别风险:如果超过 10%
if (cancellationRate > threshold) {
const advice = `GATEWAY RISK ALERT: Cancellation rate (${(cancellationRate * 100).toFixed(1)}%) is above the safe threshold (10%). ` +
`High risk of account suspension by the payment provider (PayPal/Stripe). ` +
`Suggesting immediate pause of high-risk traffic or increased anti-fraud review.`;
// 3. [UX_XAI_01] 记录决策证据链
await DecisionExplainabilityEngine.logDecision({
tenantId,
module: 'PAYMENT_SECURITY',
resourceId: gatewayId,
decisionType: 'GATEWAY_SUSPENSION_PREVENTION',
causalChain: advice,
factors: [
{ name: 'CancellationRate', value: (cancellationRate * 100).toFixed(1) + '%', weight: 0.9, impact: 'NEGATIVE' },
{ name: 'ThresholdRate', value: (threshold * 100).toFixed(1) + '%', weight: 0.1, impact: 'NEUTRAL' }
],
traceId: 'payment-risk-' + Date.now()
});
return { success: true, riskLevel: 'HIGH', cancellationRate, advice, status: 'PENDING_REVIEW' };
}
return { success: true, riskLevel: 'LOW', message: 'Gateway risk is within limits' };
} catch (err: any) {
logger.error(`[PaymentRisk][WARN] Monitoring failed: ${err.message}`);
return { success: false, error: err.message };
}
}
}