提交
This commit is contained in:
@@ -10,7 +10,7 @@ class AlipayService {
|
||||
this.alipayPublicKey = null;
|
||||
this.alipaySdk = null;
|
||||
this.isInitialized = false;
|
||||
|
||||
|
||||
this.initializeAlipay();
|
||||
}
|
||||
|
||||
@@ -25,26 +25,10 @@ class AlipayService {
|
||||
|
||||
console.log('支付宝私钥路径:', privateKeyPath);
|
||||
console.log('支付宝公钥路径:', publicKeyPath);
|
||||
|
||||
// 验证文件有效性
|
||||
if (!this.isValidFile(privateKeyPath)) {
|
||||
throw new Error(`支付宝私钥文件无效或不存在: ${privateKeyPath}`);
|
||||
}
|
||||
|
||||
if (!this.isValidFile(publicKeyPath)) {
|
||||
throw new Error(`支付宝公钥文件无效或不存在: ${publicKeyPath}`);
|
||||
}
|
||||
|
||||
console.log('尝试加载支付宝私钥文件:', privateKeyPath);
|
||||
this.privateKey = fs.readFileSync(privateKeyPath, 'utf8');
|
||||
console.log('支付宝私钥加载成功');
|
||||
|
||||
console.log('尝试加载支付宝公钥文件:', publicKeyPath);
|
||||
this.alipayPublicKey = fs.readFileSync(publicKeyPath, 'utf8');
|
||||
console.log('支付宝公钥加载成功');
|
||||
|
||||
this.initializeSDK();
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('支付宝服务初始化失败:', error.message);
|
||||
console.error('支付宝功能将不可用');
|
||||
@@ -60,7 +44,7 @@ class AlipayService {
|
||||
console.warn('支付宝密钥未加载,跳过SDK初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 支付宝配置
|
||||
this.config = {
|
||||
appId: process.env.ALIPAY_APP_ID || '2021001161683774', // 替换为实际的应用ID
|
||||
@@ -82,7 +66,7 @@ class AlipayService {
|
||||
signType: this.config.signType,
|
||||
timeout: this.config.timeout
|
||||
});
|
||||
|
||||
|
||||
this.isInitialized = true;
|
||||
console.log('支付宝SDK初始化成功');
|
||||
}
|
||||
@@ -132,16 +116,16 @@ class AlipayService {
|
||||
if (!this.isServiceAvailable()) {
|
||||
throw new Error('支付宝服务未初始化或不可用');
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const db = getDB();
|
||||
|
||||
|
||||
// 生成订单号
|
||||
const outTradeNo = this.generateOrderNo();
|
||||
const totalFee = 39900; // 399元,单位:分
|
||||
const subject = '用户注册激活费用';
|
||||
const body = `用户${username}(${phone})注册激活费用`;
|
||||
|
||||
|
||||
// 业务参数
|
||||
const bizContent = {
|
||||
out_trade_no: outTradeNo,
|
||||
@@ -149,16 +133,16 @@ class AlipayService {
|
||||
subject: subject,
|
||||
body: body,
|
||||
product_code: 'QUICK_WAP_WAY',
|
||||
quit_url: process.env.ALIPAY_QUIT_URL || 'https://your-domain.com/payment/cancel'
|
||||
quit_url: process.env.ALIPAY_QUIT_URL
|
||||
};
|
||||
|
||||
|
||||
// 使用新版SDK的pageExecute方法生成支付URL
|
||||
const payUrl = this.alipaySdk.pageExecute('alipay.trade.wap.pay', 'GET', {
|
||||
bizContent: bizContent,
|
||||
notifyUrl: process.env.ALIPAY_NOTIFY_URL || 'https://your-domain.com/api/payment/alipay/notify',
|
||||
returnUrl: process.env.ALIPAY_RETURN_URL || 'https://your-domain.com/payment/success'
|
||||
notifyUrl: process.env.ALIPAY_NOTIFY_URL,
|
||||
returnUrl: process.env.ALIPAY_RETURN_URL
|
||||
});
|
||||
|
||||
|
||||
// 保存订单到数据库
|
||||
await db.execute(
|
||||
`INSERT INTO payment_orders
|
||||
@@ -166,14 +150,14 @@ class AlipayService {
|
||||
VALUES (?, ?, ?, ?, ?, ?, NOW())`,
|
||||
[userId, outTradeNo, totalFee, body, 'ALIPAY_WAP', 'pending']
|
||||
);
|
||||
|
||||
|
||||
console.log('支付宝支付订单创建成功:', {
|
||||
userId,
|
||||
outTradeNo,
|
||||
totalFee,
|
||||
payUrl
|
||||
});
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
@@ -202,18 +186,18 @@ class AlipayService {
|
||||
if (!this.isServiceAvailable()) {
|
||||
throw new Error('支付宝服务未初始化或不可用');
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const result = await this.alipaySdk.exec('alipay.trade.query', {
|
||||
bizContent: {
|
||||
out_trade_no: outTradeNo
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (result.code === '10000') {
|
||||
// 查询成功
|
||||
const tradeStatus = result.tradeStatus;
|
||||
|
||||
|
||||
// 如果支付成功,更新数据库
|
||||
if (tradeStatus === 'TRADE_SUCCESS') {
|
||||
await this.updatePaymentStatus(outTradeNo, {
|
||||
@@ -222,7 +206,7 @@ class AlipayService {
|
||||
paidAt: new Date()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
@@ -256,7 +240,7 @@ class AlipayService {
|
||||
async updatePaymentStatus(outTradeNo, updateData) {
|
||||
try {
|
||||
const db = getDB();
|
||||
|
||||
|
||||
// 更新订单状态
|
||||
await db.execute(
|
||||
`UPDATE payment_orders
|
||||
@@ -264,21 +248,21 @@ class AlipayService {
|
||||
WHERE out_trade_no = ?`,
|
||||
[updateData.status, updateData.transactionId, updateData.paidAt, outTradeNo]
|
||||
);
|
||||
|
||||
|
||||
// 如果支付成功,更新用户支付状态
|
||||
if (updateData.status === 'paid') {
|
||||
const [orders] = await db.execute(
|
||||
'SELECT user_id FROM payment_orders WHERE out_trade_no = ?',
|
||||
[outTradeNo]
|
||||
);
|
||||
|
||||
|
||||
if (orders.length > 0) {
|
||||
const userId = orders[0].user_id;
|
||||
await db.execute(
|
||||
'UPDATE users SET payment_status = ? WHERE id = ?',
|
||||
['paid', userId]
|
||||
);
|
||||
|
||||
|
||||
console.log('用户支付状态更新成功:', { userId, outTradeNo });
|
||||
}
|
||||
}
|
||||
@@ -299,7 +283,7 @@ class AlipayService {
|
||||
console.error('支付宝服务未初始化,无法验证签名');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
return this.alipaySdk.checkNotifySign(params);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user