Notify.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\PayOrderModel;
  5. use app\utils\LogUtil;
  6. use think\Db;
  7. use addons\epay\library\Service;
  8. use think\Request;
  9. /**
  10. * 订单支付回调
  11. */
  12. class Notify extends Api
  13. {
  14. // 日志模块名称
  15. const LOG_MODULE = 'Notify';
  16. protected $noNeedLogin = ['*'];
  17. protected $noNeedRight = ['*'];
  18. public function __construct(Request $request = null)
  19. {
  20. parent::__construct($request);
  21. //日志统一写入
  22. register_shutdown_function([new LogUtil, 'close']);
  23. LogUtil::getInstance('Api/'); //设置日志存入通道
  24. }
  25. //人民币充值 异步回调对外方法
  26. public function recharge_notify_base(){
  27. //验签
  28. $paytype = input('paytype','wechat');
  29. $notify_file = $this->notify_log_start($paytype);
  30. $pay = Service::checkNotify($paytype);
  31. if (!$pay) {
  32. echo '签名错误';
  33. exit;
  34. }
  35. //验证,拿订单号等信息
  36. $data = $pay->verify();
  37. $out_trade_no = $data['out_trade_no'];
  38. //订单查询
  39. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  40. if(empty($info)){
  41. echo $pay->success();
  42. exit;
  43. }
  44. if($info['order_status'] != 0)
  45. {
  46. echo $pay->success();
  47. exit;
  48. }
  49. //你可以在此编写订单逻辑
  50. $rs = $this->recharge_notify_do($out_trade_no);
  51. if($rs === false){
  52. //不论结果都应返回success
  53. echo $pay->success();
  54. exit;
  55. }else{
  56. //不论结果都应返回success
  57. echo $pay->success();
  58. exit;
  59. }
  60. //默认
  61. echo $pay->success();
  62. exit;
  63. }
  64. //人民币充值 逻辑
  65. private function recharge_notify_do($out_trade_no){
  66. Db::startTrans();
  67. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  68. if (empty($orderInfo)) {
  69. Db::rollback();
  70. return false;
  71. }
  72. if($orderInfo['order_status'] != 0){
  73. Db::rollback();
  74. return false;
  75. }
  76. //逻辑开始
  77. $order_amount = $orderInfo['order_amount'];
  78. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'money',$order_amount,10, '充值','pay_order',$orderInfo['id']);
  79. if($result['status']===false)
  80. {
  81. Db::rollback();
  82. return false;
  83. }
  84. //逻辑结束
  85. //状态
  86. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  87. if($ros === false) {
  88. Db::rollback();
  89. return false;
  90. }
  91. //默认提交
  92. Db::commit();
  93. return true;
  94. }
  95. //充值VIP 异步回调对外方法
  96. public function vip_notify_base(){
  97. //验签
  98. $paytype = input('paytype','wechat');
  99. $notify_file = $this->notify_log_start($paytype);
  100. $pay = Service::checkNotify($paytype);
  101. if (!$pay) {
  102. echo '签名错误';
  103. exit;
  104. }
  105. //验证,拿订单号等信息
  106. $data = $pay->verify();
  107. $out_trade_no = $data['out_trade_no'];
  108. //订单查询
  109. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  110. if(empty($info)){
  111. echo $pay->success();
  112. exit;
  113. }
  114. if($info['order_status'] != 0)
  115. {
  116. echo $pay->success();
  117. exit;
  118. }
  119. //你可以在此编写订单逻辑
  120. $rs = $this->vip_notify_do($out_trade_no);
  121. if($rs === false){
  122. //不论结果都应返回success
  123. echo $pay->success();
  124. exit;
  125. }else{
  126. //不论结果都应返回success
  127. echo $pay->success();
  128. exit;
  129. }
  130. //默认
  131. echo $pay->success();
  132. exit;
  133. }
  134. //充值金币 逻辑
  135. private function vip_notify_do($out_trade_no){
  136. Db::startTrans();
  137. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  138. if (empty($orderInfo)) {
  139. Db::rollback();
  140. return false;
  141. }
  142. if($orderInfo['order_status'] != 0){
  143. Db::rollback();
  144. return false;
  145. }
  146. //逻辑开始
  147. //先充值
  148. $args = json_decode($orderInfo['args'],true);
  149. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  150. if($user_info['vip_endtime'] < time()){
  151. //过期了
  152. $vip_endtime = time() + (intval($args['days']) * 86400);
  153. $vip_type = 1;
  154. }else{
  155. //追加vip
  156. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  157. $vip_type = 2;
  158. }
  159. $update_data = [
  160. 'vip_endtime'=>$vip_endtime,
  161. ];
  162. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  163. //记录日志
  164. $log_data = [
  165. 'user_id' => $orderInfo['user_id'],
  166. 'before' => $user_info['vip_endtime'],
  167. 'change_value' => intval($args['days']) * 86400,
  168. 'remain' => $vip_endtime,
  169. 'remark' => '购买vip',
  170. 'createtime' => time(),
  171. 'vip_type' => $vip_type,
  172. ];
  173. Db::name('user_vip_log')->insertGetId($log_data);
  174. if($result === false)
  175. {
  176. Db::rollback();
  177. return false;
  178. }
  179. //逻辑结束
  180. //状态
  181. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  182. if($ros === false) {
  183. Db::rollback();
  184. return false;
  185. }
  186. //默认提交
  187. Db::commit();
  188. return true;
  189. }
  190. //异步日志
  191. private function notify_log_start($paytype = 'wechat'){
  192. //记录支付回调数据
  193. ignore_user_abort(); // run script in background
  194. set_time_limit(30);
  195. // 日志文件 start
  196. $log_base_dir = '../paylog/'.$paytype.'/';
  197. if (!is_dir($log_base_dir))
  198. {
  199. mkdir($log_base_dir, 0770, true);
  200. @chmod($log_base_dir, 0770);
  201. }
  202. $notify_file = $log_base_dir.'notify.txt';
  203. if(!file_exists($notify_file)) {
  204. @touch($notify_file);
  205. @chmod($notify_file, 0770);
  206. }
  207. if(filesize($notify_file)>5242880)//大于5M自动切换
  208. {
  209. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  210. }
  211. if(!file_exists($notify_file)) {
  212. @touch($notify_file);
  213. @chmod($notify_file, 0770);
  214. }
  215. // 日志文件 end
  216. //开始写入
  217. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  218. if($_REQUEST && $paytype == 'alipay') {
  219. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  220. } else {
  221. $xml = file_get_contents("php://input");
  222. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  223. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  224. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  225. }
  226. ini_set('display_errors','On');
  227. return $notify_file;
  228. }
  229. // 老年大学活动报名支付回调
  230. public function university_event_wechat()
  231. {
  232. LogUtil::info('老年大学活动 微信支付回调', self::LOG_MODULE,__FUNCTION__, $this->request->param());
  233. $pay = Service::checkNotify('wechat');
  234. if (!$pay){
  235. LogUtil::info('签名错误', self::LOG_MODULE,__FUNCTION__);
  236. return $this->error('签名错误 err01');
  237. }
  238. $data = $pay->verify();
  239. $out_trade_no = $data['out_trade_no'];
  240. [$res,$msg] = PayOrderModel::university_event($out_trade_no);
  241. if (!$res){
  242. LogUtil::info($msg, self::LOG_MODULE,__FUNCTION__);
  243. return $this->error('签名错误 err02');
  244. }
  245. LogUtil::info('处理成功', self::LOG_MODULE,__FUNCTION__);
  246. return $pay->success();
  247. }
  248. // 老年大学活动报名支付回调
  249. public function university_event_alipay()
  250. {
  251. LogUtil::info('老年大学活动 支付宝支付回调', self::LOG_MODULE,__FUNCTION__, $this->request->param());
  252. $pay = Service::checkNotify('alipay');
  253. if (!$pay){
  254. LogUtil::info('签名错误', self::LOG_MODULE,__FUNCTION__);
  255. return $this->error('签名错误 err01');
  256. }
  257. $data = $pay->verify();
  258. $out_trade_no = $data['out_trade_no'];
  259. [$res,$msg] = PayOrderModel::university_event($out_trade_no);
  260. if (!$res){
  261. LogUtil::info($msg, self::LOG_MODULE,__FUNCTION__);
  262. return $this->error('签名错误 err02');
  263. }
  264. LogUtil::info('处理成功', self::LOG_MODULE,__FUNCTION__);
  265. return $pay->success();
  266. }
  267. }