254 lines
8.0 KiB
PHP
Raw Permalink Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
use app\channel\service\SystemService;
/**
* 通用渠道接入服务
* Class Common
* @package app\channel\service
*/
class Common extends Core
{
# 这几个可以后台设置
protected $host = 'host';
protected $mid = 'mid';
protected $token = 'token';
protected mixed $api = 'api';
protected mixed $system = [];
public function __construct($async = false, $data = array(), $channel = array(), $product = array())
{
parent::__construct($async, $data, $channel, $product);
$this->system = SystemService::instance()->getOne($this->channel['system_id']);
}
# 提交接口进行充值
public function __call($name, $param = array())
{
$param = $param[0];
# 获取提交的接口
$api = SystemService::instance()->getApi($name, $this->system['id'], 1);
$request = $check = array();
foreach ($api['col'] as $k => $v) {
if ($v['type'] == 1) {
$request[$v['name']] = $v['value'];
} elseif ($v['type'] == 2) {
$request[$v['name']] = 'card';
$check['card'] = $v['name'];
} elseif ($v['type'] == 3) {
$request[$v['name']] = 'mobile';
$check['mobile'] = $v['name'];
} elseif ($v['type'] == 4) {
$request[$v['name']] = $param['order'];
} elseif ($v['type'] == 5) {
$request[$v['name']] = $this->mid;
} elseif ($v['type'] == 6) {
$request[$v['name']] = $this->token;
} elseif ($v['type'] == 7) {
$request[$v['name']] = $this->getNotify($param['order'], 1, $api['id']);
} elseif ($v['type'] == 8) {
$request[$v['name']] = $this->_sign($request, $param);
} elseif ($v['type'] == 9) {
$request[$v['name']] = time();
} elseif ($v['type'] == 10) {
$request[$v['name']] = date('YmdHis');
} elseif ($v['type'] == 11) {
$request[$v['name']] = date('Y-m-d H:i:s');
} elseif ($v['type'] == 12) {
$request[$v['name']] = 'cash';
$check['cash'] = $v['name'];
} elseif ($v['type'] == 13) {
$request[$v['name']] = 'cnum';
$check['cnum'] = $v['name'];
} elseif ($v['type'] == 14) {
$request[$v['name']] = 'cpwd';
$check['cpwd'] = $v['name'];
} elseif ($v['type'] == 15) {
$request[$v['name']] = 'idCard';
$check['idCard'] = $v['name'];
} elseif ($v['type'] == 16) {
$request[$v['name']] = 'realName';
$check['realName'] = $v['name'];
} elseif ($v['type'] == 17) {
$request[$v['name']] = $this->proid($param, $v['value']);
if (!$request[$v['name']]) {
return 'error';
}
} elseif ($v['type'] == 18) {
$request[$v['name']] = $name;
}
}
$param = $this->param($param, $check);
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
return $param;
}
if ($this->system['sign'] == 4) {
$this->token = $this->mid . '|' . $this->token;
$request = $this->getParam($request, $this->token);
} elseif ($this->system['sign'] == 5) {
$request = $this->getParam($request, $this->token, 2);
}
$url = $this->host . $api['api'];
if ($api['request_method'] == 1) {
$response = $this->curl('post', $url, $request);
} elseif ($api['request_method'] == 2) {
$response = $this->curl('post', $url, $request, true);
} elseif ($api['request_method'] == 3) {
$response = $this->curl('get', $url, $request);
} elseif ($api['request_method'] == 4) {
$response = $this->curl('get', $url, $request, true);
}
$response = $this->response($response, $api);
$channel_order_id = '';
$key = $api['channel_order_id'];
if ($key) {
if (str_contains($key, '=')) {
$temp = explode('=', $key);
if (isset($response['array'][$temp[0]][$temp[1]])) {
$channel_order_id = $response['array'][$temp[0]][$temp[1]];
}
} else {
if (isset($response['array'][$key])) {
$channel_order_id = $response['array'][$key];
}
}
}
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['msg'];
}
# 数据响应格式处理
public function response($data, $api)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array[$api['success_key']]) && $array[$api['success_key']] == $api['success_value']) {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = $array[$api['msg_key']] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
# 获取产品id
public function proid($param, $value)
{
if (str_contains($value, '=')) {
if (!isset($param['cash'])) {
return false;
}
parse_str($value, $data);
$cash = $param['cash'];
if (isset($data[$cash])) {
return $data[$cash];
} else {
return false;
}
}
return $value;
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$api = SystemService::instance()->getApiInfo($data['s_api_id']);
$request = $data;
unset($request['sign']);
unset($request['s_order']);
unset($request['s_api_id']);
$sign = $this->_sign($request);
if ($sign != $data['sign']) {
return false;
}
$result = array();
$result['cash'] = 1;
$status = $api['notify_status_col'];
// yes=1&no=2&other=3
parse_str($api['notify_status_value'], $value);
foreach ($value as $k => $v) {
if ($k == 'yes' && $data[$status] == $v) {
# 成功
$result['status'] = 2;
} elseif ($k == 'no' && $data[$status] == $v) {
# 失败
$result['status'] = 3;
} elseif ($k == 'other' && $data[$status] == $v) {
# 异常
$result['status'] = 5;
} else {
$result['status'] = 4;
}
}
# 流水号
$key = $api['notify_number'];
if ($key) {
if (str_contains($key, '=')) {
$temp = explode('=', $key);
if (isset($data[$temp[0]][$temp[1]])) {
$data['s_nubmer'] = $result['s_nubmer'] = $data[$temp[0]][$temp[1]];
}
} else {
if (isset($data[$key])) {
$data['s_nubmer'] = $result['s_nubmer'] = $data[$key];
}
}
}
$result['yes'] = $api['notify_msg'];
$result['data'] = $data;
return $result;
}
# 查询接口
public function query($param)
{
}
private function _sign($request)
{
ksort($request);
$signature_string = '';
foreach ($request as $k => $v) {
if (isset($request[$k]) && $request[$k]) {
$signature_string .= $k . '' . $request[$k];
}
}
$signature_string .= $this->token;
return md5($signature_string);
}
}