Pay.php 5.8 KB

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