Notify.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 订单支付回调
  8. */
  9. class Notify extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. //充值金币 异步回调对外方法
  14. public function recharge_notify_base(){
  15. //验签
  16. $paytype = input('paytype','wechat');
  17. $notify_file = $this->notify_log_start($paytype);
  18. $pay = Service::checkNotify($paytype);
  19. if (!$pay) {
  20. echo '签名错误';
  21. exit;
  22. }
  23. //验证,拿订单号等信息
  24. $data = $pay->verify();
  25. $out_trade_no = $data['out_trade_no'];
  26. //订单查询
  27. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  28. if(empty($info)){
  29. echo $pay->success();
  30. exit;
  31. }
  32. if($info['order_status'] != 0)
  33. {
  34. echo $pay->success();
  35. exit;
  36. }
  37. //你可以在此编写订单逻辑
  38. $rs = $this->recharge_notify_do($out_trade_no);
  39. if($rs === false){
  40. //不论结果都应返回success
  41. echo $pay->success();
  42. exit;
  43. }else{
  44. //不论结果都应返回success
  45. echo $pay->success();
  46. exit;
  47. }
  48. //默认
  49. echo $pay->success();
  50. exit;
  51. }
  52. //充值金币 逻辑
  53. private function recharge_notify_do($out_trade_no){
  54. Db::startTrans();
  55. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  56. if (empty($orderInfo)) {
  57. Db::rollback();
  58. return false;
  59. }
  60. if($orderInfo['order_status'] != 0){
  61. Db::rollback();
  62. return false;
  63. }
  64. //逻辑开始
  65. $args = json_decode($orderInfo['args'],true);
  66. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$orderInfo['id']);
  67. if($result['status']===false)
  68. {
  69. Db::rollback();
  70. return false;
  71. }
  72. //上供到上级
  73. //逻辑结束
  74. //状态
  75. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  76. if($ros === false) {
  77. Db::rollback();
  78. return false;
  79. }
  80. //默认提交
  81. Db::commit();
  82. return true;
  83. }
  84. public function test(){
  85. // $this->takecash(5,10000,'take_cash',1);
  86. // $this->shouyi(1,10000,'user_match_audio_log',1);
  87. // $this->shouyi(2,10000,'user_match_audio_log',1);
  88. // $this->shouyi(3,10000,'user_match_audio_log',1);
  89. // $this->recharge(5,10000,'',1);
  90. // $this->recharge(4,10000,'',1);
  91. // $this->recharge(3,10000,'',1);
  92. // $this->recharge(2,10000,'',1);
  93. // $this->recharge(1,10000,'',1);
  94. }
  95. //已搬走,后台提现审核通过的方法 admin/takecash/takecash
  96. //普通邀请关系,提现
  97. /*public function takecash($user_id,$gold,$table,$table_id){
  98. }*/
  99. //已搬走,lavarel,alltools类
  100. //四大收益,视频,音频,私聊,礼物
  101. public function shouyi($user_id,$gold,$table,$table_id){
  102. //精确小数点
  103. bcscale(2);
  104. //枚举
  105. $table_enum = [
  106. 'user_match_audio_log' => [
  107. 'prefix'=>'audio',
  108. 'remark' =>'语音',
  109. ],
  110. 'user_match_typing_log' => [
  111. 'prefix'=>'typing',
  112. 'remark' =>'私聊',
  113. ],
  114. 'user_match_video_log' => [
  115. 'prefix'=>'video',
  116. 'remark' =>'视频',
  117. ],
  118. 'gift_user_typing' => [
  119. 'prefix'=>'gift',
  120. 'remark' =>'收礼',
  121. ],
  122. ];
  123. //变量前缀,钱包备注
  124. $prefix = $table_enum[$table]['prefix'];
  125. $remark = $table_enum[$table]['remark'];
  126. //收益者本人
  127. $userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$user_id)->find();
  128. //先确认自己的等级
  129. if($userinfo['group_id'] == 1){
  130. //充值者上级
  131. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
  132. if(empty($intro_userinfo)){return true;}
  133. //上级是二级邀请B1,贡献给A
  134. if($intro_userinfo['group_id'] == 2){
  135. //充值者上上级,也就是A
  136. $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();
  137. if(empty($intro_intro_userinfo)){return true;}
  138. if($intro_intro_userinfo['group_id'] == 3){
  139. $shouyi_c2a_rate = config('site.shouyi_c2a_rate');
  140. $jewelA = bcdiv(bcmul($gold,$shouyi_c2a_rate),100);
  141. $result = model('Wallet')->lockChangeAccountRemain($intro_intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
  142. if($result['status']===false)
  143. {
  144. Db::rollback();
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. //上级是A
  151. if($intro_userinfo['group_id'] == 3){
  152. $shouyi_b2a_rate = config('site.shouyi_b2a_rate');//b2a 与 b02a相等
  153. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  154. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
  155. if($result['status']===false)
  156. {
  157. Db::rollback();
  158. return false;
  159. }
  160. return true;//结束了
  161. }
  162. }
  163. if($userinfo['group_id'] == 2){
  164. //充值者上级
  165. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
  166. if(empty($intro_userinfo)){return true;}
  167. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  168. /*if($intro_userinfo['group_id'] == 3){//不是返给一级代理,而是给上级,因为可能和上级解绑放到自己名下了 */
  169. $shouyi_b2a_rate = config('site.shouyi_b2a_rate');//b2a 与 b02a相等
  170. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  171. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
  172. if($result['status']===false)
  173. {
  174. Db::rollback();
  175. return false;
  176. }
  177. return true;//结束了
  178. /*}*/
  179. }
  180. //没有推荐人,自己等级最高
  181. if($userinfo['group_id'] == 3 && !empty($userinfo['intro_uid'])){
  182. //充值者上级
  183. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
  184. if(empty($intro_userinfo)){return true;}
  185. $shouyi_b2a_rate = config('site.shouyi_b2a_rate');
  186. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  187. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,52, $userinfo['username'].$remark,$table,$table_id);
  188. if($result['status']===false)
  189. {
  190. Db::rollback();
  191. return false;
  192. }
  193. return true;//结束了
  194. }
  195. return true;
  196. }
  197. //已搬走,lavarel,alltools类
  198. //充值后奖金
  199. public function recharge($user_id,$gold,$table,$payorder_id){
  200. //充值者本人
  201. $userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$user_id)->find();
  202. //个人直推邀请充值
  203. if(!empty($userinfo['invite_uid']) && $userinfo['gender'] == 1 && empty($userinfo['intro_uid'])){
  204. $recharge_invite_rate = config('site.invite_recharge_rate');
  205. $jewel = bcdiv(bcmul($gold,$recharge_invite_rate,1),100,1);
  206. $result = model('Wallet')->lockChangeAccountRemain($userinfo['invite_uid'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  207. if($result['status']===false)
  208. {
  209. Db::rollback();
  210. return false;
  211. }
  212. }
  213. //没有推荐人,女性充值不返
  214. if(empty($userinfo['intro_uid']) || $userinfo['gender'] == 0){
  215. return true;
  216. }
  217. //精确小数点
  218. bcscale(2);
  219. //自己等级最高
  220. if($userinfo['group_id'] == 3 && !empty($userinfo['intro_uid'])){
  221. //充值者上级
  222. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
  223. if(empty($intro_userinfo)){return true;}
  224. $recharge_b2a_rate = config('site.recharge_b2a_rate');
  225. $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
  226. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
  227. if($result['status']===false)
  228. {
  229. Db::rollback();
  230. return false;
  231. }
  232. return true;//结束了
  233. }
  234. if($userinfo['group_id'] == 2){
  235. //充值者上级
  236. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
  237. if(empty($intro_userinfo)){return true;}
  238. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  239. /*if($intro_userinfo['group_id'] == 3){*/
  240. $recharge_b2a_rate = config('site.recharge_b2a_rate');//b2a 与 b02a相等
  241. $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
  242. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
  243. if($result['status']===false)
  244. {
  245. Db::rollback();
  246. return false;
  247. }
  248. return true;//结束了
  249. /*}*/
  250. }
  251. if($userinfo['group_id'] == 1){
  252. //充值者上级
  253. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,invite_uid,agent_id,gender')->where('id',$userinfo['intro_uid'])->find();
  254. if(empty($intro_userinfo)){return true;}
  255. //上级是二级邀请B1,本人C1,贡献给B和A(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
  256. if($intro_userinfo['group_id'] == 2){
  257. //充值者上上级,也就是A
  258. $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();
  259. if(empty($intro_intro_userinfo)){return true;}
  260. if($intro_intro_userinfo['group_id'] == 3){
  261. $recharge_c2a_rate = config('site.recharge_c2a_rate');
  262. $jewelA = bcdiv(bcmul($gold,$recharge_c2a_rate),100);
  263. $result = model('Wallet')->lockChangeAccountRemain($intro_intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
  264. if($result['status']===false)
  265. {
  266. Db::rollback();
  267. return false;
  268. }
  269. }
  270. return true;
  271. }
  272. //上级是普通用户,那自己肯定也是普通,则上级可能是D1、C1,找到上上级,如果也是普通C1,停止。如果上上级是B1,则一直追到A1
  273. if(1 == 2 && $intro_userinfo['group_id'] == 1){
  274. //先给上级普通用户返
  275. $recharge_d2c_rate = config('site.recharge_d2c_rate');//d2c 或 e2d
  276. $jewelC = bcdiv(bcmul($gold,$recharge_d2c_rate),100);
  277. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'agentjewel',$jewelC,51, $userinfo['username'].'充值','pay_order',$payorder_id);
  278. if($result['status']===false)
  279. {
  280. Db::rollback();
  281. return false;
  282. }
  283. //充值者上上级
  284. $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();
  285. if(empty($intro_intro_userinfo)){return true;}
  286. //上上级也是普通C1,说明自己在E1,上级是D1,停止
  287. if($intro_intro_userinfo['group_id'] == 1){
  288. return true;
  289. }
  290. //上上级是B1,上级是C1,自己是D1,
  291. if($intro_intro_userinfo['group_id'] == 2){
  292. //充值者上上级,也就是A
  293. $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();
  294. if(empty($intro_intro_intro_userinfo)){return true;}
  295. if($intro_intro_intro_userinfo['group_id'] == 3){
  296. $recharge_c2a_rate = config('site.recharge_c2a_rate');
  297. $jewelA = bcdiv(bcmul($jewelC,$recharge_c2a_rate),100);
  298. $result = model('Wallet')->lockChangeAccountRemain($intro_intro_intro_userinfo['id'],'agentjewel',$jewelA,51, $userinfo['username'].'充值','pay_order',$payorder_id);
  299. if($result['status']===false)
  300. {
  301. Db::rollback();
  302. return false;
  303. }
  304. }
  305. }
  306. return true;
  307. }
  308. }
  309. return true;
  310. }
  311. //充值VIP 异步回调对外方法
  312. public function vip_notify_base(){
  313. //验签
  314. $paytype = input('paytype','wechat');
  315. $notify_file = $this->notify_log_start($paytype);
  316. $pay = Service::checkNotify($paytype);
  317. if (!$pay) {
  318. echo '签名错误';
  319. exit;
  320. }
  321. //验证,拿订单号等信息
  322. $data = $pay->verify();
  323. $out_trade_no = $data['out_trade_no'];
  324. //订单查询
  325. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  326. if(empty($info)){
  327. echo $pay->success();
  328. exit;
  329. }
  330. if($info['order_status'] != 0)
  331. {
  332. echo $pay->success();
  333. exit;
  334. }
  335. //你可以在此编写订单逻辑
  336. $rs = $this->vip_notify_do($out_trade_no);
  337. if($rs === false){
  338. //不论结果都应返回success
  339. echo $pay->success();
  340. exit;
  341. }else{
  342. //不论结果都应返回success
  343. echo $pay->success();
  344. exit;
  345. }
  346. //默认
  347. echo $pay->success();
  348. exit;
  349. }
  350. //充值金币 逻辑
  351. private function vip_notify_do($out_trade_no){
  352. Db::startTrans();
  353. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  354. if (empty($orderInfo)) {
  355. Db::rollback();
  356. return false;
  357. }
  358. if($orderInfo['order_status'] != 0){
  359. Db::rollback();
  360. return false;
  361. }
  362. //逻辑开始
  363. //先充值
  364. $args = json_decode($orderInfo['args'],true);
  365. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  366. if($user_info['vip_endtime'] < time()){
  367. //过期了
  368. $vip_endtime = time() + (intval($args['days']) * 86400);
  369. $vip_type = 1;
  370. }else{
  371. //追加vip
  372. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  373. $vip_type = 2;
  374. }
  375. $update_data = [
  376. 'vip_endtime'=>$vip_endtime,
  377. ];
  378. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  379. //记录日志
  380. $log_data = [
  381. 'user_id' => $orderInfo['user_id'],
  382. 'before' => $user_info['vip_endtime'],
  383. 'change_value' => intval($args['days']) * 86400,
  384. 'remain' => $vip_endtime,
  385. 'remark' => '安卓购买vip',
  386. 'createtime' => time(),
  387. 'vip_type' => $vip_type,
  388. ];
  389. Db::name('user_vip_log')->insertGetId($log_data);
  390. if($result === false)
  391. {
  392. Db::rollback();
  393. return false;
  394. }
  395. //逻辑结束
  396. //状态
  397. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  398. if($ros === false) {
  399. Db::rollback();
  400. return false;
  401. }
  402. //默认提交
  403. Db::commit();
  404. return true;
  405. }
  406. //异步日志
  407. private function notify_log_start($paytype = 'wechat'){
  408. //记录支付回调数据
  409. ignore_user_abort(); // run script in background
  410. set_time_limit(30);
  411. // 日志文件 start
  412. $log_base_dir = '../paylog/'.$paytype.'/';
  413. if (!is_dir($log_base_dir))
  414. {
  415. mkdir($log_base_dir, 0770, true);
  416. @chmod($log_base_dir, 0770);
  417. }
  418. $notify_file = $log_base_dir.'notify.txt';
  419. if(!file_exists($notify_file)) {
  420. @touch($notify_file);
  421. @chmod($notify_file, 0770);
  422. }
  423. if(filesize($notify_file)>5242880)//大于5M自动切换
  424. {
  425. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  426. }
  427. if(!file_exists($notify_file)) {
  428. @touch($notify_file);
  429. @chmod($notify_file, 0770);
  430. }
  431. // 日志文件 end
  432. //开始写入
  433. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  434. if($_REQUEST && $paytype == 'alipay') {
  435. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  436. } else {
  437. $xml = file_get_contents("php://input");
  438. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  439. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  440. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  441. }
  442. ini_set('display_errors','On');
  443. return $notify_file;
  444. }
  445. }