更新插件开发模版

This commit is contained in:
技术老胡 2024-11-04 17:27:42 +08:00
parent bab5fa4f53
commit ab65b5d622
4 changed files with 246 additions and 3 deletions

View File

@ -21,8 +21,9 @@ class IndexController
} }
public function test() public function test()
{ {
$key = "0383d7088b6947b68e4a626af119e2bd"; $pay = new \payclient\PayClass(['username'=>1531,'password'=>15646]);
return $key;
return '123';
} }
} }

View File

@ -0,0 +1,35 @@
<?php
// +----------------------------------------------------------------------
// | 支付监听配置,一个文件,一个账号
// +----------------------------------------------------------------------
return [
// 用户账号配置
'user' => [
'pid' => 1001,
'key' => '953c4d682d9ab148277b76a06e215ce7'
],
// 收款平台账号配置
'pay' => [
// 账号id
'aid' => 45,
// 收款平台
'platform' => 'storepay',
// 插件类名
'payclass' => 'ZhiHuiJingYing',
// 账号
'account' => '16546465',
// 密码
'password' => '5464564654',
// 订单查询参数配置
'query' => array (
'pageNo' => 1,
'pageSize' => 10,
'payClient' => 4,
'status' => 2,
'_t' => NULL,
'createTime_begin' => NULL,
'createTime_end' => NULL,
),
]
];

View File

