Notify.php 18 KB

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