Notify.php 9.6 KB

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