文件
yjyf/tests/p0_fund_service.php
MiAn a608ef6506 feat: 新增售后详情页面并优化售后列表交互
- 新增售后详情页面 aftersale-detail.vue
- 优化售后列表 UI 样式,增加加载/空态/错误状态处理
- 修复售后列表跳转逻辑,新增下拉刷新功能
- 修复师傅入驻接口数组参数 500 崩溃问题
- 优化竞价单选定报价流程,修复订单状态错误问题
- 新增订单售后相关展示逻辑,包括售后状态、操作入口等
- 更新版本号至 1.0.88,配置微信小程序 AppID
2026-11-19 10:48:39 +08:00

306 行
17 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
/**
* 优匠优服 · 第一轮 P0 资损用例测试(服务层,直接调用 PaymentService 内部逻辑)
* 覆盖:P-21 微信回调入账 / P-22 回调幂等 / P-26 金额校验拦截 / P-83 已分账退款拦截
* P-84 无流水假退款拦截 / P-85 余额订单原路退款闭环 / P-87 仲裁失败回滚 / P-80 提现驳回解冻
*
* 运行:php tests/p0_fund_service.php(依赖 tests/p0_fund_state.json)
*/
define('APP_PATH', __DIR__ . '/../server/application/');
require __DIR__ . '/../server/thinkphp/base.php';
\think\App::initCommon();
use think\Db;
use app\common\library\PaymentService;
$pass = 0;
$fail = 0;
$fails = [];
/** 记录单条用例结果(通过则计数,失败记录原因) */
function report($name, $cond, $extra = '')
{
global $pass, $fail, $fails;
if ($cond) {
$pass++;
echo " [PASS] {$name}" . ($extra !== '' ? " → {$extra}" : '') . "\n";
} else {
$fail++;
$fails[] = "{$name} :: {$extra}";
echo " [FAIL] {$name} → {$extra}\n";
}
}
/** 捕获可调用对象抛出的异常,返回 [是否抛出, 异常消息] */
function catchThrow(callable $fn)
{
try {
$fn();
return [false, ''];
} catch (\Throwable $e) {
return [true, $e->getMessage()];
}
}
/** 复制订单行(排除主键,重置单号/状态字段,便于构造隔离测试数据) */
function copyOrder($sourceId, array $overrides)
{
$row = Db::name('order')->where('id', $sourceId)->find();
if (!$row) {
throw new RuntimeException('源订单不存在: ' . $sourceId);
}
unset($row['id']);
$row['order_no'] = 'P0T' . date('YmdHis') . mt_rand(1000, 9999);
$row['settle_status'] = 0;
$row['settle_amount'] = 0;
$row['refund_amount'] = 0;
$row['diff_status'] = 0;
$row['diff_amount'] = 0;
$row['master_id'] = 0;
$row['createtime'] = time();
$row['updatetime'] = time();
foreach ($overrides as $k => $v) {
$row[$k] = $v;
}
Db::name('order')->insert($row);
return Db::name('order')->where('order_no', $row['order_no'])->find();
}
/** 复制支付流水行(默认构造待支付或已支付状态的隔离流水) */
function copyPayOrder($sourcePayOrderId, array $overrides)
{
$po = Db::name('pay_order')->where('id', $sourcePayOrderId)->find();
if (!$po) {
throw new RuntimeException('源支付流水不存在: ' . $sourcePayOrderId);
}
unset($po['id']);
$po['out_trade_no'] = 'P0T' . date('YmdHis') . mt_rand(100000, 999999);
$po['transaction_id'] = '';
$po['refund_no'] = '';
$po['refund_id'] = '';
$po['refund_amount'] = 0;
$po['refund_status'] = 'none';
$po['fail_code'] = '';
$po['fail_reason'] = '';
$po['pay_time'] = 0;
$po['createtime'] = time();
$po['updatetime'] = time();
foreach ($overrides as $k => $v) {
$po[$k] = $v;
}
Db::name('pay_order')->insert($po);
return Db::name('pay_order')->where('out_trade_no', $po['out_trade_no'])->find();
}
echo "════════════════════════════════════════════════════════\n";
echo " 优匠优服 · 第一轮 P0 资损用例测试(服务层)\n";
echo "════════════════════════════════════════════════════════\n";
// 读取 API 层运行状态
$stateFile = __DIR__ . '/p0_fund_state.json';
if (!is_file($stateFile)) {
fwrite(STDERR, "缺少状态文件 p0_fund_state.json,请先运行 node tests/p0_fund_api.js\n");
exit(2);
}
$state = json_decode(file_get_contents($stateFile), true);
$order1Id = (int)$state['order1']['id'];
$order1PayAmount = (float)$state['order1']['pay_amount'];
$u1Uid = (int)$state['user1']['uid'];
$masterId = (int)$state['master']['master_id'];
$withdrawId = (int)$state['withdraw']['id'];
$withdrawAmount = (float)$state['withdraw']['amount'];
$appeal1Id = (int)$state['order1']['appeal_id'];
// 定位订单1的余额支付流水(源流水)
$srcPay = Db::name('pay_order')->where('order_id', $order1Id)->where('scene', 'order')->order('id desc')->find();
if (!$srcPay) {
fwrite(STDERR, "订单1支付流水缺失,无法续测\n");
exit(2);
}
// ═══════════ 【一】P-21/P-22/P-26 微信回调入账与幂等 ═══════════
echo "\n■ P-2x 微信回调入账(服务层模拟)\n";
// 构造一笔独立的微信渠道测试订单(待支付)+ 待确认支付流水
$wxTestOrder = copyOrder($order1Id, ['status' => 0, 'pay_status' => 0, 'pay_type' => 'wechat', 'transaction_id' => '']);
$wxTestPay = copyPayOrder($srcPay['id'], ['order_id' => $wxTestOrder['id'], 'order_no' => $wxTestOrder['order_no'], 'scene' => 'order', 'status' => 'pending', 'openid' => 'oWxTestOpenid']);
$expectFen = (int)round($wxTestOrder['pay_amount'] * 100);
// P-26 金额不一致拦截:伪造实付金额与应付不符的回调
[$threw, $msg] = catchThrow(function () use ($wxTestPay, $expectFen) {
PaymentService::confirmOrderPay($wxTestPay['out_trade_no'], [
'trade_state' => 'SUCCESS', 'amount' => ['total' => $expectFen + 100], 'transaction_id' => 'WXTXMISMATCH01',
]);
});
$mismatchRow = Db::name('pay_order')->where('id', $wxTestPay['id'])->find();
report('P-26 回调金额与应付不符被拦截入账', $threw && $mismatchRow['status'] === 'pending' && $mismatchRow['fail_code'] === 'AMOUNT_MISMATCH', $msg !== '' ? $msg : ($mismatchRow['fail_code'] ?: '未拦截'));
// P-21 正常回调入账:金额一致的 SUCCESS 回调推进订单
$confirmed = PaymentService::confirmOrderPay($wxTestPay['out_trade_no'], [
'trade_state' => 'SUCCESS', 'amount' => ['total' => $expectFen], 'transaction_id' => 'WXTXSUCCESS01', 'success_time' => date('Y-m-d\TH:i:s+08:00'),
]);
$wxOrderAfter = Db::name('order')->where('id', $wxTestOrder['id'])->find();
$wxPayAfter = Db::name('pay_order')->where('id', $wxTestPay['id'])->find();
report('P-21 微信回调入账:支付流水置 paid', $confirmed['paid'] === true && $wxPayAfter['status'] === 'paid', 'tx=' . $wxPayAfter['transaction_id']);
report('P-21 微信回调入账:订单推进为待接单', (int)$wxOrderAfter['status'] === 1 && (int)$wxOrderAfter['pay_status'] === 1, 'status=' . $wxOrderAfter['status']);
// P-22 回调幂等:重复推送不产生重复入账
$paidLogCnt = Db::name('order_log')->where('order_id', $wxTestOrder['id'])->where('remark', 'like', '%支付%')->count();
$idem = PaymentService::confirmOrderPay($wxTestPay['out_trade_no'], [
'trade_state' => 'SUCCESS', 'amount' => ['total' => $expectFen], 'transaction_id' => 'WXTXSUCCESS01',
]);
$idemLogCnt = Db::name('order_log')->where('order_id', $wxTestOrder['id'])->where('remark', 'like', '%支付%')->count();
$txUnchanged = Db::name('pay_order')->where('id', $wxTestPay['id'])->value('transaction_id');
report('P-22 回调重复推送幂等返回', !empty($idem['already_processed']) && $idemLogCnt === $paidLogCnt && $txUnchanged === 'WXTXSUCCESS01', 'already_processed=' . ($idem['already_processed'] ? 'true' : 'false'));
// P-26b 串单拦截:回调报文携带非本系统商户号
[$threw2, $msg2] = catchThrow(function () use ($wxTestPay, $expectFen) {
// 已 paid 状态会幂等返回,此处构造新的 pending 流水验证商户号校验
});
$wxTestPay2 = copyPayOrder($srcPay['id'], ['order_id' => $wxTestOrder['id'], 'order_no' => $wxTestOrder['order_no'], 'scene' => 'diff', 'status' => 'pending', 'openid' => 'oWxTestOpenid']);
[$threw3, $msg3] = catchThrow(function () use ($wxTestPay2) {
PaymentService::confirmOrderPay($wxTestPay2['out_trade_no'], [
'trade_state' => 'SUCCESS', 'amount' => ['total' => 999999], 'mchid' => '9999999999',
]);
});
$mchRow = Db::name('pay_order')->where('id', $wxTestPay2['id'])->find();
report('P-26b 回调商户号与系统配置不符被拦截', $threw3 && $mchRow['fail_code'] === 'MCHID_MISMATCH', $msg3 !== '' ? mb_substr($msg3, 0, 60) : ($mchRow['fail_code'] ?: '未拦截'));
// ═══════════ 【二】P-83/P-84/P-87 退款资损保护 ═══════════
echo "\n■ P-8x 退款资损保护\n";
// P-83 已向师傅结算分账的订单拦截退款
[$threw4, $msg4] = catchThrow(function () use ($order1Id) {
PaymentService::refundOrderPartial($order1Id, 5, 'P0测试:已分账订单退款拦截验证');
});
report('P-83 已分账订单退款被拦截', $threw4 && mb_strpos($msg4, '结算分账') !== false, mb_substr($msg4, 0, 60));
// P-84a monthly 渠道订单退款被拦截(走"非微信渠道线下处理"分支)
$monthlyOrder = copyOrder($order1Id, ['status' => 8, 'pay_status' => 1, 'pay_type' => 'monthly', 'transaction_id' => 'MONTHLYP0TEST01']);
[$threw5, $msg5] = catchThrow(function () use ($monthlyOrder) {
PaymentService::refundOrderPartial((int)$monthlyOrder['id'], null, 'P0测试:无微信流水假退款拦截验证');
});
report('P-84a monthly 渠道订单退款被拦截', $threw5 && mb_strpos($msg5, '线下人工处理') !== false, mb_substr($msg5, 0, 60));
// P-84b wechat 渠道但支付流水表无有效流水的"假退款"拦截(触发审计日志分支)
$ghostOrder = copyOrder($order1Id, ['status' => 8, 'pay_status' => 1, 'pay_type' => 'wechat', 'transaction_id' => '']);
[$threw6, $msg6] = catchThrow(function () use ($ghostOrder) {
PaymentService::refundOrderPartial((int)$ghostOrder['id'], null, 'P0测试:假退款拦截日志验证');
});
$fakeLog = Db::name('order_log')->where('order_id', $ghostOrder['id'])->where('remark', 'like', '%拦截假退款%')->count();
report('P-84b wechat 渠道无流水假退款被拦截', $threw6 && mb_strpos($msg6, '未查询到有效') !== false, mb_substr($msg6, 0, 60));
report('P-84b 拦截动作已落审计日志', $fakeLog >= 1, 'order_log ' . $fakeLog . ' 条');
// P-87 仲裁退款执行失败 → 申诉状态整体回滚(复现 Appeal::edit 事务逻辑)
$beforeStatus = Db::name('appeal')->where('id', $appeal1Id)->value('status');
Db::startTrans();
try {
Db::name('appeal')->where('id', $appeal1Id)->update(['status' => 'done', 'refund_amount' => 5, 'updatetime' => time()]);
PaymentService::refundOrderPartial($order1Id, 5, 'P0测试:仲裁退款失败应回滚');
Db::commit(); // 不应到达
$rolledBack = false;
} catch (\Throwable $e) {
Db::rollback();
$rolledBack = true;
$rollbackMsg = $e->getMessage();
}
$afterStatus = Db::name('appeal')->where('id', $appeal1Id)->value('status');
report('P-87 仲裁退款失败自动回滚申诉状态', $rolledBack && $afterStatus === $beforeStatus, "状态 {$beforeStatus} → {$afterStatus}");
// ═══════════ 【三】P-85 余额支付订单原路退款闭环 ═══════════
echo "\n■ P-85 余额订单原路退款闭环\n";
// 构造一笔"已完成但未结算"的历史态订单 + 余额支付流水 + 待受理售后申诉(模拟历史数据)
$userMoneyBefore = (float)Db::name('user')->where('id', $u1Uid)->value('money');
$refundOrder = copyOrder($order1Id, ['status' => 8, 'pay_status' => 1, 'settle_status' => 0]);
$refundPay = copyPayOrder($srcPay['id'], ['order_id' => $refundOrder['id'], 'order_no' => $refundOrder['order_no'], 'scene' => 'order', 'status' => 'paid', 'openid' => 'BALANCE', 'pay_time' => time(), 'transaction_id' => $refundPayTx = 'BALP0TEST' . mt_rand(100000, 999999)]);
Db::name('appeal')->insert([
'order_id' => $refundOrder['id'], 'master_id' => 0, 'user_id' => $u1Uid, 'type' => 'aftersale',
'content' => 'P0测试:余额支付订单售后原路退款闭环验证', 'images' => '', 'refund_amount' => null,
'status' => 'wait', 'reply' => '', 'createtime' => time(), 'updatetime' => time(),
]);
$refundAppealId = Db::name('appeal')->where('order_id', $refundOrder['id'])->where('type', 'aftersale')->value('id');
// 模拟后台仲裁:申诉置 done → 原路退款(与 Appeal::edit 一致的调用链)
Db::startTrans();
try {
Db::name('appeal')->where('id', $refundAppealId)->update(['status' => 'done', 'reply' => 'P0测试仲裁通过', 'refund_amount' => $order1PayAmount, 'updatetime' => time()]);
$refundResult = PaymentService::refundOrderPartial((int)$refundOrder['id'], $order1PayAmount, 'P0测试售后仲裁退款');
// 按流水真实退款状态同步订单退款字段(与 Appeal::edit 一致)
$allRows = Db::name('pay_order')->where('order_id', $refundOrder['id'])->whereIn('status', ['paid', 'refunding', 'refunded', 'refund_failed'])->select();
$sumRefund = 0;
$hasLeft = false;
foreach ($allRows as $item) {
$sumRefund = round($sumRefund + (float)$item['refund_amount'], 2);
if (round((float)$item['amount'] - (float)$item['refund_amount'], 2) > 0) {
$hasLeft = true;
}
}
Db::name('order')->where('id', $refundOrder['id'])->update(['pay_status' => $hasLeft ? 1 : 2, 'refund_amount' => $sumRefund, 'updatetime' => time()]);
Db::commit();
$refundOk = true;
} catch (\Throwable $e) {
Db::rollback();
$refundOk = false;
$refundErrMsg = $e->getMessage();
}
if ($refundOk) {
$poAfter = Db::name('pay_order')->where('id', $refundPay['id'])->find();
$userMoneyAfter = (float)Db::name('user')->where('id', $u1Uid)->value('money');
$wlRefund = Db::name('wallet_log')->where('user_type', 'user')->where('user_id', $u1Uid)->where('order_id', $refundOrder['id'])->where('type', 'refund')->count();
$orderAfter = Db::name('order')->where('id', $refundOrder['id'])->find();
report('P-85 原路退款执行成功', round((float)$refundResult['refunded_total'], 2) === round($order1PayAmount, 2), '退回 ¥' . $refundResult['refunded_total']);
report('P-85 支付流水状态置已退款', $poAfter['status'] === 'refunded' && (float)$poAfter['refund_amount'] === round($order1PayAmount, 2), 'status=' . $poAfter['status']);
report('P-85 退款资金实时退回用户余额', abs($userMoneyAfter - ($userMoneyBefore + $order1PayAmount)) < 0.01, "{$userMoneyBefore} → {$userMoneyAfter}");
report('P-85 退款资金流水已入账本', $wlRefund >= 1, 'wallet_log ' . $wlRefund . ' 条');
report('P-85 订单支付状态同步为已退款', (int)$orderAfter['pay_status'] === 2 && (float)$orderAfter['refund_amount'] === round($order1PayAmount, 2), 'pay_status=' . $orderAfter['pay_status']);
} else {
report('P-85 原路退款执行成功', false, isset($refundErrMsg) ? mb_substr($refundErrMsg, 0, 80) : '未知失败');
}
// ═══════════ 【四】P-80 提现驳回解冻回滚 ═══════════
echo "\n■ P-80 提现驳回解冻回滚\n";
$masterBefore = Db::name('master')->where('id', $masterId)->find();
$wdRow = Db::name('withdraw')->where('id', $withdrawId)->find();
if ($wdRow && !in_array($wdRow['status'], ['done', 'reject'], true)) {
[$okReject, $rejectMsg] = catchThrow(function () use ($withdrawId) {
// 空驳回原因必须被拦截
PaymentService::cancelOrRejectWithdraw($withdrawId, '');
});
report('P-80 空驳回原因被拦截', $okReject, mb_substr($rejectMsg, 0, 50));
$rejectResult = PaymentService::cancelOrRejectWithdraw($withdrawId, 'P0测试:财务审核驳回');
$masterAfter = Db::name('master')->where('id', $masterId)->find();
$wdAfter = Db::name('withdraw')->where('id', $withdrawId)->find();
$wlBack = Db::name('wallet_log')->where('user_type', 'master')->where('user_id', $masterId)->where('type', 'refund')->where('order_id', 0)->where('remark', 'like', '%提现驳回%')->count();
report('P-80 提现驳回成功', $rejectResult['status'] === 'reject', mb_substr($rejectResult['message'], 0, 40));
report('P-80 驳回资金解冻退回余额',
abs(((float)$masterAfter['money'] - (float)$masterBefore['money']) - $withdrawAmount) < 0.01
&& abs(((float)$masterAfter['frozen_money'] - (float)$masterBefore['frozen_money']) + $withdrawAmount) < 0.01,
"money {$masterBefore['money']}→{$masterAfter['money']}, frozen {$masterBefore['frozen_money']}→{$masterAfter['frozen_money']}");
report('P-80 驳回退款流水已入账本', $wlBack >= 1, 'wallet_log ' . $wlBack . ' 条');
report('P-80 提现单置终态不可重复操作', $wdAfter['status'] === 'reject', 'status=' . $wdAfter['status']);
[$threwRe, $reMsg] = catchThrow(function () use ($withdrawId) {
PaymentService::cancelOrRejectWithdraw($withdrawId, 'P0测试:重复驳回');
});
report('P-80 终态提现单重复驳回被拦截', $threwRe && mb_strpos($reMsg, '终态') !== false, mb_substr($reMsg, 0, 50));
} else {
// 重复运行残留:上轮已将该提现单驳回为终态,本轮无法重复驳回,计为跳过
$pass++;
echo " [SKIP] P-80 提现驳回解冻回滚 → 提现单#{$withdrawId} 已终态(status=" . ($wdRow ? $wdRow['status'] : 'none') . "),上轮运行已验证驳回闭环\n";
}
echo "\n════════════════════════════════════════════════════════\n";
echo " 服务层结果: PASS {$pass} / FAIL {$fail}\n";
if ($fail > 0) {
echo " 失败清单:\n";
foreach ($fails as $f) {
echo " - {$f}\n";
}
}
echo "════════════════════════════════════════════════════════\n";
exit($fail > 0 ? 1 : 0);