224 lines
6.9 KiB
PHP
224 lines
6.9 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
namespace app\channel\service\system;
|
||
|
|
||
|
#模板
|
||
|
class Youyuwang extends Core
|
||
|
{
|
||
|
protected $host = ''; // 主机,不设置,后台
|
||
|
protected $mid = ''; // 商户id
|
||
|
protected $token = ''; // 密钥
|
||
|
protected $api = '';
|
||
|
//话费直充
|
||
|
public function cardbuy($param){
|
||
|
// $check 前面为我们系统参宿,后面为文档中上游对应参数
|
||
|
# card(油卡账户),mobile(手机号,只在话费充值有),account(账户,除话费充值均为这个),以上三个基本上都是单独存在,不会同时存在,所以选择好判断.
|
||
|
# cash(充值金额),num(充值数量,非必须存在)
|
||
|
$check = array
|
||
|
(
|
||
|
'num' => 'quantity',
|
||
|
);
|
||
|
|
||
|
|
||
|
if(!isset($param['num'])) $param['num'] = 1;
|
||
|
|
||
|
return $this->submit($param, $check);
|
||
|
}
|
||
|
|
||
|
|
||
|
# 通知处理 主要返回状态 2是成功 3是失败
|
||
|
public function notify($data)
|
||
|
{
|
||
|
$result = array();
|
||
|
$result['cash'] = 1;
|
||
|
$result['status'] = 4;
|
||
|
#以上基本不要变
|
||
|
|
||
|
if(isset($data['query_status'])){
|
||
|
if ($data['query_status'] == 200) {
|
||
|
$result['status'] = 2;
|
||
|
} else if($data['query_status'] == 500) {
|
||
|
$result['status'] = 3;
|
||
|
}else {
|
||
|
$result['status'] = 4;
|
||
|
}
|
||
|
}else{
|
||
|
if ($data['status'] == 200) {
|
||
|
$result['status'] = 2;
|
||
|
} else if($data['status'] == 500) {
|
||
|
$result['status'] = 3;
|
||
|
}else {
|
||
|
$result['status'] = 4;
|
||
|
}
|
||
|
}
|
||
|
#以下处理逻辑可以自由
|
||
|
if(isset($data['kami'])&&$data['kami']){
|
||
|
$result['kami']=$data['kami'];
|
||
|
$result['status'] = 7;
|
||
|
}
|
||
|
if(isset($data['cards'])&&$data['cards']){
|
||
|
if(is_string($data['cards'])) $data['cards'] = json_decode($data['cards'],true);
|
||
|
|
||
|
$result['kami'] = array
|
||
|
(
|
||
|
'cardno' => $data['cards'][0]['card_no'] ?? '',
|
||
|
'cardpwd' => $data['cards'][0]['card_password'],
|
||
|
'expired' => $data['cards'][0]['expired_at'] ?? '',
|
||
|
// 'value' => $array['data']['count_money'],
|
||
|
);
|
||
|
$data['kami']=$result['kami'];
|
||
|
$result['status'] = 7;
|
||
|
}
|
||
|
|
||
|
$result['yes'] = 'ok'; //响应给上游的数据
|
||
|
|
||
|
$result['data'] = $data;
|
||
|
|
||
|
return $result;
|
||
|
}
|
||
|
public function query($order){
|
||
|
$request = $this->public_param('esales.order.get');
|
||
|
$request['out_order_id'] = $order['order_id'];
|
||
|
$request['sign'] = $this->_sign($request);
|
||
|
$url = $this->host ;
|
||
|
$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,true);
|
||
|
|
||
|
$result = array();
|
||
|
$result['status'] = 4;
|
||
|
if ($array['code']=='ok' ) {
|
||
|
if($array['state'] ==200){
|
||
|
$result['query_status'] = 200;
|
||
|
|
||
|
$result['status'] = 2;
|
||
|
}elseif ($array['state'] == 500){
|
||
|
$result['query_status'] = 500;
|
||
|
$result['status'] = 3;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
if(isset($array['data']['cards'])){
|
||
|
$result['kami']=array(
|
||
|
'cardno' => isset($array['data']['cards'][0]['card_no'])?$array['data']['cards'][0]['card_no']:'',
|
||
|
'cardpwd' => $array['data']['cards'][0]['card_password'],
|
||
|
'expired' => $array['data']['cards'][0]['expired_at'],
|
||
|
);
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
# 数据响应格式处理
|
||
|
public function response($data)
|
||
|
{
|
||
|
$log['type'] = 'response';
|
||
|
$log['data'] = $data;
|
||
|
$log['config'] = $this->data;
|
||
|
$this->log($log);
|
||
|
|
||
|
#以上不要做任何变动
|
||
|
|
||
|
|
||
|
|
||
|
$array = $this->json_decode($data); //响应数据为json
|
||
|
|
||
|
|
||
|
if (!$array) {
|
||
|
$msg = 'error';
|
||
|
} elseif (isset($array['code']) && $array['code']=='0') { //修改判断逻辑为确定提交成功即可
|
||
|
# 正确
|
||
|
$msg = 'ok';
|
||
|
} else {
|
||
|
# 错误
|
||
|
$msg = $array['message'] ?? 'error'; //前面message根据响应内容改成对应的错误提示
|
||
|
}
|
||
|
|
||
|
return array
|
||
|
(
|
||
|
'msg' => $msg,
|
||
|
'data' => $data,
|
||
|
'array' => $array,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
# 查询余额接口
|
||
|
public function account($day)
|
||
|
{
|
||
|
$request = $this->public_param('esales.customer.info');
|
||
|
$request['sign'] = $this->_sign($request);
|
||
|
$url = $this->host;
|
||
|
$response = $this->curl('post', $url, $request);
|
||
|
$response = $this->response($response);
|
||
|
|
||
|
$datas = $response['array']['data'];
|
||
|
if(is_array($datas) && isset($datas['balance']) && $datas['balance'] ){
|
||
|
$datas['account'] = $datas['balance']; //此处为余额
|
||
|
}
|
||
|
|
||
|
return $datas;
|
||
|
}
|
||
|
|
||
|
#获取公共参数
|
||
|
public function public_param($method)
|
||
|
{
|
||
|
$param = array();
|
||
|
$param['customer_id'] = $this->mid;
|
||
|
$param['method'] = $method;
|
||
|
$param['timestamp']=time();
|
||
|
return $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['customer_id']=$this->mid;
|
||
|
$request['method'] = 'esales.order.purchase';
|
||
|
$request['timestamp']=time();
|
||
|
|
||
|
$request['notify_url']=$this->getNotify($param['order'], 1); //回调方法默认不用改
|
||
|
$request['out_order_id']=$param['order']; //我方系统订单号
|
||
|
$request['product_id']=$this->getGid($param['cash']); //此方法为根据面值获取产品id,如无需可注释
|
||
|
// unset($request['cash']);
|
||
|
$request['sign'] = $this->_sign($request);
|
||
|
$url = $this->host ;
|
||
|
|
||
|
$response = $this->curl('post', $url, $request);
|
||
|
$response = $this->response($response);
|
||
|
$channel_order_id = '';
|
||
|
|
||
|
#如果出现响应有上游订单号,可以如下
|
||
|
if (isset($response["array"]['data']['order_id'])) {
|
||
|
$channel_order_id = $response["array"]['data']['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)
|
||
|
{
|
||
|
// unset($request['cash']);
|
||
|
ksort($request);
|
||
|
$str=$this->token;
|
||
|
foreach($request as $k=>$v){
|
||
|
$str.=$k.$v;
|
||
|
}
|
||
|
return strtolower(md5($str));
|
||
|
}
|
||
|
}
|