fix: 优化支付适配器配置和错误处理机制
- 新增BasePaymentAdapter配置状态管理 - 添加isConfigured属性跟踪配置状态 - 支持适配器配置验证机制 - 优化DouyinPaymentAdapter配置处理 - 配置缺失时警告而非抛异常,允许应用正常启动 - 在创建订单时检查配置状态并抛出友好错误 - 提升系统容错性和用户体验
This commit is contained in:
parent
573b43f171
commit
a859f0306a
|
|
@ -38,6 +38,7 @@ import {
|
|||
@Injectable()
|
||||
export abstract class BasePaymentAdapter implements IPaymentAdapter {
|
||||
protected readonly logger = new Logger(this.constructor.name);
|
||||
protected isConfigured: boolean = false;
|
||||
|
||||
abstract platform: PlatformType;
|
||||
abstract paymentMethod: PaymentMethod;
|
||||
|
|
|
|||
|
|
@ -100,9 +100,13 @@ export class DouyinPaymentAdapter extends BasePaymentAdapter {
|
|||
!this.privateKey
|
||||
) {
|
||||
const message = '抖音支付配置不完整,相关功能将不可用。请配置环境变量: DOUYIN_MINIPROGRAM_APPID, DOUYIN_MINIPROGRAM_SECRET, DOUYIN_MERCHANT_ID, DOUYIN_PRIVATE_KEY';
|
||||
this.logger.error(message);
|
||||
throw new ConfigurationException(message, 'DOUYIN_PAYMENT_CONFIG');
|
||||
this.logger.warn(message);
|
||||
// 不抛出异常,允许应用正常启动,但标记为未配置状态
|
||||
this.isConfigured = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.isConfigured = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -114,6 +118,10 @@ export class DouyinPaymentAdapter extends BasePaymentAdapter {
|
|||
async createPaymentOrder(
|
||||
orderData: CreatePaymentOrderData,
|
||||
): Promise<PaymentOrderResult> {
|
||||
if (!this.isConfigured) {
|
||||
throw new PaymentException('抖音支付未正确配置', undefined, 'PAYMENT_NOT_CONFIGURED');
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 参数验证
|
||||
if (!this.validateAmount(orderData.amount)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue