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.

467 lines
16 KiB
PHP

<?php
namespace Callback\Controller;
use Think\Controller;
use Common\Api\GameApi;
use Org\UcenterSDK\Ucservice;
/**
* 支付回调控制器
* @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 ($d['pay_amount'] != $data['money']) {
$this->record_logs("支付金额不一致".$data['out_trade_no'].":".$data['money']);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 = $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_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);
}
}