Notify.php 9.4 KB

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