Files
ai_dianshang/miniprogram/pages/debug/payment-test.js
2025-11-17 13:32:54 +08:00

174 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { config } from '../../config/index';
Page({
data: {
testResult: ''
},
// 测试正确的支付参数(标准格式)
testWechatPayment() {
const paymentParams = {
timeStamp: String(Math.floor(Date.now() / 1000)),
nonceStr: 'test_nonce_str_' + Math.random().toString(36).substr(2, 15),
package: 'prepay_id=wx_test_prepay_id_123456789',
signType: 'MD5',
paySign: 'test_pay_sign_123456789'
};
console.log('测试标准支付参数:', paymentParams);
console.log('参数详情:');
console.log('- timeStamp:', paymentParams.timeStamp, '(类型:', typeof paymentParams.timeStamp, ')');
console.log('- nonceStr:', paymentParams.nonceStr, '(长度:', paymentParams.nonceStr.length, ')');
console.log('- package:', paymentParams.package, '(格式检查: prepay_id=前缀)');
console.log('- signType:', paymentParams.signType);
console.log('- paySign:', paymentParams.paySign, '(长度:', paymentParams.paySign.length, ')');
wx.requestPayment({
...paymentParams,
success: (res) => {
console.log('支付成功:', res);
this.setData({
testResult: '支付成功: ' + JSON.stringify(res)
});
},
fail: (err) => {
console.log('支付失败:', err);
console.log('错误详情:');
console.log('- errMsg:', err.errMsg);
console.log('- errCode:', err.errCode);
this.setData({
testResult: '支付失败: ' + JSON.stringify(err) + '\n错误信息: ' + err.errMsg
});
}
});
},
// 测试错误的package格式不含prepay_id前缀
testWrongPackageFormat() {
const paymentParams = {
timeStamp: String(Math.floor(Date.now() / 1000)),
nonceStr: 'test_nonce_str_' + Math.random().toString(36).substr(2, 15),
package: 'wx_test_prepay_id_123456789', // 错误格式缺少prepay_id=前缀
signType: 'MD5',
paySign: 'test_pay_sign_123456789'
};
console.log('测试错误package格式:', paymentParams);
wx.requestPayment({
...paymentParams,
success: (res) => {
console.log('支付成功:', res);
this.setData({
testResult: '支付成功: ' + JSON.stringify(res)
});
},
fail: (err) => {
console.log('支付失败:', err);
this.setData({
testResult: '支付失败: ' + JSON.stringify(err)
});
}
});
},
// 测试空的package参数
testEmptyPackage() {
const paymentParams = {
timeStamp: String(Math.floor(Date.now() / 1000)),
nonceStr: 'test_nonce_str_' + Math.random().toString(36).substr(2, 15),
package: '', // 空的package
signType: 'MD5',
paySign: 'test_pay_sign_123456789'
};
console.log('测试空package参数:', paymentParams);
wx.requestPayment({
...paymentParams,
success: (res) => {
console.log('支付成功:', res);
this.setData({
testResult: '支付成功: ' + JSON.stringify(res)
});
},
fail: (err) => {
console.log('支付失败:', err);
this.setData({
testResult: '支付失败: ' + JSON.stringify(err)
});
}
});
},
// 测试真实的后端API调用
testRealPaymentAPI() {
console.log('开始测试真实后端API...');
wx.request({
url: `${config.apiBase}/payment/create`,
method: 'POST',
header: {
'Content-Type': 'application/json'
},
data: {
amount: 1,
description: '测试支付'
},
success: (res) => {
console.log('后端API返回:', res.data);
if (res.data.code === 200 && res.data.data) {
const paymentData = res.data.data;
console.log('后端返回的支付参数:', paymentData);
console.log('参数验证:');
console.log('- timeStamp:', paymentData.timeStamp, '(类型:', typeof paymentData.timeStamp, ')');
console.log('- nonceStr:', paymentData.nonceStr, '(长度:', paymentData.nonceStr ? paymentData.nonceStr.length : 'undefined', ')');
console.log('- package:', paymentData.package, '(格式:', paymentData.package ? (paymentData.package.startsWith('prepay_id=') ? '正确' : '错误') : 'undefined', ')');
console.log('- signType:', paymentData.signType);
console.log('- paySign:', paymentData.paySign, '(长度:', paymentData.paySign ? paymentData.paySign.length : 'undefined', ')');
// 检查是否包含total_fee参数
if (paymentData.total_fee !== undefined) {
console.warn('⚠️ 发现total_fee参数:', paymentData.total_fee);
}
wx.requestPayment({
...paymentData,
success: (payRes) => {
console.log('支付成功:', payRes);
this.setData({
testResult: '真实API支付成功: ' + JSON.stringify(payRes)
});
},
fail: (payErr) => {
console.log('支付失败:', payErr);
console.log('错误详情:');
console.log('- errMsg:', payErr.errMsg);
console.log('- errCode:', payErr.errCode);
this.setData({
testResult: '真实API支付失败: ' + JSON.stringify(payErr) + '\n错误信息: ' + payErr.errMsg
});
}
});
} else {
console.log('API返回错误:', res.data);
this.setData({
testResult: 'API调用失败: ' + JSON.stringify(res.data)
});
}
},
fail: (err) => {
console.log('API请求失败:', err);
this.setData({
testResult: 'API请求失败: ' + JSON.stringify(err)
});
}
});
},
// 清除测试结果
clearResult() {
this.setData({
testResult: ''
});
}
});