REAPI/app/channel/service/system/Meicheng.php

252 lines
6.6 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
/**
* 话费服务
* Class Common
* @package app\channel\service
*/
class Meicheng extends Core
{
# 这几个可以后台设置
protected $host = 'host';
protected $mid = 'mid';
protected $token = 'token';
protected $api = 'api';
# 电话充值
public function dhcz($param)
{
$check = array
(
'mobile' => 'mobile',
'cash' => 'price',
'paytype' => 'paytype',
);
$this->api = 'onlinepay.do';
if (isset($param['isp']) && $param['isp']) {
$isp = $param['isp'];
} else {
$isp = $this->isp($param['mobile']);
2024-09-29 15:43:18 +08:00
}
$paytype = '';
if ($isp == 1) {
$paytype = 'yd';
}
if ($isp == 2) {
$paytype = 'lt';
}
if ($isp == 3) {
$paytype = 'dx';
}
$param['paytype'] = $paytype;
return $this->submit($param, $check);
}
public function ydcz($param)
{
$param['paytype'] = 'yd';
return $this->hf_submit($param);
}
public function ltcz($param)
{
$param['paytype'] = 'lt';
return $this->hf_submit($param);
}
public function dxcz($param)
{
$param['paytype'] = 'dx';
return $this->hf_submit($param);
}
public function dxcz_lt_mnp($param)
{
$param['paytype'] = 'dx';
return $this->hf_submit($param);
}
public function hf_submit($param)
{
$check = array
(
'mobile' => 'mobile',
'cash' => 'price',
'paytype' => 'paytype',
's_num'=>'num'
);
$param['s_num'] = 1;
$this->api = 'onlinepay.do';
return $this->submit($param, $check);
}
# 查询余额接口
public function account()
{
// $request['day'] = $day;
$request['userid'] = $this->mid;
$request['sign'] = $this->_sign($request, array('userid'));
$url = $this->host . 'searchbalance.do';
$response = $this->curl('get', $url, $request);
// var_dump($response);die;
$log['type'] = 'account_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$array = (array) simplexml_load_string($response);
if (!$array) {
return false;
} elseif (isset($array['resultno']) && $array['resultno'] == '1') {
$array['account'] = $array['balance'];
# 正确
return $array;
} else {
# 错误
return false;
}
}
2024-09-29 15:43:18 +08:00
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
unset($request['sign']);
unset($request['s_order']);
$sign = strtoupper($this->_sign($request, array('userid', 'orderid', 'sporderid', 'merchantsubmittime', 'resultno')));
if ($sign != $data['sign']) {
return false;
}
$result = array();
$result['cash'] = 1;
if ($data['resultno'] == 1) {
$result['status'] = 2;
} elseif ($data['resultno'] == 9) {
$result['status'] = 3;
} else {
$result['status'] = 4;
}
if (isset($data['remark1']) && $data['remark1']) {
# 流水号
$data['s_number'] = $result['s_number'] = $data['remark1'];
2024-09-29 15:43:18 +08:00
}
$result['yes'] = 'OK';
$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['resultno']) && $array['resultno'] == '0') {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
# 查询接口
public function query($param)
{
$request['userid'] = $this->mid;
$request['sporderid'] = $param['order_id'];
$request['sign'] = $this->_sign($request, array('userid', 'sporderid'));
$url = $this->host . 'searchpay.do';
$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 (isset($array['resultno']) ) {
if($array['resultno'] == '1'){
$result['status'] = 2;
}elseif ($array['resultno'] == '9'){
$result['status'] = 3;
}
}
return $result;
2024-09-29 15:43:18 +08:00
}
# 提交数据
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['num'] = $request['num']??1;
2024-09-29 15:43:18 +08:00
$request['productid'] = '';
2024-09-29 15:43:18 +08:00
$request['userid'] = $this->mid;
$request['sporderid'] = $param['order'];
$request['spordertime'] = date('YmdHis');
$request['back_url'] = $this->getNotify($param['order'], 1);
$request['productid'] = $this->getGid($param['cash']);
$request['sign'] = $this->_sign($request, array('userid', 'productid', 'price', 'num', 'mobile', 'spordertime', 'sporderid'));
$url = $this->host . $this->api;
$response = $this->curl('get', $url, $request);
$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);
}
}