123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?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){
-
- echo $pay->success();
- exit;
- }else{
-
- 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(){
- }
-
-
-
-
-
- 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;}
-
- if($intro_userinfo['group_id'] == 2){
-
- $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;
- }
- }
- 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['agent_id'])->find();
- if(empty($intro_userinfo)){return true;}
-
- if($intro_userinfo['group_id'] == 3){
- $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;
- }
- }
-
- 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;
- }
-
-
- 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['group_id'] == 3 || $userinfo['gender'] == 0){
- return true;
- }
-
- $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;}
-
- bcscale(2);
-
- if($intro_userinfo['group_id'] == 3){
- $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($intro_userinfo['group_id'] == 2 && $intro_userinfo['group_id'] > $userinfo['group_id']){
-
- $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;}
- 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;
- }
-
-
- if($intro_userinfo['group_id'] == 1 && $userinfo['group_id'] == 1){
-
- $recharge_d2c_rate = config('site.recharge_d2c_rate');
- $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;}
-
- if($intro_intro_userinfo['group_id'] == 1){
- return true;
- }
-
- if($intro_intro_userinfo['group_id'] == 2){
-
- $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;
- }
-
- 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){
-
- echo $pay->success();
- exit;
- }else{
-
- 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_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();
- set_time_limit(30);
-
- $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)
- {
- 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);
- }
-
-
- $_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;
- }
- }
|