Notify.php 12 KB

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