Notify.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use wxpay;
  6. use addons\epay\library\Service;
  7. /**
  8. * 支付回调
  9. */
  10. class Notify extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. // 支付回调
  15. public function notify() {
  16. // require_once($_SERVER['DOCUMENT_ROOT'] . "/Plugins/WxPay/WxPay.php");
  17. $wxPay = new wxpay\WxPay();
  18. //验证是否是微信发送
  19. $flag = $wxPay->WxPayNotifyCheck();
  20. // error_log(print_r($flag, 1), 3, 'post.txt');
  21. // LL($flag,'./log1.txt');
  22. //验证成功
  23. if ($flag['status']) {
  24. if ($flag['data']['return_code'] == 'SUCCESS' && $flag['data']['result_code'] == 'SUCCESS') {
  25. $total_fee = trim($flag['data']['total_fee']) / 100; //支付的金额, 单位:分转成元
  26. $order_sn = trim($flag['data']['out_trade_no']);//商家订单号
  27. $PayResult = Db::name('rechar_order');
  28. $where['order_no'] = $order_sn;
  29. $order_info = $PayResult->where($where)->find();
  30. if ($order_info['money'] != $total_fee) {
  31. $r_arr['return_code'] = 'FAIL';
  32. $r_arr['return_msg'] = '回调失败';
  33. echo $wxPay->arrayToXml($r_arr);
  34. die;
  35. }
  36. if ($order_info['status'] == 1) {
  37. $r_arr['return_code'] = 'SUCCESS';
  38. $r_arr['return_msg'] = '回调成功';
  39. echo $wxPay->arrayToXml($r_arr);
  40. die;
  41. }
  42. //构建支付订单处理结果信息
  43. $_data['transaction_id'] = trim($flag['data']['transaction_id']); //微信交易凭证
  44. $_data['status'] = 1;
  45. $_data['updatetime'] = time();
  46. //支付订单类型
  47. if ($order_info['purpose'] == 1) {
  48. //查询订单
  49. $active_order = Db::name('active_order')->find($order_info['relation_id']);
  50. //开启事务
  51. Db::startTrans();
  52. //修改报名订单状态
  53. $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']]);
  54. //修改报名人员信息状态
  55. $active_people_rs = Db::name('active_people')->where(['order_id' => $order_info['relation_id'], 'status' => 0])->setField(['status' => 1, 'updatetime' => time()]);
  56. //给上级发送优惠券
  57. $user_info = Db::name('user')->find($order_info['user_id']);
  58. //发放优惠券状态
  59. $invite_coupon_rs = true;
  60. if ($user_info['pre_user_id']) {
  61. //查询报名活动优惠券
  62. $invite_coupon = Db::name('coupon')->where(['purpose' => 5, 'status' => 1])->order('weigh desc, id desc')->find();
  63. if ($invite_coupon) {
  64. $invite_coupon_data = [
  65. 'user_id' => $user_info['pre_user_id'],
  66. 'coupon_id' => $invite_coupon['id'],
  67. 'title' => $invite_coupon['title'],
  68. 'desc' => $invite_coupon['desc'],
  69. 'type' => $invite_coupon['type'],
  70. 'money' => $invite_coupon['money'],
  71. 'minmoney' => $invite_coupon['minmoney'],
  72. 'purpose' => $invite_coupon['purpose'],
  73. 'starttime' => time(),
  74. 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
  75. 'active_id' => $active_order['active_id'],
  76. 'order_id' => $active_order['id'],
  77. 'createtime' => time()
  78. ];
  79. $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
  80. }
  81. }
  82. //活动标题
  83. $active_title = Db::name('active')->where(['id' => $active_order['active_id']])->value('title');
  84. //发送消息
  85. $data = [
  86. 'user_id' => $order_info['user_id'],
  87. 'type' => 1,
  88. 'title' => '活动通知',
  89. 'content' => '您已成功报名' . $active_title . '活动',
  90. 'createtime' => time()
  91. ];
  92. $sys_rs = Db::name('sys_msg')->insertGetId($data);
  93. //增加成长值
  94. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  95. $balance = floor($paygrowth * $order_info['money']);
  96. $paygrowth_rs = 1;
  97. if ($balance > 0) {
  98. $paygrowth_rs = create_growth_log($balance, '微信支付活动订单', $order_info['user_id'], 3);
  99. }
  100. if ($active_order_rs && $active_people_rs && $invite_coupon_rs && $sys_rs && $paygrowth_rs == 1) {
  101. Db::commit();
  102. } else {
  103. Db::rollback();
  104. $_data['pay_status'] = 1; //回调状态: 1=支付订单回调失败,2=充值回调失败,3=开通会员回调失败
  105. }
  106. } elseif ($order_info['purpose'] == 2) {
  107. //充值
  108. //查询充值信息
  109. $recharge = Db::name('recharge')->find($order_info['relation_id']);
  110. //开启事务
  111. Db::startTrans();
  112. //增加用户余额
  113. $rs = create_log($recharge['price'], '充值', $order_info['user_id'], 1, $order_info['id']);
  114. //发放优惠券状态
  115. $invite_coupon_rs = true;
  116. if ($recharge['coupon_id'] && $recharge['couponnum'] > 0) {
  117. //查询报名活动优惠券
  118. $invite_coupon = Db::name('coupon')->where(['id' => $recharge['coupon_id'], 'purpose' => 0, 'status' => 1])->find();
  119. if ($invite_coupon) {
  120. $invite_coupon_data = [
  121. 'user_id' => $order_info['user_id'],
  122. 'coupon_id' => $invite_coupon['id'],
  123. 'title' => $invite_coupon['title'],
  124. 'desc' => $invite_coupon['desc'],
  125. 'type' => $invite_coupon['type'],
  126. 'money' => $invite_coupon['money'],
  127. 'minmoney' => $invite_coupon['minmoney'],
  128. 'purpose' => $invite_coupon['purpose'],
  129. 'starttime' => time(),
  130. 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
  131. 'active_id' => 0,
  132. 'order_id' => 0,
  133. 'createtime' => time()
  134. ];
  135. for ($i=1; $i<=$recharge['couponnum']; $i++) {
  136. $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
  137. if (!$invite_coupon_rs) {
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. //增加成长值
  144. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  145. $balance = floor($paygrowth * $order_info['money']);
  146. $paygrowth_rs = 1;
  147. if ($balance > 0) {
  148. $paygrowth_rs = create_growth_log($balance, '充值', $order_info['user_id'], 2);
  149. }
  150. if ($rs == 1 && $invite_coupon_rs && $paygrowth_rs == 1) {
  151. Db::commit();
  152. } else {
  153. Db::rollback();
  154. $_data['pay_status'] = 2; //回调状态: 1=支付订单回调失败,2=充值回调失败,3=开通会员回调失败
  155. }
  156. }
  157. $PayResult->where($where)->setField($_data);
  158. $r_arr['return_code'] = 'SUCCESS';
  159. $r_arr['return_msg'] = '回调成功';
  160. echo $wxPay->arrayToXml($r_arr);
  161. die;
  162. }
  163. }
  164. $r_arr['return_code'] = 'FAIL';
  165. $r_arr['return_msg'] = '回调失败';
  166. echo $wxPay->arrayToXml($r_arr);
  167. file_put_contents('wx_pay_error_logs.txt', date('Y-m-d H:i:s').'支付失败!'.json_encode($flag), FILE_APPEND);
  168. die;
  169. }
  170. /**
  171. * 支付回调
  172. */
  173. public function notify1() {
  174. $paytype = $this->request->param('paytype');
  175. $pay = Service::checkNotify($paytype);
  176. // Log::record("=======================", 'info');
  177. // Log::record("pay:", 'info');
  178. // Log::record($pay, 'info');
  179. // Log::record("=======================", 'info');
  180. if (!$pay) {
  181. echo '签名错误';
  182. return;
  183. }
  184. $data = $pay->verify();
  185. $time = time();
  186. try {
  187. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  188. $out_trade_no = $data['out_trade_no'];
  189. Db::startTrans();
  190. try{
  191. $orderModel = new \app\common\model\RecharOrder();
  192. $userModel = new \app\common\model\User();
  193. $orderInfo = $orderModel->where(["order_no"=>$out_trade_no])->find();
  194. $userInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  195. $res1 = false;
  196. $res2 = false;
  197. $res3 = false;
  198. if($orderInfo['status'] == 0) {
  199. // 修改订单状态
  200. $res1 = $orderModel->update(["status"=>1,"updatetime"=>$time],["order_no"=>$out_trade_no, 'status' => 0]);
  201. // 充值后
  202. switch($orderInfo['purpose']) {
  203. case 1:
  204. // 修改用户为已缴纳认证费
  205. $userInfo->recharge_auth = 1;
  206. $res2 = $userInfo->save();
  207. $res3 = true;
  208. break;
  209. case 2:
  210. // 添加有眼缘记录--单次付费
  211. $data = [];
  212. $data['user_id'] = $userInfo->id;
  213. $data['fate_user_id'] = $orderInfo->fate_user_id;
  214. if(!\app\common\model\UserFate::where($data)->find()) {
  215. $data['createtime'] = $time;
  216. $res2 = \app\common\model\UserFate::insert($data);
  217. } else {
  218. $res2 = true;
  219. }
  220. // 返利
  221. $preuser_id = $userInfo->pre_user_id;
  222. if($userInfo->vip_duetime < $time) {
  223. $memo = '用户充值获得收益!';
  224. $profit = $payamount * config('site.userProfitRate') * 0.01;
  225. } else {
  226. $memo = '会员充值获得收益!';
  227. $profit = $payamount * config('site.vipProfitRate') * 0.01;
  228. }
  229. if($profit>=0.01 && $preuser_id > 0) {
  230. $res3 = \app\common\model\User::profit($profit,$preuser_id,$memo);
  231. } else {
  232. $res3 = true;
  233. }
  234. break;
  235. case 3:
  236. // 获取vip配置信息
  237. $vip_config_id = $orderInfo['vip_config_id'];
  238. $vipConfigInfo = \app\admin\model\vip\Config::where(['id'=>$vip_config_id])->find();
  239. // 计算vip到期时间
  240. if($userInfo->vip_duetime < $time) {
  241. $duetime = strtotime ( '+'.intval($vipConfigInfo['time']).' month' );
  242. } else {
  243. $duetime = strtotime ( '+'.intval($vipConfigInfo['time']).' month',$userInfo->vip_duetime);
  244. }
  245. // 修改用户为vip
  246. if($userInfo->vip_duetime <= time()) {
  247. $userInfo->view_count = $vipConfigInfo->give_times; // 今日剩余次数
  248. $userInfo->view_date = date('Y-m-d'); // 次数最后更新日期
  249. }
  250. $userInfo->vip_duetime = $duetime;
  251. $res2 = $userInfo->save();
  252. // 添加查看次数
  253. $data = [];
  254. $data['user_id'] = $userInfo->id;
  255. $data['end_date'] = date("Y-m-d",$duetime);
  256. $data['times'] = $vipConfigInfo->give_times;
  257. $data['createtime'] = time();
  258. $res3 = \app\common\model\GiveFateLog::insert($data);
  259. break;
  260. case 4:
  261. // 获取钻石配置信息
  262. $vip_config_id = $orderInfo['vip_config_id'];
  263. $vipConfigInfo = Db::name('diamond')->where(['id'=>$vip_config_id])->find();
  264. //修改用户钻石余额
  265. $diamond = $userInfo['diamond'] + $vipConfigInfo['number'];
  266. $res2 = Db::name('user')->where(['id' => $userInfo['id'], 'diamond' => $userInfo['diamond']])->setField('diamond', $diamond);
  267. // 添加钻石明细
  268. $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $userInfo['id']])->order('id', 'desc')->find();
  269. if (!$diamond_log && $userInfo['diamond'] > 0) {
  270. $res3 = false;
  271. break;
  272. }
  273. if ($diamond_log && $diamond_log['after'] != $userInfo['diamond']) {
  274. $res3 = false;
  275. break;
  276. }
  277. $data = [];
  278. $data['user_id'] = $userInfo['id'];
  279. $data['diamond'] = $vipConfigInfo['number'];
  280. $data['before'] = $userInfo['diamond'];
  281. $data['after'] = $diamond;
  282. $data['memo'] = '充值';
  283. $data['createtime'] = time();
  284. $res3 = Db::name('user_diamond_log')->insertGetId($data);
  285. break;
  286. }
  287. }
  288. if($res1 && $res2 !== false && $res3) {
  289. Db::commit();
  290. } else {
  291. Db::rollback();
  292. }
  293. }catch (ValidateException $e) {
  294. Db::rollback();
  295. $this->error($e->getMessage());
  296. } catch (PDOException $e) {
  297. Db::rollback();
  298. $this->error($e->getMessage());
  299. } catch (Exception $e) {
  300. Db::rollback();
  301. $this->error($e->getMessage());
  302. }
  303. //你可以在此编写订单逻辑
  304. } catch (Exception $e) {
  305. }
  306. echo $pay->success();
  307. }
  308. //自动取消超时未支付的活动订单
  309. public function cancelorder() {
  310. set_time_limit(0);
  311. $where = array(
  312. 'status' => 0,
  313. 'createtime' => ['lt', time() - 1920], //32分钟之前
  314. );
  315. $active_order = Db::name('active_order');
  316. $list = $active_order->where($where)->limit(200)->select();
  317. if (!$list) {
  318. echo 'mei shu ju';
  319. die;
  320. }
  321. $active_people = Db::name('active_people');
  322. $active_people_modify = Db::name('active_people_modify');
  323. $active = Db::name('active');
  324. foreach ($list as &$v) {
  325. //开启事务
  326. Db::startTrans();
  327. //修改订单信息
  328. $rs = $active_order->where(['id' => $v['id'], 'status' => 0])->setField('status', 3);
  329. if (!$rs) {
  330. Db::rollback();
  331. continue;
  332. }
  333. //修改活动人员
  334. $rt = $active_people->where(['order_id' => $v['id'], 'status' => 0])->setField(['status' => 3, 'modifystatus' => 0]);
  335. if (!$rt) {
  336. Db::rollback();
  337. continue;
  338. }
  339. //修改活动人员修改记录
  340. $res = $active_people_modify->where(['order_id' => $v['id'], 'status' => 0])->setField('status', 2);
  341. if ($res === false) {
  342. Db::rollback();
  343. continue;
  344. }
  345. //减少活动已报名人数
  346. $active_info = $active->find($v['active_id']);
  347. $currentperson = $active_info['currentperson'] - $v['number'];
  348. $active_rs = $active->where(['id' => $v['active_id'], 'currentperson' => $active_info['currentperson']])->setField('currentperson', $currentperson);
  349. if (!$active_rs) {
  350. Db::rollback();
  351. continue;
  352. }
  353. Db::commit();
  354. }
  355. echo 'wan bi';
  356. die;
  357. }
  358. //消息推送 我报名参加活动的前一天会提前推送消息,时间 地点 及一些提醒
  359. public function sysmsg() {
  360. set_time_limit(0);
  361. $where = array(
  362. 'starttime' => ['elt', time() + 86400], //活动开始前一天
  363. 'msgstatus' => 0,
  364. 'status' => ['neq', 3]
  365. );
  366. $active = Db::name('active');
  367. $list = $active->where($where)->limit(200)->select();
  368. if (!$list) {
  369. echo 'mei shu ju';
  370. die;
  371. }
  372. $active_order = Db::name('active_order');
  373. $sys_msg = Db::name('sys_msg'); //系统消息
  374. $data['type'] = 1;
  375. $data['title'] = '活动通知';
  376. $data['createtime'] = time();
  377. foreach ($list as &$v) {
  378. //修改活动消息通知状态
  379. $active->where(['id' => $v['id']])->setField('msgstatus', 1);
  380. //发送通知
  381. $data['content'] = '您报名的' . $v['title'] . '活动即将开始,请规划好您的时间。活动集合时间:' . date('Y-m-d H:i', $v['collectiontime']) . ',活动集合地点:' . $v['collectionplace'];
  382. //查询报名用户id
  383. $user_ids = $active_order->where(['active_id' => $v['id'], 'status' => 1])->column('user_id');
  384. if ($user_ids) {
  385. foreach ($user_ids as &$va) {
  386. $data['user_id'] = $va;
  387. $sys_msg->insertGetId($data);
  388. }
  389. }
  390. }
  391. echo 'wan bi';
  392. die;
  393. }
  394. //每年1月1重置免费次数
  395. public function resetfreenumber() {
  396. set_time_limit(0);
  397. //每年开始时间
  398. $time = strtotime(date('Y-1-1 0:0:0', time()));
  399. $where = array(
  400. 'freenumbertime' => ['lt', $time]
  401. );
  402. $user = Db::name('user');
  403. $list = $user->where($where)->limit(200)->select();
  404. if (!$list) {
  405. echo 'mei shu ju';
  406. die;
  407. }
  408. $vip = Db::name('vip');
  409. foreach ($list as &$v) {
  410. $free = $vip->find($v['growthlevel']);
  411. $user->where(['id' => $v['id'], 'freenumber' => $v['freenumber']])->setField(['freenumber' => $free['free'], 'freenumbertime' => time()]);
  412. }
  413. echo 'wan bi';
  414. die;
  415. }
  416. //自动结束已完成订单
  417. public function finishorder() {
  418. set_time_limit(0);
  419. $where = array(
  420. 'status' => 1,
  421. 'endtime' => ['lt', time()]
  422. );
  423. $active = Db::name('active');
  424. $list = $active->where($where)->limit(100)->select();
  425. if (!$list) {
  426. echo 'mei shu ju';
  427. die;
  428. }
  429. $active_order = Db::name('active_order');
  430. $active_people = Db::name('active_people');
  431. $active_people_modify = Db::name('active_people_modify');
  432. $active_refund = Db::name('active_refund');
  433. foreach ($list as &$v) {
  434. //开启事务
  435. Db::startTrans();
  436. //活动结束
  437. $active_rs = $active->where(['id' => $v['id'], 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
  438. if (!$active_rs) {
  439. Db::rollback();
  440. continue;
  441. }
  442. //修改活动订单状态
  443. $order_rs = $active_order->where(['active_id' => $v['id'], 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
  444. if ($order_rs === false) {
  445. Db::rollback();
  446. continue;
  447. }
  448. //修改活动人员状态
  449. $people_rs = $active_people->where(['active_id' => $v['id'], 'status' => 1])->setField(['status' => 2, 'modifystatus' => 0, 'updatetime' => time()]);
  450. if ($people_rs === false) {
  451. Db::rollback();
  452. continue;
  453. }
  454. //修改活动人员信息修改表
  455. $people_modify_rs = $active_people_modify->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  456. if ($people_modify_rs === false) {
  457. Db::rollback();
  458. continue;
  459. }
  460. //修改活动申请取消表
  461. $refund_rs = $active_refund->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  462. if ($refund_rs === false) {
  463. Db::rollback();
  464. continue;
  465. }
  466. Db::commit();
  467. }
  468. echo 'wan bi';
  469. die;
  470. }
  471. //自动取消未成行订单
  472. public function cancelminpeopleorder() {
  473. set_time_limit(0);
  474. $where = array(
  475. 'status' => 0,
  476. 'signupendtime' => ['lt', time()]
  477. );
  478. $active = Db::name('active');
  479. $list = $active->where($where)->limit(100)->select();
  480. if (!$list) {
  481. echo 'mei shu ju';
  482. die;
  483. }
  484. $active_order = Db::name('active_order');
  485. $active_people = Db::name('active_people');
  486. $active_people_modify = Db::name('active_people_modify');
  487. $active_refund = Db::name('active_refund');
  488. $user_coupon = Db::name('user_coupon'); //用户优惠券表
  489. $sys_msg = Db::name('sys_msg'); //消息通知
  490. foreach ($list as &$v) {
  491. //开启事务
  492. Db::startTrans();
  493. //活动结束
  494. $active_rs = $active->where(['id' => $v['id'], 'status' => 0])->setField(['status' => 3, 'updatetime' => time()]);
  495. if (!$active_rs) {
  496. Db::rollback();
  497. continue;
  498. }
  499. //修改活动人员状态
  500. $people_rs = $active_people->where(['active_id' => $v['id'], 'status' => 1])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
  501. if ($people_rs === false) {
  502. Db::rollback();
  503. continue;
  504. }
  505. //修改活动人员信息修改表
  506. $people_modify_rs = $active_people_modify->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  507. if ($people_modify_rs === false) {
  508. Db::rollback();
  509. continue;
  510. }
  511. //修改活动申请取消表
  512. $refund_rs = $active_refund->where(['active_id' => $v['id'], 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  513. if ($refund_rs === false) {
  514. Db::rollback();
  515. continue;
  516. }
  517. //查询活动订单
  518. $active_order_list = $active_order->where(['active_id' => $v['id'], 'status' => 1])->select();
  519. if ($active_order_list) {
  520. //活动订单修改信息
  521. $order_data['status'] = 3; //状态:0=待付款,1=待出行,2=已完成,3=已取消
  522. $order_data['updatetime'] = time();
  523. foreach ($active_order_list as &$vv) {
  524. //退还支付金额
  525. if ($vv['price'] > 0) {
  526. $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
  527. $order_data['refundprice'] = $vv['price'];//退款金额
  528. $order_data['refundtime'] = time();
  529. if ($vv['paytype'] == 0) {
  530. //余额支付, 退回余额
  531. $rs = create_log($vv['price'], '活动取消返还', $vv['user_id'], 3, $vv['id']);
  532. if ($rs != 1) {
  533. $order_data['refundstatus'] = 2;
  534. }
  535. } elseif ($vv['paytype'] == 1) {
  536. //微信支付, 退回微信
  537. //扣除用户成长值
  538. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  539. $balance = floor($paygrowth * $vv['price']);
  540. if ($balance > 0) {
  541. $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $vv['user_id'], 4, $vv['id']);
  542. }
  543. //退款单号
  544. $order_data['refund_no'] = $vv['order_sn'];
  545. //调用微信退款
  546. $wxData['transaction_id'] = $vv['transaction_id'];
  547. $wxData['out_refund_no'] = $order_data['refund_no'];
  548. $wxData['total_fee'] = (int)($vv['price'] * 100);//1 微信支付 单位为分
  549. $wxData['refund_fee'] = (int)($vv['price'] * 100);//1 微信支付 单位为分
  550. $wxData['refund_desc'] = '活动取消返还';
  551. // require_once("Plugins/WxPay/WxPay.php");
  552. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  553. $back = $wxPay->WxPayRefund($wxData);
  554. if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
  555. $order_data['refundstatus'] = 2;
  556. }
  557. }
  558. }
  559. //修改活动订单状态
  560. $order_rs = $active_order->where(['id' => $vv['id'], 'status' => 1])->setField($order_data);
  561. if ($order_rs === false) {
  562. Db::rollback();
  563. continue;
  564. }
  565. //上级优惠券设为过期
  566. $user_coupon->where(['active_id' => $vv['id'], 'status' => 0])->setField('endtime', time() - 1);
  567. //发送消息
  568. $data = [
  569. 'user_id' => $vv['user_id'],
  570. 'type' => 1,
  571. 'title' => '活动通知',
  572. 'content' => '您报名的' . $v['title'] . '活动已取消',
  573. 'createtime' => time()
  574. ];
  575. $sys_msg->insertGetId($data);
  576. }
  577. }
  578. Db::commit();
  579. }
  580. echo 'wan bi';
  581. die;
  582. }
  583. //自动上架活动
  584. public function showactive() {
  585. set_time_limit(0);
  586. $time = time();
  587. $where = array(
  588. 'showstatus' => 0,
  589. 'is_autoshow' => 1,
  590. 'showtime' => ['elt', $time]
  591. );
  592. $active = Db::name('active');
  593. $list = $active->where($where)->limit(200)->select();
  594. if (!$list) {
  595. echo 'mei shu ju';
  596. die;
  597. }
  598. foreach ($list as &$v) {
  599. $active->where(['id' => $v['id'], 'showstatus' => 0])->setField(['showstatus' => 1]);
  600. }
  601. echo 'wan bi';
  602. die;
  603. }
  604. }