4 Commits

Author SHA1 Message Date
技术老胡
898daee64c 文件调整 2024-12-14 09:22:00 +08:00
技术老胡
b266e1ad7c 更新文件结构 2024-12-12 17:14:01 +08:00
技术老胡
e85e71f8d9 更新后台管理接口 2024-12-12 16:45:57 +08:00
技术老胡
6a8ec71b8d 收款通知逻辑更新,完全改为本地接口,避免网络提交带来的延迟 2024-12-12 16:34:39 +08:00
10 changed files with 93 additions and 242 deletions

12
!!.env
View File

@@ -1,12 +0,0 @@
APP_DEBUG = true
DB_TYPE = mysql
DB_HOST = 127.0.0.1
DB_NAME = test
DB_USER = test
DB_PASS = 123456
DB_PORT = 3306
DB_CHARSET = utf8
DB_PREFIX = mpay_
DEFAULT_LANG = zh-cn

View File

@@ -13,14 +13,14 @@ class ConsoleController extends BaseController
// 后台主页 // 后台主页
public function index() public function index()
{ {
View::assign('version', 'v1.0.1'); View::assign('version', 'v1');
return View::fetch(); return View::fetch();
} }
// 管理菜单 // 管理菜单
public function menu() public function menu()
{ {
// 加载菜单配置 // 加载菜单配置
$menu = \think\facade\Config::load("extendconfig/menu", 'extendconfig'); $menu = \think\facade\Config::load("extend/menu", 'extend');
return json($menu); return json($menu);
} }
// 管理菜单 // 管理菜单

View File

