插件开发模版更新

This commit is contained in:
技术老胡 2024-11-05 15:46:51 +08:00
parent ab65b5d622
commit 6acecb0596
6 changed files with 142 additions and 117 deletions

View File

@ -282,7 +282,7 @@ class PayController
$payclient_path = "\\payclient\\{$payclient_name}"; $payclient_path = "\\payclient\\{$payclient_name}";
$Payclient = new $payclient_path($config); $Payclient = new $payclient_path($config);
// 获取支付明细 // 获取支付明细
$records = $Payclient->payQuery($query); $records = $Payclient->getOrderInfo($query);
if ($records) { if ($records) {
// 提交收款记录 // 提交收款记录
$upres = $Mpay->upRecords($records, $aid); $upres = $Mpay->upRecords($records, $aid);

View File

@ -49,7 +49,7 @@ return array(
array( array(
'platform' => 'ysepay', 'platform' => 'ysepay',
'name' => '小Y经营', 'name' => '小Y经营',
'class_name' => 'Ysepay', 'class_name' => 'YsePay',
'price' => '99.00', 'price' => '99.00',
'describe' => '为商户和消费者提供安全、便捷、高效的支付产品与服务助力商户提升运营效率,实现数字化运营', 'describe' => '为商户和消费者提供安全、便捷、高效的支付产品与服务助力商户提升运营效率,实现数字化运营',
'website' => 'https://xym.ysepay.com/', 'website' => 'https://xym.ysepay.com/',

View File

@ -27,7 +27,7 @@ class PayClass
// 验证码接口 // 验证码接口
private $captcha_path = '/saas_merchant_management/public/captcha'; private $captcha_path = '/saas_merchant_management/public/captcha';
function __construct($config) function __construct(array $config)
{ {
$this->username = $config['username']; $this->username = $config['username'];
$this->password = $config['password']; $this->password = $config['password'];
@ -35,18 +35,18 @@ class PayClass
// 检查token/cookie目录 // 检查token/cookie目录
$dir_path = runtime_path() . "token/{$this->pay_type}/"; $dir_path = runtime_path() . "token/{$this->pay_type}/";
if (!is_dir($dir_path)) { if (!is_dir($dir_path)) {
if (!mkdir($dir_path, 755, true)) echo '目录创建失败'; if (!mkdir($dir_path, 755, true)) echo '创建token/cookie目录失败';
} }
// token/cookie文件路径 // token/cookie文件路径
$this->token_path = $dir_path . md5($this->username . $this->password . __CLASS__) . '.json'; $this->token_path = $dir_path . md5($this->username . $this->password . __CLASS__) . '.json';
$this->cookie_path = $dir_path . md5($this->username . $this->password . __CLASS__) . '.txt'; $this->cookie_path = $dir_path . md5($this->username . $this->password . __CLASS__) . '.txt';
// 检查token文件 // 检查token文件
if (!file_exists($this->token_path)) { 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')])); file_put_contents($this->token_path, json_encode(['token' => 'ok', 'update_time' => date('Y-m-d H:i:s')]));
} }
} }
// 获取订单信息 // 获取订单信息
public function getOrderInfo(array $query) public function getOrderInfo(array $query): array
{ {
$order_list = $this->queryOrder($query); $order_list = $this->queryOrder($query);
$orders = []; $orders = [];
@ -82,7 +82,7 @@ class PayClass
if ($result['code'] === 0) { if ($result['code'] === 0) {
$order_list = $result['data']['list']; $order_list = $result['data']['list'];
} else { } else {
// 重试2 // 重试3
if ($times < 3) { if ($times < 3) {
$this->updateToken(); $this->updateToken();
$order_list = $this->queryOrder($query, $times + 1); $order_list = $this->queryOrder($query, $times + 1);
@ -137,12 +137,13 @@ class PayClass
$this->saveToken($data['data']); $this->saveToken($data['data']);
return true; return true;
} else { } else {
// 重试2次 // 重试3次
$is_login = false;
if ($times < 3) { if ($times < 3) {
$is_login = false;
$is_login = $this->login($times + 1); $is_login = $this->login($times + 1);
return $is_login; return $is_login;
} }
return $is_login;
} }
} }
// 更新token // 更新token
@ -162,7 +163,7 @@ class PayClass
private function saveToken($data) private function saveToken($data)
{ {
$token = $data['token']; $token = $data['token'];
file_put_contents($this->token_path, ['token' => $token, 'update_time' => date('Y-m-d H:i:s')]); file_put_contents($this->token_path, json_encode(['token' => $token, 'update_time' => date('Y-m-d H:i:s')]));
} }
// 解析验证码 // 解析验证码
private function getCaptchaInfo(string $image = '', string $typeid = '3'): string private function getCaptchaInfo(string $image = '', string $typeid = '3'): string

View File

@ -1,41 +1,113 @@
<?php <?php
declare(strict_types=1);
namespace payclient; namespace payclient;
class ShouQianBa class ShouQianBa
{ {
// 收款平台
private $pay_type = 'sqbpay';
// 收款平台账号
private $username; private $username;
// 平台登陆密码
private $password; private $password;
// token保存路径
private $token_path; private $token_path;
private $host = 'https://web-platforms-msp.shouqianba.com'; // Cookie保存路径
private $login = '/api/login/ucUser/login'; private $cookie_path;
private $find_transactions = '/api/transaction/findTransactions'; // 当前时间戳
private $refresh_token = '/api/login/ucUser/refreshToken'; private $now;
// 收款平台网站
private $payhost = 'https://web-platforms-msp.shouqianba.com';
// 用户登陆接口
private $login_path = '/api/login/ucUser/login';
// 订单查询接口
private $order_query_path = '/api/transaction/findTransactions';
// 刷新Token接口
private $refresh_token_path = '/api/login/ucUser/refreshToken';
function __construct($config) function __construct(array $config)
{ {
$this->username = $config['username']; $this->username = $config['username'];
$this->password = $config['password']; $this->password = md5($config['password']);
// 检查token目录 $this->now = time();
$dir_path = runtime_path() . 'token/sqbpay'; // 检查token/cookie目录
$dir_path = runtime_path() . "token/{$this->pay_type}/";
if (!is_dir($dir_path)) { if (!is_dir($dir_path)) {
if (!mkdir($dir_path, 755, true)) { if (!mkdir($dir_path, 755, true)) echo '创建token/cookie目录失败';
echo '目录创建失败';
}
} }
$this->token_path = $dir_path . '/' . md5($this->username . $this->password . 'sqbpay') . '.json'; // 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)) { if (!file_exists($this->token_path)) {
// 失效Token刷新 file_put_contents($this->token_path, json_encode(['token' => 'ok', 'update_time' => date('Y-m-d H:i:s')]));
$token_info = ['token' => 'Y2FjZmRiMi04MjRjLTQ2NDgtYTU0Ny1lNzg2MDllMTQ1ZTI6MTcxOTQ1Mzg0MzU3OTozNjAwMDAw.txcnX60Za8', 'expire_time' => 1719453843579];
file_put_contents($this->token_path, json_encode($token_info));
} }
} }
// 登陆账号 // 获取订单信息
private function login(): array public function getOrderInfo(array $query): array
{ {
$order_list = $this->queryOrder($query);
$orders = [];
if (!$order_list) return $orders;
$payways = [2 => 'alipay', 3 => 'wxpay'];
foreach ($order_list as $value) {
$order = [];
// 平台订单流水号
$order['order_no'] = $value['order_sn'];
// 支付类型
$order['payway'] = $payways[$value['payway']];
// 收款金额
$order['price'] = (float)($value['original_amount'] / 100);
// 收款渠道(二维码编号)
$order['channel'] = $value['terminal_device_fingerprint'];
// 添加到订单列表
$orders[] = $order;
}
return $orders;
}
// 查询订单
private function queryOrder(array $query, $times = 0): array
{
// 查询订单列表
$token = $this->getToken();
$url = $this->payhost . $this->order_query_path . '?client_version=7.0.0&token=' . $token;
$header = ['Content-Type: application/json;charset=UTF-8'];
$new_query = $this->getOrderQuery($query);
$res = $this->getHttpResponse($url, $header, json_encode($new_query));
$result = json_decode($res, true);
// 检查订单信息
$order_list = [];
if ($result['code'] === 50000) {
$order_list = $result['data']['records'];
} else {
// 重试1次
if ($times < 1) {
$this->updateToken();
$order_list = $this->queryOrder($query, $times + 1);
}
}
return $order_list;
}
// 构建订单查询数组信息
private function getOrderQuery(array $query): array
{
$new_query = $query;
$now = $this->now;
$begin_time = (int)(($now - 175) . mt_rand(100, 999));
$end_time = (int)($now . mt_rand(100, 999));
$query['date_start'] = $begin_time;
$query['date_end'] = $end_time;
return $new_query;
}
// 登陆账号
private function login($times = 0): bool
{
$url = $this->payhost . $this->login_path;
$user_info = [ $user_info = [
"username" => $this->username, "username" => $this->username,
"password" => md5($this->password), "password" => $this->password,
"uc_device" => [ "uc_device" => [
"device_type" => 2, "device_type" => 2,
"default_device" => 0, "default_device" => 0,
@ -46,100 +118,50 @@ class ShouQianBa
"device_brand" => "Chrome" "device_brand" => "Chrome"
] ]
]; ];
$header = [ $header = ['Content-Type:application/json;charset=UTF-8', 'Host:web-platforms-msp.shouqianba.com', 'Origin:https://s.shouqianba.com', 'Referer:https://s.shouqianba.com/login'];
'Content-Type:application/json;charset=UTF-8',
'Host:web-platforms-msp.shouqianba.com',
'Origin:https://s.shouqianba.com',
'Referer:https://s.shouqianba.com/login',
];
$url = $this->host . $this->login;
$res = $this->getHttpResponse($url, $header, json_encode($user_info)); $res = $this->getHttpResponse($url, $header, json_encode($user_info));
$arr_res = json_decode($res, true); $data = json_decode($res, true);
$mchUserTokenInfo = $arr_res['data']['mchUserTokenInfo']; if ($data['code'] === 50000 && $data['data']['code'] === 50000) {
$token_info = []; // 保存token
$token_info['token'] = $mchUserTokenInfo['token']; $this->saveToken($data['data']['mchUserTokenInfo']);
$token_info['expire_time'] = $mchUserTokenInfo['expire_time']; return true;
// 保存登陆token } else {
file_put_contents($this->token_path, json_encode($token_info)); // 重试2次
// 返回新token $is_login = false;
return $token_info; if ($times < 2) {
$is_login = $this->login($times + 1);
return $is_login;
}
return $is_login;
}
}
// 更新token
private function updateToken(): bool
{
$token = $this->getToken();
$url = $this->payhost . $this->refresh_token_path . '?token=' . $token;
$header = ["Authorization:Bearer {$token}"];
$res = $this->getHttpResponse($url, $header, true);
$data = json_decode($res, true);
if ($data['data']['status'] === 0) {
// 登陆刷新Token
$this->login();
} else {
$this->saveToken($data['data']);
}
return true;
} }
// 获取token // 获取token
private function getToken(): string private function getToken(): string
{ {
$token_info_find = json_decode(file_get_contents($this->token_path), true); $token_info = json_decode(file_get_contents($this->token_path), true);
// 有效期判断 return $token_info['token'];
$expire_time = intval($token_info_find['expire_time'] / 1000);
$token = '';
if ($expire_time - time() < 600) {
// 刷新token
$url = $this->host . $this->refresh_token . '?token=' . $token_info_find['token'];
$header = ["Authorization:Bearer {$token_info_find['token']}"];
$res = $this->getHttpResponse($url, $header, true);
$arr_res = json_decode($res, true);
if ($arr_res['data']['status'] === 0) {
// 登陆更新token
$token = ($this->login())['token'];
} else {
$token = $arr_res['data']['token'];
$token_info = [];
$token_info['token'] = $token;
$token_info['expire_time'] = $arr_res['data']['expire_time'];
// 保存Token
file_put_contents($this->token_path, json_encode($token_info));
}
} else {
$token = $token_info_find['token'];
}
return $token;
} }
// 接口客户端 // 保存token
public function payQuery(array $query): array private function saveToken($data)
{ {
$token = $this->getToken(); $token = $data['token'];
if ($token) { file_put_contents($this->token_path, json_encode(['token' => $token, 'update_time' => date('Y-m-d H:i:s')]));
$url = $this->host . $this->find_transactions . '?client_version=7.0.0&token=' . $token;
$header = ['Content-Type: application/json;charset=UTF-8'];
// 构建订单查询
$now = time();
$begin_time = ($now - 175) * 1000;
$end_time = $now * 1000;
$query['date_start'] = $begin_time;
$query['date_end'] = $end_time;
// 查询订单列表
$res_order = $this->getHttpResponse($url, $header, json_encode($query));
$arr_res = json_decode($res_order, true);
$list = [];
if ($arr_res['code'] === 50000) {
$list = $arr_res['data']['records'];
}
// 重构订单流水,返回数组
$moneyList = [];
if ($list) {
$order_no = [];
$payway = [];
$price = [];
$channel = [];
$payways = [2 => 'alipay', 3 => 'wxpay'];
foreach ($list as $value) {
// 平台订单流水号
$order_no[] = $value['order_sn'];
// 支付类型
$payway[] = $payways[$value['payway']];
// 收款金额
$price[] = $value['original_amount'] / 100;
// 收款渠道(二维码编号)
$channel[] = $value['terminal_device_fingerprint'];
}
$moneyList['order_no'] = $order_no;
$moneyList['payway'] = $payway;
$moneyList['price'] = $price;
$moneyList['channel'] = $channel;
}
return $moneyList;
} else {
return [];
}
} }
// 请求外部资源 // 请求外部资源
private function getHttpResponse($url, $header = [], $post = null, $timeout = 10) private function getHttpResponse($url, $header = [], $post = null, $timeout = 10)
@ -148,11 +170,13 @@ class ShouQianBa
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 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) { if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else { } else {
$httpheader[] = "Accept: */*"; $httpheader[] = "Accept: */*";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8"; $httpheader[] = "Accept-Language: zh-CN,zh;q=0.9";
$httpheader[] = "Connection: close"; $httpheader[] = "Connection: close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
} }

Binary file not shown.

View File

@ -1 +1 @@
{"code":0,"msg":"没有新订单"} {"code":1,"msg":"\u67091\u4e2a\u65b0\u8ba2\u5355","orders":[{"id":36315,"pid":1001,"aid":7,"cid":7,"patt":1}]}