feat: 新增后台人工充值功能及支付结果页
- 新增后台人工充值模块,支持上传支付凭证 - 新增支付结果查询接口及页面 - 新增订单支付成功跳转结果页逻辑 - 更新钱包流水类型标签区分充值方式 - 新增插件路由 PHP 8 兼容错误报告设置
这个提交包含在:
35
tests/admin_recharge_test.php
普通文件
35
tests/admin_recharge_test.php
普通文件
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
define('APP_PATH', __DIR__ . '/../server/application/');
|
||||
require __DIR__ . '/../server/thinkphp/base.php';
|
||||
\think\App::initCommon();
|
||||
|
||||
use app\common\library\AdminRechargeService;
|
||||
use think\Db;
|
||||
|
||||
function check($condition, $message) { if (!$condition) { throw new RuntimeException($message); } }
|
||||
function rejected($fn, $message) { try { $fn(); } catch (RuntimeException $e) { return; } throw new RuntimeException($message); }
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$uid = Db::name('user')->insertGetId(['username' => 'recharge_test_' . time(), 'money' => '10.10']);
|
||||
$url = '/uploads/test-recharge-' . time() . '.png';
|
||||
Db::name('attachment')->insert(['admin_id' => 1, 'url' => $url, 'category' => 'recharge', 'mimetype' => 'image/png']);
|
||||
$nonce = bin2hex(random_bytes(16));
|
||||
rejected(function () use ($uid, $nonce) { AdminRechargeService::credit($uid, 1, '2.00', '', $nonce); }, 'Missing receipt accepted');
|
||||
rejected(function () use ($uid, $url, $nonce) { AdminRechargeService::credit($uid, 1, '0.001', $url, $nonce); }, 'Invalid amount accepted');
|
||||
rejected(function () use ($uid, $url, $nonce) { AdminRechargeService::credit($uid, 1, '0', $url, $nonce); }, 'Zero amount accepted');
|
||||
rejected(function () use ($uid, $url, $nonce) { AdminRechargeService::credit($uid, 1, '99999999.99', $url, $nonce); }, 'Balance overflow accepted');
|
||||
rejected(function () use ($uid, $url, $nonce) { AdminRechargeService::credit($uid, 2, '2.00', $url, $nonce); }, 'Another administrator receipt accepted');
|
||||
$result = AdminRechargeService::credit($uid, 1, '2.20', $url, $nonce);
|
||||
check($result['balance'] === '12.30', 'Balance incorrect');
|
||||
check(Db::name('user')->where('id', $uid)->value('money') === '12.30', 'User balance not saved');
|
||||
$log = Db::name('wallet_log')->where('id', $result['wallet_log_id'])->find();
|
||||
check($log && $log['type'] === 'recharge' && $log['amount'] === '2.20' && $log['balance'] === '12.30', 'Wallet log incorrect');
|
||||
$audit = Db::name('admin_recharge')->where('wallet_log_id', $result['wallet_log_id'])->find();
|
||||
check($audit && $audit['voucher'] === $url && (int)$audit['admin_id'] === 1, 'Receipt or operator not saved');
|
||||
rejected(function () use ($uid, $url, $nonce) { AdminRechargeService::credit($uid, 1, '2.20', $url, $nonce); }, 'Duplicate request accepted');
|
||||
check(Db::name('user')->where('id', $uid)->value('money') === '12.30', 'Duplicate changed balance');
|
||||
echo "Admin recharge integration checks passed\n";
|
||||
} finally {
|
||||
Db::rollback();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
define('APP_PATH', __DIR__ . '/../server/application/');
|
||||
require __DIR__ . '/../server/thinkphp/base.php';
|
||||
\think\App::initCommon();
|
||||
|
||||
use app\common\library\OrderPaymentResultService;
|
||||
use think\Db;
|
||||
|
||||
function verifyResult($condition, $message) { if (!$condition) throw new RuntimeException($message); }
|
||||
function mustReject($fn, $message) { try { $fn(); } catch (InvalidArgumentException $e) { return; } throw new RuntimeException($message); }
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$order = ['id' => 2147483000, 'order_no' => 'RESULT-TEST', 'service_name' => '测试服务', 'pay_status' => 1, 'pay_type' => 'balance', 'pay_amount' => '120.00', 'pay_time' => time()];
|
||||
$trade = 'RESULT' . bin2hex(random_bytes(10));
|
||||
Db::name('pay_order')->insert(['out_trade_no' => $trade, 'order_id' => $order['id'], 'order_no' => $order['order_no'], 'user_id' => 1, 'scene' => 'diff', 'openid' => 'BALANCE', 'amount' => '12.34', 'status' => 'paid', 'pay_time' => time()]);
|
||||
$result = OrderPaymentResultService::getResult($order, $trade);
|
||||
verifyResult($result['amount'] === '12.34' && $result['pay_type'] === 'balance' && $result['scene'] === 'diff', 'Must use the actual payment transaction');
|
||||
Db::name('pay_order')->where('out_trade_no', $trade)->update(['openid' => 'WX_TEST_OPENID', 'scene' => 'quote']);
|
||||
$wechat = OrderPaymentResultService::getResult($order, $trade);
|
||||
verifyResult($wechat['pay_type'] === 'wechat' && $wechat['scene'] === 'quote' && $wechat['amount'] === '12.34', 'WeChat payment result incorrect');
|
||||
mustReject(function () use ($order, $trade) { $other = $order; $other['id']--; OrderPaymentResultService::getResult($other, $trade); }, 'Another order transaction accepted');
|
||||
Db::name('pay_order')->where('out_trade_no', $trade)->update(['status' => 'pending']);
|
||||
mustReject(function () use ($order, $trade) { OrderPaymentResultService::getResult($order, $trade); }, 'Unpaid transaction accepted');
|
||||
Db::name('pay_order')->where('out_trade_no', $trade)->update(['status' => 'refunded']);
|
||||
mustReject(function () use ($order, $trade) { OrderPaymentResultService::getResult($order, $trade); }, 'Refunded transaction shown as successful');
|
||||
mustReject(function () use ($order) { OrderPaymentResultService::getResult($order, ''); }, 'Missing transaction accepted for nonzero payment');
|
||||
$order['pay_type'] = 'coupon'; $order['pay_amount'] = '0.00';
|
||||
$zero = OrderPaymentResultService::getResult($order, '');
|
||||
verifyResult($zero['amount'] === '0.00' && $zero['pay_type'] === 'coupon', 'Zero payment not recognized');
|
||||
$order['pay_status'] = 0;
|
||||
mustReject(function () use ($order) { OrderPaymentResultService::getResult($order, ''); }, 'Unpaid zero order accepted');
|
||||
echo "Order payment result checks passed\n";
|
||||
} finally {
|
||||
Db::rollback();
|
||||
}
|
||||
38
tests/wallet_bills_api_test.js
普通文件
38
tests/wallet_bills_api_test.js
普通文件
@@ -0,0 +1,38 @@
|
||||
const assert = require('assert');
|
||||
const { execFileSync } = require('child_process');
|
||||
const http = require('http');
|
||||
const mysql = process.env.MYSQL_BIN;
|
||||
const base = process.env.API_BASE || 'http://yjyf:9560/api';
|
||||
function sql(query) {
|
||||
return execFileSync(mysql, ['-h127.0.0.1', '-u' + process.env.DB_USER, '-D' + process.env.DB_NAME, '-N', '-B', '-e', query], { encoding: 'utf8' }).trim();
|
||||
}
|
||||
function request(path, data, token) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const body = data && new URLSearchParams(data).toString();
|
||||
const url = new URL(base + path);
|
||||
const req = http.request(url, { method: body ? 'POST' : 'GET', headers: { ...(body ? {'content-type': 'application/x-www-form-urlencoded', 'content-length': Buffer.byteLength(body)} : {}), ...(token ? {token} : {}) } }, res => {
|
||||
let text = ''; res.on('data', chunk => text += chunk); res.on('end', () => { try { resolve(JSON.parse(text)); } catch (e) { reject(e); } });
|
||||
});
|
||||
req.on('error', reject); if (body) req.write(body); req.end();
|
||||
});
|
||||
}
|
||||
(async () => {
|
||||
const login = await request('/auth/login', {mobile: process.env.TEST_MOBILE, password: process.env.TEST_PASSWORD});
|
||||
assert.strictEqual(login.code, 1, 'login failed');
|
||||
const {token, userinfo} = login.data;
|
||||
const uid = Number(userinfo.id);
|
||||
assert(Number.isInteger(uid) && uid > 0);
|
||||
for (const type of ['all', 'recharge', 'consume', 'refund']) {
|
||||
const filter = type === 'all' ? '' : ` AND type='${type}'`;
|
||||
const expected = sql(`SELECT id FROM fa_wallet_log WHERE user_type='user' AND user_id=${uid}${filter} ORDER BY id DESC`);
|
||||
const ids = expected ? expected.split(/\s+/).map(Number) : [];
|
||||
for (const page of [1, 2]) {
|
||||
const res = await request(`/user/walletLogs?type=${type}&page=${page}&limit=3`, null, token);
|
||||
assert.strictEqual(res.code, 1, `${type} page ${page} request failed`);
|
||||
assert.strictEqual(res.data.total, ids.length, `${type} total`);
|
||||
assert.deepStrictEqual(res.data.list.map(row => row.id), ids.slice((page - 1) * 3, page * 3), `${type} page ${page} must contain only matching user records`);
|
||||
assert.strictEqual(res.data.has_more, page * 3 < ids.length, `${type} has_more`);
|
||||
}
|
||||
}
|
||||
console.log('Wallet bill user, type, count and pagination checks passed');
|
||||
})().catch(err => { console.error(err); process.exitCode = 1; });
|
||||
在新工单中引用
屏蔽一个用户