REAPI/app/channel/service/system/Cardshang.php
2024-09-29 15:43:18 +08:00

325 lines
8.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\channel\service\system;
use app\channel\service\CardService;
use app\merchant\service\OrderService;
use app\channel\service\ProductMappingService;
/**
* 数据服务
* Class Bodingcheng
* @package app\channel\service
*/
class Cardshang extends Core
{
# 这几个可以后台设置
protected $host = '';
protected $mid = '';
protected $token = '';
protected $card = array();
protected $order_id = '';
protected $api = '';
protected $sign = array();
protected $Operator = '';
protected $lprefix = '';
# 电话充值
public function dhcz($param)
{
$check = array
(
'mobile' => 'phone',
'cash' => 'money',
'type'=>'type',
);
$param['type'] = '1';
// if (isset($param['isp']) && $param['isp']) {
// $param['nSortType']= $param['isp'];
// }
// $param['nProductClass']=1;
// $param['nProductType']=1;
// $this->sign = array('szAgentId', 'szOrderId', 'szPhoneNum', 'nMoney', 'nSortType', 'nProductClass','nProductType','szProductId','szTimeStamp');
$this->api = 'card';
return $this->submit($param, $check);
}
public function ltcz($param)
{
$check = array
(
'mobile' => 'phone',
'cash' => 'money',
'type'=>'type',
);
$param['type'] = '1';
$this->api = 'card';
return $this->submit($param, $check);
}
public function dxcz($param)
{
$check = array
(
'mobile' => 'phone',
'cash' => 'money',
'type'=>'type',
);
$param['type'] = '1';
$this->api = 'card';
return $this->submit($param, $check);
}
public function ydcz($param)
{
$check = array
(
'mobile' => 'phone',
'cash' => 'money',
'type'=>'type',
);
$param['type'] = '1';
$this->api = 'card';
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
$result = array();
$result['cash'] = 1;
$result['status'] = 4;
if(is_string($data)){
$data = json_decode($data, true);
}
if ($data['return_code'] == "SUCCESS") {
if($data['return_msg']['attach'] == "支付成功"){
$result['status'] = 2;
}
} else if($data['return_code'] == "FAIL") {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
$result['yes'] = '{"return_code":"OK"}';
$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['return_code']) && $array['return_code'] == 'SUCCESS') {
# 正确
$msg = 'ok';
}
// }
else {
# 错误
$msg = $array['desc'] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
# 订单查询
public function ddcx($param)
{
$check = array
(
'order_id' => 'szOrderId',
);
$this->sign = array('szAgentId', 'szOrderId');
$this->api = 'api/old/queryorder';
return $this->submit($param, $check);
}
# 账户余额
public function balance($param)
{
$check = array
(
);
$this->sign = array('szAgentId');
$this->api = 'api/old/queryBalance';
return $this->submit($param, $check);
}
# 查询余额接口
public function account($day)
{
$request['day'] = $day;
$request['appid'] = $this->mid;
$request['api_product'] = 'query';
$this->token = $this->mid . '|' . $this->token;
$request = $this->getParam($request, $this->token);
$url = $this->host . 'api/old/queryBalance';
$response = $this->curl('post', $url, $request);
$response = $this->response($response);
return $response['array']['data'];
}
# 查询接口
public function query($order)
{
$request['szAgentId'] = $this->mid;
$request['szOrderId'] = $order['order_id'];
$request['szVerifyString'] = $this->_sign1($request, array('szAgentId', 'szOrderId'));
$request['szFormat'] = 'json';
$url = $this->host . 'api/old/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 (isset($array['nRtn']) ) {
if($array['nRtn'] == '5012'){
$result['nFlag'] = 2;
$result['status'] = 2;
}elseif ($array['nRtn'] == '5013'){
$result['nFlag'] = 3;
$result['status'] = 3;
}
if (isset($array['szRtnMsg']) && $array['szRtnMsg']) {
# 流水号
// $data['szRtnMsg']
$result['szRtnMsg'] = $array['szRtnMsg'];
}
}
return $result;
}
private function _sign1($request, $param)
{
// ksort($request);
// var_dump($request);die;
$signature_string = '';
foreach ($param as $k => $v) {
// var_dump($request[$v]);die;
// if (isset($request[$v]) && $request[$v]) {
$signature_string .= $v . '=' . $request[$v] . '&';
// var_dump($signature_string);die;
}
$signature_string = substr($signature_string, 0, -1);
$signature_string .= '&szKey='.$this->token;
// var_dump($signature_string);die;
// $signature_string = iconv("UTF-8", "gbk", $signature_string);
return md5($signature_string);
}
# 提交数据
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['uid'] = $this->mid;
$request['orderid'] = $param['order'];
$request['notify_url'] = $this->getNotify($param['order'], 1);
ksort($request);
$request['sign'] = $this->_sign($request);
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request);
// $response = $this->send_post($url,$request,'POST');
$response = $this->response($response);
//$response['data'] = '';
//$response['msg'] = 'ok';
$channel_order_id = '';
if (isset($response['array']['szRtnCode']) && $response['array']['szRtnCode'] != 'success') {
#警告类型
$channel_order_id = $response['array']['szRtnCode'];
}
// $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)
{
$signature_string.=($k.'='.$v);
}
$signature_string.=$this->token;
// $signature_string .='szKey='. $this->token;
// var_dump($signature_string);exit;
return md5($signature_string);
}
public function send_post($notify_url, $post_data,$type)
{
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s
)
);
$context = stream_context_create($options);
$result = file_get_contents($notify_url, false, $context);
return $result;
}
}