Unishop.php 7.6 KB

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