123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use addons\epay\library\Service;
- /**
- * 订单支付回调
- */
- class Notify extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- //充值金币 异步回调对外方法
- public function recharge_notify_base(){
- //验签
- $paytype = input('paytype','wechat');
- $notify_file = $this->notify_log_start($paytype);
- $pay = Service::checkNotify($paytype);
- if (!$pay) {
- echo '签名错误';
- exit;
- }
- //验证,拿订单号等信息
- $data = $pay->verify();
- $out_trade_no = $data['out_trade_no'];
- //订单查询
- $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
- if(empty($info)){
- echo $pay->success();
- exit;
- }
- if($info['order_status'] != 0)
- {
- echo $pay->success();
- exit;
- }
- //你可以在此编写订单逻辑
- $rs = $this->recharge_notify_do($out_trade_no);
- if($rs === false){
- //不论结果都应返回success
- echo $pay->success();
- exit;
- }else{
- //不论结果都应返回success
- echo $pay->success();
- exit;
- }
- //默认
- echo $pay->success();
- exit;
- }
- //充值金币 逻辑
- private function recharge_notify_do($out_trade_no){
- Db::startTrans();
- $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
- if (empty($orderInfo)) {
- Db::rollback();
- return false;
- }
- if($orderInfo['order_status'] != 0){
- Db::rollback();
- return false;
- }
- //逻辑开始
- $args = json_decode($orderInfo['args'],true);
- $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$orderInfo['id']);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- //上供到上级
- //逻辑结束
- //状态
- $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
- if($ros === false) {
- Db::rollback();
- return false;
- }
- //默认提交
- Db::commit();
- return true;
- }
- public function test(){
- // $this->takecash(5,10000,'take_cash',1);
- // $this->shouyi(1,10000,'user_match_audio_log',1);
- // $this->shouyi(2,10000,'user_match_audio_log',1);
- // $this->shouyi(3,10000,'user_match_audio_log',1);
- // $this->recharge(5,10000,'',1);
- // $this->recharge(4,10000,'',1);
- // $this->recharge(3,10000,'',1);
- // $this->recharge(2,10000,'',1);
- // $this->recharge(1,10000,'',1);
- }
- //已搬走,后台提现审核通过的方法 admin/takecash/takecash
- //普通邀请关系,提现
- /*public function takecash($user_id,$gold,$table,$table_id){
- }*/
- //已搬走,lavarel,alltools类
- //四大收益,视频,音频,私聊,礼物
- public function shouyi($user_id,$gold,$table,$table_id){
- //精确小数点
- bcscale(2);
- //枚举
- $table_enum = [
- 'user_match_audio_log' => [
- 'prefix'=>'audio',
- 'remark' =>'语音',
- ],
- 'user_match_typing_log' => [
- 'prefix'=>'typing',
- 'remark' =>'私聊',
- ],
- 'user_match_video_log' => [
- 'prefix'=>'video',
- 'remark' =>'视频',
- ],
- 'gift_user_typing' => [
- 'prefix'=>'gift',
- 'remark' =>'收礼',
- ],
- ];
- //变量前缀,钱包备注
- $prefix = $table_enum[$table]['prefix'];
- $remark = $table_enum[$table]['remark'];
- //收益者本人
- $userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$user_id)->find();
- //先确认自己的等级
- if($userinfo['group_id'] == 1){
- //充值者上级
- $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
- if(empty($intro_userinfo)){return true;}
- //上级是二级邀请B1,贡献给A
- if($intro_userinfo['group_id'] == 2){
- //充值者上上级,也就是A
- $intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$intro_userinfo['agent_id'])->find();
- if(empty($intro_intro_userinfo)){return true;}
- if($intro_intro_userinfo['group_id'] == 3){
- $shouyi_c2a_rate = config('site.shouyi_c2a_rate');
- $jewelA = bcdiv(bcmul($gold,$shouyi_c2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- }
- return true;
- }
- //上级是A
- if($intro_userinfo['group_id'] == 3){
- $shouyi_b2a_rate = config('site.shouyi_b2a_rate');//b2a 与 b02a相等
- $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- return true;//结束了
- }
- }
- if($userinfo['group_id'] == 2){
- //充值者上级
- $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
- if(empty($intro_userinfo)){return true;}
- //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
- /*if($intro_userinfo['group_id'] == 3){//不是返给一级代理,而是给上级,因为可能和上级解绑放到自己名下了 */
- $shouyi_b2a_rate = config('site.shouyi_b2a_rate');//b2a 与 b02a相等
- $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- return true;//结束了
- /*}*/
- }
- //没有推荐人,自己等级最高
- if($userinfo['group_id'] == 3 && !empty($userinfo['intro_uid'])){
- //充值者上级
- $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
- if(empty($intro_userinfo)){return true;}
- $shouyi_b2a_rate = config('site.shouyi_b2a_rate');
- $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- return true;//结束了
- }
- return true;
- }
- //已搬走,lavarel,alltools类
- //充值后奖金
- public function recharge($user_id,$gold,$table,$payorder_id){
- //充值者本人
- $userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$user_id)->find();
- //个人直推邀请充值
- if(!empty($userinfo['invite_uid']) && $userinfo['gender'] == 1 && empty($userinfo['intro_uid'])){
- $recharge_invite_rate = config('site.invite_recharge_rate');
- $jewel = bcdiv(bcmul($gold,$recharge_invite_rate,1),100,1);
- $result = model('Wallet')->lockChangeAccountRemain($userinfo['invite_uid'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- }
- //没有推荐人,女性充值不返
- if(empty($userinfo['intro_uid']) || $userinfo['gender'] == 0){
- return true;
- }
- //精确小数点
- bcscale(2);
- //自己等级最高
- if($userinfo['group_id'] == 3 && !empty($userinfo['intro_uid'])){
- //充值者上级
- $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
- if(empty($intro_userinfo)){return true;}
- $recharge_b2a_rate = config('site.recharge_b2a_rate');
- $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- return true;//结束了
- }
- if($userinfo['group_id'] == 2){
- //充值者上级
- $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
- if(empty($intro_userinfo)){return true;}
- //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
- /*if($intro_userinfo['group_id'] == 3){*/
- $recharge_b2a_rate = config('site.recharge_b2a_rate');//b2a 与 b02a相等
- $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- return true;//结束了
- /*}*/
- }
- if($userinfo['group_id'] == 1){
- //充值者上级
- $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
- if(empty($intro_userinfo)){return true;}
- //上级是二级邀请B1,本人C1,贡献给B和A(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
- if($intro_userinfo['group_id'] == 2){
- //充值者上上级,也就是A
- $intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$intro_userinfo['agent_id'])->find();
- if(empty($intro_intro_userinfo)){return true;}
- if($intro_intro_userinfo['group_id'] == 3){
- $recharge_c2a_rate = config('site.recharge_c2a_rate');
- $jewelA = bcdiv(bcmul($gold,$recharge_c2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- }
- return true;
- }
- //上级是普通用户,那自己肯定也是普通,则上级可能是D1、C1,找到上上级,如果也是普通C1,停止。如果上上级是B1,则一直追到A1
- if(1 == 2 && $intro_userinfo['group_id'] == 1){
- //先给上级普通用户返
- $recharge_d2c_rate = config('site.recharge_d2c_rate');//d2c 或 e2d
- $jewelC = bcdiv(bcmul($gold,$recharge_d2c_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelC,51, $userinfo['username'].'充值','pay_order',$payorder_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- //充值者上上级
- $intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$intro_userinfo['intro_uid'])->find();
- if(empty($intro_intro_userinfo)){return true;}
- //上上级也是普通C1,说明自己在E1,上级是D1,停止
- if($intro_intro_userinfo['group_id'] == 1){
- return true;
- }
- //上上级是B1,上级是C1,自己是D1,
- if($intro_intro_userinfo['group_id'] == 2){
- //充值者上上级,也就是A
- $intro_intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$intro_intro_userinfo['intro_uid'])->find();
- if(empty($intro_intro_intro_userinfo)){return true;}
- if($intro_intro_intro_userinfo['group_id'] == 3){
- $recharge_c2a_rate = config('site.recharge_c2a_rate');
- $jewelA = bcdiv(bcmul($jewelC,$recharge_c2a_rate),100);
- $result = model('Wallet')->lockChangeAccountRemain($intro_intro_intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
- }
- }
- return true;
- }
- }
- return true;
- }
- //充值VIP 异步回调对外方法
- public function vip_notify_base(){
- //验签
- $paytype = input('paytype','wechat');
- $notify_file = $this->notify_log_start($paytype);
- $pay = Service::checkNotify($paytype);
- if (!$pay) {
- echo '签名错误';
- exit;
- }
- //验证,拿订单号等信息
- $data = $pay->verify();
- $out_trade_no = $data['out_trade_no'];
- //订单查询
- $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
- if(empty($info)){
- echo $pay->success();
- exit;
- }
- if($info['order_status'] != 0)
- {
- echo $pay->success();
- exit;
- }
- //你可以在此编写订单逻辑
- $rs = $this->vip_notify_do($out_trade_no);
- if($rs === false){
- //不论结果都应返回success
- echo $pay->success();
- exit;
- }else{
- //不论结果都应返回success
- echo $pay->success();
- exit;
- }
- //默认
- echo $pay->success();
- exit;
- }
- //充值金币 逻辑
- private function vip_notify_do($out_trade_no){
- Db::startTrans();
- $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
- if (empty($orderInfo)) {
- Db::rollback();
- return false;
- }
- if($orderInfo['order_status'] != 0){
- Db::rollback();
- return false;
- }
- //逻辑开始
- //先充值
- $args = json_decode($orderInfo['args'],true);
- $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
- if($user_info['vip_endtime'] < time()){
- //过期了
- $vip_endtime = time() + (intval($args['days']) * 86400);
- $vip_type = 1;
- }else{
- //追加vip
- $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
- $vip_type = 2;
- }
- $update_data = [
- 'vip_endtime'=>$vip_endtime,
- ];
- $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
- //记录日志
- $log_data = [
- 'user_id' => $orderInfo['user_id'],
- 'before' => $user_info['vip_endtime'],
- 'change_value' => intval($args['days']) * 86400,
- 'remain' => $vip_endtime,
- 'remark' => '安卓购买vip',
- 'createtime' => time(),
- 'vip_type' => $vip_type,
- ];
- Db::name('user_vip_log')->insertGetId($log_data);
- if($result === false)
- {
- Db::rollback();
- return false;
- }
- //逻辑结束
- //状态
- $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
- if($ros === false) {
- Db::rollback();
- return false;
- }
- //默认提交
- Db::commit();
- return true;
- }
- //异步日志
- private function notify_log_start($paytype = 'wechat'){
- //记录支付回调数据
- ignore_user_abort(); // run script in background
- set_time_limit(30);
- // 日志文件 start
- $log_base_dir = '../paylog/'.$paytype.'/';
- if (!is_dir($log_base_dir))
- {
- mkdir($log_base_dir, 0770, true);
- @chmod($log_base_dir, 0770);
- }
- $notify_file = $log_base_dir.'notify.txt';
- if(!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- if(filesize($notify_file)>5242880)//大于5M自动切换
- {
- rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
- }
- if(!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- // 日志文件 end
- //开始写入
- $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
- if($_REQUEST && $paytype == 'alipay') {
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
- } else {
- $xml = file_get_contents("php://input");
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
- $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
- }
- ini_set('display_errors','On');
- return $notify_file;
- }
- }
|