258 lines
12 KiB
PHP
258 lines
12 KiB
PHP
<?php
|
|
namespace app\onlinechip\controller\traits;
|
|
use think\Db;
|
|
use think\Log;
|
|
use think\Request;
|
|
|
|
trait hipPay{
|
|
|
|
/**
|
|
* 创建支付订单和返回支付链接
|
|
* @return \think\response\Json
|
|
* DateTime: 2019/12/27 18:42
|
|
*/
|
|
public function hip_online_order(){
|
|
$user_id = intval(Request::instance()->post('user_id'));
|
|
$money = intval(Request::instance()->post('price'));
|
|
$client = intval(Request::instance()->post('client'));
|
|
$username = trim(Request::instance()->post('username'));
|
|
$user = Db::name('user')->where(array('id' => $user_id, 'status' => 1, 'is_delete' => 0, 'agent' => 0))->find();
|
|
$zdlIdArr = explode(',',$user['agent_parent_id_path']);
|
|
|
|
$area_list = config('area_list');
|
|
//判断是否金流支线,如果不是的话,不允许充值
|
|
$isAllow = false;
|
|
foreach($area_list[$user['area_id']]['limit_agent'] as $allowAgentId){
|
|
if(in_array($allowAgentId,$zdlIdArr)){
|
|
$isAllow = true;
|
|
}
|
|
}
|
|
if(!$isAllow){
|
|
return json(array('status' => 0, 'message' => '该账号暂不支持在线充值'));
|
|
}
|
|
|
|
$zdlId = $zdlIdArr[0];
|
|
$zdl = Db::name('user')->where(array('id' => $zdlId, 'status' => 1, 'is_delete' => 0, 'agent' => 1))->find();
|
|
if(!$zdl){
|
|
return json(array('status' => 0, 'message' => '所属代理账号异常,暂不能充值'));
|
|
}
|
|
if($zdl['money'] < $money){
|
|
return json(array('status' => 0, 'message' => '所属代理余额不足,暂不能充值'));
|
|
}
|
|
$pay_list = config('pay_list');
|
|
if($user && $user_id > 0 && $money > 0 && $client > 0){
|
|
$pay_orderid = date("YmdHis").rand(10,99); //订单号
|
|
$pay_amount = $money; //交易金额
|
|
$pay_notifyurl = $pay_list['hipPay']['callback_url']; //服务端返回地址
|
|
$pay_notifyurl_page = $pay_list['hipPay']['notify_url']; //服务端返回地址--页面
|
|
$tjurl = "http://www.hip168.net/api/getway01/CodeRequest.ashx"; //提交地址
|
|
$native = [
|
|
'Merchent'=>'JF',
|
|
'OrderID'=>$pay_orderid,
|
|
'Total'=>$pay_amount,
|
|
'ReAUrl'=>$pay_notifyurl,
|
|
'ReBUrl'=>$pay_notifyurl_page,
|
|
'Name'=>$user['username']
|
|
];
|
|
$postData = http_build_query($native);
|
|
$orderUrl = $tjurl.'?'.$postData;
|
|
//创建订单
|
|
$order_record = array();
|
|
$order_record['pay_channel_id'] = 3;
|
|
$order_record['pay_channel_name'] = 'hipPay';
|
|
$order_record['user_id'] = $user_id;
|
|
$order_record['order_sn'] = $pay_orderid;
|
|
$order_record['appid'] = 0;//不需要
|
|
$order_record['api_key'] = '';//不需要
|
|
$order_record['money'] = $pay_amount;
|
|
$order_record['sign'] = '';//不需要
|
|
$order_record['create_time'] = time();
|
|
$order_record['back_order_sn'] = '';
|
|
$order_record['client'] = $client;
|
|
$order_id = Db::name('order_record')->insertGetId($order_record);
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 3;
|
|
$order_log['pay_channel_name'] = 'hipPay';
|
|
$order_log['money'] = $pay_amount;
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = 'user_id:'.$user_id.',用户名:'.$username.',发起充值:'.$pay_amount.',充值时间:'.date('Y-m-d H:i:s',time());
|
|
$order_log['order_id'] = $order_id;
|
|
$order_log['client'] = $client;
|
|
Db::name('order_log')->insert($order_log);
|
|
return json(array('status' => 1, 'message' => '发起支付成功,即将跳转...', 'url' => $orderUrl));
|
|
}else{
|
|
return json(array('status' => 0, 'message' => '支付发起失败,请稍后再试'));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 支付码显示页面
|
|
* @return mixed
|
|
* @throws \think\Exception
|
|
* DateTime: 2020/1/10 15:32
|
|
*/
|
|
public function hip_call_back_url(){
|
|
if(Request::instance()->get()){
|
|
$Ordernum = Request::instance()->get('Ordernum');
|
|
$StoreCode = Request::instance()->get('StoreCode');
|
|
$Total = Request::instance()->get('Total');
|
|
$mobileUrl = Request::instance()->get('mobileUrl');
|
|
if($Ordernum && $StoreCode && $Total && $mobileUrl){
|
|
$order = Db::name('order_record')->where(['order_sn'=>$Ordernum,'status'=>0])->find();
|
|
if($order && $order['money'] == $Total){
|
|
Db::name('order_record')->where(['id'=>$order['id']])->update(['back_order_sn'=>$StoreCode]);
|
|
$this->assign('StoreCode',$StoreCode);
|
|
$this->assign('Total',$Total);
|
|
$this->assign('mobileUrl',$mobileUrl);
|
|
return $this->fetch();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 充值回调
|
|
* @throws \think\Exception
|
|
* DateTime: 2019/12/30 13:49
|
|
*/
|
|
public function hip_call_back(){
|
|
$get = Request::instance()->get();
|
|
$serializeData = json_encode($get);
|
|
if(getIP() != '203.66.45.226'){
|
|
Log::record(array_merge($get,['ip'=>getIP()]),'error_pay_callback_illegal_request');//非法请求
|
|
exit();
|
|
}
|
|
|
|
$Ordernum = Request::instance()->get('Ordernum');
|
|
$StoreCode = Request::instance()->get('StoreCode');
|
|
$Status = Request::instance()->get('Status');
|
|
$Total = Request::instance()->get('Total');
|
|
|
|
Log::record(Request::instance()->get(),'callback');
|
|
if($Status != '0000' || !$StoreCode || !$Ordernum || !$Total){
|
|
Log::record(Request::instance()->get(),'error_pay_callback');
|
|
exit();
|
|
}
|
|
$order = Db::name('order_record')->where(['order_sn'=>$Ordernum,'back_order_sn'=>$StoreCode])->find();
|
|
if(!$order){
|
|
$order_log['pay_channel_id'] = 3;
|
|
$order_log['pay_channel_name'] = 'hipPay';
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是查询不到订单号,回调内容:".$serializeData;
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');//订单不存在,回调失败
|
|
}
|
|
$user = Db::name('user')->where(array('id' => $order['user_id'], 'status' => 1, 'is_delete' => 0, 'agent' => 0))->find();
|
|
if(!$user){
|
|
$update['status'] = 1;
|
|
$update['remake'] = '收到回调,无法查询到会员';
|
|
Db::name('order_record')->where('id',$order['id'])->update($update);
|
|
$order_log['pay_channel_id'] = 3;
|
|
$order_log['pay_channel_name'] = 'hipPay';
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是查询不到会员信息,会员可能已经删除或者停用,回调内容:".$serializeData;
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');//用户不存在,回调失败
|
|
}
|
|
|
|
//返回金额
|
|
if($Total != $order['money']){
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 3;
|
|
$order_log['pay_channel_name'] = 'hipPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "收到回调,但是金额不对,回调内容:".$serializeData;
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');//金额不对,回调失败
|
|
}
|
|
|
|
//总代理金额
|
|
$zdlIdArr = explode(',',$user['agent_parent_id_path']);
|
|
$zdlId = $zdlIdArr[0];
|
|
$zdl = Db::name('user')->where(array('id' => $zdlId, 'status' => 1, 'is_delete' => 0, 'agent' => 1))->find();
|
|
if(!$zdl || $zdl['money'] < $order['money']){
|
|
$update['status'] = 1;
|
|
$update['remake'] = '无法找到总代理,或者总代理账号余分不足,交易暂停';
|
|
Db::name('order_record')->where('id',$order['id'])->update($update);
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 3;
|
|
$order_log['pay_channel_name'] = 'hipPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "无法找到总代理,或者总代理账号余分不足,回调内容:".$serializeData;
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
exit('OK');//找不到总代理,回调失败
|
|
}
|
|
|
|
//更新订单表
|
|
$call_back_data['back_money'] = $Total;
|
|
$call_back_data['back_sign'] = '';
|
|
$call_back_data['back_time'] = time();
|
|
|
|
$call_back_data['status'] = 3;
|
|
$call_back_data['agent_parent_id'] = $user['agent_parent_id'];
|
|
$call_back_data['agent_parent_username'] = $user['agent_parent_username'];
|
|
$call_back_data['zdl_id'] = $zdl['id'];
|
|
$call_back_data['zdl_username'] = $zdl['username'];
|
|
$call_back_data['zdl_money_before'] = $zdl['money'];
|
|
$call_back_data['zdl_money_after'] = $zdl['money'] - $order['money'];
|
|
$call_back_data['money_before'] = $user['money'];
|
|
$call_back_data['money_after'] = $user['money'] + $order['money'];
|
|
Db::name('order_record')->where(array('id' => $order['id']))->update($call_back_data);
|
|
|
|
//记录支付日志
|
|
$order_log = array();
|
|
$order_log['pay_channel_id'] = 3;
|
|
$order_log['pay_channel_name'] = 'hipPay';
|
|
$order_log['money'] = $order['money'];
|
|
$order_log['create_time'] = time();
|
|
$order_log['remake'] = "回调支付成功";
|
|
$order_log['order_id'] = $order['id'];
|
|
Db::name('order_log')->insert($order_log);
|
|
//插入上分表并增加余分
|
|
$recharge = array();
|
|
$recharge['type'] = 4;
|
|
$recharge['amount'] = $order['money'];
|
|
$recharge['mode'] = 1;
|
|
$recharge['agent_or_admin'] = 3;
|
|
$recharge['controller_id'] = $zdl['id'];
|
|
$recharge['controller_username'] = $zdl['username'];
|
|
$recharge['controller_nickname'] = $zdl['nickname'];
|
|
$recharge['controller_type'] = '第三方充值平台充值,扣取总代理余分';
|
|
$recharge['controller_old_money'] = $zdl['money'];
|
|
$recharge['controller_new_money'] = $zdl['money'] - $order['money'];
|
|
$recharge['user_id'] = $order['user_id'];
|
|
$recharge['user_type'] = $user['agent'];
|
|
$recharge['user_agent_level'] = 0;
|
|
$recharge['username_for'] = $user['username'];
|
|
$recharge['nickname_for'] = $user['nickname'];
|
|
$recharge['user_parent_id'] = $user['agent_parent_id'];
|
|
$recharge['create_time'] = time();
|
|
$recharge['old_money'] = $user['money'];
|
|
$recharge['new_money'] = $user['money'] + $order['money'];
|
|
$recharge['controller_system'] = 3;
|
|
$recharge['remake'] = '第三方充值成功';
|
|
Db::name('recharge')->insert($recharge);
|
|
$updateZdl = array();
|
|
$updateZdl['money'] = $zdl['money'] - $order['money'];
|
|
$updateZdl['last_recharge_out'] = $order['money'];
|
|
$updateZdl['last_recharge_out_time'] = time();
|
|
$updateZdl['recharge_out_count'] = $zdl['recharge_out_count'] + 1;
|
|
$updateZdl['recharge_out_total_amount'] = $zdl['recharge_out_total_amount'] + $order['money'];
|
|
$updateUser = array();
|
|
$updateUser['money'] = $user['money'] + $order['money'];
|
|
$updateUser['last_recharge'] = $order['money'];
|
|
$updateUser['last_recharge_time'] = time();
|
|
$updateUser['recharge_count'] = $user['recharge_count'] + 1;
|
|
$updateUser['recharge_total_amount'] = $user['recharge_total_amount'] + $order['money'];
|
|
Db::name('user')->where(array('id' => $zdl['id']))->limit(1)->update($updateZdl);
|
|
Db::name('user')->where(array('id' => $order['user_id']))->limit(1)->update($updateUser);
|
|
exit('OK');
|
|
}
|
|
}
|