| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- // 使用正确的JWT secret测试
- const crypto = require('crypto');
- // 生成一个有效的JWT token
- function generateTestToken() {
- const header = Buffer.from(JSON.stringify({
- alg: 'HS256',
- typ: 'JWT'
- })).toString('base64url');
- const payload = Buffer.from(JSON.stringify({
- id: 1,
- username: 'test',
- tenantId: 1,
- iat: Math.floor(Date.now() / 1000),
- exp: Math.floor(Date.now() / 1000) + 3600 // 1小时后过期
- })).toString('base64url');
- const secret = 'your-secret-key'; // 与JWTUtil中的默认值一致
- const signature = crypto
- .createHmac('sha256', secret)
- .update(`${header}.${payload}`)
- .digest('base64url');
- return `${header}.${payload}.${signature}`;
- }
- async function testWithValidToken() {
- console.log('=== 使用有效token测试支付成功触发API ===\n');
- const token = generateTestToken();
- console.log(`生成的有效token: ${token.substring(0, 50)}...\n`);
- const apiUrl = 'http://localhost:8080/api/v1/payments/payment/trigger-success';
- const testOrderId = 25; // 支付成功的订单ID
- try {
- const response = await fetch(apiUrl, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${token}`
- },
- body: JSON.stringify({
- orderId: testOrderId
- })
- });
- console.log(`状态码: ${response.status}`);
- console.log(`状态文本: ${response.statusText}`);
- if (response.ok) {
- const result = await response.json();
- console.log('成功响应:', JSON.stringify(result, null, 2));
- console.log(`\n✅ API调用成功`);
- // 等待一会儿,然后检查数据库
- console.log('\n等待2秒后检查数据库...');
- await new Promise(resolve => setTimeout(resolve, 2000));
- const { exec } = require('child_process');
- exec(
- 'psql -h 127.0.0.1 -U postgres -d postgres -c "SELECT id, tenant_id, task_id, order_id, print_status, scheduled_at, created_at FROM feie_print_task_mt WHERE order_id = 25 OR order_id = 999 ORDER BY created_at DESC"',
- (error, stdout, stderr) => {
- if (error) {
- console.error('查询数据库失败:', error.message);
- } else {
- console.log('订单ID 25的打印任务:');
- console.log(stdout);
- }
- }
- );
- } else {
- const errorText = await response.text();
- console.log('错误响应:', errorText);
- console.log(`\n❌ API调用失败`);
- }
- } catch (error) {
- console.error('请求失败:', error.message);
- }
- }
- // 运行测试
- testWithValidToken().catch(error => {
- console.error('测试脚本执行失败:', error);
- });
|