Notify.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. }
  132. //上级是普通用户,则上级可能是C1、D1,再向上找到B
  133. }
  134. //充值VIP 异步回调对外方法
  135. public function vip_notify_base(){
  136. //验签
  137. $paytype = input('paytype','wechat');
  138. $notify_file = $this->notify_log_start($paytype);
  139. $pay = Service::checkNotify($paytype);
  140. if (!$pay) {
  141. echo '签名错误';
  142. exit;
  143. }
  144. //验证,拿订单号等信息
  145. $data = $pay->verify();
  146. $out_trade_no = $data['out_trade_no'];
  147. //订单查询
  148. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  149. if(empty($info)){
  150. echo $pay->success();
  151. exit;
  152. }
  153. if($info['order_status'] != 0)
  154. {
  155. echo $pay->success();
  156. exit;
  157. }
  158. //你可以在此编写订单逻辑
  159. $rs = $this->vip_notify_do($out_trade_no);
  160. if($rs === false){
  161. //不论结果都应返回success
  162. echo $pay->success();
  163. exit;
  164. }else{
  165. //不论结果都应返回success
  166. echo $pay->success();
  167. exit;
  168. }
  169. //默认
  170. echo $pay->success();
  171. exit;
  172. }
  173. //充值金币 逻辑
  174. private function vip_notify_do($out_trade_no){
  175. Db::startTrans();
  176. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  177. if (empty($orderInfo)) {
  178. Db::rollback();
  179. return false;
  180. }
  181. if($orderInfo['order_status'] != 0){
  182. Db::rollback();
  183. return false;
  184. }
  185. //逻辑开始
  186. //先充值
  187. $args = json_decode($orderInfo['args'],true);
  188. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  189. if($user_info['vip_endtime'] < time()){
  190. //过期了
  191. $vip_endtime = time() + (intval($args['days']) * 86400);
  192. $vip_type = 1;
  193. }else{
  194. //追加vip
  195. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  196. $vip_type = 2;
  197. }
  198. $update_data = [
  199. 'vip_endtime'=>$vip_endtime,
  200. ];
  201. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  202. //记录日志
  203. $log_data = [
  204. 'user_id' => $orderInfo['user_id'],
  205. 'before' => $user_info['vip_endtime'],
  206. 'change_value' => intval($args['days']) * 86400,
  207. 'remain' => $vip_endtime,
  208. 'remark' => '安卓购买vip',
  209. 'createtime' => time(),
  210. 'vip_type' => $vip_type,
  211. ];
  212. Db::name('user_vip_log')->insertGetId($log_data);
  213. if($result === false)
  214. {
  215. Db::rollback();
  216. return false;
  217. }
  218. //逻辑结束
  219. //状态
  220. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  221. if($ros === false) {
  222. Db::rollback();
  223. return false;
  224. }
  225. //默认提交
  226. Db::commit();
  227. return true;
  228. }
  229. //异步日志
  230. private function notify_log_start($paytype = 'wechat'){
  231. //记录支付回调数据
  232. ignore_user_abort(); // run script in background
  233. set_time_limit(30);
  234. // 日志文件 start
  235. $log_base_dir = '../paylog/'.$paytype.'/';
  236. if (!is_dir($log_base_dir))
  237. {
  238. mkdir($log_base_dir, 0770, true);
  239. @chmod($log_base_dir, 0770);
  240. }
  241. $notify_file = $log_base_dir.'notify.txt';
  242. if(!file_exists($notify_file)) {
  243. @touch($notify_file);
  244. @chmod($notify_file, 0770);
  245. }
  246. if(filesize($notify_file)>5242880)//大于5M自动切换
  247. {
  248. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  249. }
  250. if(!file_exists($notify_file)) {
  251. @touch($notify_file);
  252. @chmod($notify_file, 0770);
  253. }
  254. // 日志文件 end
  255. //开始写入
  256. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  257. if($_REQUEST && $paytype == 'alipay') {
  258. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  259. } else {
  260. $xml = file_get_contents("php://input");
  261. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  262. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  263. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  264. }
  265. ini_set('display_errors','On');
  266. return $notify_file;
  267. }
  268. }