Notify.php 18 KB

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