PaymentController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\HelpOrderEvent;
  4. use App\Events\NewOrder;
  5. use App\Events\RetailOutboundEvent;
  6. use App\Http\Middleware\AssitMiddleware;
  7. use App\Models\Amount;
  8. use App\Models\Assit;
  9. use App\Models\DwbsUser;
  10. use App\Models\Goods;
  11. use App\Models\GoodSku;
  12. use App\Models\IntegralW;
  13. use App\Models\Order;
  14. use App\Models\OrderW;
  15. use App\Models\OrderDetailW;
  16. use App\Models\StoreGoods;
  17. use App\Models\StoreGoodsSku;
  18. use App\Models\InventoryW;
  19. use App\Models\Store;
  20. use App\Models\User;
  21. use App\Models\UserW;
  22. use App\Models\UserZ;
  23. use App\Models\VipLogW;
  24. use App\Models\LogW;
  25. use App\Models\ZbsBasic;
  26. use App\Models\ZbsEnroll;
  27. use App\Models\ZbsOrder;
  28. use App\Models\OrderOnceW;
  29. use EasyWeChat\Factory;
  30. use Carbon\Carbon;
  31. use Illuminate\Http\Request;
  32. use Illuminate\Support\Facades\Auth;
  33. use Illuminate\Support\Facades\Cache;
  34. use Illuminate\Support\Facades\DB;
  35. use Illuminate\Support\Facades\Log;
  36. class PaymentController extends Controller
  37. {
  38. public function pay_test(Request $request)
  39. {
  40. $order_no=$request->input('order_no');
  41. $order=OrderW::where('order_no',$order_no)->lockForUpdate()->first();
  42. $store=Store::where('id',$order->store_id)->first();
  43. if(empty($store)){
  44. return $this->error('450001','','店铺不存在或者已被删除');
  45. }
  46. if($store->status==1){
  47. return $this->error('450001','','店铺已被禁用');
  48. }
  49. if($order->apply_cancel == 2){
  50. return $this->error('450001','','订单已取消');
  51. }
  52. if($order->is_pay != 0 || $order->status != 0){
  53. return $this->error('450001','','订单状态有误');
  54. }
  55. if($order->account <= 0){
  56. return $this->error('450001','','支付金额有误');
  57. }
  58. //修改订单状态
  59. $order->status = 1;
  60. $order->is_help = 1;
  61. $order->is_pay = 1;
  62. $order->pay_type = 1;
  63. $order->save();
  64. return response()->json([
  65. 'error_code'=>200,
  66. 'msg'=>'成功',
  67. ]);
  68. }
  69. public function pay(Request $request)
  70. {
  71. Log::info($request);
  72. $order_no=$request->input('order_no');
  73. $tradeType=$request->input('type','APP');//支付场景,APP/JSAPI
  74. $is_assit=$request->input('is_assit',0);
  75. if(!in_array($tradeType,['APP','JSAPI'])){
  76. return $this->error('450001','支付场景有误');
  77. }
  78. try{
  79. DB::transaction(function()use($order_no, $is_assit ,&$order ,&$seconds){
  80. $order=OrderW::where('order_no',$order_no)->lockForUpdate()->first();
  81. $seconds=Carbon::now()->subSeconds(60)->toDateTimeString();//限制调起支付60秒
  82. if(empty($order)){
  83. throw new \Exception("订单有误");
  84. }
  85. if($order->prepare_pay_at >= $seconds){
  86. throw new \Exception("订单正在支付中");
  87. }
  88. if($is_assit){
  89. $order->self_pay=2;
  90. }else{
  91. $order->self_pay=1;
  92. }
  93. $order->prepare_pay_at=Carbon::now();
  94. $order->save();
  95. });
  96. }catch(\Exception $e){
  97. return $this->error('450001',$e->getMessage());
  98. }
  99. $store=Store::where('id',$order->store_id)->first();
  100. if(empty($store)){
  101. return $this->error('450001','店铺不存在或者已被删除');
  102. }
  103. if($store->status==1){
  104. return $this->error('450001','店铺已被禁用');
  105. }
  106. if($order->apply_cancel == 2){
  107. return $this->error('450001','订单已取消');
  108. }
  109. if($order->is_pay != 0 || $order->status != 0){
  110. return $this->error('450001','订单状态有误');
  111. }
  112. if($order->account <= 0){
  113. return $this->error('450001','支付金额有误');
  114. }
  115. $options = $this->options($order->store_id,$tradeType);
  116. $payment = Factory::payment($options);
  117. $jssdk = $payment->jssdk;
  118. Log::info('订单支付金额:'.$order->account);
  119. $two_minutes=Carbon::now()->addSeconds(55)->toDateTimeString();//限制支付时间55秒
  120. $cancel_time=Carbon::parse($order->created_at)->addMinutes(59)->toDateTimeString();//系统自动取消时间前一分钟
  121. if(Carbon::now() >= $cancel_time){
  122. return $this->error('450001','订单将被取消');
  123. }
  124. if($two_minutes <= $cancel_time){
  125. $expire=date("YmdHis",strtotime($two_minutes));
  126. }else{
  127. $expire=date("YmdHis",strtotime($cancel_time));
  128. }
  129. $attributes = [
  130. 'time_expire' => $expire,
  131. // 'trade_type' => 'JSAPI',
  132. 'trade_type' => $tradeType, // 支付方式,小程序支付使用JSAPI、APP
  133. 'body' => '订单支付', // 订单说明
  134. 'out_trade_no' => mt_rand('1000','9999').'_'.$order_no, // 自定义订单号
  135. 'total_fee' => $order->account*100, // 单位:分
  136. // 'total_fee' => 1, // 单位:分
  137. 'profit_sharing' => 'Y' //分账
  138. ];
  139. if($tradeType=='JSAPI'){
  140. if($is_assit){ //助手支付
  141. $openid=Assit::where('agent_id',Auth::user()->id)->value('openid');
  142. if(empty($openid)){
  143. return $this->error('450001','参数有误,请重新登录助手帐号');
  144. }
  145. }else{
  146. $openid=User::where('id',Auth::user()->id)->value('openid');
  147. }
  148. $attributes['openid']=$openid; // 当前用户的openId
  149. }
  150. Log::info([$options,$attributes,$order->account*100]);
  151. $result = $payment->order->unify($attributes);
  152. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  153. Log::info($result);
  154. $order_info['order']=$order_no;
  155. $order_info['price']=$order->account;
  156. Cache::put('prepay_id:'.$result['prepay_id'],$order_info,3600*2);
  157. $prepayId = $result['prepay_id'];
  158. if($tradeType == 'JSAPI'){
  159. $config = $jssdk->sdkConfig($prepayId, false);
  160. }else{
  161. $config = $jssdk->appConfig($prepayId);
  162. }
  163. Log::info($config);
  164. return response()->json([
  165. 'error_code'=>200,
  166. 'code'=>200,
  167. 'msg'=>'成功',
  168. 'data'=>$config
  169. ]);
  170. }
  171. if ($result['return_code'] == 'FAIL' && array_key_exists('return_msg', $result)) {
  172. Log::info('订单支付失败:'.$order->user_id.'/'.$order_no.'/'.$order->account.'/'.$result['return_msg']);
  173. return response()->json([
  174. 'error_code'=>4011,
  175. 'code'=>4011,
  176. 'msg'=> $result['return_msg'],
  177. ]);
  178. }
  179. Log::info('订单支付失败:'.$order->user_id.'/'.$order_no.'/'.$order->account.'/'.$result['err_code_des']);
  180. return response()->json([
  181. 'error_code'=>4011,
  182. 'code'=>4011,
  183. 'msg'=> $result['err_code_des'],
  184. ]);
  185. }
  186. public function getOrderNo(Request $request){
  187. $prepay_id=$request->input('prepay_id');
  188. $order_info=Cache::get('prepay_id:'.$prepay_id);
  189. if($order_info){
  190. Cache::forget('prepay_id:'.$prepay_id);
  191. }
  192. return $this->success($order_info);
  193. }
  194. protected function options($id,$type)
  195. {
  196. if($type=='APP'){
  197. $arr= [
  198. 'app_id' => config('wechat.daweiboshi_app.default.app_id'),
  199. 'key' => config('wechat.payment.test.key'),
  200. 'mch_id' => config('wechat.payment.test.mch_id'),
  201. 'notify_url' => url('api/payment/app_notify'),
  202. ];
  203. }else{
  204. $arr= [
  205. 'app_id' => config('wechat.payment.test.app_id'),
  206. 'key' => config('wechat.payment.test.key'),
  207. 'mch_id' => config('wechat.payment.test.mch_id'),
  208. 'notify_url' => url('api/payment/h5_notify'),
  209. ];
  210. }
  211. if($id){
  212. $arr['sub_mch_id'] = Store::where('id',$id)->value('sub_mchid');
  213. }
  214. return $arr;
  215. }
  216. public function h5_notify(Request $request)
  217. {
  218. Log::info('h5支付完成开始微信回调');
  219. $options = $this->options(null,'JSAPI');
  220. $payment = Factory::payment($options);
  221. $response = $payment->handlePaidNotify(function ($message, $fail)
  222. {
  223. // 根据返回的订单号查询订单数据
  224. Log::error($message);
  225. LogW::create([
  226. 'content'=>json_encode($message),
  227. 'type'=>1
  228. ]);
  229. $rand_no=explode('_',$message['out_trade_no'])[0];
  230. $message['out_trade_no']=explode('_',$message['out_trade_no'])[1];
  231. $order=OrderW::where('order_no',$message['out_trade_no'])->first();
  232. if ($order->is_pay==1){
  233. Log::info('订单号为:'.$message['out_trade_no'].'已经支付');
  234. if($order->wechat_pay_no != $message['transaction_id']){
  235. OrderOnceW::create([
  236. 'order_no'=>$message['out_trade_no'],
  237. 'open_id'=>$message['openid'],
  238. 'wechat_no'=>$message['transaction_id'],
  239. 'result_code'=>$message['result_code'],
  240. 'type'=> 1,
  241. ]);
  242. }
  243. return true;
  244. }
  245. if (!$order || $order->status == 1) {
  246. Log::info('订单不存在:'.$message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
  247. return true;//订单不存在 或者 订单已支付
  248. }
  249. if ($message['result_code'] === 'FAIL') { //支付失败
  250. Log::warning('[wechat-pay]:' . json_encode($message, true));
  251. return true;
  252. }
  253. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  254. // 支付成功后的业务逻辑
  255. if($message['result_code'] === 'SUCCESS') {
  256. //处理订单支付完整后的信息
  257. $this->settlement($order,$message,$rand_no);
  258. }
  259. Log::info('微信支付回调结束');
  260. return true;
  261. }else{
  262. Log::info('通信失败');
  263. return $fail('通信失败,请稍后再通知我');
  264. }
  265. });
  266. return $response;
  267. }
  268. public function app_notify(Request $request)
  269. {
  270. Log::info('app支付完成开始微信回调');
  271. $options = $this->options(null,'APP');
  272. $payment = Factory::payment($options);
  273. $response = $payment->handlePaidNotify(function ($message, $fail)
  274. {
  275. // 根据返回的订单号查询订单数据
  276. Log::error($message);
  277. LogW::create([
  278. 'content'=>json_encode($message),
  279. 'type'=>1
  280. ]);
  281. $rand_no=explode('_',$message['out_trade_no'])[0];
  282. $message['out_trade_no']=explode('_',$message['out_trade_no'])[1];
  283. $order=OrderW::where('order_no',$message['out_trade_no'])->first();
  284. if ($order->is_pay==1){
  285. Log::info('订单号为:'.$message['out_trade_no'].'已经支付');
  286. if($order->wechat_pay_no != $message['transaction_id']){
  287. OrderOnceW::create([
  288. 'order_no'=>$message['out_trade_no'],
  289. 'open_id'=>$message['openid'],
  290. 'wechat_no'=>$message['transaction_id'],
  291. 'result_code'=>$message['result_code'],
  292. 'type'=> 1,
  293. ]);
  294. }
  295. return true;
  296. }
  297. if (!$order || $order->status == 1) {
  298. Log::info('订单不存在:'.$message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
  299. return true;//订单不存在 或者 订单已支付
  300. }
  301. if ($message['result_code'] === 'FAIL') { //支付失败
  302. Log::warning('[wechat-pay]:' . json_encode($message, true));
  303. return true;
  304. }
  305. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  306. // 支付成功后的业务逻辑
  307. if($message['result_code'] === 'SUCCESS') {
  308. //处理订单支付完整后的信息
  309. $this->settlement($order,$message,$rand_no);
  310. }
  311. Log::info('微信支付回调结束');
  312. return true;
  313. }else{
  314. Log::info('通信失败');
  315. return $fail('通信失败,请稍后再通知我');
  316. }
  317. });
  318. return $response;
  319. }
  320. private function settlement($order,$message,$rand_no){
  321. $total_fee=round($message['total_fee']/100,2);
  322. DB::connection('mysql_w')->transaction(function()use($order,$message,$total_fee,$rand_no){
  323. // $basic=ZbsBasic::whereIn('keys',['round_start_time','round_end_time','season'])->pluck('value','keys')->toArray();
  324. // $start_time=$basic['round_start_time'];
  325. // $end_time=$basic['round_end_time'];
  326. // $season=$basic['season'];
  327. // Log::info('0000000-00000'.$start_time.'///'.$end_time.'///'.$season);
  328. // $sto=Store::where('id',$order->store_id)->first();
  329. // $userzs = UserZ::where('dwbs_id', $sto->user_id)->get();
  330. //
  331. // $order->is_zbs = 0;
  332. $integral=$order->total*2;
  333. $zbs=0; //不在争霸赛期间
  334. //
  335. // if(count($userzs) > 0) { //争霸赛存在该用户
  336. //// if(!empty($userz)) { //争霸赛存在该用户
  337. // $now = Carbon::now()->timestamp;
  338. // $pay_time=Carbon::parse($message['time_end'])->timestamp;
  339. // Log::info('<<<<<>>>>>'.$now.'///'.$start_time.'///'.$end_time.'///'.$pay_time);
  340. // if ($pay_time >= $start_time && $pay_time <= $end_time) { //在争霸赛期间
  341. // $uids = \Illuminate\Support\Facades\Cache::remember('uids', Carbon::now()->addHours(12), function () use ($season) {
  342. // return ZbsEnroll::where('season', $season)->where('is_refund', 0)->where('status', 0)->pluck('uid')->toArray();
  343. // });
  344. //
  345. // foreach($userzs as $key =>$val){
  346. // if (in_array($val->id, $uids)) {//报名了争霸赛
  347. // //标记争霸赛订单
  348. // $order->is_zbs = 1;
  349. // //学分变更
  350. // $integral=0;
  351. // $zbs=1; //在争霸赛期间
  352. // }
  353. // }
  354. // }
  355. // }
  356. $inte=IntegralW::where('order_no',$order->order_no)->where('store_id',$order->store_id)->where('type',1)->first();
  357. if(empty($inte)){
  358. IntegralW::create([
  359. 'store_id'=>$order->store_id,
  360. 'order_no'=>$order->order_no,
  361. 'num'=>$order->total,
  362. 'integral'=>$integral,
  363. 'integral_double'=>$integral,
  364. 'type'=>1,
  365. 'remark'=>'下单成功',
  366. 'is_zbs'=>$zbs,
  367. ]);
  368. }
  369. //修改订单状态
  370. $order->status = 1;
  371. $order->is_help = 1;
  372. $order->is_pay = 1;
  373. $order->pay_type = 1;
  374. $order->pay_money = $total_fee;
  375. $order->rand_no = $rand_no;
  376. $order->pay_open_id = $message['openid'];
  377. $order->self_pay = 1;
  378. $order->wechat_pay_no = $message['transaction_id'];
  379. $order->pay_at = $message['time_end'];
  380. $order->save();
  381. //店铺信息
  382. Log::info($message['out_trade_no']);
  383. Log::info($total_fee);
  384. $store=Store::where('id',$order->store_id)->lockForUpdate()->first();
  385. $start_money= $store->pending_amount;
  386. $store->pending_amount += $total_fee;
  387. $store->integral += $integral;
  388. $store->cycle_inte += $integral;
  389. $store->total += $order->total;
  390. $store->save();
  391. //规格销量
  392. $details=OrderDetailW::where('order_no',$order->order_no)->get();
  393. foreach($details as $key=>$val){
  394. $where=[];
  395. $where['goods_id']=$val->goods_id;
  396. $where['size']=$val->size;
  397. $where['type']=$val->type;
  398. $sku=StoreGoodsSku::where($where)->lockForUpdate()->first();
  399. $sku->sale_num += $val->num;
  400. $sku->save();
  401. //商品销量
  402. $goods=StoreGoods::where('id',$val->goods_id)->lockForUpdate()->first();
  403. $goods->sale_num += $val->num;
  404. $goods->save();
  405. //减微店代理库存
  406. $arr_test=Store::getTest();
  407. if(in_array($order->store_id,$arr_test)){ //判断是不是减库存代理
  408. Log::info('【'.$store->name.'的店铺-'.$goods->main_attr.'-'.$val->type.'-'.$val->size);
  409. $storeInventory=InventoryW::where(['store_id'=>$order->store_id,'goods_id'=>$val->goods_id,'sku_id'=>$val->sku_id])->lockForUpdate()->first();
  410. $storeInve_num=!empty($storeInventory)?$storeInventory->num:0;
  411. if($val->num > $storeInve_num){
  412. Log::info('【'.$store->name.'的店铺-'.$goods->main_attr.'-'.$val->type.'-'.$val->size.'】库存不足');
  413. }
  414. Log::info($val->num .'---'. $storeInve_num);
  415. $storeInventory->num-=$val->num;
  416. $storeInventory->save();
  417. }
  418. }
  419. //用户vip变更
  420. $vipLog=VipLogW::where('user_id',$order->user_id)
  421. ->where('store_id',$order->store_id)->first();
  422. if(empty($vipLog) && $order->total>=2){
  423. VipLogW::create([
  424. 'user_id'=>$order->user_id,
  425. 'store_id'=>$store->id,
  426. 'order_no'=>$message['out_trade_no'],
  427. 'type'=>2,//购买两套,添加vip
  428. 'remark'=>'购买自动添加vip'
  429. ]);
  430. OrderW::where('order_no',$message['out_trade_no'])->update([
  431. 'vip'=>1
  432. ]);
  433. }
  434. //支付日志
  435. Amount::create([
  436. 'user_id' => $order->user_id,
  437. 'store_id' => $order->store_id,
  438. 'order_no' => $message['out_trade_no'],
  439. 'transaction_id'=> $message['transaction_id'],
  440. 'money' => $total_fee,
  441. 'type' => 1, //支付
  442. 'status' => 1, //成功
  443. 'service_fee' => 0,
  444. 'start_money' => $start_money,
  445. 'end_money' => $store->pending_amount,
  446. 'start_amount' => $store->available_amount,//支付前可用金额
  447. 'end_amount' => $store->available_amount,//支付后可用金额
  448. 'remark' => '收款',
  449. ]);
  450. },5);
  451. //减库存
  452. DB::connection('mysql')->transaction(function()use($order) {
  453. event(new RetailOutboundEvent($order->order_no));
  454. },5);
  455. //算争霸赛学分
  456. // ZbsOrder::create([
  457. // 'order_id'=>$order->id,
  458. // 'status'=>0,
  459. // 'is_cancel'=>0,
  460. // ]);
  461. //代下单通知客户
  462. $user=UserW::where('id',$order->user_id)->first();
  463. $agent_id=Store::where('id',$order->store_id)->value('user_id');
  464. $agent = User::where('id',$agent_id)->first();
  465. if($user->openId){
  466. $data['openid']=$user->openId;
  467. $data['data']=[
  468. 'first' =>'代理【'.$agent->nickname .'】已帮您下单成功,请及时查看订单',
  469. 'keyword1' => $order->order_no,
  470. 'keyword2' => number_format($order->account, 2),
  471. 'keyword3' => $order->op_name,
  472. 'keyword4' => $agent->mobile,
  473. 'remark' => '点击链接进入系统,查看详情',
  474. ];
  475. event(new HelpOrderEvent($data));
  476. }
  477. //代下单通知代理
  478. //新订单通知
  479. // $store=Store::where('id',$order->store_id)->first();
  480. // $user=User::where('id',$store->user_id)->first();
  481. if($agent->openid){
  482. $data['openid']=$agent->openid;
  483. $data['data']=[
  484. 'first' => '您有新的客户订单,请及时处理',
  485. 'keyword1' => $user->nickname,
  486. 'keyword2' => number_format($order->account, 2),
  487. 'keyword3' => $order->order_no,
  488. 'keyword4' => $order->created_at
  489. ];
  490. event(new NewOrder($data));
  491. }
  492. }
  493. public function GetPay(){
  494. // 根据返回的订单号查询订单数据
  495. // $message=['appid' => 'wx5224793b7dc7f7b7',
  496. // 'bank_type' => 'OTHERS',
  497. // 'cash_fee' => '153700',
  498. // 'fee_type' => 'CNY',
  499. // 'is_subscribe' => 'Y',
  500. // 'mch_id' => '1603992271',
  501. // 'nonce_str' => '60628be3521c8',
  502. // 'openid' => 'ogTajwF_jHXaMTNcXLy1zT_XW-VU',
  503. // 'out_trade_no' => '2657_DX21033010243392320927494',
  504. // 'result_code' => 'SUCCESS',
  505. // 'return_code' => 'SUCCESS',
  506. // 'sign' => '8CC558CED364CB2384F188EBEF5D3255',
  507. // 'sub_mch_id' => '1607799110',
  508. // 'time_end' => '20210330102440',
  509. // 'total_fee' => '153700',
  510. // 'trade_type' => 'JSAPI',
  511. // 'transaction_id' => '4200000896202103308732225144',];
  512. // Log::error($message);
  513. Log::error('主动回调');
  514. $rand_no=explode('_',$message['out_trade_no'])[0];
  515. $message['out_trade_no']=explode('_',$message['out_trade_no'])[1];
  516. $order=OrderW::where('order_no',$message['out_trade_no'])->first();
  517. if (!$order || $order->status == 1) {
  518. Log::info('订单不存在:'.$message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
  519. return true;//订单不存在 或者 订单已支付
  520. }
  521. if ($message['result_code'] === 'FAIL') { //支付失败
  522. Log::warning('[wechat-pay]:' . json_encode($message, true));
  523. return true;
  524. }
  525. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  526. // 支付成功后的业务逻辑
  527. if($message['result_code'] === 'SUCCESS') {
  528. $total_fee=round($message['total_fee']/100,2);
  529. DB::connection('mysql_w')->transaction(function()use($order,$message,$total_fee,$rand_no){
  530. //修改订单状态
  531. $order->status = 1;
  532. $order->is_pay = 1;
  533. $order->pay_type = 1;
  534. $order->pay_money = $total_fee;
  535. $order->rand_no = $rand_no;
  536. $order->pay_open_id = $message['openid'];
  537. $order->self_pay = 1;
  538. $order->wechat_pay_no = $message['transaction_id'];
  539. $order->pay_at = $message['time_end'];
  540. $order->save();
  541. //店铺信息
  542. $store=Store::where('id',$order->store_id)->lockForUpdate()->first();
  543. $start_money= $store->pending_amount;
  544. Log::error($store->pending_amount);
  545. Log::error($total_fee);
  546. $store->pending_amount += $total_fee;
  547. $store->save();
  548. //规格销量
  549. $details=OrderDetailW::where('order_no',$order->order_no)->get();
  550. foreach($details as $key=>$val){
  551. $where=[];
  552. $where['goods_id']=$val->goods_id;
  553. $where['size']=$val->size;
  554. $where['type']=$val->type;
  555. $sku=StoreGoodsSku::where($where)->lockForUpdate()->first();
  556. $sku->sale_num += $val->num;
  557. $sku->save();
  558. //商品销量
  559. $goods=StoreGoods::where('id',$val->goods_id)->lockForUpdate()->first();
  560. $goods->sale_num += $val->num;
  561. $goods->save();
  562. }
  563. //用户vip变更
  564. $vipLog=VipLogW::where('user_id',$order->user_id)
  565. ->where('store_id',$order->store_id)->first();
  566. /*张奇新增**/
  567. UserW::where('id',$order->user_id)->update(['is_vip'=>1]);
  568. if(empty($vipLog) && $order->total>=2){
  569. VipLogW::create([
  570. 'user_id'=>$order->user_id,
  571. 'store_id'=>$store->id,
  572. 'order_no'=>$message['out_trade_no']
  573. ]);
  574. OrderW::where('order_no',$message['out_trade_no'])->update([
  575. 'vip'=>1
  576. ]);
  577. }
  578. //支付日志
  579. Amount::create([
  580. 'user_id' => $order->user_id,
  581. 'store_id' => $order->store_id,
  582. 'order_no' => $message['out_trade_no'],
  583. 'transaction_id'=> $message['transaction_id'],
  584. 'money' => $total_fee,
  585. 'type' => 1, //支付
  586. 'status' => 1, //成功
  587. 'service_fee' => 0,
  588. 'start_money' => $start_money,
  589. 'end_money' => $store->pending_amount,
  590. 'start_amount' => $store->available_amount,//支付前可用金额
  591. 'end_amount' => $store->available_amount,//支付后可用金额
  592. 'remark' => '收款',
  593. ]);
  594. },5);
  595. //减库存
  596. DB::connection('mysql')->transaction(function()use($order) {
  597. event(new RetailOutboundEvent($order->order_no));
  598. },5);
  599. //代下单通知客户
  600. $user=UserW::where('id',$order->user_id)->first();
  601. $agent_id=Store::where('id',$order->store_id)->value('user_id');
  602. $agent = User::where('id',$agent_id)->first();
  603. if($user->openId){
  604. $data['openid']=$user->openId;
  605. $data['data']=[
  606. 'first' =>'代理【'.$agent->nickname .'】已帮您下单成功,请及时查看订单',
  607. 'keyword1' => $order->order_no,
  608. 'keyword2' => number_format($order->account, 2),
  609. 'keyword3' => $order->op_name,
  610. 'keyword4' => $agent->mobile,
  611. 'remark' => '点击链接进入系统,查看详情',
  612. ];
  613. event(new HelpOrderEvent($data));
  614. }
  615. //代下单通知代理
  616. //新订单通知
  617. // $store=Store::where('id',$order->store_id)->first();
  618. // $user=User::where('id',$store->user_id)->first();
  619. if($agent->openid){
  620. $data['openid']=$agent->openid;
  621. $data['data']=[
  622. 'first' => '您有新的客户订单,请及时处理',
  623. 'keyword1' => $user->nickname,
  624. 'keyword2' => number_format($order->account, 2),
  625. 'keyword3' => $order->order_no,
  626. 'keyword4' => $order->created_at
  627. ];
  628. event(new NewOrder($data));
  629. }
  630. }
  631. return true;
  632. }
  633. }
  634. public function getc(){
  635. $arr=['DD21052507413880022425375','DD21052815445331406169875',
  636. 'DD21072617031211447413371',
  637. 'DD21072409242307226852652',
  638. 'DX21062619411395558884771',
  639. 'DX21060322150044423898976',
  640. 'DX21072722274881359336010',
  641. 'DX21072722054667762245663',
  642. 'DX21061221143924883861697',
  643. 'DX21081212124361685786288',
  644. 'DX21080516405831422592274',
  645. 'DX21081222191558554151936',
  646. 'DD21070114344465217624191',
  647. 'DX21062612370251221614394',
  648. 'DD21081810585174889697435',
  649. 'DD21081717423715556697940',
  650. 'DX21092320373487075958695',
  651. 'DD21091008310834739743203',
  652. 'DX21091516461172167273109',
  653. 'DX21091516485490330788346',
  654. 'DX21091421560651633921802',
  655. 'DX21091521223258511559791',
  656. 'DX21092320335761762613987'];
  657. $orders=OrderW::with(['store:id,name,phone','user:id,nickname,phone'])
  658. ->whereIn('order_no',$arr)
  659. ->select('order_no','status','store_id','user_id','signature','is_finish','express_info','express_time','created_at')
  660. ->orderBy('store_id')
  661. ->get();
  662. return $this->success($orders);
  663. }
  664. }