@ -0,0 +1,207 @@
<?php
declare(strict_types=1);
namespace payclient;
class PayClass
{
// 收款平台
private $pay_type = 'ysepay';
// 收款平台账号
private $username;
// 平台登陆密码
private $password;
// token保存路径
private $token_path;
// Cookie保存路径
private $cookie_path;
// 当前时间戳
private $now;
// 收款平台网站
private $payhost = 'https://xym.ysepay.com';
// 用户登陆接口
private $login_path = '/saas_merchant_management/public/login';
// 订单查询接口
private $order_query_path = '/saas_merchant_management/order/info';
// 验证码接口
private $captcha_path = '/saas_merchant_management/public/captcha';
function __construct($config)
{
$this->username = $config['username'];
$this->password = $config['password'];
$this->now = time();
// 检查token/cookie目录
$dir_path = runtime_path() . "token/{$this->pay_type}/";
if (!is_dir($dir_path)) {
if (!mkdir($dir_path, 755, true)) echo '目录创建失败';
}
// token/cookie文件路径
$this->token_path = $dir_path . md5($this->username . $this->password . __CLASS__) . '.json';
$this->cookie_path = $dir_path . md5($this->username . $this->password . __CLASS__) . '.txt';
// 检查token文件
if (!file_exists($this->token_path)) {
file_put_contents($this->token_path, \json_encode(['token' => 'ok', 'update_time' => date('Y-m-d H:i:s')]));
}
}
// 获取订单信息
public function getOrderInfo(array $query)
{
$order_list = $this->queryOrder($query);
$orders = [];
if (!$order_list) return $orders;
$payways = ['ALI' => 'alipay', 'WECHAT' => 'wxpay'];
foreach ($order_list as $value) {
$order = [];
// 平台订单流水号
$order['order_no'] = $value['tranAcpBsNo'];
// 支付类型
$order['payway'] = $payways[$value['tranChannelType']];
// 收款金额
$order['price'] = (float)$value['tranOrdAmt'];
// 收款渠道(二维码编号)
$order['channel'] = $value['bsShopCode'];
// 添加到订单列表
$orders[] = $order;
}
return $orders;
}
// 查询订单
private function queryOrder(array $query, $times = 0): array
{
// 查询订单列表
$url = $this->payhost . $this->order_query_path;
$token = $this->getToken();
$header = ['Host:xym.ysepay.com', 'token:' . $token];
$new_query = $this->getOrderQuery($query);
$res = $this->getHttpResponse($url . '?' . http_build_query($new_query), $header, json_encode($new_query));
$result = json_decode($res, true);
// 检查订单信息
$order_list = [];
if ($result['code'] === 0) {
$order_list = $result['data']['list'];
} else {
// 重试2次
if ($times < 3) {
$this->updateToken();
$order_list = $this->queryOrder($query, $times + 1);
}
}
return $order_list;
}
// 构建订单查询数组信息
private function getOrderQuery(array $query): array
{
$new_query = $query;
$now = $this->now;
$startTime = date('Y-m-d H:i:s', $now - 175);
$endTime = date('Y-m-d H:i:s', $now);
$new_query['queryStartTime'] = $startTime;
$new_query['queryEndTime'] = $endTime;
$new_query['time'] = $now . mt_rand(100, 999);
return $new_query;
}
// 获取验证码图像
private function getCaptcha(): array
{
$url = $this->payhost . $this->captcha_path . '?time=' . $this->now . mt_rand(100, 999);
$header = ['Host:xym.ysepay.com'];
$res = $this->getHttpResponse($url, $header);
$data = json_decode($res, true);
$captcha_info = [];
if ($data['code'] === 0) {
$captcha_info['randomCodeBase64'] = $data['data']['randomCodeBase64'];
$captcha_info['uuid'] = $data['data']['uuid'];
}
return $captcha_info;
}
// 登陆账号
private function login($times = 0): bool
{
$captcha_info = $this->getCaptcha();
$captcha = $this->getCaptchaInfo($captcha_info['randomCodeBase64']);
$url = $this->payhost . $this->login_path;
$user_info = [
'loginName' => $this->username,
'password' => $this->password,
'randomCode' => $captcha,
'uuid' => $captcha_info['uuid'],
'time' => (int)($this->now . mt_rand(100, 999))
];
$header = ['Host:xym.ysepay.com'];
$res = $this->getHttpResponse($url, $header, json_encode($user_info));
$data = json_decode($res, true);
if ($data['code'] === 0) {
// 如何需要就保存token
$this->saveToken($data['data']);
return true;
} else {
// 重试2次
if ($times < 3) {
$is_login = false;
$is_login = $this->login($times + 1);
return $is_login;
}
}
}
// 更新token
private function updateToken(): bool
{
$is_login = $this->login();
return $is_login;
}
// 获取token
private function getToken(): string
{
$token_info = json_decode(file_get_contents($this->token_path), true);
return $token_info['token'];
}
// 保存token
private function saveToken($data)
{
$token = $data['token'];
file_put_contents($this->token_path, ['token' => $token, 'update_time' => date('Y-m-d H:i:s')]);
}
// 解析验证码
private function getCaptchaInfo(string $image = '', string $typeid = '3'): string
{
$api_url = 'http://api.ttshitu.com/predict';
$info = ['username' => '2679275057', 'password' => '7698177hcnTJ', 'typeid' => $typeid, 'image' => $image];
$res = $this->getHttpResponse($api_url, [], json_encode($info));
$data = json_decode($res, true);
$captcha = '';
if ($data['success'] === true) {
$captcha = $data['data']['result'];
}
return $captcha;
}
// 请求外部资源
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);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
$httpheader[] = "Accept: */*";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.9";
$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;
}
}

View File

@ -72,7 +72,7 @@ CREATE TABLE `mpay_pay_account` (
`account` varchar(255) NOT NULL DEFAULT '' COMMENT '账号', `account` varchar(255) NOT NULL DEFAULT '' COMMENT '账号',
`password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码', `password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码',
`state` tinyint(4) NOT NULL DEFAULT '1' COMMENT '启用', `state` tinyint(4) NOT NULL DEFAULT '1' COMMENT '启用',
`pattern` tinyint(4) NOT NULL DEFAULT '0' COMMENT '账号监听模式', `pattern` tinyint(4) NOT NULL DEFAULT '1' COMMENT '账号监听模式',
`params` varchar(255) NOT NULL DEFAULT '' COMMENT '自定义查询', `params` varchar(255) NOT NULL DEFAULT '' COMMENT '自定义查询',
`delete_time` timestamp NULL DEFAULT NULL COMMENT '软删除', `delete_time` timestamp NULL DEFAULT NULL COMMENT '软删除',
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE