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

423 lines
11 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?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 Dayuanren 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' => 'mobile',
// 'cash' => 'amount',
);
if (isset($param['isp']) && $param['isp']) {
$this->lprefix = $param['isp'];
}
$this->api = 'yrapi.php/index/recharge';
return $this->submit($param, $check);
}
public function ltcz($param)
{
$check = array
(
'mobile' => 'mobile',
// 'cash' => 'amount',
);
$this->Operator = 2;
$this->api = 'yrapi.php/index/recharge';
return $this->submit($param, $check);
}
public function ydcz($param)
{
$check = array
(
'mobile' => 'mobile',
// 'cash' => 'amount',
);
$this->Operator = 1;
$this->api = 'yrapi.php/index/recharge';
return $this->submit($param, $check);
}
public function dxcz($param)
{
$check = array
(
'mobile' => 'mobile',
'cash' => 'amount',
);
$this->Operator = 3;
$this->api = 'yrapi.php/index/recharge';
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
$result = array();
$result['cash'] = 1;
$result['status'] = 4;
if ($data['state'] == 1 || $data['state'] ==3) {
$result['status'] = 2;
} else if($data['state'] == 2 || $data['state'] == '-1') {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
if (isset($data['voucher']) && $data['voucher']) {
# 流水号
$data['s_number'] = $data['voucher'];
2024-09-29 15:43:18 +08:00
}
$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['errno']) && $array['errno'] == '0') {
# 正确
$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['userid'] = $this->mid;
$request['out_trade_nums'] = $order['order_id'];
$request['sign'] = $this->_sign($request);
$url = $this->host . 'yrapi.php/index/check';
$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['data']['state'])) {
if ($array['data']['state'] == '1' || $array['data']['state'] == '3') {
$result['state'] = $array['data']['state'];
$result['state'] = 2;
} elseif ($array['data']['state'] == '-1' || $array['data']['state'] == '2') {
$result['state'] = $array['data']['state'];
$result['status'] = 3;
}
// if (isset($array['data']['voucher']) && $array['data']['voucher']) {
// # 流水号
//// $data['szRtnMsg']
// $result['voucher'] = $array['data']['voucher'];
// }
}
return $result;
}
# 查询接口
public function cancel($order)
{
$request['userid'] = $this->mid;
$request['out_trade_nums'] = $order['order_id'];
$request['sign'] = $this->_sign($request);
$url = $this->host . 'yrapi.php/index/cancel';
$response = $this->curl('post', $url, $request);
$log['type'] = 'cancel_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($response);
if (isset($array['errno']) ) {
if($array['errno'] == '0'){
return true;
}elseif ($array['errno'] != '0'){
return false;
}
}
return false;
}
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['userid'] = $this->mid;
$request['out_trade_num'] = $param['order'];
if(!$this->Operator){
$request['product_id'] =$this->getDhczCode($this->lprefix , $param['cash']);
}
if($this->Operator){
$request['product_id'] = $this->getGid($param['cash']);
}
// $request['szPhoneNum'] = $param['account'];
// $request['nMoney'] = $param['cash'];
// $request['nSortType'] = $this->lprefix;
// $request['notify_url'] = $this->getNotify($param['order'], 1);
$request['notify_url'] = $this->getUrl('/gateway/api.notify/dyr');
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']['data']['order_number']) && $response['array']['data']['order_number']) {
#警告类型
$channel_order_id = $response['array']['data']['order_number'];
}
// $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 getDhczCode($paytype,$cash)
{
if ($paytype == '1') {
if ($cash == 50) {
return 30;
} elseif ($cash == 100) {
return 24;
} elseif ($cash == 200) {
return 25;
}
} elseif ($paytype == '2') {
if ($cash == 50) {
return 32;
} elseif ($cash == 100) {
return 28;
} elseif ($cash == 200) {
return 29;
}
} else {
if ($cash == 50) {
return 31;
} elseif ($cash == 100) {
return 26;
} elseif ($cash == 200) {
return 27;
}
}
return false;
}
private function _sign($request, $param='')
{
// 字典排序
ksort($request);
// 拼接签名串
$sign_str = http_build_query($request) . '&apikey='. $this->token;
// var_dump($sign_str);die;
// 签名
$sign = strtoupper(md5(urldecode($sign_str)));
return $sign;
// ksort($request);
//
// $signature_string = '';
//
// foreach ($request as $k => $v) {
// if (isset($request[$k]) && $request[$k]) {
// $signature_string .= $k . '=' . $v . '&';
// }
// }
//
//
// $signature_string .='apikey='. $this->token;
//// var_dump($signature_string);exit;
// return MD5($signature_string);
}
public function send_post($notify_url, $post_data,$type)
{
$postdata = urldecode(http_build_query($post_data));
// var_dump(urldecode($postdata));die;
$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;
}
}