Pay.php 6.3 KB

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