|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
|
use App\Models\PaytaroOrder;
|
|
|
|
|
|
use App\Services\AdminPermission;
|
|
|
|
|
|
use App\Services\AdminDataMasker;
|
|
|
|
|
|
use App\Services\Payment\PaymentAmount;
|
|
|
|
|
|
use App\Services\Payment\PaytaroClient;
|
|
|
|
|
|
use App\Services\Payment\PaytaroConfig;
|
|
|
|
|
|
|
|
|
|
|
|
class PaytaroController extends AdminController
|
|
|
|
|
|
{
|
|
|
|
|
|
public function index($request, $response, $args)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->setViewTheme('venix');
|
|
|
|
|
|
$config = PaytaroConfig::values();
|
|
|
|
|
|
$browserConfig = $config;
|
|
|
|
|
|
foreach (['paytaro_gateway_url', 'paytaro_callback_base_url', 'paytaro_app_id', 'paytaro_method_uuid'] as $protectedKey) {
|
|
|
|
|
|
$browserConfig[$protectedKey . '_configured'] = trim((string) ($config[$protectedKey] ?? '')) !== '';
|
|
|
|
|
|
$browserConfig[$protectedKey] = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
$orders = null;
|
|
|
|
|
|
$summary = ['total' => 0, 'pending' => 0, 'paid' => 0, 'paid_amount' => '0.00'];
|
|
|
|
|
|
$tableError = '';
|
|
|
|
|
|
$keyword = trim((string) $request->getParam('keyword', ''));
|
|
|
|
|
|
$status = trim((string) $request->getParam('status', ''));
|
|
|
|
|
|
$page = max(1, min(100000, (int) $request->getParam('page', 1)));
|
|
|
|
|
|
try {
|
|
|
|
|
|
$query = PaytaroOrder::query()
|
|
|
|
|
|
->leftJoin('user', 'user.id', '=', 'paytaro_orders.user_id')
|
|
|
|
|
|
->select(
|
|
|
|
|
|
'paytaro_orders.id',
|
|
|
|
|
|
'paytaro_orders.user_id',
|
|
|
|
|
|
'paytaro_orders.amount',
|
|
|
|
|
|
'paytaro_orders.status',
|
|
|
|
|
|
'paytaro_orders.created_at',
|
|
|
|
|
|
'user.email as user_email'
|
|
|
|
|
|
);
|
|
|
|
|
|
if ($keyword !== '') {
|
|
|
|
|
|
if (AdminPermission::isSuperAdmin()) {
|
|
|
|
|
|
$query->where(function ($builder) use ($keyword) {
|
|
|
|
|
|
$builder->where('user.email', 'like', '%' . $keyword . '%');
|
|
|
|
|
|
if (ctype_digit($keyword)) {
|
|
|
|
|
|
$builder->orWhere('paytaro_orders.id', (int) $keyword)
|
|
|
|
|
|
->orWhere('paytaro_orders.user_id', (int) $keyword);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} elseif (ctype_digit($keyword)) {
|
|
|
|
|
|
$query->where(function ($builder) use ($keyword) {
|
|
|
|
|
|
$builder->where('paytaro_orders.id', (int) $keyword)
|
|
|
|
|
|
->orWhere('paytaro_orders.user_id', (int) $keyword);
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$query->whereRaw('1 = 0');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (in_array($status, ['creating', 'pending', 'paid', 'failed'], true)) {
|
|
|
|
|
|
$query->where('paytaro_orders.status', $status);
|
|
|
|
|
|
}
|
|
|
|
|
|
$orders = $query->orderBy('paytaro_orders.id', 'desc')->paginate(30, ['*'], 'page', $page);
|
|
|
|
|
|
$orders->setPath('/admin/paytaro');
|
|
|
|
|
|
$orders->appends(['keyword' => $keyword, 'status' => $status]);
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
|
$order->setAttribute('user_email', AdminDataMasker::email($order->user_email));
|
|
|
|
|
|
}
|
|
|
|
|
|
$summary['total'] = PaytaroOrder::count();
|
|
|
|
|
|
$summary['pending'] = PaytaroOrder::whereIn('status', ['creating', 'pending'])->count();
|
|
|
|
|
|
$summary['paid'] = PaytaroOrder::where('status', 'paid')->count();
|
|
|
|
|
|
$summary['paid_amount'] = PaymentAmount::normalize(PaytaroOrder::where('status', 'paid')->sum('amount'));
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
|
$tableError = 'Paytaro 数据表不可用,请先导入 sql/paytaro_payment.sql。';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->view()
|
|
|
|
|
|
->assign('paytaro_config', $browserConfig)
|
|
|
|
|
|
->assign('paytaro_orders', $orders)
|
|
|
|
|
|
->assign('paytaro_summary', $summary)
|
|
|
|
|
|
->assign('paytaro_table_error', $tableError)
|
|
|
|
|
|
->assign('paytaro_filters', ['keyword' => $keyword, 'status' => $status])
|
|
|
|
|
|
->display('admin/paytaro/index.tpl');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function save($request, $response, $args)
|
|
|
|
|
|
{
|
|
|
|
|
|
$payload = $request->getParsedBody();
|
|
|
|
|
|
$payload = is_array($payload) ? $payload : [];
|
|
|
|
|
|
try {
|
|
|
|
|
|
$current = PaytaroConfig::values();
|
|
|
|
|
|
$values = $this->validateConfig($payload, $current);
|
|
|
|
|
|
$secret = trim((string) ($payload['paytaro_app_key'] ?? ''));
|
|
|
|
|
|
if (strlen($secret) > 256) {
|
|
|
|
|
|
throw new \InvalidArgumentException('Paytaro 商户密钥长度不能超过 256 个字符');
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($values['paytaro_enabled'] === 'true') {
|
|
|
|
|
|
if ($values['paytaro_callback_base_url'] === '' || $values['paytaro_app_id'] === '' || $values['paytaro_method_uuid'] === '') {
|
|
|
|
|
|
throw new \InvalidArgumentException('启用 Paytaro 前必须填写回调地址、App ID 和支付宝付款方式 UUID');
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($secret === '' && !$current['paytaro_secret_configured']) {
|
|
|
|
|
|
throw new \InvalidArgumentException('启用 Paytaro 前必须填写商户密钥');
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($secret === '' && $current['paytaro_secret_configured']) {
|
|
|
|
|
|
PaytaroConfig::callbackCredentials();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
PaytaroConfig::save($values, $secret);
|
|
|
|
|
|
return $this->json($response, 1, 'Paytaro 配置保存成功');
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
|
error_log('Paytaro config save failed: ' . $exception->getMessage());
|
|
|
|
|
|
return $this->json($response, 0, $exception->getMessage(), 422);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function test($request, $response, $args)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$settings = PaytaroConfig::runtime(false);
|
|
|
|
|
|
$status = (new PaytaroClient($settings))->testConnection();
|
|
|
|
|
|
return $this->json($response, 1, '连接成功,Paytaro 网关可达(HTTP ' . $status . ')');
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
|
error_log('Paytaro connection test failed: ' . $exception->getMessage());
|
|
|
|
|
|
return $this->json($response, 0, '连接失败,请查看服务器日志', 502);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function validateConfig(array $payload, array $current)
|
|
|
|
|
|
{
|
|
|
|
|
|
$callbackUrl = rtrim(trim((string) ($payload['paytaro_callback_base_url'] ?? '')), '/');
|
|
|
|
|
|
$callbackUrl = $callbackUrl === '' ? trim((string) ($current['paytaro_callback_base_url'] ?? '')) : $callbackUrl;
|
|
|
|
|
|
if ($callbackUrl !== '' && !PaytaroConfig::isHttpsUrl($callbackUrl)) {
|
|
|
|
|
|
throw new \InvalidArgumentException('Paytaro 回调地址必须是无查询参数的 HTTPS 地址');
|
|
|
|
|
|
}
|
|
|
|
|
|
$appId = trim((string) ($payload['paytaro_app_id'] ?? ''));
|
|
|
|
|
|
$appId = $appId === '' ? trim((string) ($current['paytaro_app_id'] ?? '')) : $appId;
|
|
|
|
|
|
if ($appId !== '' && !preg_match('/^[A-Z0-9]{11}$/', $appId)) {
|
|
|
|
|
|
throw new \InvalidArgumentException('Paytaro App ID 必须是 11 位大写字母或数字');
|
|
|
|
|
|
}
|
|
|
|
|
|
$methodUuid = trim((string) ($payload['paytaro_method_uuid'] ?? ''));
|
|
|
|
|
|
$methodUuid = $methodUuid === '' ? trim((string) ($current['paytaro_method_uuid'] ?? '')) : $methodUuid;
|
|
|
|
|
|
if ($methodUuid !== '' && !preg_match('/^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$/i', $methodUuid)) {
|
|
|
|
|
|
throw new \InvalidArgumentException('Paytaro 付款方式 UUID 格式无效');
|
|
|
|
|
|
}
|
|
|
|
|
|
$minimum = PaymentAmount::normalize($payload['paytaro_min_amount'] ?? '1.00');
|
|
|
|
|
|
$maximum = PaymentAmount::normalize($payload['paytaro_max_amount'] ?? '10000.00');
|
|
|
|
|
|
if ((float) $minimum < 0.01 || (float) $maximum > 1000000 || (float) $minimum > (float) $maximum) {
|
|
|
|
|
|
throw new \InvalidArgumentException('Paytaro 金额范围无效');
|
|
|
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
|
|
|
'paytaro_enabled' => PaytaroConfig::truthy($payload['paytaro_enabled'] ?? 'false') ? 'true' : 'false',
|
|
|
|
|
|
'paytaro_test_mode' => PaytaroConfig::truthy($payload['paytaro_test_mode'] ?? 'false') ? 'true' : 'false',
|
|
|
|
|
|
'paytaro_gateway_url' => PaytaroConfig::MAPI_GATEWAY_URL,
|
|
|
|
|
|
'paytaro_callback_base_url' => $callbackUrl,
|
|
|
|
|
|
'paytaro_app_id' => $appId,
|
|
|
|
|
|
'paytaro_method_uuid' => strtolower($methodUuid),
|
|
|
|
|
|
'paytaro_min_amount' => $minimum,
|
|
|
|
|
|
'paytaro_max_amount' => $maximum,
|
|
|
|
|
|
'paytaro_request_timeout_seconds' => (string) max(3, min(60, (int) ($payload['paytaro_request_timeout_seconds'] ?? 15))),
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function json($response, $ret, $msg, $status = 200)
|
|
|
|
|
|
{
|
|
|
|
|
|
$response = $response->withStatus((int) $status)->withHeader('Content-Type', 'application/json; charset=utf-8');
|
|
|
|
|
|
$response->getBody()->write(json_encode(['ret' => (int) $ret, 'msg' => (string) $msg], JSON_UNESCAPED_UNICODE));
|
|
|
|
|
|
return $response;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|