212 lines
6.3 KiB
PHP
212 lines
6.3 KiB
PHP
<?php
|
||
|
||
namespace app\channel\service\system;
|
||
|
||
/* 余初科技--自然谷
|
||
* 文档地址:https://www.showdoc.com.cn/2163083026028110/9711339493196952
|
||
*/
|
||
class Yuchu extends Core
|
||
{
|
||
protected $host = ''; // 主机,不设置,后台
|
||
protected $mid = ''; // 商户id
|
||
protected $token = ''; // 密钥
|
||
protected $api = '';
|
||
//话费直充
|
||
public function dhcz($param){
|
||
$check = array
|
||
(
|
||
'mobile' => 'Account',
|
||
);
|
||
|
||
|
||
$this->api='api/order/SubmitOrder';
|
||
|
||
return $this->submit($param, $check);
|
||
}
|
||
#dhcz 三网他,ydcz/dxcz/ltcz各个运营商类目,不识别
|
||
|
||
public function ydcz($param)
|
||
{
|
||
return $this->dhcz($param);
|
||
}
|
||
public function dxcz($param)
|
||
{
|
||
return $this->dhcz($param);
|
||
}
|
||
public function ltcz($param)
|
||
{
|
||
return $this->dhcz($param);
|
||
}
|
||
|
||
# 通知处理 主要返回状态 2是成功 3是失败
|
||
public function notify($data)
|
||
{
|
||
$result = array();
|
||
$result['cash'] = 1;
|
||
$result['status'] = 4;
|
||
#以上基本不要变
|
||
|
||
|
||
if ($data['OrderStatus'] == 2) {
|
||
$result['status'] = 2;
|
||
} else if($data['OrderStatus'] == 3) {
|
||
$result['status'] = 3;
|
||
}else {
|
||
$result['status'] = 4;
|
||
}
|
||
|
||
|
||
if (isset($data['OrderDesc']) && $data['OrderDesc']) {
|
||
# 流水号
|
||
$data['s_number'] = $result['s_number'] = $data['OrderDesc'];
|
||
}
|
||
|
||
$result['yes'] = 'ok'; //响应给上游的数据
|
||
|
||
$result['data'] = $data;
|
||
|
||
return $result;
|
||
}
|
||
public function query($order){
|
||
$request['AgentId'] = $this->mid;
|
||
if(isset($order['num']) && $order['num']>0){
|
||
$order_id = $order['order_id'].'_'.$order['num'];
|
||
}else{
|
||
$order_id = $order['order_id'];
|
||
}
|
||
$request['OutOrderId'] = $order_id;
|
||
$request['Timestamp'] = date('YmdHis') . sprintf('%03d', round(microtime(true) * 1000) % 1000); // 修正时间戳格式
|
||
$request['Sign'] = $this->_sign($request);
|
||
$url = $this->host . 'api/order/QueryOrder';
|
||
$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);
|
||
|
||
$result = array();
|
||
$result['status'] = 4;
|
||
if ($array['Code']=='00' ) {
|
||
#判断平台订单号和订单号是否匹配
|
||
if(isset($order['channel_order_id']) && $order['channel_order_id']){
|
||
if($order['channel_order_id'] != $array['Data']['OrderId']){
|
||
$result['status'] = 4;
|
||
return $result;
|
||
}
|
||
if($array['Data']['OrderStatus'] !=2 && $array['Data']['OrderStatus'] !=3){
|
||
$result['status'] = 4;
|
||
return $result;
|
||
}else{
|
||
$result = $array['Data'];
|
||
$result['status'] = $result['OrderStatus'];
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
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
|
||
|
||
|
||
if (!$array) {
|
||
$msg = 'error';
|
||
} elseif (isset($array['Code']) && $array['Code']=='00') { //修改判断逻辑为确定提交成功即可
|
||
# 正确
|
||
$msg = 'ok';
|
||
|
||
} else {
|
||
# 错误
|
||
$msg = $array['Msg'] ?? 'error'; //前面message根据响应内容改成对应的错误提示
|
||
}
|
||
|
||
return array
|
||
(
|
||
'msg' => $msg,
|
||
'data' => $data,
|
||
'array' => $array,
|
||
);
|
||
}
|
||
|
||
# 查询余额接口
|
||
public function account($day)
|
||
{
|
||
$request['AgentId'] = $this->mid;
|
||
$request['Timestamp'] = date('YmdHis') . sprintf('%03d', round(microtime(true) * 1000) % 1000); // 修正时间戳格式
|
||
$request['Sign'] = $this->_sign($request);
|
||
$url = $this->host . 'api/order/QueryAgentYuE';
|
||
$response = $this->curl('post', $url, $request);
|
||
$response = $this->response($response);
|
||
|
||
$datas = $response['array']['Data'];
|
||
if(is_array($datas) && isset($datas['SurplusYuE']) && $datas['SurplusYuE'] ){
|
||
$datas['account'] = $datas['SurplusYuE']; //此处为余额
|
||
}
|
||
|
||
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['AgentId']=$this->mid;
|
||
|
||
$request['CallbackUrl']=$this->getNotify($param['order'], 1); //回调方法默认不用改
|
||
$request['OutOrderId']=$param['order']; //我方系统订单号
|
||
$request['ProductId']=$this->getGid($param['cash']);
|
||
$request['Timestamp'] = date('YmdHis') . sprintf('%03d', round(microtime(true) * 1000) % 1000); // 修正时间戳格式
|
||
$request['Sign'] = $this->_sign($request);
|
||
$url = $this->host . $this->api; //组装地址
|
||
|
||
#该系统提交写法:
|
||
|
||
$response = $this->curl('post', $url, $request);
|
||
$response = $this->response($response);
|
||
$channel_order_id = '';
|
||
|
||
#如果出现响应有上游订单号,可以如下
|
||
if (isset($response["array"]['Data']['OrderId'])) {
|
||
$channel_order_id = $response["array"]['Data']['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)
|
||
{
|
||
// unset($request['cash']);
|
||
$request['SignKey'] = $this->token;
|
||
ksort($request);
|
||
$str = '';
|
||
foreach($request as $k=>$v){
|
||
$str.=$k.'='.$v.'&';
|
||
}
|
||
$str = rtrim($str,'&');
|
||
return strtolower(md5($str));
|
||
}
|
||
} |