Notify.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 订单支付回调
  8. */
  9. class Notify extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. public function checkNotify($args = []){
  14. $secret = config('hitpay.salt');
  15. $input_hmac = $args['hmac'];
  16. unset($args['hmac']);
  17. //hitpay/client/generateSignatureArray
  18. $hmacSource = [];
  19. foreach ($args as $key => $val) {
  20. $hmacSource[$key] = "{$key}{$val}";
  21. }
  22. ksort($hmacSource);
  23. $sig = implode("", array_values($hmacSource));
  24. $new_hmac = hash_hmac('sha256', $sig, $secret);
  25. //判断相等
  26. if($new_hmac == $input_hmac){
  27. return true;
  28. }else{
  29. return false;
  30. }
  31. }
  32. //主动获取一次
  33. private function getPaymentStatus($payment_request_id){
  34. $apiKey = config('hitpay.apikey');
  35. $hitPayClient = new \HitPay\Client($apiKey, true);
  36. $data = $hitPayClient->getPaymentStatus($payment_request_id);
  37. return $data->status;
  38. }
  39. //充值金币 异步回调对外方法
  40. public function recharge_notify_base(){
  41. //日志
  42. $paytype = input('paytype','hitpay');
  43. $notify_file = $this->notify_log_start($paytype);
  44. //接参
  45. $field = ['payment_id','payment_request_id','phone','amount','currency','status','reference_number','hmac'];
  46. $notify_data = request_post_hub($field);
  47. //验签
  48. $checkNotify = $this->checkNotify($notify_data);
  49. if ($checkNotify !== true) {
  50. echo '签名错误';
  51. exit;
  52. }
  53. //检查支付完成 completed / failed
  54. if($notify_data['status'] != 'completed'){
  55. $now_status = $this->getPaymentStatus($notify_data['payment_request_id']);
  56. if($now_status != 'completed'){
  57. echo '没有支付完成';
  58. exit;
  59. }
  60. }
  61. //验证,拿订单号等信息
  62. $out_trade_no = $notify_data['reference_number'];
  63. //订单查询
  64. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  65. if(empty($info)){
  66. echo 'success';
  67. exit;
  68. }
  69. if($info['order_status'] != 0)
  70. {
  71. echo 'success';
  72. exit;
  73. }
  74. //你可以在此编写订单逻辑
  75. $rs = $this->recharge_notify_do($out_trade_no);
  76. if($rs === false){
  77. //不论结果都应返回success
  78. echo 'success';
  79. exit;
  80. }else{
  81. //不论结果都应返回success
  82. echo 'success';
  83. exit;
  84. }
  85. //默认
  86. echo 'success';
  87. exit;
  88. }
  89. //充值金币 逻辑
  90. private function recharge_notify_do($out_trade_no){
  91. $time = time();
  92. Db::startTrans();
  93. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  94. if (empty($orderInfo)) {
  95. Db::rollback();
  96. return false;
  97. }
  98. if($orderInfo['order_status'] != 0){
  99. Db::rollback();
  100. return false;
  101. }
  102. //逻辑开始
  103. //试课预约
  104. if($orderInfo['table_name'] == 'trylesson_order'){
  105. $update = [
  106. 'order_status' => 10,
  107. 'paytime' => $time,
  108. 'updatetime' => $time,
  109. ];
  110. $rs = Db::name('trylesson_order')->where('id',$orderInfo['table_id'])->update($update);
  111. if($rs === false){
  112. Db::rollback();
  113. return false;
  114. }
  115. }
  116. //售课预约
  117. if($orderInfo['table_name'] == 'lesson_order'){
  118. $update = [
  119. 'order_status' => 10,
  120. 'paytime' => $time,
  121. 'updatetime' => $time,
  122. ];
  123. $rs = Db::name('lesson_order')->where('id',$orderInfo['table_id'])->update($update);
  124. if($rs === false){
  125. Db::rollback();
  126. return false;
  127. }
  128. }
  129. //售课预约买套餐
  130. if($orderInfo['table_name'] == 'package_order'){
  131. //修改套餐单
  132. $update = [
  133. 'order_status' => 1,
  134. 'paytime' => $time,
  135. 'updatetime' => $time,
  136. ];
  137. $rs = Db::name('package_order')->where('order_no',$orderInfo['out_trade_no'])->update($update); //这里不用id,是因为另有赠品单
  138. if($rs === false){
  139. Db::rollback();
  140. return false;
  141. }
  142. //修改预约单。这一块最好搬到计划任务里
  143. $args = json_decode($orderInfo['args'],true);
  144. if(isset($args['lesson_order_id']) && !empty($args['lesson_order_id'])){
  145. $package_order = Db::name('package_order')->where('order_no',$orderInfo['out_trade_no'])->where('is_gift',0)->find();
  146. $lesson_order = Db::name('lesson_order')->where('id',$args['lesson_order_id'])->find();
  147. //课时能够支撑报名人数
  148. if($package_order['remain'] >= $lesson_order['number']){
  149. //扣除一节
  150. $update = [
  151. 'remain' => $package_order['remain'] - $lesson_order['number'],
  152. 'updatetime' => time(),
  153. ];
  154. $rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
  155. if($rs1 === false){
  156. Db::rollback();
  157. return false;
  158. }
  159. //修改预约单状态
  160. $update = [
  161. 'order_status' => 10,
  162. 'paytime' => $time,
  163. 'updatetime' => $time,
  164. 'package_order_id' => $package_order['id'],
  165. 'paytype' => 1, //从购买套餐中3,改为 课程套餐1
  166. ];
  167. if($lesson_order['number'] > 1){
  168. $update['package_remark'] = ($package_order['sessions'] - $package_order['remain'] + 1) . '-' . ($package_order['sessions'] - $package_order['remain'] + $lesson_order['number']) .'/'. $package_order['sessions'];
  169. }else{
  170. $update['package_remark'] = ($package_order['sessions'] - $package_order['remain'] + 1) .'/'. $package_order['sessions'];
  171. }
  172. $rs = Db::name('lesson_order')->where('id',$args['lesson_order_id'])->update($update);
  173. if($rs === false){
  174. Db::rollback();
  175. return false;
  176. }
  177. }
  178. }
  179. }
  180. //逻辑结束
  181. //状态
  182. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>$time]);
  183. if($ros === false) {
  184. Db::rollback();
  185. return false;
  186. }
  187. //默认提交
  188. Db::commit();
  189. return true;
  190. }
  191. //异步日志
  192. private function notify_log_start($paytype = 'wechat'){
  193. //记录支付回调数据
  194. ignore_user_abort(); // run script in background
  195. set_time_limit(30);
  196. // 日志文件 start
  197. $log_base_dir = '../paylog/'.$paytype.'/';
  198. if (!is_dir($log_base_dir))
  199. {
  200. mkdir($log_base_dir, 0770, true);
  201. @chmod($log_base_dir, 0770);
  202. }
  203. $notify_file = $log_base_dir.'notify.txt';
  204. if(!file_exists($notify_file)) {
  205. @touch($notify_file);
  206. @chmod($notify_file, 0770);
  207. }
  208. if(filesize($notify_file)>5242880)//大于5M自动切换
  209. {
  210. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  211. }
  212. if(!file_exists($notify_file)) {
  213. @touch($notify_file);
  214. @chmod($notify_file, 0770);
  215. }
  216. // 日志文件 end
  217. //开始写入
  218. $xml = file_get_contents("php://input");
  219. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  220. ini_set('display_errors','On');
  221. return $notify_file;
  222. }
  223. }