Pay.php 5.9 KB

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