Notify.php 16 KB

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