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.

986 lines
37 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Callback\Controller;
use Think\Controller;
use Common\Api\GameApi;
use Org\UcenterSDK\Ucservice;
use Think\Log;
use Sdk\Controller\BaseController as sdk;
use Sdk\Model\PayChannelIntentionModel;
use Base\Service\PromoteService;
/**
* 支付回调控制器
* @author 小纯洁
*/
class BaseController extends Controller
{
private $promoteService;
protected function _initialize()
{
C(api('Config/lists'));
C(sdk::get_tool());
$this->promoteService = new PromoteService();
}
/**
* 购买成功后
* @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;
}
$valueDetailLogModel = new \Sdk\Model\ValueDetailLogModel();
if($d['pay_status'] == 0){
// 折扣优先处理
if ($d['discount_amount'] > 0) { // 折扣金额大于0
$balance = M('user', 'tab_')->where(array(
'id' => $d['user_id'],
))->getField('balance');
$di = discountInfo($d['game_id']);
if (!$di || $di['status'] != 1 || $di['start_time'] > time() || $di['end_time'] < time()) {
$this->record_logs($d['discount_amount'] . "折扣失效,退还平台币" . serialize($di));
$createTime = time();
$depositId = M('deposit', "tab_")->add([
'order_number' => $data['trade_no'],
'pay_order_number' => 'PF_' . $data['out_trade_no'],
'user_id' => $d['user_id'],
'user_account' => $d['user_account'],
'user_nickname' => $d['user_nickname'],
'promote_id' => $d['promote_id'],
'promote_account' => $d['promote_account'],
'pay_amount' => $d['pay_amount'],
'pay_status' => 1,
'pay_way' => $d['pay_way'],
'pay_source' => 2, // 0官网 1推广后台 2移动端
'pay_ip' => $d['spend_ip'],
'create_time' => $createTime,
'payed_time' => time(),
'sdk_version' => $d['sdk_version'],
'check_sign' => get_check_sign($d['user_id'], $data['trade_no'], $d['pay_amount'], $createTime),
]);
$valueDetailLogModel->addLog($d['user_id'], 'PF_' . $data['out_trade_no'],
$balance, $d['pay_amount'], $balance + $d['pay_amount'], $valueDetailLogModel::BALANCE, "游戏支付时候折扣失效,增加到平台币", 2);
$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'], 'PF_' . $data['out_trade_no']);
//充值奖励积分
$this->rechargeAwardPoint($d['user_id'], $d['pay_amount']);
// 当天累计充值
$this->payTodayTotal($d['pay_amount'], $d['pay_way']);
$data_save['pay_status'] = 0;
$data_save['order_number'] = $data['trade_no'];
$data_save['payed_time'] = time();
$data_save['pay_time'] = $data_save['payed_time'];
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $spend->where($map_s)->save($data_save);
return false;
}
}
// 处理绑币平台币
if ($d['deduction_amount'] > 0) {
$bind_balance = M('user_play', 'tab_')->where(array(
'user_id' => $d['user_id'],
'game_id' => $d['game_id']
))->getField('bind_balance');
$balance = M('user', 'tab_')->where(array(
'id' => $d['user_id'],
))->getField('balance');
if ($balance+$bind_balance < $d['deduction_amount']) { // 异常 用户充值支付宝不支付 然后重新发起一笔订单支付 在回到支付宝找未支付订单支付 这种情况 加到平台币
$this->record_logs($d['deduction_amount']."平台币余额不足--》用户充值支付宝不支付 然后重新发起一笔订单支付 在回到支付宝找未支付订单支付 这种情况 加到平台币".$balance);
$createTime = time();
$depositId = M('deposit',"tab_")->add([
'order_number' => $data['trade_no'],
'pay_order_number' => 'PF_'.$data['out_trade_no'],
'user_id' => $d['user_id'],
'user_account' => $d['user_account'],
'user_nickname' => $d['user_nickname'],
'promote_id' => $d['promote_id'],
'promote_account' => $d['promote_account'],
'pay_amount' => $d['pay_amount'],
'pay_status' => 1,
'pay_way' => $d['pay_way'],
'pay_source' => 2, // 0官网 1推广后台 2移动端
'pay_ip' => $d['spend_ip'],
'create_time' => $createTime,
'payed_time' => time(),
'sdk_version' => $d['sdk_version'],
'check_sign' => get_check_sign($d['user_id'],$data['trade_no'],$d['pay_amount'],$createTime),
]);
$valueDetailLogModel->addLog($d['user_id'], 'PF_'.$data['out_trade_no'],
$balance, $d['pay_amount'], $balance+$d['pay_amount'], $valueDetailLogModel::BALANCE, "游戏支付时候抵扣金额不足,增加到平台币", 2);
$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'], 'PF_'.$data['out_trade_no']);
//充值奖励积分
$this->rechargeAwardPoint($d['user_id'],$d['pay_amount']);
// 当天累计充值
$this->payTodayTotal($d['pay_amount'], $d['pay_way']);
$data_save['pay_status'] = 0;
$data_save['order_number'] = $data['trade_no'];
$data_save['payed_time'] = time();
$data_save['pay_time'] = $data_save['payed_time'];
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $spend->where($map_s)->save($data_save);
return false;
}
$notice_order = []; // 需要通知更新的订单号
// 优先扣除绑定币
$out_trade_no = "PF_" . date('Ymd') . date('His') . sp_random_string(4);
$request = $d;
$request['discount_amount'] = 0;
$request['pay_order_number'] = $out_trade_no;
$request['price'] = $bind_balance;
$request['deduction_amount'] = 0;
$request['pay_status'] = 1;
$request["title"] = $d['props_name'];
$request['payed_time'] = time();
$request['pay_time'] = $request['payed_time'];
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
));
$request['pay_way'] = -1;
$result = $this->add_spend($request);
$this->set_ratio($request['pay_order_number'], 2); // 游戏返利
$notice_order[] = $request['pay_order_number'];
/* $valueDetailLogModel->addLog($d['user_id'], $request['pay_order_number'],
$bind_balance, -($bind_balance-$bb), $bb, $valueDetailLogModel::BANDBI, "绑币抵扣", 1);
*/
}
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;
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); // 游戏返利
$notice_order[] = $request['pay_order_number'];
$valueDetailLogModel->addLog($d['user_id'], $request['pay_order_number'],
$balance, -($mbalance), $balance - $mbalance, $valueDetailLogModel::BALANCE, "平台币抵扣", 1);
}
}
}
$data_save['pay_status'] = 1;
$data_save['order_number'] = $data['trade_no'];
$data_save['payed_time'] = time();
$data_save['pay_time'] = $data_save['payed_time'];
$map_s['pay_order_number'] = $data['out_trade_no'];
$r = $spend->where($map_s)->save($data_save);
// spend 记录传给通知cp 不直接读
$pay_data = $d;
$pay_data['pay_status'] = $data_save['pay_status'];
$pay_data['order_number'] = $data_save['order_number'];
$pay_data['payed_time'] = $data_save['payed_time'];
$pay_data['pay_time'] = $pay_data['payed_time'];
// 更新意向表支付成功记录
$payChannelIntention = new PayChannelIntentionModel();
$payChannelIntention->updatePaySuccess($data['out_trade_no']);
$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']);
// 当天累计充值
$this->payTodayTotal($d['pay_amount'], $d['pay_way']);
if($r!== false){
// 通知后台 TASK_URLhttp://47.111.118.107:9501/game-event/recharge
$url = C('TASK_URL')."/game-event/recharge";
try {
post_async($url, [
'form_params' => [
'pay_order_number' => $map_s['pay_order_number']
]
]);
} catch(\Exception $e) {
}
$game = new GameApi();
$r = $game->game_pay_notify($data, 1, 0, $pay_data);
if ($r == 'success') {
// 更新意向表游戏通知成功记录
$payChannelIntention->updateGameNotifySuccess($data['out_trade_no']);
foreach ($notice_order as $v) {
$this->record_logs("game_notice_cp:".$v);
$result = M('spend',"tab_")->where([
'pay_order_number' => $v,
'pay_status' => 1
])->setField([
'pay_game_status' => 1
]);
// 通知后台
try {
post_async($url, [
'form_params' => [
'pay_order_number' => $v
],
]);
} catch(\Exception $e) {
}
}
}
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();
// 支付金额判断 by zgc
if (isset($data['money']) && $d['pay_amount'] != $data['money']) {
$this->record_logs("支付金额不一致".$data['out_trade_no'].":".$data['money']);return false;
}
if(empty($d)){return false;}
if($d['pay_status'] == 0){
$data_save['pay_status'] = 1;
$data_save['payed_time'] = time();
$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_");
$userInfo = $user->where(array('id' => $d['user_id']))->find();
$before = $userInfo['balance'];
$after = $before + $d['pay_amount'];
$valueDetailLogModel = new \Sdk\Model\ValueDetailLogModel();
$valueDetailLogModel->addLog($d['user_id'], $data['out_trade_no'], $before, $d['pay_amount'], $after, $valueDetailLogModel::BALANCE, "平台币充值".$d['pay_amount'], 2);
// 更新意向表支付成功记录
$payChannelIntention = new PayChannelIntentionModel();
$payChannelIntention->updatePaySuccess($data['out_trade_no']);
$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']);
// 当天累计充值
$this->payTodayTotal($d['pay_amount'], $d['pay_way']);
}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();
$user_result = false;
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_")->where(array(
'id' => $user_id
))->setInc("point", $point);//被邀请人积分存储
}
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;
}
}
/*
* 当日累计充值
*/
public function payTodayTotal($price, $pay_way) {
$table = M('pay_today_total', 'tab_');
$date = date('Ymd');
$data = $table->where(array(
'pay_date' => $date,
'pay_way' => $pay_way,
))->find();
if($data){
$table->where(array(
'pay_date' => $date,
'pay_way' => $pay_way,
))->setInc("price_cnt", $price);
}else{
$table->add(array(
'pay_way' => $pay_way,
'price_cnt' => $price,
'pay_date' => date('Ymd'),
));
}
}
/**
*日志记录
*/
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;
}
public function getPromoteById($promoteId)
{
return $this->promoteService->getPromoteById($promoteId, ['top']);
}
public function getMarketAdminId($promote)
{
return $this->promoteService->getMarketAdminIdByPromote($promote);
}
public function getIsCheckValue($promote, $gameId)
{
if (is_null($promote)) {
return 1;
}
if ($this->promoteService->isGameIdSettle($promote, $gameId)) {
return 1;
} else {
return 2;
}
}
/**
*消费记录表 参数
*/
private function spend_param($param=array()){
$user_entity = get_user_entity($param['user_id']);
$promote = $this->getPromoteById($user_entity["promote_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_id'] = $param["game_player_id"];
$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['spend_time'] = NOW_TIME;
$data_spned['payed_time'] = $data_spned['pay_time'];// isset($param["payed_time"])?$param["payed_time"]:0;
$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"];
$data_spned['market_admin_id'] = $this->getMarketAdminId($promote);
$data_spned['is_check'] = $this->getIsCheckValue($promote, $param["game_id"]);
if($data_spned['pay_way'] != 7){
$discount = $this->get_discount($param['game_id'],$user_entity['promote_id'], $param["game_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;
}
} */
// 根据订单号 获取支付配置
protected function getPayConfByOrderId($orderNo) {
$order = array();
if (substr($orderNo, 0, 2) == "SP") {
$order = M('spend', 'tab_')->where([
'pay_order_number' => $orderNo
])->find();
} else {
$order = M('despo', 'tab_')->where([
'pay_order_number' => $orderNo
])->find();
}
if (!$order) {
return array(
'code' => 1,
'msg' => "订单号不存在pay_order_number".$orderNo,
'data' => array()
);
}
$payConf = M('payment_merchant', 'tab_')->where([
'id' => $order['merchant_id']
])->find();
if (!$payConf) {
return array(
'code' => 1,
'msg' => "找不到支付配置merchant_id".$order['merchant_id'],
'data' => array()
);
}
$payConf = json_decode($payConf['config'], true);
return array(
'code' => 0,
'msg' => '',
'data' => $payConf,
);
}
// 根据商户号 获取支付配置
protected function getPayConfByIdentifier($identifier) {
$payConf = M('payment_merchant', 'tab_')->where([
'identifier' => $identifier
])->find();
if (!$payConf) {
return array(
'code' => 1,
'msg' => "找不到支付配置identifier".$identifier,
'data' => array()
);
}
$payConf = json_decode($payConf['config'], true);
return array(
'code' => 0,
'msg' => '',
'data' => $payConf,
);
}
}