Notify.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 recharge($user_id,$gold,$payorder_id){
  85. //充值者本人
  86. $userinfo = Db::name('user')->field('id,username,group_id,intro_uid,gender')->where('id',$user_id)->find();
  87. //没有推荐人,自己等级最高,女性充值不返
  88. if(empty($userinfo['intro_uid']) || $userinfo['group_id'] == 3 || $userinfo['gender'] == 0){
  89. return true;
  90. }
  91. //充值者上级
  92. $intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,gender')->where('id',$userinfo['intro_uid'])->find();
  93. if(empty($intro_userinfo)){return true;}
  94. //精确小数点
  95. bcscale(1);
  96. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  97. if($intro_userinfo['group_id'] == 3){
  98. $recharge_b2a_rate = config('site.recharge_b2a_rate');
  99. $jewel = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
  100. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  101. if($result['status']===false)
  102. {
  103. Db::rollback();
  104. return false;
  105. }
  106. return true;//结束了
  107. }
  108. //上级是二级邀请B0、B1,本人C1,贡献给A和B(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
  109. if($intro_userinfo['group_id'] == 2){
  110. $recharge_c2b_rate = config('site.recharge_c2b_rate');
  111. $jewel = bcdiv(bcmul($gold,$recharge_c2b_rate),100);
  112. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  113. if($result['status']===false)
  114. {
  115. Db::rollback();
  116. return false;
  117. }
  118. //充值者上上级,也就是A
  119. $intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,gender')->where('id',$intro_userinfo['intro_uid'])->find();
  120. if(empty($intro_intro_userinfo)){return true;}
  121. if($intro_intro_userinfo['group_id'] == 3){
  122. $recharge_b2a_rate = config('site.recharge_b2a_rate');
  123. $jewel = bcdiv(bcmul($jewel,$recharge_b2a_rate),100);
  124. $result = model('Wallet')->lockChangeAccountRemain($intro_intro_userinfo['id'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  125. if($result['status']===false)
  126. {
  127. Db::rollback();
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. //上级是普通用户,那自己肯定也是普通,则上级可能是D1、C1,找到上上级,如果也是普通C1,停止。如果上上级是B1,则一直追到A1
  134. if($intro_userinfo['group_id'] == 1){
  135. //先给上级普通用户返
  136. $recharge_d2c_rate = config('site.recharge_d2c_rate');//d2c 或 e2d
  137. $jewel = bcdiv(bcmul($gold,$recharge_d2c_rate),100);
  138. $result = model('Wallet')->lockChangeAccountRemain($intro_userinfo['id'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  139. if($result['status']===false)
  140. {
  141. Db::rollback();
  142. return false;
  143. }
  144. //充值者上上级
  145. $intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,gender')->where('id',$intro_userinfo['intro_uid'])->find();
  146. if(empty($intro_intro_userinfo)){return true;}
  147. //上上级也是普通C1,说明自己在E1,上级是D1,停止
  148. if($intro_intro_userinfo['group_id'] == 1){
  149. return true;
  150. }
  151. //上上级是B1,上级是C1,自己是D1,
  152. if($intro_intro_userinfo['group_id'] == 2){
  153. //先给B1
  154. $recharge_c2b_rate = config('site.recharge_c2b_rate');
  155. $jewel = bcdiv(bcmul($gold,$recharge_c2b_rate),100);
  156. $result = model('Wallet')->lockChangeAccountRemain($intro_intro_userinfo['id'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  157. if($result['status']===false)
  158. {
  159. Db::rollback();
  160. return false;
  161. }
  162. //充值者上上级,也就是A
  163. $intro_intro_intro_userinfo = Db::name('user')->field('id,username,group_id,intro_uid,gender')->where('id',$intro_intro_userinfo['intro_uid'])->find();
  164. if(empty($intro_intro_intro_userinfo)){return true;}
  165. if($intro_intro_intro_userinfo['group_id'] == 3){
  166. $recharge_b2a_rate = config('site.recharge_b2a_rate');
  167. $jewel = bcdiv(bcmul($jewel,$recharge_b2a_rate),100);
  168. $result = model('Wallet')->lockChangeAccountRemain($intro_intro_intro_userinfo['id'],'jewel',$jewel,41, $userinfo['username'].'充值','pay_order',$payorder_id);
  169. if($result['status']===false)
  170. {
  171. Db::rollback();
  172. return false;
  173. }
  174. }
  175. }
  176. return true;
  177. }
  178. return true;
  179. }
  180. //充值VIP 异步回调对外方法
  181. public function vip_notify_base(){
  182. //验签
  183. $paytype = input('paytype','wechat');
  184. $notify_file = $this->notify_log_start($paytype);
  185. $pay = Service::checkNotify($paytype);
  186. if (!$pay) {
  187. echo '签名错误';
  188. exit;
  189. }
  190. //验证,拿订单号等信息
  191. $data = $pay->verify();
  192. $out_trade_no = $data['out_trade_no'];
  193. //订单查询
  194. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  195. if(empty($info)){
  196. echo $pay->success();
  197. exit;
  198. }
  199. if($info['order_status'] != 0)
  200. {
  201. echo $pay->success();
  202. exit;
  203. }
  204. //你可以在此编写订单逻辑
  205. $rs = $this->vip_notify_do($out_trade_no);
  206. if($rs === false){
  207. //不论结果都应返回success
  208. echo $pay->success();
  209. exit;
  210. }else{
  211. //不论结果都应返回success
  212. echo $pay->success();
  213. exit;
  214. }
  215. //默认
  216. echo $pay->success();
  217. exit;
  218. }
  219. //充值金币 逻辑
  220. private function vip_notify_do($out_trade_no){
  221. Db::startTrans();
  222. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  223. if (empty($orderInfo)) {
  224. Db::rollback();
  225. return false;
  226. }
  227. if($orderInfo['order_status'] != 0){
  228. Db::rollback();
  229. return false;
  230. }
  231. //逻辑开始
  232. //先充值
  233. $args = json_decode($orderInfo['args'],true);
  234. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  235. if($user_info['vip_endtime'] < time()){
  236. //过期了
  237. $vip_endtime = time() + (intval($args['days']) * 86400);
  238. $vip_type = 1;
  239. }else{
  240. //追加vip
  241. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  242. $vip_type = 2;
  243. }
  244. $update_data = [
  245. 'vip_endtime'=>$vip_endtime,
  246. ];
  247. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  248. //记录日志
  249. $log_data = [
  250. 'user_id' => $orderInfo['user_id'],
  251. 'before' => $user_info['vip_endtime'],
  252. 'change_value' => intval($args['days']) * 86400,
  253. 'remain' => $vip_endtime,
  254. 'remark' => '安卓购买vip',
  255. 'createtime' => time(),
  256. 'vip_type' => $vip_type,
  257. ];
  258. Db::name('user_vip_log')->insertGetId($log_data);
  259. if($result === false)
  260. {
  261. Db::rollback();
  262. return false;
  263. }
  264. //逻辑结束
  265. //状态
  266. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  267. if($ros === false) {
  268. Db::rollback();
  269. return false;
  270. }
  271. //默认提交
  272. Db::commit();
  273. return true;
  274. }
  275. //异步日志
  276. private function notify_log_start($paytype = 'wechat'){
  277. //记录支付回调数据
  278. ignore_user_abort(); // run script in background
  279. set_time_limit(30);
  280. // 日志文件 start
  281. $log_base_dir = '../paylog/'.$paytype.'/';
  282. if (!is_dir($log_base_dir))
  283. {
  284. mkdir($log_base_dir, 0770, true);
  285. @chmod($log_base_dir, 0770);
  286. }
  287. $notify_file = $log_base_dir.'notify.txt';
  288. if(!file_exists($notify_file)) {
  289. @touch($notify_file);
  290. @chmod($notify_file, 0770);
  291. }
  292. if(filesize($notify_file)>5242880)//大于5M自动切换
  293. {
  294. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  295. }
  296. if(!file_exists($notify_file)) {
  297. @touch($notify_file);
  298. @chmod($notify_file, 0770);
  299. }
  300. // 日志文件 end
  301. //开始写入
  302. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  303. if($_REQUEST && $paytype == 'alipay') {
  304. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  305. } else {
  306. $xml = file_get_contents("php://input");
  307. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  308. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  309. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  310. }
  311. ini_set('display_errors','On');
  312. return $notify_file;
  313. }
  314. }