Unishop.php 6.7 KB

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