test-with-auth.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // 测试带认证的支付成功触发API
  2. async function testPaymentTriggerWithAuth() {
  3. console.log('=== 测试带认证的支付成功触发API ===\n');
  4. const apiUrl = 'http://localhost:8080/api/v1/payments/payment/trigger-success';
  5. // 使用一个简单的测试token(实际应用中应该从登录接口获取)
  6. // 这里使用一个硬编码的测试token
  7. const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidGVuYW50SWQiOjEsImlhdCI6MTczMzgxMjAwMCwiZXhwIjoxNzMzODE1NjAwfQ.test-signature';
  8. console.log(`使用的测试token: ${token.substring(0, 50)}...\n`);
  9. // 测试不同的订单ID
  10. const testOrderIds = [25, 26, 27, 999]; // 包括一个不存在的订单ID 999
  11. for (const orderId of testOrderIds) {
  12. console.log(`测试订单ID: ${orderId}`);
  13. console.log('='.repeat(50));
  14. try {
  15. const response = await fetch(apiUrl, {
  16. method: 'POST',
  17. headers: {
  18. 'Content-Type': 'application/json',
  19. 'Authorization': `Bearer ${token}`
  20. },
  21. body: JSON.stringify({
  22. orderId: orderId
  23. })
  24. });
  25. console.log(`状态码: ${response.status}`);
  26. console.log(`状态文本: ${response.statusText}`);
  27. if (response.ok) {
  28. const result = await response.json();
  29. console.log('成功响应:', JSON.stringify(result, null, 2));
  30. console.log(`✅ 订单ID ${orderId} API调用成功\n`);
  31. } else {
  32. const errorText = await response.text();
  33. console.log('错误响应:', errorText.substring(0, 500));
  34. console.log(`❌ 订单ID ${orderId} API调用失败\n`);
  35. }
  36. } catch (error) {
  37. console.error('请求失败:', error.message);
  38. console.error('错误堆栈:', error.stack);
  39. console.log();
  40. }
  41. }
  42. // 检查数据库中的打印任务
  43. console.log('\n=== 检查数据库中的打印任务 ===\n');
  44. const { exec } = require('child_process');
  45. const checkDb = () => {
  46. return new Promise((resolve, reject) => {
  47. exec(
  48. '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 ORDER BY created_at DESC LIMIT 10"',
  49. (error, stdout, stderr) => {
  50. if (error) {
  51. reject(error);
  52. } else {
  53. resolve(stdout);
  54. }
  55. }
  56. );
  57. });
  58. };
  59. try {
  60. const dbResult = await checkDb();
  61. console.log('最新的打印任务:');
  62. console.log(dbResult);
  63. } catch (error) {
  64. console.error('查询数据库失败:', error.message);
  65. }
  66. }
  67. // 运行测试
  68. testPaymentTriggerWithAuth().catch(error => {
  69. console.error('测试脚本执行失败:', error);
  70. process.exit(1);
  71. });