Notify.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use app\common\library\Tenim;
  7. /**
  8. * 订单支付回调
  9. */
  10. class Notify extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. //充值金币 异步回调对外方法
  15. public function recharge_notify_base(){
  16. //验签
  17. $paytype = input('paytype','wechat');
  18. $notify_file = $this->notify_log_start($paytype);
  19. $pay = Service::checkNotify($paytype);
  20. if (!$pay) {
  21. echo '签名错误';
  22. exit;
  23. }
  24. //验证,拿订单号等信息
  25. $data = $pay->verify();
  26. $out_trade_no = $data['out_trade_no'];
  27. //订单查询
  28. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  29. if(empty($info)){
  30. return $pay->success()->send();
  31. exit;
  32. }
  33. if($info['order_status'] != 0)
  34. {
  35. return $pay->success()->send();
  36. exit;
  37. }
  38. //你可以在此编写订单逻辑
  39. $rs = $this->recharge_notify_do($out_trade_no);
  40. if($rs === false){
  41. //不论结果都应返回success
  42. return $pay->success()->send();
  43. exit;
  44. }else{
  45. //不论结果都应返回success
  46. return $pay->success()->send();
  47. exit;
  48. }
  49. //默认
  50. return $pay->success()->send();
  51. exit;
  52. }
  53. //充值金币 逻辑
  54. private function recharge_notify_do($out_trade_no){
  55. Db::startTrans();
  56. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  57. if (empty($orderInfo)) {
  58. Db::rollback();
  59. return false;
  60. }
  61. if($orderInfo['order_status'] != 0){
  62. Db::rollback();
  63. return false;
  64. }
  65. //逻辑开始
  66. //$args = json_decode($orderInfo['args'],true);
  67. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'money',$orderInfo['order_amount'],10, '余额充值','pay_order',$orderInfo['id']);
  68. if($result['status']===false)
  69. {
  70. Db::rollback();
  71. return false;
  72. }
  73. //逻辑结束
  74. //状态
  75. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  76. if($ros === false) {
  77. Db::rollback();
  78. return false;
  79. }
  80. //默认提交
  81. Db::commit();
  82. return true;
  83. }
  84. //充值金币 异步回调对外方法
  85. public function wenzhen_notify_base(){
  86. //验签
  87. $paytype = input('paytype','wechat');
  88. $notify_file = $this->notify_log_start($paytype);
  89. $pay = Service::checkNotify($paytype);
  90. if (!$pay) {
  91. echo '签名错误';
  92. exit;
  93. }
  94. //验证,拿订单号等信息
  95. $data = $pay->verify();
  96. $out_trade_no = $data['out_trade_no'];
  97. //订单查询
  98. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  99. if(empty($info)){
  100. return $pay->success()->send();
  101. exit;
  102. }
  103. if($info['order_status'] != 0)
  104. {
  105. return $pay->success()->send();
  106. exit;
  107. }
  108. //你可以在此编写订单逻辑
  109. $rs = $this->wenzhen_notify_do($out_trade_no);
  110. if($rs === false){
  111. //不论结果都应返回success
  112. return $pay->success()->send();
  113. exit;
  114. }else{
  115. //不论结果都应返回success
  116. return $pay->success()->send();
  117. exit;
  118. }
  119. //默认
  120. return $pay->success()->send();
  121. exit;
  122. }
  123. //充值金币 逻辑
  124. private function wenzhen_notify_do($out_trade_no){
  125. Db::startTrans();
  126. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  127. if (empty($orderInfo)) {
  128. Db::rollback();
  129. return false;
  130. }
  131. if($orderInfo['order_status'] != 0){
  132. Db::rollback();
  133. return false;
  134. }
  135. $nowtime = time();
  136. //逻辑开始
  137. $order_update = [
  138. 'pay_out_trade_no' => $out_trade_no,
  139. 'pay_type' => $orderInfo['pay_type'],
  140. 'pay_time' => $nowtime,
  141. 'status' => 10,
  142. ];
  143. $update_rs = Db::name('wenzhen_order')->where('id',$orderInfo['table_id'])->update($order_update);
  144. if($update_rs===false)
  145. {
  146. Db::rollback();
  147. return false;
  148. }
  149. //逻辑结束
  150. //状态
  151. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>$nowtime]);
  152. if($ros === false) {
  153. Db::rollback();
  154. return false;
  155. }
  156. //默认提交
  157. Db::commit();
  158. //发送im消息给医生
  159. $wenzhen_order = Db::name('wenzhen_order')->where('id',$orderInfo['table_id'])->find();
  160. if($wenzhen_order['order_type'] == 1){
  161. $tenim = new Tenim();
  162. $message = [
  163. 'businessID' => 'order_status',
  164. 'name' => '待接单',
  165. 'status' => '10',
  166. 'id' => $wenzhen_order['id'],
  167. 'content' => '已通知医生尽快接诊,超时自动取消订单并退款',
  168. ];
  169. $rs = $tenim->sendCustomMessageToUser('user'.$wenzhen_order['user_id'],'doctor'.$wenzhen_order['doctor_id'],$message);
  170. }
  171. return true;
  172. }
  173. ////////////////////////////////////////////////////////////////////////
  174. //充值VIP 异步回调对外方法
  175. public function vip_notify_base(){
  176. //验签
  177. $paytype = input('paytype','wechat');
  178. $notify_file = $this->notify_log_start($paytype);
  179. $pay = Service::checkNotify($paytype);
  180. if (!$pay) {
  181. echo '签名错误';
  182. exit;
  183. }
  184. //验证,拿订单号等信息
  185. $data = $pay->verify();
  186. $out_trade_no = $data['out_trade_no'];
  187. //订单查询
  188. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  189. if(empty($info)){
  190. return $pay->success()->send();
  191. exit;
  192. }
  193. if($info['order_status'] != 0)
  194. {
  195. return $pay->success()->send();
  196. exit;
  197. }
  198. //你可以在此编写订单逻辑
  199. $rs = $this->vip_notify_do($out_trade_no);
  200. if($rs === false){
  201. //不论结果都应返回success
  202. return $pay->success()->send();
  203. exit;
  204. }else{
  205. //不论结果都应返回success
  206. return $pay->success()->send();
  207. exit;
  208. }
  209. //默认
  210. return $pay->success()->send();
  211. exit;
  212. }
  213. //充值金币 逻辑
  214. private function vip_notify_do($out_trade_no){
  215. Db::startTrans();
  216. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  217. if (empty($orderInfo)) {
  218. Db::rollback();
  219. return false;
  220. }
  221. if($orderInfo['order_status'] != 0){
  222. Db::rollback();
  223. return false;
  224. }
  225. //逻辑开始
  226. //先充值
  227. $args = json_decode($orderInfo['args'],true);
  228. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  229. if($user_info['vip_endtime'] < time()){
  230. //过期了
  231. $vip_endtime = time() + (intval($args['days']) * 86400);
  232. $vip_type = 1;
  233. }else{
  234. //追加vip
  235. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  236. $vip_type = 2;
  237. }
  238. $update_data = [
  239. 'vip_endtime'=>$vip_endtime,
  240. ];
  241. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  242. //记录日志
  243. $log_data = [
  244. 'user_id' => $orderInfo['user_id'],
  245. 'before' => $user_info['vip_endtime'],
  246. 'change_value' => intval($args['days']) * 86400,
  247. 'remain' => $vip_endtime,
  248. 'remark' => '安卓购买vip',
  249. 'createtime' => time(),
  250. 'vip_type' => $vip_type,
  251. ];
  252. Db::name('user_vip_log')->insertGetId($log_data);
  253. if($result === false)
  254. {
  255. Db::rollback();
  256. return false;
  257. }
  258. //逻辑结束
  259. //状态
  260. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  261. if($ros === false) {
  262. Db::rollback();
  263. return false;
  264. }
  265. //默认提交
  266. Db::commit();
  267. return true;
  268. }
  269. //异步日志
  270. private function notify_log_start($paytype = 'wechat'){
  271. //记录支付回调数据
  272. ignore_user_abort(); // run script in background
  273. set_time_limit(30);
  274. // 日志文件 start
  275. $log_base_dir = '../epaylog/'.$paytype.'/';
  276. if (!is_dir($log_base_dir))
  277. {
  278. mkdir($log_base_dir, 0770, true);
  279. @chmod($log_base_dir, 0770);
  280. }
  281. $notify_file = $log_base_dir.'notify.txt';
  282. if(!file_exists($notify_file)) {
  283. @touch($notify_file);
  284. @chmod($notify_file, 0770);
  285. }
  286. if(filesize($notify_file)>5242880)//大于5M自动切换
  287. {
  288. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  289. }
  290. if(!file_exists($notify_file)) {
  291. @touch($notify_file);
  292. @chmod($notify_file, 0770);
  293. }
  294. // 日志文件 end
  295. //开始写入
  296. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  297. if($_REQUEST && $paytype == 'alipay') {
  298. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  299. } else {
  300. $xml = file_get_contents("php://input");
  301. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  302. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  303. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  304. }
  305. ini_set('display_errors','On');
  306. return $notify_file;
  307. }
  308. }