Notifynew.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 Notifynew 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. //加钱
  66. $args = json_decode($orderInfo['args'],true);
  67. $extend = [
  68. 'cityname' => $args['cityname']
  69. ];
  70. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],$args['jewel'],'+',0,'金币充值',1,'jewel',$extend);
  71. if($result['status']===false)
  72. {
  73. Db::rollback();
  74. return false;
  75. }
  76. //找到上级用户并加钱
  77. $userinfo = Db::name('user')->where('id',$orderInfo['user_id'])->field('id,nickname,pre_userid')->find();
  78. if($userinfo['pre_userid']){
  79. $bili = config('site.introsite_recharge_intro_bili') ?: 3;
  80. $jewel = bcdiv(bcmul($args['jewel'],$bili,0),100,0);
  81. if($jewel > 0){
  82. $result = model('Wallet')->lockChangeAccountRemain($userinfo['pre_userid'],$jewel,'+',0,$userinfo['nickname'].'充值金币返奖励',2,'jewel');
  83. if($result['status']===false)
  84. {
  85. Db::rollback();
  86. return false;
  87. }
  88. }
  89. }
  90. //逻辑结束
  91. //状态
  92. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  93. if($ros === false) {
  94. Db::rollback();
  95. return false;
  96. }
  97. //默认提交
  98. Db::commit();
  99. return true;
  100. }
  101. //充值VIP 异步回调对外方法
  102. public function vip_notify_base(){
  103. //验签
  104. $paytype = input('paytype','wechat');
  105. $notify_file = $this->notify_log_start($paytype);
  106. $pay = Service::checkNotify($paytype);
  107. if (!$pay) {
  108. echo '签名错误';
  109. exit;
  110. }
  111. //验证,拿订单号等信息
  112. $data = $pay->verify();
  113. $out_trade_no = $data['out_trade_no'];
  114. //订单查询
  115. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  116. if(empty($info)){
  117. echo $pay->success();
  118. exit;
  119. }
  120. if($info['order_status'] != 0)
  121. {
  122. echo $pay->success();
  123. exit;
  124. }
  125. //你可以在此编写订单逻辑
  126. $rs = $this->vip_notify_do($out_trade_no);
  127. if($rs === false){
  128. //不论结果都应返回success
  129. echo $pay->success();
  130. exit;
  131. }else{
  132. //不论结果都应返回success
  133. echo $pay->success();
  134. exit;
  135. }
  136. //默认
  137. echo $pay->success();
  138. exit;
  139. }
  140. //充值金币 逻辑
  141. private function vip_notify_do($out_trade_no){
  142. Db::startTrans();
  143. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  144. if (empty($orderInfo)) {
  145. Db::rollback();
  146. return false;
  147. }
  148. if($orderInfo['order_status'] != 0){
  149. Db::rollback();
  150. return false;
  151. }
  152. //逻辑开始
  153. //先充值
  154. $args = json_decode($orderInfo['args'],true);
  155. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  156. if($user_info['vip_endtime'] < time()){
  157. //过期了
  158. $vip_endtime = time() + (intval($args['days']) * 86400);
  159. }else{
  160. //追加vip
  161. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  162. }
  163. $update_data = [
  164. 'vip_endtime'=>$vip_endtime,
  165. ];
  166. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  167. if($result === false)
  168. {
  169. Db::rollback();
  170. return false;
  171. }
  172. //逻辑结束
  173. //状态
  174. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  175. if($ros === false) {
  176. Db::rollback();
  177. return false;
  178. }
  179. //默认提交
  180. Db::commit();
  181. return true;
  182. }
  183. //异步日志
  184. private function notify_log_start($paytype = 'wechat'){
  185. //记录支付回调数据
  186. ignore_user_abort(); // run script in background
  187. set_time_limit(30);
  188. // 日志文件 start
  189. $log_base_dir = '../paylog/'.$paytype.'/';
  190. if (!is_dir($log_base_dir))
  191. {
  192. mkdir($log_base_dir, 0770, true);
  193. @chmod($log_base_dir, 0770);
  194. }
  195. $notify_file = $log_base_dir.'notify.txt';
  196. if(!file_exists($notify_file)) {
  197. @touch($notify_file);
  198. @chmod($notify_file, 0770);
  199. }
  200. if(filesize($notify_file)>5242880)//大于5M自动切换
  201. {
  202. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  203. }
  204. if(!file_exists($notify_file)) {
  205. @touch($notify_file);
  206. @chmod($notify_file, 0770);
  207. }
  208. // 日志文件 end
  209. //开始写入
  210. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  211. if($_REQUEST && $paytype == 'alipay') {
  212. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  213. } else {
  214. $xml = file_get_contents("php://input");
  215. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  216. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  217. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  218. }
  219. ini_set('display_errors','On');
  220. return $notify_file;
  221. }
  222. }