Pay.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use fast\Random;
  5. use think\Db;
  6. use addons\epay\library\Service;
  7. use think\Exception;
  8. /**
  9. * 充值配置与充值订单
  10. */
  11. class Pay extends Api
  12. {
  13. protected $noNeedLogin = ['order_notify_base'];
  14. protected $noNeedRight = ['*'];
  15. //异步回调对外方法
  16. public function order_notify_base(){
  17. //验签
  18. $paytype = 'wechat';
  19. $notify_file = $this->notify_log_start($paytype);
  20. $pay = Service::checkNotify($paytype);
  21. if (!$pay) {
  22. echo '签名错误';
  23. exit;
  24. }
  25. //验证,拿订单号等信息
  26. $data = $pay->verify();
  27. $out_trade_no = $data['out_trade_no'];
  28. //订单查询
  29. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  30. if(empty($info)){
  31. return $pay->success()->send();
  32. exit;
  33. }
  34. if($info['order_status'] != 0)
  35. {
  36. return $pay->success()->send();
  37. exit;
  38. }
  39. //你可以在此编写订单逻辑
  40. $rs = $this->order_notify_do($out_trade_no);
  41. if($rs === false){
  42. //不论结果都应返回success
  43. return $pay->success()->send();
  44. exit;
  45. }else{
  46. //不论结果都应返回success
  47. return $pay->success()->send();
  48. exit;
  49. }
  50. //默认
  51. return $pay->success()->send();
  52. exit;
  53. }
  54. //异步逻辑
  55. private function order_notify_do($out_trade_no){
  56. Db::startTrans();
  57. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  58. if (empty($orderInfo)) {
  59. Db::rollback();
  60. return false;
  61. }
  62. if($orderInfo['order_status'] != 0){
  63. Db::rollback();
  64. return false;
  65. }
  66. //逻辑开始
  67. $update = [
  68. 'status'=>1,
  69. 'paytime'=>time(),
  70. 'pay_out_trade_no'=>$out_trade_no,
  71. ];
  72. $rs_order = Db::name('order')->where('id',$orderInfo['table_id'])->update($update);
  73. if($rs_order === false){
  74. Db::rollback();
  75. return false;
  76. }
  77. //逻辑结束
  78. //状态
  79. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  80. if($ros === false) {
  81. Db::rollback();
  82. return false;
  83. }
  84. //产品销量增加
  85. $product_id = Db::name('order')->where('id',$orderInfo['table_id'])->value('product_id');
  86. $ticket_number = Db::name('order_ticket')->where('order_id',$orderInfo['table_id'])->sum('ticket_number');
  87. Db::name('product')->where('id',$product_id)->setInc('sell_number',$ticket_number);
  88. //默认提交
  89. Db::commit();
  90. return true;
  91. }
  92. //异步日志
  93. private function notify_log_start($paytype = 'wechat'){
  94. //记录支付回调数据
  95. ignore_user_abort(); // run script in background
  96. set_time_limit(30);
  97. // 日志文件 start
  98. $log_base_dir = '../paylog/'.$paytype.'/';
  99. if (!is_dir($log_base_dir))
  100. {
  101. mkdir($log_base_dir, 0770, true);
  102. @chmod($log_base_dir, 0770);
  103. }
  104. $notify_file = $log_base_dir.'notify.txt';
  105. if(!file_exists($notify_file)) {
  106. @touch($notify_file);
  107. @chmod($notify_file, 0770);
  108. }
  109. if(filesize($notify_file)>5242880)//大于5M自动切换
  110. {
  111. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  112. }
  113. if(!file_exists($notify_file)) {
  114. @touch($notify_file);
  115. @chmod($notify_file, 0770);
  116. }
  117. // 日志文件 end
  118. //开始写入
  119. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  120. if($_REQUEST && $paytype == 'alipay') {
  121. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  122. } else {
  123. $xml = file_get_contents("php://input");
  124. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  125. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  126. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  127. }
  128. ini_set('display_errors','On');
  129. return $notify_file;
  130. }
  131. }