123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use wxpay;
- use addons\epay\library\Service;
- class Notify extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
-
- public function notify() {
- $wxPay = new wxpay\WxPay();
-
- $flag = $wxPay->WxPayNotifyCheck();
-
- if ($flag['status']) {
- if ($flag['data']['return_code'] == 'SUCCESS' && $flag['data']['result_code'] == 'SUCCESS') {
- $total_fee = trim($flag['data']['total_fee']) / 100;
- $order_sn = trim($flag['data']['out_trade_no']);
- $PayResult = Db::name('rechar_order');
- $where['order_no'] = $order_sn;
- $order_info = $PayResult->where($where)->find();
- if ($order_info['money'] != $total_fee) {
- $r_arr['return_code'] = 'FAIL';
- $r_arr['return_msg'] = '回调失败';
- echo $wxPay->arrayToXml($r_arr);
- die;
- }
- if ($order_info['status'] == 1) {
- $r_arr['return_code'] = 'SUCCESS';
- $r_arr['return_msg'] = '回调成功';
- echo $wxPay->arrayToXml($r_arr);
- die;
- }
-
- $_data['transaction_id'] = trim($flag['data']['transaction_id']);
- $_data['status'] = 1;
- $_data['updatetime'] = time();
-
- if ($order_info['purpose'] == 1) {
-
- $active_order = Db::name('active_order')->find($order_info['relation_id']);
-
- Db::startTrans();
-
- $active_order_rs = Db::name('active_order')->where(['id' => $order_info['relation_id'], 'status' => 0])->setField(['status' => 1, 'updatetime' => time(), 'transaction_id' => $_data['transaction_id']]);
-
- $active_people_rs = Db::name('active_people')->where(['order_id' => $order_info['relation_id'], 'status' => 0])->setField(['status' => 1, 'updatetime' => time()]);
-
- $user_info = Db::name('user')->find($order_info['user_id']);
-
- $invite_coupon_rs = true;
- if ($user_info['pre_user_id']) {
-
- $invite_coupon = Db::name('coupon')->where(['purpose' => 5, 'status' => 1])->order('weigh desc, id desc')->find();
- if ($invite_coupon) {
- $invite_coupon_data = [
- 'user_id' => $user_info['pre_user_id'],
- 'coupon_id' => $invite_coupon['id'],
- 'title' => $invite_coupon['title'],
- 'desc' => $invite_coupon['desc'],
- 'type' => $invite_coupon['type'],
- 'money' => $invite_coupon['money'],
- 'minmoney' => $invite_coupon['minmoney'],
- 'purpose' => $invite_coupon['purpose'],
- 'starttime' => time(),
- 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
- 'active_id' => $active_order['active_id'],
- 'order_id' => $active_order['id'],
- 'createtime' => time()
- ];
- $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
- }
- }
-
- $active_title = Db::name('active')->where(['id' => $active_order['active_id']])->value('title');
-
- $data = [
- 'user_id' => $order_info['user_id'],
- 'type' => 1,
- 'title' => '活动通知',
- 'content' => '您已成功报名' . $active_title . '活动',
- 'createtime' => time()
- ];
- $sys_rs = Db::name('sys_msg')->insertGetId($data);
-
- $paygrowth = (int)config('site.paygrowth');
- $balance = floor($paygrowth * $order_info['money']);
- $paygrowth_rs = 1;
- if ($balance > 0) {
- $paygrowth_rs = create_growth_log($balance, '微信支付活动订单', $order_info['user_id'], 3);
- }
- if ($active_order_rs && $active_people_rs && $invite_coupon_rs && $sys_rs && $paygrowth_rs == 1) {
- Db::commit();
- } else {
- Db::rollback();
- $_data['pay_status'] = 1;
- }
- } elseif ($order_info['purpose'] == 2) {
-
-
- $recharge = Db::name('recharge')->find($order_info['relation_id']);
-
- Db::startTrans();
-
- $rs = create_log($recharge['price'], '充值', $order_info['user_id'], 1, $order_info['id']);
-
- $invite_coupon_rs = true;
- if ($recharge['coupon_id'] && $recharge['couponnum'] > 0) {
-
- $invite_coupon = Db::name('coupon')->where(['id' => $recharge['coupon_id'], 'purpose' => 0, 'status' => 1])->find();
- if ($invite_coupon) {
- $invite_coupon_data = [
- 'user_id' => $order_info['user_id'],
- 'coupon_id' => $invite_coupon['id'],
- 'title' => $invite_coupon['title'],
- 'desc' => $invite_coupon['desc'],
- 'type' => $invite_coupon['type'],
- 'money' => $invite_coupon['money'],
- 'minmoney' => $invite_coupon['minmoney'],
- 'purpose' => $invite_coupon['purpose'],
- 'starttime' => time(),
- 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
- 'active_id' => 0,
- 'order_id' => 0,
- 'createtime' => time()
- ];
- for ($i=1; $i<=$recharge['couponnum']; $i++) {
- $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
- if (!$invite_coupon_rs) {
- break;
- }
- }
- }
- }
-
- $paygrowth = (int)config('site.paygrowth');
- $balance = floor($paygrowth * $order_info['money']);
- $paygrowth_rs = 1;
- if ($balance > 0) {
- $paygrowth_rs = create_growth_log($balance, '充值', $order_info['user_id'], 2);
- }
- if ($rs == 1 && $invite_coupon_rs && $paygrowth_rs == 1) {
- Db::commit();
- } else {
- Db::rollback();
- $_data['pay_status'] = 2;
- }
- }
- $PayResult->where($where)->setField($_data);
- $r_arr['return_code'] = 'SUCCESS';
- $r_arr['return_msg'] = '回调成功';
- echo $wxPay->arrayToXml($r_arr);
- die;
- }
- }
- $r_arr['return_code'] = 'FAIL';
- $r_arr['return_msg'] = '回调失败';
- echo $wxPay->arrayToXml($r_arr);
- file_put_contents('wx_pay_error_logs.txt', date('Y-m-d H:i:s').'支付失败!'.json_encode($flag), FILE_APPEND);
- die;
- }
-
- public function notify1() {
- $paytype = $this->request->param('paytype');
- $pay = Service::checkNotify($paytype);
- if (!$pay) {
- echo '签名错误';
- return;
- }
- $data = $pay->verify();
- $time = time();
- try {
- $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
- $out_trade_no = $data['out_trade_no'];
- Db::startTrans();
- try{
- $orderModel = new \app\common\model\RecharOrder();
- $userModel = new \app\common\model\User();
- $orderInfo = $orderModel->where(["order_no"=>$out_trade_no])->find();
- $userInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
- $res1 = false;
- $res2 = false;
- $res3 = false;
- if($orderInfo['status'] == 0) {
-
- $res1 = $orderModel->update(["status"=>1,"updatetime"=>$time],["order_no"=>$out_trade_no, 'status' => 0]);
-
- switch($orderInfo['purpose']) {
- case 1:
-
- $userInfo->recharge_auth = 1;
- $res2 = $userInfo->save();
- $res3 = true;
- break;
- case 2:
-
- $data = [];
- $data['user_id'] = $userInfo->id;
- $data['fate_user_id'] = $orderInfo->fate_user_id;
- if(!\app\common\model\UserFate::where($data)->find()) {
- $data['createtime'] = $time;
- $res2 = \app\common\model\UserFate::insert($data);
- } else {
- $res2 = true;
- }
-
- $preuser_id = $userInfo->pre_user_id;
- if($userInfo->vip_duetime < $time) {
- $memo = '用户充值获得收益!';
- $profit = $payamount * config('site.userProfitRate') * 0.01;
- } else {
- $memo = '会员充值获得收益!';
- $profit = $payamount * config('site.vipProfitRate') * 0.01;
- }
- if($profit>=0.01 && $preuser_id > 0) {
- $res3 = \app\common\model\User::profit($profit,$preuser_id,$memo);
- } else {
- $res3 = true;
- }
- break;
- case 3:
-
- $vip_config_id = $orderInfo['vip_config_id'];
- $vipConfigInfo = \app\admin\model\vip\Config::where(['id'=>$vip_config_id])->find();
-
- if($userInfo->vip_duetime < $time) {
- $duetime = strtotime ( '+'.intval($vipConfigInfo['time']).' month' );
- } else {
- $duetime = strtotime ( '+'.intval($vipConfigInfo['time']).' month',$userInfo->vip_duetime);
- }
-
- if($userInfo->vip_duetime <= time()) {
- $userInfo->view_count = $vipConfigInfo->give_times;
- $userInfo->view_date = date('Y-m-d');
- }
- $userInfo->vip_duetime = $duetime;
- $res2 = $userInfo->save();
-
- $data = [];
- $data['user_id'] = $userInfo->id;
- $data['end_date'] = date("Y-m-d",$duetime);
- $data['times'] = $vipConfigInfo->give_times;
- $data['createtime'] = time();
- $res3 = \app\common\model\GiveFateLog::insert($data);
- break;
- case 4:
-
- $vip_config_id = $orderInfo['vip_config_id'];
- $vipConfigInfo = Db::name('diamond')->where(['id'=>$vip_config_id])->find();
-
- $diamond = $userInfo['diamond'] + $vipConfigInfo['number'];
- $res2 = Db::name('user')->where(['id' => $userInfo['id'], 'diamond' => $userInfo['diamond']])->setField('diamond', $diamond);
-
- $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $userInfo['id']])->order('id', 'desc')->find();
- if (!$diamond_log && $userInfo['diamond'] > 0) {
- $res3 = false;
- break;
- }
- if ($diamond_log && $diamond_log['after'] != $userInfo['diamond']) {
- $res3 = false;
- break;
- }
- $data = [];
- $data['user_id'] = $userInfo['id'];
- $data['diamond'] = $vipConfigInfo['number'];
- $data['before'] = $userInfo['diamond'];
- $data['after'] = $diamond;
- $data['memo'] = '充值';
- $data['createtime'] = time();
- $res3 = Db::name('user_diamond_log')->insertGetId($data);
- break;
- }
- }
- if($res1 && $res2 !== false && $res3) {
- Db::commit();
- } else {
- Db::rollback();
- }
- }catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
-
- } catch (Exception $e) {
- }
- echo $pay->success();
- }
-
- public function cancelorder() {
- set_time_limit(0);
- $where = array(
- 'status' => 0,
- 'createtime' => ['lt', time() - 1920],
- );
- $active_order = Db::name('active_order');
- $list = $active_order->where($where)->limit(200)->select();
- if (!$list) {
- echo 'mei shu ju';
- die;
- }
- $active_people = Db::name('active_people');
- $active_people_modify = Db::name('active_people_modify');
- $active = Db::name('active');
- foreach ($list as &$v) {
-
- Db::startTrans();
-
- $rs = $active_order->where(['id' => $v['id'], 'status' => 0])->setField('status', 3);
- if (!$rs) {
- Db::rollback();
- continue;
- }
-
- $rt = $active_people->where(['order_id' => $v['id'], 'status' => 0])->setField(['status' => 3, 'modifystatus' => 0]);
- if (!$rt) {
- Db::rollback();
- continue;
- }
-
- $res = $active_people_modify->where(['order_id' => $v['id'], 'status' => 0])->setField('status', 2);
- if ($res === false) {
- Db::rollback();
- continue;
- }
-
- $active_info = $active->find($v['active_id']);
- $currentperson = $active_info['currentperson'] - $v['number'];
- $active_rs = $active->where(['id' => $v['active_id'], 'currentperson' => $active_info['currentperson']])->setField('currentperson', $currentperson);
- if (!$active_rs) {
- Db::rollback();
- continue;
- }
- Db::commit();
- }
- echo 'wan bi';
- die;
- }
-
- public function sysmsg() {
- set_time_limit(0);
- $where = array(
- 'starttime' => ['elt', time() + 86400],
- 'msgstatus' => 0,
- 'status' => ['neq', 3]
- );
- $active = Db::name('active');
- $list = $active->where($where)->limit(200)->select();
- if (!$list) {
- echo 'mei shu ju';
- die;
- }
- $active_order = Db::name('active_order');
- $sys_msg = Db::name('sys_msg');
- $data['type'] = 1;
- $data['title'] = '活动通知';
- $data['createtime'] = time();
- foreach ($list as &$v) {
-
- $active->where(['id' => $v['id']])->setField('msgstatus', 1);
-
- $data['content'] = '您报名的' . $v['title'] . '活动即将开始,请规划好您的时间。活动集合时间:' . date('Y-m-d H:i', $v['collectiontime']) . ',活动集合地点:' . $v['collectionplace'];
-
- $user_ids = $active_order->where(['active_id' => $v['id'], 'status' => 1])->column('user_id');
- if ($user_ids) {
- foreach ($user_ids as &$va) {
- $data['user_id'] = $va;
- $sys_msg->insertGetId($data);
- }
- }
- }
- echo 'wan bi';
- die;
- }
-
- public function resetfreenumber() {
- set_time_limit(0);
-
- $time = strtotime(date('Y-1-1 0:0:0', time()));
- $where = array(
- 'freenumbertime' => ['lt', $time]
- );
- $user = Db::name('user');
- $list = $user->where($where)->limit(200)->select();
- if (!$list) {
- echo 'mei shu ju';
- die;
- }
- $vip = Db::name('vip');
- foreach ($list as &$v) {
- $free = $vip->find($v['growthlevel']);
- $user->where(['id' => $v['id'], 'freenumber' => $v['freenumber']])->setField(['freenumber' => $free['free'], 'freenumbertime' => time()]);
- }
- echo 'wan bi';
- die;
- }
-
-
- public function finishorder() {
- set_time_limit(0);
- $where = array(
- 'status' => 1,
- 'endtime' => ['lt', time()]
- );
- $active = Db::name('active');
- $list = $active->where($where)->limit(100)->select();
- if (!$list) {
- echo 'mei shu ju';
- die;
- }
- $active_order = Db::name('active_order');
- $active_people = Db::name('active_people');
- $active_people_modify = Db::name('active_people_modify');
- $active_refund = Db::name('active_refund');
- foreach ($list as &$v) {
-
- Db::startTrans();
-
- $active_rs = $active->where(['id' => $v['id'], 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
- if (!$active_rs) {
- Db::rollback();
- continue;
- }
-
- $order_rs = $active_order->where(['active_id' => $v['id'], 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
- if ($order_rs === false) {
- Db::rollback();
- continue;
- }
-
- $people_rs = $active_people->where(['active_id' => $v['id'], 'status' => 1])->setField(['status' => 2, 'modifystatus' => 0, 'updatetime' => time()]);
- if ($people_rs === false) {
- Db::rollback();
- continue;
- }
-
- $people_modify_rs = $active_people_modify->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($people_modify_rs === false) {
- Db::rollback();
- continue;
- }
-
- $refund_rs = $active_refund->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($refund_rs === false) {
- Db::rollback();
- continue;
- }
- Db::commit();
- }
- echo 'wan bi';
- die;
- }
-
-
- public function cancelminpeopleorder() {
- set_time_limit(0);
- $where = array(
- 'status' => 0,
- 'signupendtime' => ['lt', time()]
- );
- $active = Db::name('active');
- $list = $active->where($where)->limit(100)->select();
- if (!$list) {
- echo 'mei shu ju';
- die;
- }
- $active_order = Db::name('active_order');
- $active_people = Db::name('active_people');
- $active_people_modify = Db::name('active_people_modify');
- $active_refund = Db::name('active_refund');
- $user_coupon = Db::name('user_coupon');
- $sys_msg = Db::name('sys_msg');
- foreach ($list as &$v) {
-
- Db::startTrans();
-
- $active_rs = $active->where(['id' => $v['id'], 'status' => 0])->setField(['status' => 3, 'updatetime' => time()]);
- if (!$active_rs) {
- Db::rollback();
- continue;
- }
-
- $people_rs = $active_people->where(['active_id' => $v['id'], 'status' => 1])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
- if ($people_rs === false) {
- Db::rollback();
- continue;
- }
-
- $people_modify_rs = $active_people_modify->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($people_modify_rs === false) {
- Db::rollback();
- continue;
- }
-
- $refund_rs = $active_refund->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($refund_rs === false) {
- Db::rollback();
- continue;
- }
-
- $active_order_list = $active_order->where(['active_id' => $v['id'], 'status' => 1])->select();
- if ($active_order_list) {
-
- $order_data['status'] = 3;
- $order_data['updatetime'] = time();
- foreach ($active_order_list as &$vv) {
-
- if ($vv['price'] > 0) {
- $order_data['refundstatus'] = 1;
- $order_data['refundprice'] = $vv['price'];
- $order_data['refundtime'] = time();
- if ($vv['paytype'] == 0) {
-
- $rs = create_log($vv['price'], '活动取消返还', $vv['user_id'], 3, $vv['id']);
- if ($rs != 1) {
- $order_data['refundstatus'] = 2;
- }
- } elseif ($vv['paytype'] == 1) {
-
-
- $paygrowth = (int)config('site.paygrowth');
- $balance = floor($paygrowth * $vv['price']);
- if ($balance > 0) {
- $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $vv['user_id'], 4, $vv['id']);
- }
-
- $order_data['refund_no'] = $vv['order_sn'];
-
- $wxData['transaction_id'] = $vv['transaction_id'];
- $wxData['out_refund_no'] = $order_data['refund_no'];
- $wxData['total_fee'] = (int)($vv['price'] * 100);
- $wxData['refund_fee'] = (int)($vv['price'] * 100);
- $wxData['refund_desc'] = '活动取消返还';
- $wxPay = new wxpay\WxPay(config('wxchatpay'));
- $back = $wxPay->WxPayRefund($wxData);
- if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
- $order_data['refundstatus'] = 2;
- }
- }
- }
-
- $order_rs = $active_order->where(['id' => $vv['id'], 'status' => 1])->setField($order_data);
- if ($order_rs === false) {
- Db::rollback();
- continue;
- }
-
- $user_coupon->where(['active_id' => $vv['id'], 'status' => 0])->setField('endtime', time() - 1);
-
- $data = [
- 'user_id' => $vv['user_id'],
- 'type' => 1,
- 'title' => '活动通知',
- 'content' => '您报名的' . $v['title'] . '活动已取消',
- 'createtime' => time()
- ];
- $sys_msg->insertGetId($data);
- }
- }
- Db::commit();
- }
- echo 'wan bi';
- die;
- }
- }
|