@@ -6,6 +6,7 @@ use think\Request;
use think\facade\View; use think\facade\View;
use app\model\User; use app\model\User;
use app\model\Order; use app\model\Order;
use app\model\PayAccount;
use app\model\PayChannel; use app\model\PayChannel;
class PayController class PayController
@@ -157,31 +158,20 @@ class PayController
} }
} }
// 处理收款通知 // 处理收款通知
public function payHeart(Request $request) private function payHeart(array $records, array $config)
{ {
$pid = $request->get('pid'); $pid = $config['pid'];
$aid = $request->get('aid'); $aid = $config['aid'];
$sign = $request->get('sign');
// 检测请求参数
if (!($pid && $aid && $sign)) {
return json(['code' => 0, 'msg' => '参数错误']);
}
// 检测收款通知 // 检测收款通知
$payList = $request->post(); if (!$records) {
if (!$payList) {
return json(['code' => 0, 'msg' => '空收款通知']); return json(['code' => 0, 'msg' => '空收款通知']);
} }
// 签名验证
$is_user = User::checkUser($pid, $sign);
if (!$is_user) {
return json(['code' => 0, 'msg' => '签名错误']);
}
// 当前用户账号 // 当前用户账号
$query = ['pid' => $pid, 'aid' => $aid]; $query = ['pid' => $pid, 'aid' => $aid];
// 排除已支付订单 // 排除已支付订单
$doneOrders = Order::scope('dealOrder')->where($query)->column('platform_order'); $doneOrders = Order::scope('dealOrder')->where($query)->column('platform_order');
$new_orders = []; $new_orders = [];
foreach ($payList as $order) { foreach ($records as $order) {
if (!in_array($order['order_no'], $doneOrders)) $new_orders[] = $order; if (!in_array($order['order_no'], $doneOrders)) $new_orders[] = $order;
} }
if (!count($new_orders)) return json(['code' => 0, 'msg' => '收款通知无新消息']); if (!count($new_orders)) return json(['code' => 0, 'msg' => '收款通知无新消息']);
@@ -233,66 +223,47 @@ class PayController
return ['order' => $order->order_id, 'code' => 0, 'msg' => 'notify fail']; return ['order' => $order->order_id, 'code' => 0, 'msg' => 'notify fail'];
} }
} }
// [定时任务]获取收款明细,提交收款通知[本地版] // [定时任务]获取收款明细,提交收款通知
public function checkPayResult(Request $request) public function checkPayResult(Request $request)
{ {
$req_info = $request->get(); $req_info = $request->get();
$req_pid = $req_info['pid']; $req_pid = $req_info['pid'];
$req_aid = $req_info['aid']; $req_aid = $req_info['aid'];
// 加载配置文件
$config = \think\facade\Config::load("payconfig/{$req_pid}_{$req_aid}", 'payconfig');
// 用户账号配置
$user_config = isset($config['user']) ? $config['user'] : [];
// 收款平台账号配置
$pay_config = isset($config['pay']) ? $config['pay'] : [];
// 配置检查
if ($user_config && $pay_config) {
// 账号配置信息
$pid = $user_config['pid'];
$aid = $pay_config['aid'];
if (!($req_pid == $pid && $req_aid == $aid)) {
return '监听收款配置不一致';
}
} else {
return '监听收款配置文件名错误';
}
// 当前站点
$user_config['host'] = \request()->domain();
// 实例化支付类
$Mpay = new \MpayClass($user_config);
// 获取订单 // 获取订单
$res_new_order = $Mpay->orderHeart(); $order_path = runtime_path() . '/order.json';
$new_order = json_decode($res_new_order, true); if (!file_exists($order_path)) return json(['code' => 3, 'msg' => '订单文件不存在']);
$new_order = json_decode(file_get_contents($order_path), true);
// 检测新订单 // 检测新订单
if ($new_order['code'] !== 1) return $res_new_order; if ($new_order['code'] !== 1) return json($new_order);
// 订单列表 // 订单列表
$order_list = $new_order['orders']; $order_list = $new_order['orders'];
// 检测本账号订单 // 检测本账号订单
$orders = []; $orders = [];
foreach ($order_list as $key => $val) { foreach ($order_list as $key => $val) {
if ($pid == $val['pid'] && $aid == $val['aid'] && $val['patt'] == 1) { if ($req_pid == $val['pid'] && $req_aid == $val['aid'] && $val['patt'] == 1) {
$orders[] = $order_list[$key]; $orders[] = $order_list[$key];
} }
} }
if (!$orders) { if (!$orders) return json(['code' => 0, 'msg' => '非本账号订单或监听模式不对']);
return \json(['code' => 0, 'msg' => '非本账号订单或监听模式不对']); // 加载配置文件
} $config = PayAccount::getAccountConfig($req_aid);
if ($config === false) return json(['code' => 4, 'msg' => '监听收款配置错误']);
// 登陆账号 // 登陆账号
$config = ['username' => $pay_config['account'], 'password' => $pay_config['password']]; $pay_config = ['username' => $config['account'], 'password' => $config['password']];
// 收款查询 // 收款查询
$query = $pay_config['query']; $query = $config['query'];
// 实例监听客户端 // 实例监听客户端
$payclient_name = $pay_config['payclass']; $payclient_name = $config['payclass'];
$payclient_path = "\\payclient\\{$payclient_name}"; $payclient_path = "\\payclient\\{$payclient_name}";
$Payclient = new $payclient_path($config); $Payclient = new $payclient_path($pay_config);
// 获取支付明细 // 获取支付明细
$records = $Payclient->getOrderInfo($query); $records = $Payclient->getOrderInfo($query);
if ($records) { if ($records) {
// 提交收款记录 // 提交收款记录
$upres = $Mpay->upRecords($records, $aid); $upres = $this->payHeart($records, $config);
return $upres; return $upres;
} else { } else {
return \json(['code' => 0, 'msg' => '查询空订单'], 320); return json(['code' => 0, 'msg' => '查询空订单'], 320);
} }
} }
// [定时任务]监听新订单,生成JSON文件信息 // [定时任务]监听新订单,生成JSON文件信息
@@ -337,10 +308,11 @@ class PayController
$action = isset($info['action']) ? $info['action'] : ''; $action = isset($info['action']) ? $info['action'] : '';
if ($action === 'mpay') { if ($action === 'mpay') {
$data = json_decode($info['data'], true); $data = json_decode($info['data'], true);
$config = \think\facade\Config::load("payconfig/{$data['pid']}_{$data['aid']}", 'payconfig'); $config = PayAccount::getAccountConfig($data['aid'], $data['pid']);
$payclient_path = "\\payclient\\{$config['pay']['payclass']}"; $payclient_path = "\\payclient\\{$config['payclass']}";
$Payclient = new $payclient_path($info, $config, $request->domain()); $Payclient = new $payclient_path($info, $config);
$Payclient->notify(); $res = $Payclient->notify();
$this->payHeart($res, $config);
return 200; return 200;
} else { } else {
return 202; return 202;

View File

@@ -7,7 +7,6 @@ namespace app\controller\api;
use app\BaseController; use app\BaseController;
use app\model\PayAccount; use app\model\PayAccount;
use app\model\PayChannel; use app\model\PayChannel;
use app\model\User;
class PayManageController extends BaseController class PayManageController extends BaseController
{ {
@@ -28,9 +27,9 @@ class PayManageController extends BaseController
$aid = $this->request->post('aid'); $aid = $this->request->post('aid');
$res = PayChannel::where(['account_id' => $aid])->order('last_time', 'desc')->select(); $res = PayChannel::where(['account_id' => $aid])->order('last_time', 'desc')->select();
if ($res) { if ($res) {
return \json(\backMsg(0, '获取成功', $res)); return json(backMsg(0, '获取成功', $res));
} else { } else {
return \json(\backMsg(1, '失败')); return json(backMsg(1, '失败'));
} }
} }
// 账号状态 // 账号状态
@@ -39,9 +38,9 @@ class PayManageController extends BaseController
$info = $this->request->post(); $info = $this->request->post();
$up_res = PayAccount::update($info); $up_res = PayAccount::update($info);
if ($up_res) { if ($up_res) {
return json(\backMsg(0, '成功')); return json(backMsg(0, '成功'));
} else { } else {
return json(\backMsg(1, '失败')); return json(backMsg(1, '失败'));
} }
} }
// 添加账号 // 添加账号
@@ -53,17 +52,13 @@ class PayManageController extends BaseController
$info['params'] = '{}'; $info['params'] = '{}';
$check_acc = PayAccount::where(['account' => $info['account'], 'platform' => $info['platform'], 'pid' => $pid])->find(); $check_acc = PayAccount::where(['account' => $info['account'], 'platform' => $info['platform'], 'pid' => $pid])->find();
if ($check_acc) { if ($check_acc) {
return \json(\backMsg(1, '账号已存在')); return json(backMsg(1, '账号已存在'));
} }
$acc = PayAccount::create($info); $acc = PayAccount::create($info);
if ($acc) { if ($acc) {
$state = $this->createAccountConfig($acc); return json(backMsg(0, '添加成功'));
if (!$state) {
return json(\backMsg(1, '自字义参数错误'));
}
return \json(\backMsg(0, '添加成功'));
} else { } else {
return \json(\backMsg(1, '添加失败')); return json(backMsg(1, '添加失败'));
} }
} }
// 编辑账号 // 编辑账号
@@ -72,14 +67,9 @@ class PayManageController extends BaseController
$info = $this->request->post(); $info = $this->request->post();
$up_res = PayAccount::update($info); $up_res = PayAccount::update($info);
if ($up_res) { if ($up_res) {
$acc = PayAccount::find($info['id']); return json(backMsg(0, '修改成功'));
$state = $this->createAccountConfig($acc);
if (!$state) {
return json(\backMsg(1, '自字义参数错误'));
}
return json(\backMsg(0, '修改成功'));
} else { } else {
return json(\backMsg(1, '修改失败')); return json(backMsg(1, '修改失败'));
} }
} }
// 删除账号 // 删除账号
@@ -89,13 +79,9 @@ class PayManageController extends BaseController
$res = PayAccount::destroy($ids); $res = PayAccount::destroy($ids);
$res2 = PayChannel::whereIn('account_id', $ids)->select()->delete(); $res2 = PayChannel::whereIn('account_id', $ids)->select()->delete();
if ($res && $res2) { if ($res && $res2) {
$accs = PayAccount::whereIn('id', $ids)->withTrashed()->select(); return json(backMsg(0, '已删除'));
foreach ($accs as $acc) {
$this->delAccountConfig($acc);
}
return \json(\backMsg(0, '已删除'));
} else { } else {
return \json(\backMsg(1, '失败')); return json(backMsg(1, '失败'));
} }
} }
// 添加收款终端 // 添加收款终端
@@ -104,13 +90,13 @@ class PayManageController extends BaseController
$info = $this->request->post(); $info = $this->request->post();
$check = PayChannel::where(['account_id' => $info['account_id'], 'channel' => $info['channel']])->count(); $check = PayChannel::where(['account_id' => $info['account_id'], 'channel' => $info['channel']])->count();
if ($check) { if ($check) {
return \json(\backMsg(1, '编号已存在')); return json(backMsg(1, '编号已存在'));
} }
$res = PayChannel::create($info); $res = PayChannel::create($info);
if ($res) { if ($res) {
return \json(\backMsg(0, '添加成功')); return json(backMsg(0, '添加成功'));
} else { } else {
return \json(\backMsg(1, '添加失败')); return json(backMsg(1, '添加失败'));
} }
} }
// 编辑收款终端 // 编辑收款终端
@@ -119,9 +105,9 @@ class PayManageController extends BaseController
$info = $this->request->post(); $info = $this->request->post();
$up_res = PayChannel::update($info); $up_res = PayChannel::update($info);
if ($up_res) { if ($up_res) {
return json(\backMsg(0, '修改成功')); return json(backMsg(0, '修改成功'));
} else { } else {
return json(\backMsg(1, '修改失败')); return json(backMsg(1, '修改失败'));
} }
} }
// 删除收款终端 // 删除收款终端
@@ -130,17 +116,9 @@ class PayManageController extends BaseController
$cid = $this->request->post('id'); $cid = $this->request->post('id');
$res = PayChannel::destroy($cid); $res = PayChannel::destroy($cid);
if ($res) { if ($res) {
return \json(\backMsg(0, '已删除')); return json(backMsg(0, '已删除'));
} else { } else {
return \json(\backMsg(1, '失败')); return json(backMsg(1, '失败'));
}
}
// 删除账号配置
public function delAccountConfig($acc)
{
$path = config_path() . "/payconfig/{$acc->pid}_{$acc->id}.php";
if (file_exists($path)) {
unlink($path);
} }
} }
// 上传二维码图片 // 上传二维码图片
@@ -166,25 +144,17 @@ class PayManageController extends BaseController
$req_pid = $req_info['pid']; $req_pid = $req_info['pid'];
$req_aid = $req_info['aid']; $req_aid = $req_info['aid'];
// 加载配置文件 // 加载配置文件
$config = \think\facade\Config::load("payconfig/{$req_pid}_{$req_aid}", 'payconfig'); $config = PayAccount::getAccountConfig($req_aid);
// 收款平台账号配置 if ($config === false) return json(backMsg(1, '账号配置文件错误'));
$pay_config = isset($config['pay']) ? $config['pay'] : []; if ($req_aid != $config['aid'] || $req_pid != session('pid')) return json(backMsg(1, '监听收款配置不一致'));
// 配置检查
if ($pay_config) {
// 账号配置信息
$aid = $pay_config['aid'];
if ($req_aid != $aid) return '监听收款配置不一致';
} else {
return '监听收款配置文件名错误';
}
// 登陆账号 // 登陆账号
$config = ['username' => $pay_config['account'], 'password' => $pay_config['password']]; $pay_config = ['username' => $config['account'], 'password' => $config['password']];
// 收款查询 // 收款查询
$query = $pay_config['query']; $query = $config['query'];
// 实例监听客户端 // 实例监听客户端
$payclient_name = $pay_config['payclass']; $payclient_name = $config['payclass'];
$payclient_path = "\\payclient\\{$payclient_name}"; $payclient_path = "\\payclient\\{$payclient_name}";
$Payclient = new $payclient_path($config); $Payclient = new $payclient_path($pay_config);
// 获取支付明细 // 获取支付明细
$records = $Payclient->getOrderInfo($query); $records = $Payclient->getOrderInfo($query);
if ($records) { if ($records) {
@@ -194,51 +164,4 @@ class PayManageController extends BaseController
return json(backMsg(1, '查询空订单')); return json(backMsg(1, '查询空订单'));
} }
} }
// 生成账号配置
private function createAccountConfig($acc)
{
$params = \json_decode($acc->params, \true);
if ($params === null) {
return false; // 自定义参数错误
}
$platform = \app\controller\api\PluginController::getPluginInfo($acc->getData('platform'));
$user = User::where('pid', $acc->pid)->find();
$query_tpl = $platform['query'];
$query = var_export(\array_merge($query_tpl, $params), \true);
$config = <<<EOF
<?php
// +----------------------------------------------------------------------
// | 支付监听配置,一个文件,一个账号
// +----------------------------------------------------------------------
return [
// 用户账号配置
'user' => [
'pid' => {$user->pid},
'key' => '{$user->secret_key}'
],
// 收款平台账号配置
'pay' => [
// 账号id
'aid' => $acc->id,
// 收款平台
'platform' => '{$acc->getData('platform')}',
// 插件类名
'payclass' => '{$platform['class_name']}',
// 账号
'account' => '{$acc->account}',
// 密码
'password' => '{$acc->password}',
// 订单查询参数配置
'query' => {$query},
]
];
EOF;
$name = "{$user->pid}_{$acc->id}";
$path = config_path() . "/payconfig/{$name}.php";
\file_put_contents($path, $config);
return true;
}
} }

