Notify.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 gold_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('pay_no',$out_trade_no)->find();
  28. if(empty($info)){
  29. echo $pay->success();
  30. exit;
  31. }
  32. if($info['status'] != 0)
  33. {
  34. echo $pay->success();
  35. exit;
  36. }
  37. //你可以在此编写订单逻辑
  38. $rs = $this->gold_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 gold_notify_do($out_trade_no){
  54. Db::startTrans();
  55. $orderInfo = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->lock(true)->find();
  56. if (empty($orderInfo)) {
  57. Db::rollback();
  58. return false;
  59. }
  60. if($orderInfo['status'] != 0){
  61. Db::rollback();
  62. return false;
  63. }
  64. //逻辑开始
  65. $remark = $orderInfo['payment_class'] == 'alipay' ? '支付宝' : '微信';
  66. $args = json_decode($orderInfo['args'],true);
  67. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['gold'],10, $remark.'充值金币','pay_order',$orderInfo['id'],1);
  68. if($result['status']===false)
  69. {
  70. Db::rollback();
  71. return false;
  72. }
  73. //若有上级,给上级返利
  74. if ($args['intro_uid']) {
  75. //获取返利比率
  76. $type = 1;
  77. if ($type == 1){
  78. $intro_recharge_rebate_rate = 10;
  79. }else{
  80. $agent_info = Db::name('user')->where(['id' => $args['intro_uid']])->field('is_agent,h_intro_recharge_rebate_rate')->find();
  81. $intro_recharge_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_recharge_rebate_rate'] : (int)config('site.intro_recharge_rebate_rate');
  82. }
  83. if ($intro_recharge_rebate_rate > 0 && $intro_recharge_rebate_rate <= 100) {
  84. //上级获得金额数量
  85. $intro_uid_money = bcdiv(bcmul($args['money'],$intro_recharge_rebate_rate,2),100,2);
  86. if ($intro_uid_money > 0) {
  87. $intro_result = model('wallet')->lockChangeAccountRemain($args['intro_uid'],$orderInfo['user_id'],'money',$intro_uid_money,65,'邀请朋友充值金币奖励','pay_order',$orderInfo['id']);
  88. if($intro_result['status']===false)
  89. {
  90. Db::rollback();
  91. return false;
  92. }
  93. }
  94. }
  95. }
  96. //首充
  97. $first_map = [
  98. 'uid' => $orderInfo['user_id'],
  99. ];
  100. $first_check = Db::name('user_paygold_log')->where($first_map)->find();
  101. if(empty($first_check)){
  102. //首充日志
  103. $first_map['createtime'] = time();
  104. $first_log_id = Db::name('user_paygold_log')->insertGetId($first_map);
  105. if(!$first_log_id){
  106. Db::rollback();
  107. return false;
  108. }
  109. //送金币
  110. if(isset($args['first_gold']) && $args['first_gold'] > 0){
  111. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['first_gold'],14, '充值金币首充奖励','pay_order',$orderInfo['id']);
  112. if($result['status']===false)
  113. {
  114. Db::rollback();
  115. return false;
  116. }
  117. }
  118. //送vip
  119. if(isset($args['first_vipdays']) && $args['first_vipdays'] > 0){
  120. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  121. if($user_info['vip_endtime'] < time()){
  122. //过期了
  123. $vip_endtime = time() + (intval($args['first_vipdays']) * 86400);
  124. $vip_type = 1;
  125. }else{
  126. //追加vip
  127. $vip_endtime = $user_info['vip_endtime'] + (intval($args['first_vipdays']) * 86400);
  128. $vip_type = 2;
  129. }
  130. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update(['vip_endtime'=>$vip_endtime]);
  131. if($result === false)
  132. {
  133. Db::rollback();
  134. return false;
  135. }
  136. //记录日志
  137. $log_data = [
  138. 'user_id' => $orderInfo['user_id'],
  139. 'before' => $user_info['vip_endtime'],
  140. 'change_value' => intval($args['first_vipdays']) * 86400,
  141. 'remain' => $vip_endtime,
  142. 'remark' => '首充赠送vip',
  143. 'createtime' => time(),
  144. 'vip_type' => $vip_type,
  145. ];
  146. Db::name('user_vip_log')->insertGetId($log_data);
  147. }
  148. }
  149. //vip用户充值额外送金币
  150. if(isset($args['vip_gold']) && $args['vip_gold'] > 0){
  151. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['vip_gold'],101, 'VIP用户充值金币赠送','pay_order',$orderInfo['id']);
  152. if($result['status']===false)
  153. {
  154. Db::rollback();
  155. return false;
  156. }
  157. }
  158. //用户充值额外送钻石
  159. if(isset($args['jewel']) && $args['jewel'] > 0){
  160. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'jewel',$args['jewel'],35, '充值金币赠送钻石','pay_order',$orderInfo['id']);
  161. if($result['status']===false)
  162. {
  163. Db::rollback();
  164. return false;
  165. }
  166. }
  167. //逻辑结束
  168. //状态
  169. $ros = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->update(['status'=>1,'notifytime'=>time()]);
  170. if($ros === false) {
  171. Db::rollback();
  172. return false;
  173. }
  174. //默认提交
  175. Db::commit();
  176. return true;
  177. }
  178. //充值VIP 异步回调对外方法
  179. public function vip_notify_base(){
  180. //验签
  181. $paytype = input('paytype','wechat');
  182. $notify_file = $this->notify_log_start($paytype);
  183. $pay = Service::checkNotify($paytype);
  184. if (!$pay) {
  185. echo '签名错误';
  186. exit;
  187. }
  188. //验证,拿订单号等信息
  189. $data = $pay->verify();
  190. $out_trade_no = $data['out_trade_no'];
  191. //订单查询
  192. $info = Db::name('pay_order')->where('pay_no',$out_trade_no)->find();
  193. if(empty($info)){
  194. echo $pay->success();
  195. exit;
  196. }
  197. if($info['status'] != 0)
  198. {
  199. echo $pay->success();
  200. exit;
  201. }
  202. //你可以在此编写订单逻辑
  203. $rs = $this->vip_notify_do($out_trade_no);
  204. if($rs === false){
  205. //不论结果都应返回success
  206. echo $pay->success();
  207. exit;
  208. }else{
  209. //不论结果都应返回success
  210. echo $pay->success();
  211. exit;
  212. }
  213. //默认
  214. echo $pay->success();
  215. exit;
  216. }
  217. //充值金币 逻辑
  218. private function vip_notify_do($out_trade_no){
  219. Db::startTrans();
  220. $orderInfo = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->lock(true)->find();
  221. if (empty($orderInfo)) {
  222. Db::rollback();
  223. return false;
  224. }
  225. if($orderInfo['status'] != 0){
  226. Db::rollback();
  227. return false;
  228. }
  229. //逻辑开始
  230. //先充值
  231. $args = json_decode($orderInfo['args'],true);
  232. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  233. if($user_info['vip_endtime'] < time()){
  234. //过期了
  235. $vip_endtime = time() + (intval($args['days']) * 86400);
  236. $vip_type = 1;
  237. }else{
  238. //追加vip
  239. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  240. $vip_type = 2;
  241. }
  242. $update_data = [
  243. 'vip_endtime'=>$vip_endtime,
  244. 'vip_level' =>$args['vip_level'], //修改等级,同级不影响,向上立刻改,向下不允许
  245. 'pay_money' => bcadd($user_info['pay_money'], $args['money'], 2) //pay_money第2处修改,$args['money']是vip配置表的人民币价格
  246. ];
  247. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  248. if($result === false)
  249. {
  250. Db::rollback();
  251. return false;
  252. }
  253. //记录日志
  254. $log_data = [
  255. 'user_id' => $orderInfo['user_id'],
  256. 'before' => $user_info['vip_endtime'],
  257. 'change_value' => intval($args['days']) * 86400,
  258. 'remain' => $vip_endtime,
  259. 'remark' => '购买vip',
  260. 'createtime' => time(),
  261. 'vip_type' => $vip_type,
  262. ];
  263. Db::name('user_vip_log')->insertGetId($log_data);
  264. if ($args['gold_num'] > 0) {
  265. //赠送金币
  266. $wallet_rs = model('wallet')->lockChangeAccountRemain($orderInfo['user_id'], 0,'gold', $args['gold_num'], 17, '开通会员赠送金币');
  267. if ($wallet_rs['status'] === false) {
  268. Db::rollback();
  269. return false;
  270. }
  271. }
  272. //逻辑结束
  273. //状态
  274. $ros = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->update(['status'=>1,'notifytime'=>time()]);
  275. if($ros === false) {
  276. Db::rollback();
  277. return false;
  278. }
  279. //默认提交
  280. Db::commit();
  281. return true;
  282. }
  283. //异步日志
  284. private function notify_log_start($paytype = 'wechat'){
  285. //记录支付回调数据
  286. ignore_user_abort(); // run script in background
  287. set_time_limit(30);
  288. // 日志文件 start
  289. $log_base_dir = '../paylog/'.$paytype.'/';
  290. if (!is_dir($log_base_dir))
  291. {
  292. mkdir($log_base_dir, 0770, true);
  293. @chmod($log_base_dir, 0770);
  294. }
  295. $notify_file = $log_base_dir.'notify.txt';
  296. if(!file_exists($notify_file)) {
  297. @touch($notify_file);
  298. @chmod($notify_file, 0770);
  299. }
  300. if(filesize($notify_file)>5242880)//大于5M自动切换
  301. {
  302. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  303. }
  304. if(!file_exists($notify_file)) {
  305. @touch($notify_file);
  306. @chmod($notify_file, 0770);
  307. }
  308. // 日志文件 end
  309. //开始写入
  310. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  311. if($_REQUEST && $paytype == 'alipay') {
  312. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  313. } else {
  314. $xml = file_get_contents("php://input");
  315. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  316. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  317. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  318. }
  319. ini_set('display_errors','On');
  320. return $notify_file;
  321. }
  322. }