Notify.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. $wenzhen_order = Db::name('wenzhen_order')->where('id',$orderInfo['table_id'])->lock(true)->find();
  138. if($wenzhen_order['status'] != 0 && $wenzhen_order['status'] != 3 ){
  139. Db::rollback();
  140. return false;
  141. }
  142. $order_update = [
  143. 'pay_out_trade_no' => $out_trade_no,
  144. 'pay_type' => $orderInfo['pay_type'],
  145. 'pay_time' => $nowtime,
  146. 'status' => 10,
  147. 'cancel_time' => 0,
  148. 'cancel_reason' => '',
  149. 'finish_time' => 0,
  150. ];
  151. $update_rs = Db::name('wenzhen_order')->where('id',$orderInfo['table_id'])->update($order_update);
  152. if($update_rs===false)
  153. {
  154. Db::rollback();
  155. return false;
  156. }
  157. //逻辑结束
  158. //状态
  159. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>$nowtime]);
  160. if($ros === false) {
  161. Db::rollback();
  162. return false;
  163. }
  164. //默认提交
  165. Db::commit();
  166. //发送im消息给医生
  167. if($wenzhen_order['order_type'] == 1){
  168. $tenim = new Tenim();
  169. $message = [
  170. 'businessID' => 'order_status',
  171. 'name' => '待接单',
  172. 'status' => '10',
  173. 'id' => $wenzhen_order['id'],
  174. 'content' => '已通知医生尽快接诊,超时未接诊的将自动取消订单并退款',
  175. ];
  176. $rs = $tenim->sendCustomMessageToUser('user'.$wenzhen_order['user_id'],'doctor'.$wenzhen_order['doctor_id'],$message);
  177. }
  178. return true;
  179. }
  180. ////////////////////////////////////////////////////////////////////////
  181. //充值VIP 异步回调对外方法
  182. public function vip_notify_base(){
  183. //验签
  184. $paytype = input('paytype','wechat');
  185. $notify_file = $this->notify_log_start($paytype);
  186. $pay = Service::checkNotify($paytype);
  187. if (!$pay) {
  188. echo '签名错误';
  189. exit;
  190. }
  191. //验证,拿订单号等信息
  192. $data = $pay->verify();
  193. $out_trade_no = $data['out_trade_no'];
  194. //订单查询
  195. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  196. if(empty($info)){
  197. return $pay->success()->send();
  198. exit;
  199. }
  200. if($info['order_status'] != 0)
  201. {
  202. return $pay->success()->send();
  203. exit;
  204. }
  205. //你可以在此编写订单逻辑
  206. $rs = $this->vip_notify_do($out_trade_no);
  207. if($rs === false){
  208. //不论结果都应返回success
  209. return $pay->success()->send();
  210. exit;
  211. }else{
  212. //不论结果都应返回success
  213. return $pay->success()->send();
  214. exit;
  215. }
  216. //默认
  217. return $pay->success()->send();
  218. exit;
  219. }
  220. //充值金币 逻辑
  221. private function vip_notify_do($out_trade_no){
  222. Db::startTrans();
  223. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  224. if (empty($orderInfo)) {
  225. Db::rollback();
  226. return false;
  227. }
  228. if($orderInfo['order_status'] != 0){
  229. Db::rollback();
  230. return false;
  231. }
  232. //逻辑开始
  233. //先充值
  234. $args = json_decode($orderInfo['args'],true);
  235. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  236. if($user_info['vip_endtime'] < time()){
  237. //过期了
  238. $vip_endtime = time() + (intval($args['days']) * 86400);
  239. $vip_type = 1;
  240. }else{
  241. //追加vip
  242. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  243. $vip_type = 2;
  244. }
  245. $update_data = [
  246. 'vip_endtime'=>$vip_endtime,
  247. ];
  248. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  249. //记录日志
  250. $log_data = [
  251. 'user_id' => $orderInfo['user_id'],
  252. 'before' => $user_info['vip_endtime'],
  253. 'change_value' => intval($args['days']) * 86400,
  254. 'remain' => $vip_endtime,
  255. 'remark' => '安卓购买vip',
  256. 'createtime' => time(),
  257. 'vip_type' => $vip_type,
  258. ];
  259. Db::name('user_vip_log')->insertGetId($log_data);
  260. if($result === false)
  261. {
  262. Db::rollback();
  263. return false;
  264. }
  265. //逻辑结束
  266. //状态
  267. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  268. if($ros === false) {
  269. Db::rollback();
  270. return false;
  271. }
  272. //默认提交
  273. Db::commit();
  274. return true;
  275. }
  276. //异步日志
  277. private function notify_log_start($paytype = 'wechat'){
  278. //记录支付回调数据
  279. ignore_user_abort(); // run script in background
  280. set_time_limit(30);
  281. // 日志文件 start
  282. $log_base_dir = '../epaylog/'.$paytype.'/';
  283. if (!is_dir($log_base_dir))
  284. {
  285. mkdir($log_base_dir, 0770, true);
  286. @chmod($log_base_dir, 0770);
  287. }
  288. $notify_file = $log_base_dir.'notify.txt';
  289. if(!file_exists($notify_file)) {
  290. @touch($notify_file);
  291. @chmod($notify_file, 0770);
  292. }
  293. if(filesize($notify_file)>5242880)//大于5M自动切换
  294. {
  295. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  296. }
  297. if(!file_exists($notify_file)) {
  298. @touch($notify_file);
  299. @chmod($notify_file, 0770);
  300. }
  301. // 日志文件 end
  302. //开始写入
  303. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  304. if($_REQUEST && $paytype == 'alipay') {
  305. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  306. } else {
  307. $xml = file_get_contents("php://input");
  308. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  309. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  310. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  311. }
  312. ini_set('display_errors','On');
  313. return $notify_file;
  314. }
  315. }