Notify.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. //售课预约买套餐
  135. if($orderInfo['table_name'] == 'package_order'){
  136. //修改套餐单
  137. $update = [
  138. 'order_status' => 1,
  139. 'paytime' => $time,
  140. 'updatetime' => $time,
  141. ];
  142. $rs = Db::name('package_order')->where('order_no',$orderInfo['out_trade_no'])->update($update); //这里不用id,是因为另有赠品单,两个一起更新
  143. if($rs === false){
  144. Db::rollback();
  145. return false;
  146. }
  147. //修改预约单。这一块最好搬到计划任务里
  148. $args = json_decode($orderInfo['args'],true);
  149. if(isset($args['lesson_order_id']) && !empty($args['lesson_order_id'])){
  150. $package_order = Db::name('package_order')->where('order_no',$orderInfo['out_trade_no'])->where('is_gift',0)->find();
  151. $lesson_order = Db::name('lesson_order')->where('id',$args['lesson_order_id'])->find();
  152. //课时能够支撑报名人数
  153. if($package_order['remain'] >= $lesson_order['usernumber_hours']){
  154. //扣除一节
  155. $update = [
  156. 'remain' => bcsub($package_order['remain'],$lesson_order['usernumber_hours'],1),
  157. 'updatetime' => time(),
  158. ];
  159. $rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
  160. if($rs1 === false){
  161. Db::rollback();
  162. return false;
  163. }
  164. //修改预约单状态
  165. $update = [
  166. 'order_status' => 10,
  167. 'paytime' => $time,
  168. 'updatetime' => $time,
  169. 'package_order_id' => $package_order['id'],
  170. 'paytype' => 1, //从购买套餐中3,改为 课程套餐1
  171. ];
  172. if($lesson_order['usernumber_hours'] > 1){
  173. $update['package_remark'] = ($package_order['sessions'] - $package_order['remain'] + 1) . '-' . ($package_order['sessions'] - $package_order['remain'] + $lesson_order['usernumber_hours']) .'/'. $package_order['sessions'];
  174. }else{
  175. $update['package_remark'] = ($package_order['sessions'] - $package_order['remain'] + 1) .'/'. $package_order['sessions'];
  176. }
  177. $rs = Db::name('lesson_order')->where('id',$args['lesson_order_id'])->update($update);
  178. if($rs === false){
  179. Db::rollback();
  180. return false;
  181. }
  182. }else{
  183. //新买的课时不足以支撑这次的报名人数,不处理
  184. }
  185. }
  186. }
  187. //逻辑结束
  188. //状态
  189. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>$time,'payment_id'=>$payment_id]);
  190. if($ros === false) {
  191. Db::rollback();
  192. return false;
  193. }
  194. //默认提交
  195. Db::commit();
  196. return true;
  197. }
  198. //异步日志
  199. private function notify_log_start($paytype = 'wechat'){
  200. //记录支付回调数据
  201. ignore_user_abort(); // run script in background
  202. set_time_limit(30);
  203. // 日志文件 start
  204. $log_base_dir = '../paylog/'.$paytype.'/';
  205. if (!is_dir($log_base_dir))
  206. {
  207. mkdir($log_base_dir, 0770, true);
  208. @chmod($log_base_dir, 0770);
  209. }
  210. $notify_file = $log_base_dir.'notify.txt';
  211. if(!file_exists($notify_file)) {
  212. @touch($notify_file);
  213. @chmod($notify_file, 0770);
  214. }
  215. if(filesize($notify_file)>5242880)//大于5M自动切换
  216. {
  217. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  218. }
  219. if(!file_exists($notify_file)) {
  220. @touch($notify_file);
  221. @chmod($notify_file, 0770);
  222. }
  223. // 日志文件 end
  224. //开始写入
  225. $xml = file_get_contents("php://input");
  226. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  227. ini_set('display_errors','On');
  228. return $notify_file;
  229. }
  230. }