156 lines
4.3 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
/**
* 服务
* Class Bodingcheng
* @package app\channel\service
*/
class Haixin extends Core
{
# 这几个可以后台设置
protected $host = 'host';
protected $mid = 'mid';
protected $token = 'token';
protected $api = 'order/submit';
# 手机充值
public function dhcz($param)
{
$check = array
(
'mobile' => 'number',
'cash' => 'checkPrice',
'productType' => 'productType',
);
$param['productType'] = 1;
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
unset($request['sign']);
unset($request['s_order']);
$sign = $this->_sign($request, array('userid', 'key', 'code', 'orderId', 'requestId'));
if ($sign != $data['sign']) {
return false;
}
$result = array();
$result['cash'] = 1;
if ($data['code'] == '200') {
$result['status'] = 2;
} elseif ($data['code'] == '505') {
$result['status'] = 3;
} else {
$result['status'] = 4;
}
if (isset($data['certificate']) && $data['certificate']) {
# 流水号
$data['s_nubmer'] = $result['s_nubmer'] = $data['certificate'];
}
$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 = $this->json_decode($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array['code']) && $array['code'] == '200') {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = isset($array['msg']) ? $array['msg'] : 'error';
}
return array
(
'msg' => $msg,
'data' => $data,
'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['checkPrice'] = round($request['checkPrice'] * $this->product['percent'], 4);
$request['requestId'] = $param['order'];
$request['callbackUrl'] = $this->getNotify($param['order'], 1);
$request['productId'] = $this->getCode($param['cash']);
if (!$request['productId']) {
return 'order';
}
$header['Authorization-User'] = $this->mid;
$request['sign'] = $this->_sign($request, array('userid', 'key', 'productId', 'requestId', 'number'));
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request, 'post', $header);
$response = $this->response($response);
//$response['data'] = '';
//$response['msg'] = 'ok';
$channel_order_id = '';
if (isset($response['array']['data']['orderId']) && $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 getCode($cash)
{
if ($cash == 30) {
return 10365;
} elseif ($cash == 50) {
return 10366;
} elseif ($cash == 100) {
return 10367;
} elseif ($cash == 200) {
return 10368;
}
return 0000;
}
private function _sign($request, $param)
{
$request['userid'] = $this->mid;
$request['key'] = $this->token;
$signature_string = '';
foreach ($param as $k => $v) {
if (isset($request[$v]) && $request[$v]) {
$signature_string .= $request[$v];
}
}
return md5($signature_string);
}
}