Notify.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 = 0;
  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. $before_level = 0;
  126. $after_level = 10;
  127. }else{
  128. //追加vip
  129. $vip_endtime = $user_info['vip_endtime'] + (intval($args['first_vipdays']) * 86400);
  130. $vip_type = 2;
  131. $before_level = $user_info['vip_level'];
  132. $after_level = $user_info['vip_level'];
  133. }
  134. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update(['vip_endtime'=>$vip_endtime]);
  135. if($result === false)
  136. {
  137. Db::rollback();
  138. return false;
  139. }
  140. //记录日志
  141. $log_data = [
  142. 'user_id' => $orderInfo['user_id'],
  143. 'before' => $user_info['vip_endtime'],
  144. 'change_value' => intval($args['first_vipdays']) * 86400,
  145. 'remain' => $vip_endtime,
  146. 'remark' => '首充赠送vip',
  147. 'createtime' => time(),
  148. 'vip_type' => $vip_type,
  149. 'before_level' => $before_level,
  150. 'after_level' => $after_level,
  151. ];
  152. Db::name('user_vip_log')->insertGetId($log_data);
  153. }
  154. }
  155. //vip用户充值额外送金币
  156. if(isset($args['vip_gold']) && $args['vip_gold'] > 0){
  157. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],0,'gold',$args['vip_gold'],101, 'VIP用户充值金币赠送','pay_order',$orderInfo['id']);
  158. if($result['status']===false)
  159. {
  160. Db::rollback();
  161. return false;
  162. }
  163. }
  164. //逻辑结束
  165. //状态
  166. $ros = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->update(['status'=>1,'notifytime'=>time()]);
  167. if($ros === false) {
  168. Db::rollback();
  169. return false;
  170. }
  171. //默认提交
  172. Db::commit();
  173. return true;
  174. }
  175. //充值VIP 异步回调对外方法
  176. public function vip_notify_base(){
  177. //验签
  178. $paytype = input('paytype','wechat');
  179. $notify_file = $this->notify_log_start($paytype);
  180. $pay = Service::checkNotify($paytype);
  181. if (!$pay) {
  182. echo '签名错误';
  183. exit;
  184. }
  185. //验证,拿订单号等信息
  186. $data = $pay->verify();
  187. $out_trade_no = $data['out_trade_no'];
  188. //订单查询
  189. $info = Db::name('pay_order')->where('pay_no',$out_trade_no)->find();
  190. if(empty($info)){
  191. echo $pay->success();
  192. exit;
  193. }
  194. if($info['status'] != 0)
  195. {
  196. echo $pay->success();
  197. exit;
  198. }
  199. //你可以在此编写订单逻辑
  200. $rs = $this->vip_notify_do($out_trade_no);
  201. if($rs === false){
  202. //不论结果都应返回success
  203. echo $pay->success();
  204. exit;
  205. }else{
  206. //不论结果都应返回success
  207. echo $pay->success();
  208. exit;
  209. }
  210. //默认
  211. echo $pay->success();
  212. exit;
  213. }
  214. public function vip_notify_test(){
  215. $out_trade_no = input('out_trade_no','');
  216. $rs = $this->vip_notify_do($out_trade_no);
  217. dump($rs);
  218. }
  219. //充值金币 逻辑
  220. private function vip_notify_do($out_trade_no){
  221. Db::startTrans();
  222. $orderInfo = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->lock(true)->find();
  223. if (empty($orderInfo)) {
  224. Db::rollback();
  225. return false;
  226. }
  227. if($orderInfo['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. $before_level = 0;
  240. $after_level = $args['vip_level'];
  241. }else{
  242. //追加vip
  243. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  244. $vip_type = 2;
  245. $before_level = $user_info['vip_level'];
  246. $after_level = $args['vip_level'];
  247. }
  248. $update_data = [
  249. 'vip_endtime'=>$vip_endtime,
  250. 'vip_level' =>$args['vip_level'], //修改等级,同级不影响,向上立刻改,向下不允许
  251. 'pay_money' => bcadd($user_info['pay_money'], $args['money'], 2) //pay_money第2处修改,$args['money']是vip配置表的人民币价格
  252. ];
  253. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  254. if($result === false)
  255. {
  256. Db::rollback();
  257. return false;
  258. }
  259. //记录日志
  260. $log_data = [
  261. 'user_id' => $orderInfo['user_id'],
  262. 'before' => $user_info['vip_endtime'],
  263. 'change_value' => intval($args['days']) * 86400,
  264. 'remain' => $vip_endtime,
  265. 'remark' => '购买vip',
  266. 'createtime' => time(),
  267. 'vip_type' => $vip_type,
  268. 'before_level' => $before_level,
  269. 'after_level' => $after_level,
  270. ];
  271. Db::name('user_vip_log')->insertGetId($log_data);
  272. if ($args['gold_num'] > 0) {
  273. //赠送金币
  274. $wallet_rs = model('wallet')->lockChangeAccountRemain($orderInfo['user_id'], 0,'gold', $args['gold_num'], 17, '开通会员赠送金币');
  275. if ($wallet_rs['status'] === false) {
  276. Db::rollback();
  277. return false;
  278. }
  279. }
  280. //逻辑结束
  281. //状态
  282. $ros = Db::name('pay_order')->where(['pay_no' => $out_trade_no])->update(['status'=>1,'notifytime'=>time()]);
  283. if($ros === false) {
  284. Db::rollback();
  285. return false;
  286. }
  287. //默认提交
  288. Db::commit();
  289. return true;
  290. }
  291. //异步日志
  292. private function notify_log_start($paytype = 'wechat'){
  293. //记录支付回调数据
  294. ignore_user_abort(); // run script in background
  295. set_time_limit(30);
  296. // 日志文件 start
  297. $log_base_dir = '../paylog/'.$paytype.'/';
  298. if (!is_dir($log_base_dir))
  299. {
  300. mkdir($log_base_dir, 0770, true);
  301. @chmod($log_base_dir, 0770);
  302. }
  303. $notify_file = $log_base_dir.'notify.txt';
  304. if(!file_exists($notify_file)) {
  305. @touch($notify_file);
  306. @chmod($notify_file, 0770);
  307. }
  308. if(filesize($notify_file)>5242880)//大于5M自动切换
  309. {
  310. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  311. }
  312. if(!file_exists($notify_file)) {
  313. @touch($notify_file);
  314. @chmod($notify_file, 0770);
  315. }
  316. // 日志文件 end
  317. //开始写入
  318. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  319. if($_REQUEST && $paytype == 'alipay') {
  320. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  321. } else {
  322. $xml = file_get_contents("php://input");
  323. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  324. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  325. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  326. }
  327. ini_set('display_errors','On');
  328. return $notify_file;
  329. }
  330. }