mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-05 09:34:26 +08:00
丰富基础代码
This commit is contained in:
177
app/http/admin/controller/MerchantAppController.php
Normal file
177
app/http/admin/controller/MerchantAppController.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\repositories\MerchantAppRepository;
|
||||
use app\repositories\MerchantRepository;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
* 商户应用管理
|
||||
*/
|
||||
class MerchantAppController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected MerchantAppRepository $merchantAppRepository,
|
||||
protected MerchantRepository $merchantRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/merchant-app/list
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
$page = (int)$request->get('page', 1);
|
||||
$pageSize = (int)$request->get('page_size', 10);
|
||||
|
||||
$filters = [
|
||||
'merchant_id' => (int)$request->get('merchant_id', 0),
|
||||
'status' => $request->get('status', ''),
|
||||
'app_id' => trim((string)$request->get('app_id', '')),
|
||||
'app_name' => trim((string)$request->get('app_name', '')),
|
||||
'api_type' => trim((string)$request->get('api_type', '')),
|
||||
];
|
||||
|
||||
$paginator = $this->merchantAppRepository->searchPaginate($filters, $page, $pageSize);
|
||||
return $this->page($paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/merchant-app/detail?id=1
|
||||
*/
|
||||
public function detail(Request $request)
|
||||
{
|
||||
$id = (int)$request->get('id', 0);
|
||||
if ($id <= 0) {
|
||||
return $this->fail('应用ID不能为空', 400);
|
||||
}
|
||||
|
||||
$row = $this->merchantAppRepository->find($id);
|
||||
if (!$row) {
|
||||
return $this->fail('应用不存在', 404);
|
||||
}
|
||||
|
||||
return $this->success($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/merchant-app/save
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
$data = $request->post();
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
|
||||
$merchantId = (int)($data['merchant_id'] ?? 0);
|
||||
$apiType = trim((string)($data['api_type'] ?? 'epay'));
|
||||
$appId = trim((string)($data['app_id'] ?? ''));
|
||||
$appName = trim((string)($data['app_name'] ?? ''));
|
||||
$status = (int)($data['status'] ?? 1);
|
||||
|
||||
if ($merchantId <= 0 || $appId === '' || $appName === '') {
|
||||
return $this->fail('商户、应用ID、应用名称不能为空', 400);
|
||||
}
|
||||
|
||||
$merchant = $this->merchantRepository->find($merchantId);
|
||||
if (!$merchant) {
|
||||
return $this->fail('商户不存在', 404);
|
||||
}
|
||||
|
||||
if (!in_array($apiType, ['openapi', 'epay', 'custom', 'default'], true)) {
|
||||
return $this->fail('api_type 不合法', 400);
|
||||
}
|
||||
|
||||
if ($id > 0) {
|
||||
$row = $this->merchantAppRepository->find($id);
|
||||
if (!$row) {
|
||||
return $this->fail('应用不存在', 404);
|
||||
}
|
||||
|
||||
// app_id 变更需校验唯一
|
||||
if ($row->app_id !== $appId) {
|
||||
$exists = $this->merchantAppRepository->findAnyByAppId($appId);
|
||||
if ($exists) {
|
||||
return $this->fail('应用ID已存在', 400);
|
||||
}
|
||||
}
|
||||
|
||||
$update = [
|
||||
'merchant_id' => $merchantId,
|
||||
'api_type' => $apiType,
|
||||
'app_id' => $appId,
|
||||
'app_name' => $appName,
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
// 可选:前端传入 app_secret 才更新
|
||||
if (!empty($data['app_secret'])) {
|
||||
$update['app_secret'] = (string)$data['app_secret'];
|
||||
}
|
||||
|
||||
$this->merchantAppRepository->updateById($id, $update);
|
||||
} else {
|
||||
$exists = $this->merchantAppRepository->findAnyByAppId($appId);
|
||||
if ($exists) {
|
||||
return $this->fail('应用ID已存在', 400);
|
||||
}
|
||||
|
||||
$secret = !empty($data['app_secret']) ? (string)$data['app_secret'] : $this->generateSecret();
|
||||
$this->merchantAppRepository->create([
|
||||
'merchant_id' => $merchantId,
|
||||
'api_type' => $apiType,
|
||||
'app_id' => $appId,
|
||||
'app_secret' => $secret,
|
||||
'app_name' => $appName,
|
||||
'status' => $status,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success(null, '保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/merchant-app/reset-secret
|
||||
*/
|
||||
public function resetSecret(Request $request)
|
||||
{
|
||||
$id = (int)$request->post('id', 0);
|
||||
if ($id <= 0) {
|
||||
return $this->fail('应用ID不能为空', 400);
|
||||
}
|
||||
|
||||
$row = $this->merchantAppRepository->find($id);
|
||||
if (!$row) {
|
||||
return $this->fail('应用不存在', 404);
|
||||
}
|
||||
|
||||
$secret = $this->generateSecret();
|
||||
$this->merchantAppRepository->updateById($id, ['app_secret' => $secret]);
|
||||
|
||||
return $this->success(['app_secret' => $secret], '重置成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/merchant-app/toggle
|
||||
*/
|
||||
public function toggle(Request $request)
|
||||
{
|
||||
$id = (int)$request->post('id', 0);
|
||||
$status = $request->post('status', null);
|
||||
|
||||
if ($id <= 0 || $status === null) {
|
||||
return $this->fail('参数错误', 400);
|
||||
}
|
||||
|
||||
$ok = $this->merchantAppRepository->updateById($id, ['status' => (int)$status]);
|
||||
return $ok ? $this->success(null, '操作成功') : $this->fail('操作失败', 500);
|
||||
}
|
||||
|
||||
private function generateSecret(): string
|
||||
{
|
||||
$raw = random_bytes(24);
|
||||
return rtrim(strtr(base64_encode($raw), '+/', '-_'), '=');
|
||||
}
|
||||
}
|
||||
|
||||
116
app/http/admin/controller/MerchantController.php
Normal file
116
app/http/admin/controller/MerchantController.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\repositories\MerchantRepository;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
* 商户管理
|
||||
*/
|
||||
class MerchantController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected MerchantRepository $merchantRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/merchant/list
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
$page = (int)$request->get('page', 1);
|
||||
$pageSize = (int)$request->get('page_size', 10);
|
||||
|
||||
$filters = [
|
||||
'status' => $request->get('status', ''),
|
||||
'merchant_no' => trim((string)$request->get('merchant_no', '')),
|
||||
'merchant_name' => trim((string)$request->get('merchant_name', '')),
|
||||
];
|
||||
|
||||
$paginator = $this->merchantRepository->searchPaginate($filters, $page, $pageSize);
|
||||
return $this->page($paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/merchant/detail?id=1
|
||||
*/
|
||||
public function detail(Request $request)
|
||||
{
|
||||
$id = (int)$request->get('id', 0);
|
||||
if ($id <= 0) {
|
||||
return $this->fail('商户ID不能为空', 400);
|
||||
}
|
||||
|
||||
$row = $this->merchantRepository->find($id);
|
||||
if (!$row) {
|
||||
return $this->fail('商户不存在', 404);
|
||||
}
|
||||
|
||||
return $this->success($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/merchant/save
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
$data = $request->post();
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
|
||||
$merchantNo = trim((string)($data['merchant_no'] ?? ''));
|
||||
$merchantName = trim((string)($data['merchant_name'] ?? ''));
|
||||
$fundsMode = trim((string)($data['funds_mode'] ?? 'direct'));
|
||||
$status = (int)($data['status'] ?? 1);
|
||||
|
||||
if ($merchantNo === '' || $merchantName === '') {
|
||||
return $this->fail('商户号与商户名称不能为空', 400);
|
||||
}
|
||||
|
||||
if (!in_array($fundsMode, ['direct', 'wallet', 'hybrid'], true)) {
|
||||
return $this->fail('资金模式不合法', 400);
|
||||
}
|
||||
|
||||
if ($id > 0) {
|
||||
$this->merchantRepository->updateById($id, [
|
||||
'merchant_no' => $merchantNo,
|
||||
'merchant_name' => $merchantName,
|
||||
'funds_mode' => $fundsMode,
|
||||
'status' => $status,
|
||||
]);
|
||||
} else {
|
||||
$exists = $this->merchantRepository->findByMerchantNo($merchantNo);
|
||||
if ($exists) {
|
||||
return $this->fail('商户号已存在', 400);
|
||||
}
|
||||
|
||||
$this->merchantRepository->create([
|
||||
'merchant_no' => $merchantNo,
|
||||
'merchant_name' => $merchantName,
|
||||
'funds_mode' => $fundsMode,
|
||||
'status' => $status,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success(null, '保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/merchant/toggle
|
||||
*/
|
||||
public function toggle(Request $request)
|
||||
{
|
||||
$id = (int)$request->post('id', 0);
|
||||
$status = $request->post('status', null);
|
||||
|
||||
if ($id <= 0 || $status === null) {
|
||||
return $this->fail('参数错误', 400);
|
||||
}
|
||||
|
||||
$ok = $this->merchantRepository->updateById($id, ['status' => (int)$status]);
|
||||
return $ok ? $this->success(null, '操作成功') : $this->fail('操作失败', 500);
|
||||
}
|
||||
}
|
||||
|
||||
100
app/http/admin/controller/OrderController.php
Normal file
100
app/http/admin/controller/OrderController.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\repositories\PaymentMethodRepository;
|
||||
use app\repositories\PaymentOrderRepository;
|
||||
use app\services\PayOrderService;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
* 订单管理
|
||||
*/
|
||||
class OrderController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected PaymentOrderRepository $orderRepository,
|
||||
protected PaymentMethodRepository $methodRepository,
|
||||
protected PayOrderService $payOrderService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/order/list
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
$page = (int)$request->get('page', 1);
|
||||
$pageSize = (int)$request->get('page_size', 10);
|
||||
|
||||
$methodCode = trim((string)$request->get('method_code', ''));
|
||||
$methodId = 0;
|
||||
if ($methodCode !== '') {
|
||||
$method = $this->methodRepository->findAnyByCode($methodCode);
|
||||
$methodId = $method ? (int)$method->id : 0;
|
||||
}
|
||||
|
||||
$filters = [
|
||||
'merchant_id' => (int)$request->get('merchant_id', 0),
|
||||
'merchant_app_id' => (int)$request->get('merchant_app_id', 0),
|
||||
'method_id' => $methodId,
|
||||
'channel_id' => (int)$request->get('channel_id', 0),
|
||||
'status' => $request->get('status', ''),
|
||||
'order_id' => trim((string)$request->get('order_id', '')),
|
||||
'mch_order_no' => trim((string)$request->get('mch_order_no', '')),
|
||||
'created_from' => trim((string)$request->get('created_from', '')),
|
||||
'created_to' => trim((string)$request->get('created_to', '')),
|
||||
];
|
||||
|
||||
$paginator = $this->orderRepository->searchPaginate($filters, $page, $pageSize);
|
||||
return $this->page($paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/order/detail?id=1 或 order_id=P...
|
||||
*/
|
||||
public function detail(Request $request)
|
||||
{
|
||||
$id = (int)$request->get('id', 0);
|
||||
$orderId = trim((string)$request->get('order_id', ''));
|
||||
|
||||
if ($id > 0) {
|
||||
$row = $this->orderRepository->find($id);
|
||||
} elseif ($orderId !== '') {
|
||||
$row = $this->orderRepository->findByOrderId($orderId);
|
||||
} else {
|
||||
return $this->fail('参数错误', 400);
|
||||
}
|
||||
|
||||
if (!$row) {
|
||||
return $this->fail('订单不存在', 404);
|
||||
}
|
||||
|
||||
return $this->success($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/order/refund
|
||||
* - order_id: 系统订单号
|
||||
* - refund_amount: 退款金额
|
||||
*/
|
||||
public function refund(Request $request)
|
||||
{
|
||||
$orderId = trim((string)$request->post('order_id', ''));
|
||||
$refundAmount = (float)$request->post('refund_amount', 0);
|
||||
$refundReason = trim((string)$request->post('refund_reason', ''));
|
||||
|
||||
try {
|
||||
$result = $this->payOrderService->refundOrder([
|
||||
'order_id' => $orderId,
|
||||
'refund_amount' => $refundAmount,
|
||||
'refund_reason' => $refundReason,
|
||||
]);
|
||||
return $this->success($result, '退款发起成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail($e->getMessage(), 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
96
app/http/admin/controller/PayMethodController.php
Normal file
96
app/http/admin/controller/PayMethodController.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\repositories\PaymentMethodRepository;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
* 支付方式管理
|
||||
*/
|
||||
class PayMethodController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected PaymentMethodRepository $methodRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/pay-method/list
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
$page = (int)$request->get('page', 1);
|
||||
$pageSize = (int)$request->get('page_size', 10);
|
||||
|
||||
$filters = [
|
||||
'status' => $request->get('status', ''),
|
||||
'method_code' => trim((string)$request->get('method_code', '')),
|
||||
'method_name' => trim((string)$request->get('method_name', '')),
|
||||
];
|
||||
|
||||
$paginator = $this->methodRepository->searchPaginate($filters, $page, $pageSize);
|
||||
return $this->page($paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/pay-method/save
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
$data = $request->post();
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
|
||||
$code = trim((string)($data['method_code'] ?? ''));
|
||||
$name = trim((string)($data['method_name'] ?? ''));
|
||||
$icon = trim((string)($data['icon'] ?? ''));
|
||||
$sort = (int)($data['sort'] ?? 0);
|
||||
$status = (int)($data['status'] ?? 1);
|
||||
|
||||
if ($code === '' || $name === '') {
|
||||
return $this->fail('支付方式编码与名称不能为空', 400);
|
||||
}
|
||||
|
||||
if ($id > 0) {
|
||||
$this->methodRepository->updateById($id, [
|
||||
'method_code' => $code,
|
||||
'method_name' => $name,
|
||||
'icon' => $icon,
|
||||
'sort' => $sort,
|
||||
'status' => $status,
|
||||
]);
|
||||
} else {
|
||||
$exists = $this->methodRepository->findAnyByCode($code);
|
||||
if ($exists) {
|
||||
return $this->fail('支付方式编码已存在', 400);
|
||||
}
|
||||
$this->methodRepository->create([
|
||||
'method_code' => $code,
|
||||
'method_name' => $name,
|
||||
'icon' => $icon,
|
||||
'sort' => $sort,
|
||||
'status' => $status,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success(null, '保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/pay-method/toggle
|
||||
*/
|
||||
public function toggle(Request $request)
|
||||
{
|
||||
$id = (int)$request->post('id', 0);
|
||||
$status = $request->post('status', null);
|
||||
|
||||
if ($id <= 0 || $status === null) {
|
||||
return $this->fail('参数错误', 400);
|
||||
}
|
||||
|
||||
$ok = $this->methodRepository->updateById($id, ['status' => (int)$status]);
|
||||
return $ok ? $this->success(null, '操作成功') : $this->fail('操作失败', 500);
|
||||
}
|
||||
}
|
||||
|
||||
85
app/http/admin/controller/PayPluginController.php
Normal file
85
app/http/admin/controller/PayPluginController.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\repositories\PaymentPluginRepository;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
* 插件注册表管理(ma_pay_plugin)
|
||||
*
|
||||
* 注意:与 /channel/plugin/* 的“插件能力读取(schema/products)”不同,这里负责维护插件注册表本身。
|
||||
*/
|
||||
class PayPluginController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected PaymentPluginRepository $pluginRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /adminapi/pay-plugin/list
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
$page = (int)$request->get('page', 1);
|
||||
$pageSize = (int)$request->get('page_size', 10);
|
||||
|
||||
$filters = [
|
||||
'status' => $request->get('status', ''),
|
||||
'plugin_code' => trim((string)$request->get('plugin_code', '')),
|
||||
'plugin_name' => trim((string)$request->get('plugin_name', '')),
|
||||
];
|
||||
|
||||
$paginator = $this->pluginRepository->searchPaginate($filters, $page, $pageSize);
|
||||
return $this->page($paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/pay-plugin/save
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
$data = $request->post();
|
||||
|
||||
$pluginCode = trim((string)($data['plugin_code'] ?? ''));
|
||||
$pluginName = trim((string)($data['plugin_name'] ?? ''));
|
||||
$className = trim((string)($data['class_name'] ?? ''));
|
||||
$status = (int)($data['status'] ?? 1);
|
||||
|
||||
if ($pluginCode === '' || $pluginName === '') {
|
||||
return $this->fail('插件编码与名称不能为空', 400);
|
||||
}
|
||||
|
||||
if ($className === '') {
|
||||
// 默认约定类名
|
||||
$className = 'app\\common\\payment\\' . ucfirst($pluginCode) . 'Payment';
|
||||
}
|
||||
|
||||
$this->pluginRepository->upsertByCode($pluginCode, [
|
||||
'plugin_name' => $pluginName,
|
||||
'class_name' => $className,
|
||||
'status' => $status,
|
||||
]);
|
||||
|
||||
return $this->success(null, '保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /adminapi/pay-plugin/toggle
|
||||
*/
|
||||
public function toggle(Request $request)
|
||||
{
|
||||
$pluginCode = trim((string)$request->post('plugin_code', ''));
|
||||
$status = $request->post('status', null);
|
||||
|
||||
if ($pluginCode === '' || $status === null) {
|
||||
return $this->fail('参数错误', 400);
|
||||
}
|
||||
|
||||
$ok = $this->pluginRepository->updateStatus($pluginCode, (int)$status);
|
||||
return $ok ? $this->success(null, '操作成功') : $this->fail('操作失败', 500);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user