You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

678 lines
24 KiB
PHP

<?php
namespace Callback\Controller;
use Think\Controller;
use Common\Api\GameApi;
use Org\UcenterSDK\Ucservice;
use Think\Log;
/**
* 支付回调控制器
* @author 小纯洁
*/
class BaseController extends Controller {
protected function _initialize(){
C(api('Config/lists'));
}
/**
* 购买成功后
* @author 鹿文学
*/
protected function set_order($data) {
$order = M('Order','tab_');
$map['order_number'] = $data['out_trade_no'];
$result_data = $order->where($map)->find();
if(empty($result_data)) {$this->record_logs("数据异常");return false;}
$other = $order->where(array('merchandise_id'=>$result_data['merchandise_id'],'pay_status'=>1))->find();
if(!empty($other)){
$datasave['pay_status'] = 1;
$datasave['is_refund'] = 2;
$res = $order->where($map)->save($datasave);
return false;
}
if($result_data['pay_status'] == 0) {
$data_save['pay_status'] = 1;
$data_save['trade_no'] = $data['trade_no'];
$data_save['pay_time'] = NOW_TIME;
$res = $order->where($map)->save($data_save);
if($res!== false) {
M('Merchandise','tab_')->where(['id'=>$result_data['merchandise_id']])->save(['status'=>4]);
M('user','tab_')->where(['id'=>$result_data['buyer_id']])->setInc("cumulative",$result_data['order_price']);
return true;
} else {
$this->record_logs("修改数据失败");
}
} else {
return true;
}
}
/**
*充值到游戏成功后修改充值状态和设置游戏币
*/
protected function set_spend($data){
$spend = M('Spend',"tab_");
$map['pay_order_number'] = $data['out_trade_no'];
$d = $spend->where($map)->find();
if(empty($d)){$this->record_logs("数据异常");return false;}
// 支付金额判断 by zgc
if (isset($data['money']) && $d['pay_amount'] != $data['money']) {
$this->record_logs("支付金额不一致".$data['out_trade_no'].":".$data['money']);return false;
}
if($d['pay_status'] == 0){
// 处理绑币平台币优先处理
if ($d['deduction_amount'] > 0) {
$out_trade_no = "PF_" . date('Ymd') . date('His') . sp_random_string(4);
$bind_balance = M('user_play', 'tab_')->where(array(
'user_id' => $d['user_id'],
'game_id' => $d['game_id']
))->getField('bind_balance');
$request = $d;
$request['pay_order_number'] = $out_trade_no;
$request['price'] = $bind_balance;
$request['deduction_amount'] = 0;
$request['pay_way'] = -1;
$request['pay_status'] = 1;
if ($d['deduction_amount'] > $bind_balance)
$bb = 0;
else
$bb = $bind_balance - $d['deduction_amount'];
if ($bind_balance > 0) {
M('user_play', 'tab_')->where(array(
'user_id' => $d['user_id'],
'game_id' => $d['game_id']
))->save(array(
'bind_balance' => $bb
));
$result = $this->add_spend($request);
$this->set_ratio($request['pay_order_number'], 2); // 游戏返利
}
if ($bb == 0) {
$mbalance = $d['deduction_amount'] - $bind_balance;
if ($mbalance > 0) {
$request['pay_way'] = 0;
$request['pay_order_number'] = $request['pay_order_number']."_1";
$request['price'] = $mbalance;
$balance = M('user', 'tab_')->where(array(
'id' => $d['user_id'],
))->getField('balance');
if ($balance < $mbalance) { // 异常
$this->record_logs($mbalance."平台币余额不足".$balance);return false;
}
M('user', 'tab_')->where(array(
'id' => $d['user_id']
))->save(array(
'balance' => $balance - $mbalance,
));
$result = $this->add_spend($request);
$this->set_ratio($request['pay_order_number'], 2); // 游戏返利
}
}
}
$data_save['pay_status'] = 1;
$data_save['order_number'] = $data['trade_no'];
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $spend->where($map_s)->save($data_save);
$this->set_ratio($d['pay_order_number']);
//APP邀请好友消费奖励平台币
$this->inviteFriendAward($d['user_id'],$d['pay_amount'],$data['out_trade_no']);
//充值奖励积分
$this->rechargeAwardPoint($d['user_id'],$d['pay_amount']);
//累计消费
$this->changeCumulative('Spend',$d['pay_order_number']);
if($r!== false){
$game = new GameApi();
$game->game_pay_notify($data,1);
return true;
}else{
$this->record_logs("修改数据失败");
}
}
else{
return true;
}
}
/**
*充值平台币成功后的设置
*/
protected function set_deposit($data){
$deposit = M('deposit',"tab_");
$map['pay_order_number'] = $data['out_trade_no'];
$d = $deposit->where($map)->find();
if(empty($d)){return false;}
if($d['pay_status'] == 0){
$data_save['pay_status'] = 1;
$data_save['order_number'] = $data['trade_no'];
$data_save['check_sign'] = get_check_sign($d['user_id'],$data['trade_no'],$d['pay_amount'],$d['create_time']);
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $deposit->where($map_s)->save($data_save);
if($r !== false){
$user = M("user","tab_");
$user->where("id=".$d['user_id'])->setInc("balance",$d['pay_amount']);
$user->where("id=".$d['user_id'])->setInc("cumulative",$d['pay_amount']);
//APP邀请好友消费奖励平台币
$this->inviteFriendAward($d['user_id'],$d['pay_amount'],$data['out_trade_no']);
//充值奖励积分
$this->rechargeAwardPoint($d['user_id'],$d['pay_amount']);
}else{
$this->record_logs("修改数据失败");
}
return true;
}
else{
return true;
}
}
/**
* 充值推广平台币
*/
protected function set_balance($data){
$balance = M("balance", "tab_");
$map['pay_order_number'] = $data['out_trade_no'];
$orderinfo = $balance->where($map)->find();
if($orderinfo['pay_status'] == 0){
$data_save['pay_status'] = 1;
$data_save['order_number'] = $data['trade_no'];
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $balance->where($map_s)->save($data_save);
if($r!== false){
$promote = M("promote","tab_");
$map['account'] = $orderinfo['recharge_account'];
$promote->where($map)->setInc("balance_coin",$orderinfo['money']);
}else{
$this->record_logs("修改数据失败");
}
return true;
}else{
return true;
}
}
/**
*设置代充数据信息
*/
protected function set_agent($data){
$agent = M("agent","tab_");
$map['pay_order_number'] = $data['out_trade_no'];
$d = $agent->where($map)->find();
if(empty($d)){return false;}
if($d['pay_status'] == 0){
$data_save['pay_status'] = 1;
$data_save['order_number'] = $data['trade_no'];
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $agent->where($map_s)->save($data_save);
if($r!== false){
$user = M("user_play","tab_");
$map_play['user_id'] = $d['user_id'];
$map_play['game_id'] = $d['game_id'];
$res = $user->where($map_play)->setInc("bind_balance",$d['amount']);
}else{
$this->record_logs("修改数据失败");
}
return true;
}
else{
return true;
}
}
/*
* 超级签支付成功通知验证后调用
* return 1订单不存在 2已支付 0成功 -1数据错误
*/
protected function supersign_pay_notify($data) {
$order = M('game_supersign', 'tab_')->where(array(
'order_id' => $data['order_id']
))->find();
if (!$order) return 0;
if ($order['pay_status'] == 1) return 0;
$r = M('game_supersign', 'tab_')->where(array(
'order_id' => $data['order_id'],
'pay_status' => 0,
))->save(array(
'trade_id' => $data['trade_id'],
'pay_status' => 1,
'pay_way'=>$data['pay_way'],
'pay_time' => time()
));
if (!$r) return -1;
return 1;
}
/**
* 绑币充值回调
* @param $data
* @return bool
* author: xmy 280564871@qq.com
*/
protected function set_bind_recharge($data)
{
$bind_recharge = M("bind_recharge", "tab_");
$map['pay_order_number'] = $data['out_trade_no'];
$d = $bind_recharge->where($map)->find();
if (empty($d)) {
return false;
}
if ($d['pay_status'] == 0) {
$data_save['pay_status'] = 1;
$data_save['order_number'] = $data['trade_no'];
$data_save['check_sign'] = get_check_sign($d['user_id'],$data['trade_no'],$d['amount'],$d['create_time']);
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $bind_recharge->where($map_s)->save($data_save);
if ($r !== false) {
$user = M("user_play", "tab_");
$map_play['user_id'] = $d['user_id'];
$map_play['game_id'] = $d['game_id'];
$res = $user->where($map_play)->setInc("bind_balance", $d['amount']);
//APP邀请好友消费奖励平台币
$this->inviteFriendAward($d['user_id'],$d['real_amount'],$data['out_trade_no']);
//充值奖励积分
$this->rechargeAwardPoint($d['user_id'],$d['real_amount']);
} else {
$this->record_logs("修改数据失败");
}
return true;
} else {
return true;
}
}
/**
* 获取奖励平台币 总额
* @param $invite_id
* @param $user_id
* @return mixed
* author: xmy 280564871@qq.com
*/
protected function getUserInviteCoin($invite_id,$user_id){
$tab_sha=M('share_record','tab_');
$map['invite_id'] = $invite_id;
$map['user_id'] = $user_id;
$data = $tab_sha->field("sum(award_coin) as award_coin")
->where($map)
->group("user_id")
->find();
return $data['award_coin'];
}
/**
* 邀请好友消费奖励平台币
* @param $user_id
* @param $pay_amount
* @param $order_number
* @return bool
* author: xmy 280564871@qq.com
*/
protected function inviteFriendAward($user_id,$pay_amount,$order_number){
$tab_sha=M('share_record','tab_');
$map['user_id'] = $user_id;
$share_record = $tab_sha->where($map)->find();
if(empty($share_record)){
return true;
}
$invite_id = $share_record['invite_id'];
//计算奖励
$award_coin = round($pay_amount * 0.05,2);
//增加邀请用户 平台币
$invite = M("user","tab_")->find($invite_id);//邀请人
//获取该邀请人共获得多少平台币
$total = $this->getUserInviteCoin($invite_id,$user_id);
//是否到达上限
if($total >= 100){
return true;
}
if($total+$award_coin > 100){
$award_coin = 100 - $total;
}
//$award_coin=99;
$invite['balance'] += $award_coin;
//奖励平台币记录
$share_record['create_time'] = time();
$share_record['award_coin'] = $award_coin;
$share_record['order_number'] = $order_number;
unset($share_record['id']);
//开启事务
$tab_sha->startTrans();
$record_result = $tab_sha->add($share_record);//平台币奖励记录
$invite_result = M("user","tab_")->save($invite);//邀请人平台币存储
if($record_result === false || $invite_result === false){
$tab_sha->rollback();
return false;
}else{
$tab_sha->commit();
return true;
}
}
/**
* 获取积分方式
* @param $key
* @return bool|mixed
* author: xmy 280564871@qq.com
*/
public function getPointType($key){
$map['key'] = $key;
$map['status'] = 1;
$type = M("point_type","tab_")->where($map)->find();
if(empty($type)){
// $this->error = "此奖励不存在或被禁用";
return false;
}
return $type;
}
/**
* 充值获得积分
* @param $user_id
* @param $pay_amount
* @return bool
* author: xmy 280564871@qq.com
*/
protected function rechargeAwardPoint($user_id,$pay_amount){
$point_recorddd=M('point_record','tab_');
$user = M("user","tab_")->find($user_id);
if(empty($user_id)){
return true;
}
//奖励用户积分
$point_type = $this->getPointType("recharge_spend");
$point = intval($point_type['point'] * $pay_amount);
$point_recorddd->startTrans();
if($point > 0){
$user['point'] += $point;
//积分记录
$data['user_id'] = $user_id;
$data['type_id'] = $point_type['id'];
$data['point'] = $point;
$data['create_time'] = time();
$data['type'] = 1;
$point_result = M("point_record","tab_")->add($data);//积分记录存储
}
$user_result = M("user","tab_")->save($user);//被邀请人积分存储
if($point_result === false || $user_result === false){
$point_recorddd->rollback();
return false;
}else{
$point_recorddd->commit();
return true;
}
}
/**
*游戏返利
*/
protected function set_ratio($data)
{
$map['pay_order_number']=$data;
$spend=M("Spend","tab_")->where($map)->find();
$reb_map['game_id']=$spend['game_id'];
$time = time();
$reb_map['starttime'] = ['lt',$time];
$reb_map_str = "endtime > {$time} or endtime = 0";
if($spend['promote_id'] == 0){
$reb_map['promote_id'] = ['in','0,-1'];
}else{
$reb_map['promote_id'] = ['neq',0];
}
$rebate=M("Rebate","tab_")->where($reb_map)->where($reb_map_str)->find();
if (!empty($rebate)) {
if ($rebate['money'] > 0 && $rebate['status'] == 1) {
if ($spend['pay_amount'] >= $rebate['money']) {
$this->compute($spend, $rebate);
} else {
return false;
}
} else {
$this->compute($spend, $rebate);
}
}else {
return false;
}
}
//计算返利
protected function compute($spend,$rebate){
$user_map['user_id']=$spend['user_id'];
$user_map['game_id']=$spend['game_id'];
$bind_balance=$spend['pay_amount']*($rebate['ratio']/100);
$spend['ratio']=$rebate['ratio'];
$spend['ratio_amount']=$bind_balance;
M("rebate_list","tab_")->add($this->add_rebate_list($spend));
$re=M("UserPlay","tab_")->where($user_map)->setInc("bind_balance",$bind_balance);
return $re;
}
/**
*返利记录
*/
protected function add_rebate_list($data){
$add['pay_order_number']=$data['pay_order_number'];
$add['game_id']=$data['game_id'];
$add['game_name']=$data['game_name'];
$add['user_id']=$data['user_id'];
$add['user_name'] = $data['user_account'];
$add['pay_amount']=$data['pay_amount'];
$add['ratio']=$data['ratio'];
$add['ratio_amount']=$data['ratio_amount'];
$add['promote_id']=$data['promote_id'];
$add['promote_name']=$data['promote_account'];
$add['create_time']=time();
return $add;
}
/**
* 累计消费
* lcj
*/
protected function changeCumulative($table,$order=''){
$table = M($table,'tab_');
$data = $table->where(array('pay_order_number'=>$order))->find();
if($data){
//平台币 绑币不累计消费
if($data['pay_way']>0){
$user = M('user','tab_');
$user->where(array('id'=>$data['user_id']))->setInc("cumulative",$data['pay_amount']);
//小号充值则大号小号一起累计
if($data['small_id']>0){
$user->where(array('id'=>$data['small_id']))->setInc("cumulative",$data['pay_amount']);
}
}
}else{
return false;
}
}
/**
*日志记录
*/
protected function record_logs($msg=""){
\Think\Log::record($msg);
}
protected function wite_text($txt,$name){
$myfile = fopen($name, "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
}
/**
*消费表添加数据
*/
public function add_spend($data,$type=0){
$spend = M("spend","tab_");
$spend_data = $this->spend_param($data);
$ordercheck = $spend->where(array('pay_order_number'=>$spend_data["pay_order_number"]))->find();
if($ordercheck)
{
if($type==1){
redirect(U('Spend/notice',array('user_id'=>$ordercheck['user_id'],'game_id'=>$ordercheck['game_id'],'msg'=>'订单已经存在,请刷新充值页面重新下单!')));exit;
}else{
$this->set_message(0,'fail',"订单已经存在,请刷新充值页面重新下单!");
}
}
// if(!is_check_apply_promote($spend_data['game_id'],$spend_data['promote_id'])){
// $spend_data['promote_id']=0;
// $spend_data['promote_account']="自然注册";
// }
$result = $spend->add($spend_data);
return $result;
}
/**
*消费记录表 参数
*/
private function spend_param($param=array()){
$user_entity = get_user_entity($param['user_id']);
$data_spned['user_id'] = $param["user_id"];
$data_spned['user_account'] = $user_entity["account"];
$data_spned['user_nickname'] = $user_entity["nickname"];
$data_spned['game_id'] = $param["game_id"];
$data_spned['game_appid'] = $param["game_appid"];
$data_spned['game_name'] = get_game_name($param["game_id"]);
$data_spned['selle_ratio'] = get_game_selle_ratio($param["game_id"]);
$data_spned['server_id'] = $param["server_id"];
$data_spned['server_name'] = $param["server_name"];
$data_spned['game_player_name'] = $param["game_player_name"];
$data_spned['promote_id'] = $user_entity["promote_id"];
$data_spned['promote_account'] = $user_entity["promote_account"];
$data_spned['order_number'] = $param["order_number"];
$data_spned['pay_order_number'] = $param["pay_order_number"];
$data_spned['props_name'] = $param["title"];
$data_spned['cost'] = $param["price"];//原价
$data_spned['deduction_amount'] = $param["deduction_amount"];// 抵扣价格
$data_spned['discount_amount'] = $param["discount_amount"];// 折扣价格
$data_spned['pay_time'] = NOW_TIME;
$data_spned['pay_status'] = $param["pay_status"];
$data_spned['pay_game_status'] = 0;
$data_spned['extend'] = $param['extend'];
$data_spned['pay_way'] = $param["pay_way"];
if($data_spned['pay_way'] != 7){
$discount = $this->get_discount($param['game_id'],$user_entity['promote_id'],$param['user_id']);
$data_spned['pay_amount'] = $param["price"] * $discount['discount']/10;//实付金额
$data_spned['discount_type'] = $discount['discount_type'];
}else{//苹果支付不计入折扣
$data_spned['pay_amount'] = $param["price"];
$data_spned['discount_type'] = 0;
}
if (isset($param['actual_amount']) && $param['actual_amount'])
$data_spned['pay_amount'] = $param['actual_amount']; // 实际支付价格
$data_spned['spend_ip'] = $param["spend_ip"];
$data_spned['sdk_version'] = $param["sdk_version"];
if($param['small_user_id'] != $param['user_id']) {
$data_spned['small_id'] = $param["small_user_id"];
$data_spned['small_account'] = get_user_account_by_id($param['small_user_id']);
}
return $data_spned;
}
/**
* 获取用户折扣
* @param $game_id 游戏ID
* @param $promote_id 渠道ID
* @param $user_id 用户ID
* @return mixed
*/
protected function get_discount($game_id,$promote_id,$user_id){
//获取折扣
$map['game_id'] = $game_id;
$map['promote_id'] =-1;
$map['status'] = 2;
$discount = M('Promote_welfare','tab_')->where($map)->find();
if(null==$discount){
$map['game_id'] = $game_id;
$map['promote_id'] = $promote_id == 0 ? 0 : -2;
$map['status'] = 2;
$discount = M('Promote_welfare','tab_')->where($map)->find();
}
$discount = discount_data($discount);
if(empty($discount)){
$res['discount'] = 10;
$res['discount_type'] = 0;//无折扣
return $res;
}
//判断用户是否为首冲
$where['game_id'] = $game_id;
$where['user_id'] = $user_id;
$where['pay_status'] = 1;
$data = M('bind_spend','tab_')->where($where)->find();
if(!empty($data) || !empty(M('spend','tab_')->where($where)->find())){
$res['discount'] = $discount['continue_discount'];//续冲
$res['discount_type'] = 2;
}else{
$res['discount'] = $discount['first_discount'];//首冲
$res['discount_type'] = 1;
}
return $res;
}
/**
*游戏返利
*$type 类型 1 非绑定平台币 2绑定平台币
*/
/* public function set_ratio($data,$type = 1){
$map['pay_order_number']=$data;
$map['pay_way'] = ($type==2) ? -1 : 0; // 判断支付类型
$spend=M("Spend","tab_")->where($map)->find();
$reb_map['game_id']=$spend['game_id'];
$time = time();
$reb_map['starttime'] = ['lt',$time];
$reb_map_str = "endtime > {$time} or endtime = 0";
switch ($spend['promote_id']) {
case 0:
$reb_map['promote_id'] = array('in','0,-1');
break;
default:
$reb_map['promote_id'] = array('in','1,-1');
break;
}
//如果是绑币则判断是否开启绑币返利
if($type == 2){$reb_map['bind_status'] = 1;}
$rebate=M("Rebate","tab_")->where($reb_map)->where($reb_map_str)->find();//
if (!empty($rebate)) {
if($rebate['money']>0 && $rebate['status']==1){
if($spend['pay_amount']>=$rebate['money']){
$this->compute($spend,$rebate);
}else{
return false;
}
}else{
$this->compute($spend,$rebate);
}
}else{
return false;
}
} */
}