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('pay_no',$out_trade_no)->find(); if(empty($info)){ echo $pay->success(); exit; } if($info['status'] != 0) { echo $pay->success(); exit; } //你可以在此编写订单逻辑 $rs = $this->gold_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 gold_notify_do($out_trade_no){ Db::startTrans(); $orderInfo = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->lock(true)->find(); if (empty($orderInfo)) { Db::rollback(); return false; } if($orderInfo['status'] != 0){ Db::rollback(); return false; } //逻辑开始 $remark = $orderInfo['payment_class'] == 'alipay' ? '支付宝' : '微信'; $args = json_decode($orderInfo['args'],true); $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['gold'],10, $remark.'充值金币','pay_order',$orderInfo['id'],1); if($result['status']===false) { Db::rollback(); return false; } //若有上级,给上级返利 if ($args['intro_uid']) { //获取返利比率 $type = 0; if ($type == 1){ $intro_recharge_rebate_rate = 10; }else{ $agent_info = Db::name('user')->where(['id' => $args['intro_uid']])->field('is_agent,h_intro_recharge_rebate_rate')->find(); $intro_recharge_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_recharge_rebate_rate'] : (int)config('site.intro_recharge_rebate_rate'); } if ($intro_recharge_rebate_rate > 0 && $intro_recharge_rebate_rate <= 100) { //上级获得金额数量 $intro_uid_money = bcdiv(bcmul($args['money'],$intro_recharge_rebate_rate,2),100,2); if ($intro_uid_money > 0) { $intro_result = model('wallet')->lockChangeAccountRemain($args['intro_uid'],$orderInfo['user_id'],'money',$intro_uid_money,65,'邀请朋友充值金币奖励','pay_order',$orderInfo['id']); if($intro_result['status']===false) { Db::rollback(); return false; } } } } //首充 $first_map = [ 'uid' => $orderInfo['user_id'], ]; $first_check = Db::name('user_paygold_log')->where($first_map)->find(); if(empty($first_check)){ //首充日志 $first_map['createtime'] = time(); $first_log_id = Db::name('user_paygold_log')->insertGetId($first_map); if(!$first_log_id){ Db::rollback(); return false; } //送金币 if(isset($args['first_gold']) && $args['first_gold'] > 0){ $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['first_gold'],14, '充值金币首充奖励','pay_order',$orderInfo['id']); if($result['status']===false) { Db::rollback(); return false; } } //送vip if(isset($args['first_vipdays']) && $args['first_vipdays'] > 0){ $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['first_vipdays']) * 86400); $vip_type = 1; $before_level = 0; $after_level = 10; }else{ //追加vip $vip_endtime = $user_info['vip_endtime'] + (intval($args['first_vipdays']) * 86400); $vip_type = 2; $before_level = $user_info['vip_level']; $after_level = $user_info['vip_level']; } $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update(['vip_endtime'=>$vip_endtime]); if($result === false) { Db::rollback(); return false; } //记录日志 $log_data = [ 'user_id' => $orderInfo['user_id'], 'before' => $user_info['vip_endtime'], 'change_value' => intval($args['first_vipdays']) * 86400, 'remain' => $vip_endtime, 'remark' => '首充赠送vip', 'createtime' => time(), 'vip_type' => $vip_type, 'before_level' => $before_level, 'after_level' => $after_level, ]; Db::name('user_vip_log')->insertGetId($log_data); } } //vip用户充值额外送金币 if(isset($args['vip_gold']) && $args['vip_gold'] > 0){ $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['vip_gold'],101, 'VIP用户充值金币赠送','pay_order',$orderInfo['id']); if($result['status']===false) { Db::rollback(); return false; } } //逻辑结束 //状态 $ros = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->update(['status'=>1,'notifytime'=>time()]); if($ros === false) { Db::rollback(); return false; } //默认提交 Db::commit(); 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('pay_no',$out_trade_no)->find(); if(empty($info)){ echo $pay->success(); exit; } if($info['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; } public function vip_notify_test(){ $out_trade_no = input('out_trade_no',''); $rs = $this->vip_notify_do($out_trade_no); dump($rs); } //充值金币 逻辑 private function vip_notify_do($out_trade_no){ Db::startTrans(); $orderInfo = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->lock(true)->find(); if (empty($orderInfo)) { Db::rollback(); return false; } if($orderInfo['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; $before_level = 0; $after_level = $args['vip_level']; }else{ //追加vip $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400); $vip_type = 2; $before_level = $user_info['vip_level']; $after_level = $args['vip_level']; } $update_data = [ 'vip_endtime'=>$vip_endtime, 'vip_level' =>$args['vip_level'], //修改等级,同级不影响,向上立刻改,向下不允许 'pay_money' => bcadd($user_info['pay_money'], $args['money'], 2) //pay_money第2处修改,$args['money']是vip配置表的人民币价格 ]; $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data); if($result === false) { Db::rollback(); return false; } //记录日志 $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, 'before_level' => $before_level, 'after_level' => $after_level, ]; Db::name('user_vip_log')->insertGetId($log_data); if ($args['gold_num'] > 0) { //赠送金币 $wallet_rs = model('wallet')->lockChangeAccountRemain($orderInfo['user_id'], 0,'gold', $args['gold_num'], 17, '开通会员赠送金币'); if ($wallet_rs['status'] === false) { Db::rollback(); return false; } } //逻辑结束 //状态 $ros = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->update(['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; } }