Unishop.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if(!in_array($pay_type,['wechat','alipay','wallet'])){
  27. $this->error();
  28. }
  29. $uid = $this->auth->id;
  30. $map = [
  31. 'id' => $orderid,
  32. 'user_id' => $uid,
  33. 'status' => 1,
  34. 'have_paid'=> 0,
  35. ];
  36. $orderinfo = Db::name('unishop_order')->where($map)->find();
  37. if(empty($orderinfo)){
  38. $this->error('请刷新重试');
  39. }
  40. //余额支付
  41. if($pay_type == 'wallet'){
  42. Db::startTrans();
  43. //扣钱
  44. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'money',$orderinfo['total_price'],31,'购物','unishop_order',$orderid);
  45. if($rs_wallet['status'] == false){
  46. Db::rollback();
  47. $this->error($rs_wallet['msg']);
  48. }
  49. //订单改为已支付
  50. $order_update['pay_type'] = 'wallet';
  51. $order_update['have_paid'] = time();
  52. $update_rs = Db::name('unishop_order')->where('id',$orderid)->where('have_paid',0)->update($order_update);
  53. if(!$update_rs){
  54. Db::rollback();
  55. $this->error('支付失败,重试一下吧');
  56. }
  57. Db::commit();
  58. $result = [
  59. 'pay_type' => $pay_type,
  60. 'pay_params' => '',
  61. ];
  62. $this->success(1,$result);
  63. }
  64. //创建订单
  65. $data = [];
  66. $data['user_id'] = $uid;
  67. $data['out_trade_no'] = createUniqueNo('U',$uid); // 数据库订单号加密
  68. $data['order_amount'] = $orderinfo['total_price'];
  69. $data['createtime'] = time();
  70. $data['pay_type'] = $pay_type;
  71. $data['platform'] = $platform;
  72. $data['order_status'] = 0;
  73. $data['table_name'] = 'unishop_order';
  74. $data['table_id'] = $orderid;
  75. $orderid = Db::name('pay_order')->insertGetId($data);
  76. //下单
  77. $params = [
  78. 'type' => $pay_type,
  79. 'orderid' => $data['out_trade_no'],
  80. 'title' => '支付订单',
  81. 'amount' => $data['order_amount'],
  82. // 'amount' => 0.01,
  83. 'method' => $platform,
  84. 'notifyurl' => config('pay_notify_url').'/api/unishop/order_notify_base/paytype/'.$pay_type,
  85. 'returnurl' => '',
  86. ];
  87. $res = Service::submitOrder($params);
  88. $result = [
  89. 'pay_type'=> $pay_type,
  90. 'pay_params'=> $pay_type
  91. ];
  92. if($pay_type == 'wechat'){
  93. $result['pay_params'] = json_decode($res,true);
  94. }else{
  95. $result['pay_params'] = $res;
  96. }
  97. $this->success(1,$result);
  98. }
  99. //异步回调对外方法
  100. public function order_notify_base(){
  101. //验签
  102. $paytype = input('paytype','alipay');
  103. $notify_file = $this->notify_log_start($paytype);
  104. $pay = Service::checkNotify($paytype);
  105. if (!$pay) {
  106. echo '签名错误';
  107. exit;
  108. }
  109. //验证,拿订单号等信息
  110. $data = $pay->verify();
  111. $out_trade_no = $data['out_trade_no'];
  112. //订单查询
  113. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  114. if(empty($info)){
  115. return $pay->success()->send();
  116. exit;
  117. }
  118. if($info['order_status'] != 0)
  119. {
  120. return $pay->success()->send();
  121. exit;
  122. }
  123. //你可以在此编写订单逻辑
  124. $rs = $this->order_notify_do($out_trade_no,$paytype);
  125. if($rs === false){
  126. //不论结果都应返回success
  127. return $pay->success()->send();
  128. exit;
  129. }else{
  130. //不论结果都应返回success
  131. return $pay->success()->send();
  132. exit;
  133. }
  134. //默认
  135. return $pay->success()->send();
  136. exit;
  137. }
  138. //异步逻辑
  139. private function order_notify_do($out_trade_no,$paytype){
  140. Db::startTrans();
  141. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  142. if (empty($orderInfo)) {
  143. Db::rollback();
  144. return false;
  145. }
  146. if($orderInfo['order_status'] != 0){
  147. Db::rollback();
  148. return false;
  149. }
  150. //逻辑开始
  151. $nowtime = time();
  152. $paytype_enum = [
  153. 'wechat' => 3,
  154. 'alipay' => 4,
  155. ];
  156. //订单改为已支付
  157. $update = [
  158. 'have_paid'=>$nowtime,
  159. 'pay_type' =>$paytype_enum[$paytype]
  160. ];
  161. $rs_order = Db::name('unishop_order')->where('id',$orderInfo['table_id'])->update($update);
  162. if($rs_order === false){
  163. Db::rollback();
  164. return false;
  165. }
  166. //逻辑结束
  167. //状态
  168. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  169. if($ros === false) {
  170. Db::rollback();
  171. return false;
  172. }
  173. //默认提交
  174. Db::commit();
  175. return true;
  176. }
  177. //异步日志
  178. private function notify_log_start($paytype = 'wechat'){
  179. //记录支付回调数据
  180. ignore_user_abort(); // run script in background
  181. set_time_limit(30);
  182. // 日志文件 start
  183. $log_base_dir = '../epaylog/'.$paytype.'/';
  184. if (!is_dir($log_base_dir))
  185. {
  186. mkdir($log_base_dir, 0770, true);
  187. @chmod($log_base_dir, 0770);
  188. }
  189. $notify_file = $log_base_dir.'notify.txt';
  190. if(!file_exists($notify_file)) {
  191. @touch($notify_file);
  192. @chmod($notify_file, 0770);
  193. }
  194. if(filesize($notify_file)>5242880)//大于5M自动切换
  195. {
  196. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  197. }
  198. if(!file_exists($notify_file)) {
  199. @touch($notify_file);
  200. @chmod($notify_file, 0770);
  201. }
  202. // 日志文件 end
  203. //开始写入
  204. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  205. if($_REQUEST && $paytype == 'alipay') {
  206. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  207. } else {
  208. $xml = file_get_contents("php://input");
  209. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  210. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  211. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  212. }
  213. ini_set('display_errors','On');
  214. return $notify_file;
  215. }
  216. }