Notifynew.php 7.9 KB

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