24a4b60517 refactor(gateway): 重构 v4 版本接口签名验证逻辑
- 在 AuthService 中添加对 sign 和 signature 参数的处理
- 更新 Core 控制器中的签名验证逻辑
- 新增 Goods 控制器,实现商品列表查询功能
- 修改 Handle 控制器的日志记录逻辑
- 优化 JunKa、Lingshi 等渠道服务类的实现
- 更新 MerchantService 中的回调处理逻辑
-调整 OrderService 中的订单查询和更新逻辑
- 新增 Qiling 和 Reapi 渠道服务类
2025-04-17 17:21:00 +08:00

207 lines
6.1 KiB
PHP

<?php
namespace app\channel\service\system;
#灵石
class Lingshi extends Core
{
protected $host = ''; // 主机,不设置,后台
protected $mid = ''; // 商户id
protected $token = ''; // 密钥
protected $api = '';
protected $sign = [];
//话费直充
public function dy($param){
$check = array
(
'account' => 'account',
'cash'=>'faceValue',
'isp'=>'isp'
);
$param['isp'] = 1031;
$this->api='api/create/dy';
$this->sign = array('account', 'appId', 'faceValue', 'notify', 'orderId');
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$result = array();
$result['cash'] = 1;
$result['status'] = 4;
#以上基本不要变
if(isset($data['query_status'])){
if ($data['query_status'] == 200) {
$result['status'] = 2;
} else if($data['query_status'] == 500) {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
}else{
if ($data['code'] == '0') {
$result['status'] = 2;
} else if($data['code'] == 1) {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
}
// if(isset($data['kami'])&&$data['kami']){
// $result['kami']=$data['kami'];
// }
if (isset($data['msg']) && $data['msg']) {
# 流水号
$data['s_number'] = $result['s_number'] = $data['msg'];
}
$result['yes'] = 'success'; //响应给上游的数据
$result['data'] = $data;
return $result;
}
public function query($order){
// $request['appId'] = $this->mid;
// $request['orderId'] = $order['channel_order_id'];
// $request['sign'] = $this->_sign($request, ['appId','orderId']);
// $url = $this->host . 'api/order/query';
// $response = $this->curl('post', $url, $request);
//
// #记录日志
// $log['type'] = 'query_response';
// $log['data'] = $response;
// $log['config'] = $this->data;
// $this->log($log);
// #记录借宿
//
// $array = $this->json_decode($response,true);
//
// $result = array();
// $result['status'] = 4;
// if ($array['code']=='ok' ) {
// if($array['state'] ==200){
// $result['query_status'] = 200;
//
// $result['status'] = 2;
// }elseif ($array['state'] == 500){
// $result['query_status'] = 500;
// $result['status'] = 3;
// }
//
// }
// if(isset($array['data']['cards'])){
// $result['kami']=array(
// 'cardno' => isset($array['data']['cards'][0]['card_no'])?$array['data']['cards'][0]['card_no']:'',
// 'cardpwd' => $array['data']['cards'][0]['card_password'],
// 'expired' => $array['data']['cards'][0]['expired_at'],
// );
// }
// return $result;
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
#以上不要做任何变动
$array = $this->json_decode($data); //响应数据为json
#如果响应为XML格式
// $array = (array) simplexml_load_string($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array['code']) && $array['code']=='0') { //修改判断逻辑为确定提交成功即可
# 正确
$msg = 'ok';
} else {
# 错误
$msg = $array['msg'] ?? 'error'; //前面message根据响应内容改成对应的错误提示
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
# 查询余额接口
public function account($day)
{
$request['appId'] = $this->mid;
$request['sign'] = $this->_sign($request,['appId']);
$url = $this->host . 'api/balance';
$response = $this->curl('post', $url, $request,true);
$response = $this->response($response);
$datas = $response['array'];
if(is_array($datas) && isset($datas['balance']) && $datas['balance'] ){
$datas['account'] = $datas['balance']; //此处为余额
}
return $datas;
}
# 提交数据
private function submit($param, $check)
{
#提交处理
$param = $this->param($param, $check);
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
return $param;
}
$request = $param['detail']; //上面替换剩下的
#结束初步
$request['appId']=$this->mid;
$request['notify']=$this->getNotify($param['order'], 1); //回调方法默认不用改
$request['orderId']=$param['order']; //我方系统订单号
$request['sign'] = $this->_sign($request, $this->sign);
$url = $this->host . $this->api; //组装地址
$response = $this->curl('post', $url, $request,true); //为post请求,json提交
$response = $this->response($response);
$channel_order_id = '';
#如果出现响应有上游订单号,可以如下
if (isset($response["array"]['orderId'])) {
$channel_order_id = $response["array"]['orderId'];
}
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['msg'];
}
private function _sign($request, $param)
{
//ksort($request);
$signature_string = '';
foreach ($param as $k => $v) {
if (isset($request[$v])) {
$signature_string .= $v . '=' . $request[$v] . '&';
}
}
$signature_string .= 'key=' . $this->token;
return md5($signature_string);
}
}