Notify.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace app\api\controller;
  3. use think\Controller;
  4. use think\Db;
  5. /**
  6. * 订单支付回调
  7. */
  8. class Notify extends Controller
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. public function checkNotify($args = []){
  13. $secret = config('hitpay.salt');
  14. $input_hmac = $args['hmac'];
  15. unset($args['hmac']);
  16. //hitpay/client/generateSignatureArray
  17. $hmacSource = [];
  18. foreach ($args as $key => $val) {
  19. $hmacSource[$key] = "{$key}{$val}";
  20. }
  21. ksort($hmacSource);
  22. $sig = implode("", array_values($hmacSource));
  23. $new_hmac = hash_hmac('sha256', $sig, $secret);
  24. //判断相等
  25. if($new_hmac == $input_hmac){
  26. return true;
  27. }else{
  28. return false;
  29. }
  30. }
  31. //主动获取一次
  32. private function getPaymentStatus($payment_request_id){
  33. $apiKey = config('hitpay.apikey');
  34. $hitPayClient = new \HitPay\Client($apiKey, true);
  35. $data = $hitPayClient->getPaymentStatus($payment_request_id);
  36. return $data->status;
  37. }
  38. //充值金币 异步回调对外方法
  39. public function recharge_notify_base(){
  40. //日志
  41. $paytype = 'hitpay';
  42. $notify_file = $this->notify_log_start($paytype);
  43. //接参
  44. $field = ['payment_id','payment_request_id','phone','amount','currency','status','reference_number','hmac'];
  45. $notify_data = request_post_hub($field);
  46. //验签
  47. $checkNotify = $this->checkNotify($notify_data);
  48. if ($checkNotify !== true) {
  49. echo '签名错误';
  50. exit;
  51. }
  52. //检查支付完成 completed / failed
  53. if($notify_data['status'] != 'completed'){
  54. $now_status = $this->getPaymentStatus($notify_data['payment_request_id']);
  55. if($now_status != 'completed'){
  56. echo '没有支付完成';
  57. exit;
  58. }
  59. }
  60. //验证,拿订单号等信息
  61. $out_trade_no = $notify_data['reference_number'];
  62. $payment_request_id = $notify_data['payment_request_id'];
  63. $payment_id = $notify_data['payment_id'];
  64. //订单查询
  65. $map = [
  66. 'out_trade_no' => $out_trade_no,
  67. 'payment_request_id' => $payment_request_id,
  68. ];
  69. $info = Db::name('pay_order')->where($map)->find();
  70. if(empty($info)){
  71. echo 'success';
  72. exit;
  73. }
  74. if($info['order_status'] != 0)
  75. {
  76. echo 'success';
  77. exit;
  78. }
  79. //你可以在此编写订单逻辑
  80. $rs = $this->recharge_notify_do($out_trade_no,$payment_request_id,$payment_id);
  81. if($rs === false){
  82. //不论结果都应返回success
  83. echo 'success';
  84. exit;
  85. }else{
  86. //不论结果都应返回success
  87. echo 'success';
  88. exit;
  89. }
  90. //默认
  91. echo 'success';
  92. exit;
  93. }
  94. //充值金币 逻辑
  95. private function recharge_notify_do($out_trade_no,$payment_request_id,$payment_id){
  96. $time = time();
  97. Db::startTrans();
  98. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  99. if (empty($orderInfo)) {
  100. Db::rollback();
  101. return false;
  102. }
  103. if($orderInfo['order_status'] != 0){
  104. Db::rollback();
  105. return false;
  106. }
  107. //逻辑开始
  108. //试课预约
  109. if($orderInfo['table_name'] == 'trylesson_order'){
  110. $update = [
  111. 'order_status' => 10,
  112. 'paytime' => $time,
  113. 'updatetime' => $time,
  114. ];
  115. $rs = Db::name('trylesson_order')->where('id',$orderInfo['table_id'])->update($update);
  116. if($rs === false){
  117. Db::rollback();
  118. return false;
  119. }
  120. }
  121. //售课预约
  122. if($orderInfo['table_name'] == 'lesson_order'){
  123. $update = [
  124. 'order_status' => 10,
  125. 'paytime' => $time,
  126. 'updatetime' => $time,
  127. ];
  128. $rs = Db::name('lesson_order')->where('id',$orderInfo['table_id'])->update($update);
  129. if($rs === false){
  130. Db::rollback();
  131. return false;
  132. }
  133. //更新已预约人数
  134. $lesson_order = Db::name('lesson_order')->where('id',$orderInfo['table_id'])->find();
  135. $pay_number = Db::name('lesson_order')->where('slot_id',$lesson_order['slot_id'])->where('order_status',10)->sum('usernumber');
  136. $rs_slot = Db::name('lesson_slot')->where('id',$lesson_order['slot_id'])->update(['bookednum' => $pay_number]);
  137. if($rs_slot === false){
  138. Db::rollback();
  139. return false;
  140. }
  141. //赠送积分
  142. $wallet_rs = model('wallet')->lockChangeAccountRemain($orderInfo['user_id'],'score',$orderInfo['order_amount'],3,'线上预约课程','lesson_order',$orderInfo['table_id']);
  143. if($wallet_rs['status'] === false){
  144. Db::rollback();
  145. return false;
  146. }
  147. }
  148. //售课预约买套餐
  149. if($orderInfo['table_name'] == 'package_order'){
  150. //配套主订单
  151. $package_order = Db::name('package_order')->where('id',$orderInfo['table_id'])->find();
  152. //修改套餐订单支付状态
  153. $update = [
  154. 'order_status' => 1,
  155. 'paytime' => $time,
  156. 'updatetime' => $time,
  157. ];
  158. //小配套自动激活
  159. if($package_order['remain'] < 5){
  160. $update['use_status'] = 1;
  161. $update['starttime'] = $time;
  162. $update['endtime'] = $time + ($package_order['days'] * 86400); //主配套与赠品的时间保持一样就可以
  163. }
  164. //初次购买,主订单增加赠送小时数
  165. $check_first = Db::name('package_order')->where('user_id',$package_order['user_id'])->where('package_id',$package_order['package_id'])->where('order_status',1)->where('is_gift',0)->find();
  166. if(empty($check_first)){
  167. $sessions_first = Db::name('lesson_package')->where('id',$package_order['package_id'])->value('sessions_first');
  168. if($sessions_first > 0){
  169. $rs_first = Db::name('package_order')->where('id',$orderInfo['table_id'])->update(['sessions'=>$package_order['sessions'] + $sessions_first]);
  170. if($rs_first === false){
  171. Db::rollback();
  172. return false;
  173. }
  174. }
  175. }
  176. //修改套餐订单支付状态
  177. $rs = Db::name('package_order')->where('order_no',$orderInfo['out_trade_no'])->update($update); //这里不用id,是因为另有赠品单,两个一起更新
  178. if($rs === false){
  179. Db::rollback();
  180. return false;
  181. }
  182. //大于等于5小时的
  183. if($package_order['remain'] >= 5){
  184. //赠送积分
  185. $wallet_rs = model('wallet')->lockChangeAccountRemain($orderInfo['user_id'],'score',$orderInfo['order_amount'],2,'线上购买配套','package_order',$orderInfo['table_id']);
  186. if($wallet_rs['status'] === false){
  187. Db::rollback();
  188. return false;
  189. }
  190. //新会员变成旧会员
  191. $oldstatus = Db::name('user')->where('id',$orderInfo['user_id'])->value('oldstatus');
  192. if($oldstatus == 0){
  193. $user_rs = Db::name('user')->where('id',$orderInfo['user_id'])->update(['oldstatus'=>1]);
  194. if($user_rs === false){
  195. Db::rollback();
  196. return false;
  197. }
  198. }
  199. }
  200. //小于五小时的,因为已激活。修改预约单状态,减掉相应课时
  201. $args = json_decode($orderInfo['args'],true);
  202. if($package_order['remain'] < 5 && isset($args['lesson_order_id']) && !empty($args['lesson_order_id'])){
  203. // $package_order = Db::name('package_order')->where('order_no',$orderInfo['out_trade_no'])->where('is_gift',0)->find();
  204. $lesson_order = Db::name('lesson_order')->where('id',$args['lesson_order_id'])->find();
  205. //课时能够支撑报名人数
  206. if($package_order['remain'] >= $lesson_order['usernumber_hours']){
  207. //扣除一节
  208. $update = [
  209. 'remain' => bcsub($package_order['remain'],$lesson_order['usernumber_hours'],1),
  210. 'updatetime' => time(),
  211. ];
  212. $rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
  213. if($rs1 === false){
  214. Db::rollback();
  215. return false;
  216. }
  217. //修改预约单状态
  218. $update = [
  219. 'order_status' => 10,
  220. 'paytime' => $time,
  221. 'updatetime' => $time,
  222. 'package_order_id' => $package_order['id'],
  223. 'paytype' => 1, //从购买套餐中3,改为 课程套餐1
  224. ];
  225. $update['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $lesson_order['usernumber_hours']) .'/'. $package_order['sessions'];
  226. $rs = Db::name('lesson_order')->where('id',$args['lesson_order_id'])->update($update);
  227. if($rs === false){
  228. Db::rollback();
  229. return false;
  230. }
  231. //更新已预约人数
  232. $pay_number = Db::name('lesson_order')->where('slot_id',$lesson_order['slot_id'])->where('order_status',10)->sum('usernumber');
  233. $rs_slot = Db::name('lesson_slot')->where('id',$lesson_order['slot_id'])->update(['bookednum' => $pay_number]);
  234. if($rs_slot === false){
  235. Db::rollback();
  236. return false;
  237. }
  238. }else{
  239. //新买的课时不足以支撑这次的报名人数,不处理
  240. }
  241. }
  242. }
  243. //逻辑结束
  244. //状态
  245. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>$time,'payment_id'=>$payment_id]);
  246. if($ros === false) {
  247. Db::rollback();
  248. return false;
  249. }
  250. //默认提交
  251. Db::commit();
  252. return true;
  253. }
  254. //异步日志
  255. private function notify_log_start($paytype = 'wechat'){
  256. //记录支付回调数据
  257. ignore_user_abort(); // run script in background
  258. set_time_limit(30);
  259. // 日志文件 start
  260. $log_base_dir = '../paylog/'.$paytype.'/';
  261. if (!is_dir($log_base_dir))
  262. {
  263. mkdir($log_base_dir, 0770, true);
  264. @chmod($log_base_dir, 0770);
  265. }
  266. $notify_file = $log_base_dir.'notify.txt';
  267. if(!file_exists($notify_file)) {
  268. @touch($notify_file);
  269. @chmod($notify_file, 0770);
  270. }
  271. if(filesize($notify_file)>5242880)//大于5M自动切换
  272. {
  273. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  274. }
  275. if(!file_exists($notify_file)) {
  276. @touch($notify_file);
  277. @chmod($notify_file, 0770);
  278. }
  279. // 日志文件 end
  280. //开始写入
  281. $xml = file_get_contents("php://input");
  282. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  283. ini_set('display_errors','On');
  284. return $notify_file;
  285. }
  286. }