319 lines
10 KiB
PHP
319 lines
10 KiB
PHP
<?php
|
||
|
||
namespace app\common\model;
|
||
|
||
use think\Db;
|
||
use wxpay\lib\WxPayConfig;
|
||
use think\Loader;
|
||
/**
|
||
* 支付
|
||
*/
|
||
class Pay extends Base{
|
||
|
||
|
||
/***充值过程***/
|
||
public function recharge($order) {
|
||
|
||
$payinfo = db('pay_setting')->where('id',$order['payid'])->find();
|
||
//回传支付信息
|
||
$userinfo = db('member')->field('username,avatar,reg_time,channel,clickid,ldurl,wxid,adid,aidname,userType,vip_enddate')->where('uid',$order['uid'])->find();
|
||
//到账处理
|
||
$this->addaccount($order['uid'],$payinfo,$order['type'],$userinfo['vip_enddate']);
|
||
//查看是否后台配置了回传比例 和是否该条充值类型设置了回传 然后回传
|
||
$ispay = $payinfo['ispay'];
|
||
$testTrBookInfo = db('member_clickid')->where([
|
||
'uid'=>$order['uid'],
|
||
'bookid'=>$order['articleid']
|
||
])->order('id desc')->find();
|
||
if($testTrBookInfo){
|
||
$userinfo['ldurl'] = $testTrBookInfo['ldurl'];
|
||
$userinfo['clickid'] = $testTrBookInfo['clickid'];
|
||
$isordertr = true;
|
||
$adid = $testTrBookInfo['ad_id'];
|
||
$aidname = $testTrBookInfo['ad_name'];
|
||
}else{
|
||
$isordertr = false;
|
||
$userinfo['ldurl'] = 0;
|
||
$userinfo['clickid'] = 0;
|
||
$adid = 0;
|
||
$aidname = 0;
|
||
}
|
||
|
||
if($adid || $aidname){
|
||
db('order')->where(['uid'=>$order['uid'],'out_trade_no'=>$order['out_trade_no']])->update(['adid'=>$adid,'aidname'=>$aidname]);
|
||
}
|
||
$is = db('order')->where(['uid'=>$order['uid'],'status'=>1,'channel'=>$order['channel']])->count('id');
|
||
if($ispay==1 && $isordertr){
|
||
if($userinfo['clickid'] && ($is < 2)){
|
||
import("Payreturn.Payreturn", EXTEND_PATH,'.php');
|
||
$YueKaiPay = new \Payreturn();
|
||
if($order['ldurl']){
|
||
$payratio = db('transfer')->field('id,novelId,pageurl,controlaccout_open,intercept,intercept_time')->where('id',$userinfo['ldurl'])->find();
|
||
if($payratio){
|
||
$redata = $this->aotucontr($payratio,$adid);
|
||
$scale = $redata['scale'];
|
||
$trPayNum = $redata['trPayNum'];
|
||
$logtxt = $redata['logtxt'];
|
||
$istransfer = $this->needtwo($scale,$trPayNum);
|
||
$scale = $scale.$logtxt;
|
||
if($istransfer){
|
||
$returnpaylog = array(
|
||
'uid'=>$order['uid'],
|
||
'ldurl'=>$userinfo['ldurl'],
|
||
'type'=>'回传比例:'.$scale.'回传成功 推广链接ID:'.$userinfo['ldurl'],
|
||
'addtime'=>time(),
|
||
'status'=>1,
|
||
'channel'=>$userinfo['channel'],
|
||
'clickid'=>$userinfo['clickid'],
|
||
'money'=>floatval($order['money']),
|
||
'adid'=>$adid,
|
||
'aidname'=>$aidname
|
||
);
|
||
//写入回传日志 成功记录
|
||
M('returnpay_log')->insert($returnpaylog);
|
||
$YueKaiPay->transpays($userinfo['clickid']);
|
||
}else{
|
||
$returnpaylog = array(
|
||
'uid'=>$order['uid'],
|
||
'ldurl'=>$userinfo['ldurl'],
|
||
'type'=>'回传比例:'.$scale.'不回传 推广链接ID:'.$userinfo['ldurl'],
|
||
'addtime'=>time(),
|
||
'status'=>2,
|
||
'channel'=>$userinfo['channel'],
|
||
'clickid'=>$userinfo['clickid'],
|
||
'money'=>floatval($order['money']),
|
||
'adid'=>$adid,
|
||
'aidname'=>$aidname
|
||
);
|
||
//写入回传日志 失败记录
|
||
M('returnpay_log')->insert($returnpaylog);
|
||
// 支付用户没回传的clickid存起来
|
||
$payClickidData = array(
|
||
'uid'=>$order['uid'],
|
||
'clickid'=>$userinfo['clickid'],
|
||
'addtime'=>time(),
|
||
'ldurl'=>$userinfo['ldurl'],
|
||
'linkid'=>$userinfo['channel'],
|
||
'book'=>'',
|
||
'money'=>floatval($order['money']),
|
||
'adid'=>$adid,
|
||
'aidname'=>$aidname
|
||
);
|
||
$ispayclickid = M('payclickid')->where('uid',$order['uid'])->value('id');
|
||
if($ispayclickid==''){
|
||
M('payclickid')->insert($payClickidData);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
$count = db('order')->where(['uid'=>$order['uid'],'status'=>1,'ldurl'=>$order['ldurl']])->count('id');
|
||
if($count>1){
|
||
db('order')->where(['uid'=>$order['uid'],'out_trade_no'=>$order['out_trade_no']])->update(['is_complex'=>$count]);
|
||
}
|
||
|
||
if(empty($ispay)){
|
||
db('order')->where(['uid'=>$order['uid'],'out_trade_no'=>$order['out_trade_no']])->update(['ispay'=>2]);
|
||
}
|
||
|
||
}
|
||
|
||
public function aotucontr($payratio,$adid){
|
||
import("Controlaccout.Controlaccout", EXTEND_PATH,'.php');
|
||
$Controlaccout = new \Controlaccout($payratio['pageurl'],$adid);
|
||
$istim = time() - 1140;
|
||
if($istim<$payratio['intercept_time'] && $payratio['intercept']==1){
|
||
//系统19分钟内自动补了一笔需要拦截一笔
|
||
$logtxt = '系统自动补了一笔需要拦截一笔';
|
||
$trPayNum = 0;
|
||
$scale = 0;
|
||
db('transfer')->where('id',$payratio['id'])->update(['intercept'=>0]);
|
||
}else{
|
||
$planratio = db('planratio_set')->where(['tid'=>$payratio['id'],'adid'=>$adid])->find();
|
||
if(empty($planratio)){
|
||
$bidconf = $Controlaccout->bidconf;
|
||
$initialval = empty($bidconf['initial'])?0:$bidconf['initial'];
|
||
$planratio['id'] = db('planratio_set')->insertGetId([
|
||
'tid'=>$payratio['id'],
|
||
'adid'=>$adid,
|
||
'value'=>$initialval
|
||
]);
|
||
$planratio = db('planratio_set')->where('id',$planratio['id'])->find();
|
||
}
|
||
$logtxt = '';
|
||
//校验计划消耗是否达标
|
||
$ischeckexpend = $Controlaccout->checkexpend();
|
||
if($ischeckexpend){
|
||
$numberofpayments = $planratio['counts']+1;
|
||
$firstchargenum = $planratio['firstchargenum']+1;
|
||
db('planratio_set')->where(['id'=>$planratio['id']])->update([
|
||
'counts'=>$numberofpayments,
|
||
'update_time'=>time(),
|
||
'firstchargenum'=>$firstchargenum
|
||
]);
|
||
$trPayNum = $planratio['counts'];
|
||
$scale = $planratio['value'];
|
||
//判断手动模式 自动模式
|
||
if($payratio['controlaccout_open']){
|
||
$Controlaccout->checkfirstcharge($payratio['id']);
|
||
}
|
||
}else{
|
||
$logtxt = '今日计划消耗未达标';
|
||
$trPayNum = 0;
|
||
$scale = 0;
|
||
}
|
||
}
|
||
return array(
|
||
'logtxt'=>$logtxt,
|
||
'trPayNum'=>$trPayNum,
|
||
'scale'=>$scale
|
||
);
|
||
}
|
||
|
||
public function checkAdTd($uid,$adid,$tid){
|
||
$arr = array();
|
||
$is = db('planratio_set')->where('adid',$adid)->find();
|
||
if($is){
|
||
if($is['tid']==$tid){
|
||
$arr['status'] = 0;
|
||
}else{
|
||
$arr['status'] = 1;
|
||
$arr['tid'] = $is['tid'];
|
||
$channel = db('transfer')->where('id',$arr['tid'])->value('linkid');
|
||
$arr['channel'] = $channel;
|
||
db('member')->where('uid',$uid)->update(['channel'=>$channel,'ldurl'=>$arr['tid']]);
|
||
db('member_clickid')->where('uid',$uid)->update(['linkid'=>$channel,'ldurl'=>$arr['tid']]);
|
||
}
|
||
}else{
|
||
$arr['status'] = 0;
|
||
}
|
||
return $arr;
|
||
}
|
||
|
||
//到账处理
|
||
public function addaccount($uid,$data,$type,$vip){
|
||
if($type==1 || $type==3){
|
||
$egold = $data['egold'] + $data['giveegold'];
|
||
db('member')->where('uid',$uid)->update([
|
||
'pay' => ['exp','pay+1'],
|
||
'egold' => ['exp','egold+'.$egold]
|
||
]);
|
||
$this->chongzhilog($uid,1,'普通充值',$data['price'],$egold);
|
||
}elseif($type==2){
|
||
$end = $data['duration'] * 86400;
|
||
$end = (empty($vip) || $vip<time())?time()+$end:$vip+$end;
|
||
db('member')->where('uid',$uid)->update([
|
||
'pay'=> ['exp','pay+1'],
|
||
'is_vip'=>1,
|
||
'vip_enddate'=>$end
|
||
]);
|
||
$this->chongzhilog($uid,1,'VIP会员',$data['price'],0);
|
||
}
|
||
}
|
||
|
||
// 按百分比控制回传 参数:回传比例 充值次数 现在在用
|
||
public function needtwo($paym,$countpay){
|
||
if($paym<=0){
|
||
return false;
|
||
}
|
||
if ($paym>=100){
|
||
return true;
|
||
}
|
||
$mod = $countpay % 100;
|
||
$step = 100.0 / $paym;
|
||
for($i = 0;$i<100;$i+=$step){
|
||
$round = round($i);
|
||
if ($mod == $round) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
// 判断 间隔时间内是否超出了设置的回传限制
|
||
public function ishc($time,$num,$numlog){
|
||
if($time==0){
|
||
return true;
|
||
}else{
|
||
if($numlog>=$num){
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* createorder [创建订单]
|
||
*
|
||
* @param type $money [充值金额]
|
||
* @param type $type [充值种类]
|
||
* @param type $paytype [支付方式]
|
||
* @param type $order [订单号]
|
||
*
|
||
* @return type
|
||
**/
|
||
public function createorder($money,$type,$paytype,$order,$uid,$vid=0){
|
||
$user = db('member')->where('uid='.$uid)->find();
|
||
$data['ldurl'] = $user['ldurl'];
|
||
$data['out_trade_no'] = $order;
|
||
$data['paytype'] = $paytype;
|
||
$data['money'] = $money;
|
||
$data['type'] = $type;
|
||
$data['create_time'] = time();
|
||
$data['update_time'] = time();
|
||
$data['status'] = 0;
|
||
$data['uid'] = $uid;
|
||
$data['userType'] = $user['userType'];
|
||
$data['articleid'] = $vid;
|
||
$res = db('order')->insert($data);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* chongzhilog [增加充值记录]
|
||
*
|
||
* @param type $uid [desc]
|
||
* @param type $type [desc]
|
||
* @param type $title [desc]
|
||
* @param type $money [金额]
|
||
* @param type $egold [阅读币数量]
|
||
*
|
||
* @return type
|
||
**/
|
||
|
||
public function chongzhilog($uid,$type,$title,$money,$egold){
|
||
$wxid = Db::name('member')->where('uid',$uid)->value('wxid');
|
||
$log['wxid'] = $wxid;
|
||
$log['user_id'] =$uid;
|
||
$log['type'] =$type;
|
||
$log['title'] =$title;
|
||
$log['money'] =$money;
|
||
$log['shubi'] =$egold;
|
||
$log['created_at'] =time();
|
||
Db::name('chongzhi_log')->insert($log);
|
||
}
|
||
|
||
/******URL参数转换******/
|
||
public function convertUrlQuery($query){
|
||
$queryParts = explode('&', $query);
|
||
$params = array();
|
||
foreach ($queryParts as $param) {
|
||
$item = explode('=', $param);
|
||
$params[$item[0]] = $item[1];
|
||
}
|
||
return $params;
|
||
}
|
||
|
||
public function wrlog($txt){
|
||
$log = json_encode($txt);
|
||
$log .= ' 时间:'.date("Y-m-d H:i:s",time());
|
||
$log .= "\n";
|
||
@file_put_contents('log/Userverification.txt',$log,FILE_APPEND);
|
||
}
|
||
|
||
|
||
} |