Pay.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. //微信小程序、微信app下单使用。
  17. public function pay_order(){
  18. $pay_type = input('pay_type','wechat');
  19. $platform = input('platform','miniapp');
  20. $orderid = input('orderid','0');
  21. $uid = $this->auth->id;
  22. $map = [
  23. 'id' => $orderid,
  24. 'user_id' => $uid,
  25. 'status' => 0,
  26. 'pay_type' => 1,
  27. ];
  28. $orderinfo = Db::name('order')->where($map)->find();
  29. if(empty($orderinfo)){
  30. $this->error('请刷新重试');
  31. }
  32. //创建订单
  33. $data['user_id'] = $uid;
  34. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  35. $data['order_amount'] = $orderinfo['pay_fee'];
  36. $data['createtime'] = time();
  37. $data['pay_type'] = $pay_type;
  38. $data['platform'] = $platform;
  39. $data['order_status'] = 0;
  40. $data['table_name'] = 'order';
  41. $data['table_id'] = $orderid;
  42. $orderid = Db::name('pay_order')->insertGetId($data);
  43. $openid = $this->auth->mini_openid;
  44. //下单
  45. $params = [
  46. 'type' => $pay_type,
  47. 'orderid' => $data['out_trade_no'],
  48. 'title' => '支付订单',
  49. 'amount' => $data['order_amount'],
  50. 'method' => $platform,
  51. 'openid' => $openid,
  52. 'notifyurl' => config('pay_notify_url').'/api/pay/order_notify_base/paytype/'.$pay_type,
  53. 'returnurl' => config('h5_url').'/#/page/',
  54. ];
  55. $res = Service::submitOrder($params);
  56. if($pay_type == 'wechat'){
  57. $this->success('success',json_decode($res,true));
  58. }else{
  59. $this->success('success',$res);
  60. }
  61. }
  62. //异步回调对外方法
  63. public function order_notify_base(){
  64. //验签
  65. $paytype = input('paytype','wechat');
  66. $notify_file = $this->notify_log_start($paytype);
  67. $pay = Service::checkNotify($paytype);
  68. if (!$pay) {
  69. echo '签名错误';
  70. exit;
  71. }
  72. //验证,拿订单号等信息
  73. $data = $pay->verify();
  74. $out_trade_no = $data['out_trade_no'];
  75. //订单查询
  76. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  77. if(empty($info)){
  78. return $pay->success()->send();
  79. exit;
  80. }
  81. if($info['order_status'] != 0)
  82. {
  83. return $pay->success()->send();
  84. exit;
  85. }
  86. //你可以在此编写订单逻辑
  87. $rs = $this->order_notify_do($out_trade_no);
  88. if($rs === false){
  89. //不论结果都应返回success
  90. return $pay->success()->send();
  91. exit;
  92. }else{
  93. //不论结果都应返回success
  94. return $pay->success()->send();
  95. exit;
  96. }
  97. //默认
  98. return $pay->success()->send();
  99. exit;
  100. }
  101. //异步逻辑
  102. private function order_notify_do($out_trade_no){
  103. Db::startTrans();
  104. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  105. if (empty($orderInfo)) {
  106. Db::rollback();
  107. return false;
  108. }
  109. if($orderInfo['order_status'] != 0){
  110. Db::rollback();
  111. return false;
  112. }
  113. //逻辑开始
  114. $update = [
  115. 'status'=>1,
  116. 'paytime'=>time(),
  117. 'pay_out_trade_no'=>$out_trade_no,
  118. ];
  119. $rs_order = Db::name('order')->where('id',$orderInfo['table_id'])->update($update);
  120. if($rs_order === false){
  121. Db::rollback();
  122. return false;
  123. }
  124. //逻辑结束
  125. //状态
  126. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  127. if($ros === false) {
  128. Db::rollback();
  129. return false;
  130. }
  131. //默认提交
  132. Db::commit();
  133. return true;
  134. }
  135. //异步日志
  136. private function notify_log_start($paytype = 'wechat'){
  137. //记录支付回调数据
  138. ignore_user_abort(); // run script in background
  139. set_time_limit(30);
  140. // 日志文件 start
  141. $log_base_dir = '../paylog/'.$paytype.'/';
  142. if (!is_dir($log_base_dir))
  143. {
  144. mkdir($log_base_dir, 0770, true);
  145. @chmod($log_base_dir, 0770);
  146. }
  147. $notify_file = $log_base_dir.'notify.txt';
  148. if(!file_exists($notify_file)) {
  149. @touch($notify_file);
  150. @chmod($notify_file, 0770);
  151. }
  152. if(filesize($notify_file)>5242880)//大于5M自动切换
  153. {
  154. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  155. }
  156. if(!file_exists($notify_file)) {
  157. @touch($notify_file);
  158. @chmod($notify_file, 0770);
  159. }
  160. // 日志文件 end
  161. //开始写入
  162. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  163. if($_REQUEST && $paytype == 'alipay') {
  164. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  165. } else {
  166. $xml = file_get_contents("php://input");
  167. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  168. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  169. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  170. }
  171. ini_set('display_errors','On');
  172. return $notify_file;
  173. }
  174. }