2024-09-29 15:43:18 +08:00

175 lines
4.5 KiB
PHP

<?php
namespace app\channel\service\system;
/**
* 话费服务
* Class Common
* @package app\channel\service
*/
class Shhf extends Core
{
# 这几个可以后台设置
protected $host = 'host';
protected $mid = 'mid';
protected $token = 'token';
protected $api = 'api';
# 电话充值
public function dhcz($param)
{
$check = array
(
'mobile' => 'card_no',
'cash' => 'amount',
'ips_type' => 'ips_type',
);
$this->api = 'channel/hfapi/recharge';
if (isset($param['isp']) && $param['isp']) {
$isp = $param['isp'];
} else {
$isp = $this->isp($param['mobile']);
}
$ips_type = '';
if ($isp == 1) {
$ips_type = '2';
}
if ($isp == 2) {
$ips_type = '3';
}
if ($isp == 3) {
$ips_type = '1';
}
$param['ips_type'] = $ips_type;
$this->ips_type = $ips_type;
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
unset($request['sign']);
unset($request['s_order']);
$sign = $this->_sign($request);
if ($sign != $data['sign']) {
//return false;
}
$result = array();
$result['cash'] = 1;
if ($data['status'] == 1) {
$result['status'] = 2;
} elseif ($data['status'] == 2) {
$result['status'] = 3;
} else {
$result['status'] = 4;
}
if (isset($data['trans_no']) && $data['trans_no']) {
# 流水号
$data['s_nubmer'] = $result['s_nubmer'] = $data['trans_no'];
}
$result['yes'] = 'success';
$result['data'] = $data;
return $result;
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
$array = (array) simplexml_load_string($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array['code']) && $array['code'] == '200') {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = isset($array['message']) ? $array['message'] : 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
# 查询接口
public function query($param)
{
}
# 提交数据
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['user_id'] = $this->mid;
$request['merchant_order_no'] = $param['order'];
$request['valid_min'] = 60*3;
$request['notify_url'] = $this->getNotify($param['order'], 1);
$request['sign'] = $this->_sign($request);
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request);
$response = $this->response($response);
$channel_order_id = '';
$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)
{
ksort($request);
$signature_string = '';
foreach ($request as $k => $v) {
if ($v) {
$signature_string .= $k . '=' . $v . '&';
}
}
$signature_string .= 'key=' . $this->token;
return md5($signature_string);
}
public function isp($mobile)
{
//return $this->getphonetype($mobile);
$url = 'http://cx.shouji.360.cn/phonearea.php?number=' . $mobile;
$data = $this->curl('get', $url);
if ($data) {
$data = json_decode($data, true);
if ($data && isset($data['data']['sp'])) {
$sp = $data['data']['sp'];
if ($sp == '移动') {
return 1;
} elseif ($sp == '联通') {
return 2;
} elseif ($sp == '电信') {
return 3;
}
}
}
return 4;
}
}