View File

@@ -171,7 +171,7 @@ class PluginController extends BaseController
// 获取插件配置 // 获取插件配置
private static function getPluginConfig(): array private static function getPluginConfig(): array
{ {
$payplugin_path = config_path() . '/extendconfig/payplugin.php'; $payplugin_path = config_path() . '/extend/payplugin.php';
if (!file_exists($payplugin_path)) { if (!file_exists($payplugin_path)) {
return []; return [];
} }
@@ -182,7 +182,7 @@ class PluginController extends BaseController
// 保存插件配置 // 保存插件配置
private function savePluginConfig(array $config, string $note = '说明') private function savePluginConfig(array $config, string $note = '说明')
{ {
$payplugin_path = config_path() . '/extendconfig/payplugin.php'; $payplugin_path = config_path() . '/extend/payplugin.php';
$note_tpl = <<<EOF $note_tpl = <<<EOF
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | $note // | $note

View File

@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace app\model; namespace app\model;
use app\BaseModel; use app\BaseModel;
use app\model\User;
use app\controller\api\PluginController;
class PayAccount extends BaseModel class PayAccount extends BaseModel
{ {
@@ -24,10 +26,44 @@ class PayAccount extends BaseModel
} }
return self::withCount(['payChannel' => 'channel'])->where($select); return self::withCount(['payChannel' => 'channel'])->where($select);
} }
// 获取账号配置
public static function getAccountConfig($aid, $pid = null): array|bool
{
$aid_info = self::find($aid);
// 插件配置
$platform = PluginController::getPluginInfo($aid_info->getData('platform'));
// 查询参数
$params = json_decode($aid_info->params, true);
$query = array_merge($platform['query'], $params);
if ($aid_info && $platform) {
$config = [
'pid' => $aid_info->pid,
// 账号id
'aid' => $aid_info->id,
// 收款平台
'platform' => $aid_info->getData('platform'),
// 插件类名
'payclass' => $platform['class_name'],
// 账号
'account' => $aid_info->account,
// 密码
'password' => $aid_info->password,
// 订单查询参数配置
'query' => $query,
];
if ($pid !== null) {
$pid_info = User::where('pid', $pid)->find();
$config['key'] = $pid_info->secret_key;
}
return $config;
} else {
return false;
}
}
// 获取器 // 获取器
public function getPlatformAttr($value) public function getPlatformAttr($value)
{ {
$payplugin_path = config_path() . '/extendconfig/payplugin.php'; $payplugin_path = config_path() . '/extend/payplugin.php';
if (!file_exists($payplugin_path)) { if (!file_exists($payplugin_path)) {
return []; return [];
} }

View File

@@ -1,2 +0,0 @@
*
!.gitignore

View File

@@ -1,66 +0,0 @@
<?php
declare(strict_types=1);
class MpayClass
{
private $pid;
private $key;
private $host;
private $check_neworder_url;
private $submit_records_url;
function __construct($config)
{
$this->pid = $config['pid'];
$this->key = $config['key'];
$this->host = $config['host'];
$this->check_neworder_url = $this->host . '/order.php';
$this->submit_records_url = $this->host . '/payHeart';
}
// 查询新订单
public function orderHeart()
{
$url = $this->check_neworder_url . "?pid={$this->pid}&sign={$this->getSign()}";
$res = $this->getHttpResponse($url);
return $res;
}
// 提交收款明细
public function upRecords($records, $aid)
{
$header = ['Content-Type: application/json;charset=UTF-8'];
$url = $this->submit_records_url . "?pid={$this->pid}&aid={$aid}&sign={$this->getSign()}";
$res = $this->getHttpResponse($url, $header, json_encode($records));
return $res;
}
// 签名方法
private function getSign()
{
return md5($this->pid . $this->key);
}
// 请求外部资源
private function getHttpResponse($url, $header = [], $post = null, $timeout = 10)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
$httpheader[] = "Accept: */*";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
$httpheader[] = "Connection: close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
}
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}