Pay.php 6.3 KB

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