You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							607 lines
						
					
					
						
							21 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							607 lines
						
					
					
						
							21 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Http\Controllers\Api;
							 | 
						|
								
							 | 
						|
								use App\Common\PayType;
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Http\Controllers\Controller;
							 | 
						|
								use App\Jobs\OrderTimeout;
							 | 
						|
								use App\Models\AdminSetting;
							 | 
						|
								use App\Models\Agent;
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use App\Models\AgentProductSpec;
							 | 
						|
								use App\Models\AgentSetting;
							 | 
						|
								use App\Models\Coupon;
							 | 
						|
								use App\Models\OrderProductItem;
							 | 
						|
								use App\Models\Product;
							 | 
						|
								use App\Models\ProductSpec;
							 | 
						|
								use App\Models\SystemSetting;
							 | 
						|
								use App\Models\User;
							 | 
						|
								use App\Models\Order;
							 | 
						|
								use App\Service\OpenPlatform;
							 | 
						|
								use EasyWeChat\Factory;
							 | 
						|
								use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
							 | 
						|
								use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
							 | 
						|
								use EasyWeChat\Kernel\Http\StreamResponse;
							 | 
						|
								use GuzzleHttp\Exception\GuzzleException;
							 | 
						|
								use Illuminate\Http\Request;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								use App\Common\OrderStatus as Status;
							 | 
						|
								use Illuminate\Support\Facades\Storage;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 订单
							 | 
						|
								 * Class OrderController
							 | 
						|
								 * @package App\Http\Controllers\Api
							 | 
						|
								 */
							 | 
						|
								class OrderController extends Controller
							 | 
						|
								{
							 | 
						|
									//订单列表
							 | 
						|
								    public function index(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['page', 'status']);
							 | 
						|
										$request->validate([
							 | 
						|
											'page' => 'regex:/^\d+$/',
							 | 
						|
											'status' => 'nullable|regex:/^\d+(,\d+)*$/'
							 | 
						|
										], [
							 | 
						|
											'page.regex' => '页码错误',
							 | 
						|
											'status.regex' => '订单状态错误'
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$order_list = Order::where('user_id', $this->user_id);
							 | 
						|
										if (isset($formData['status'])) {
							 | 
						|
											if (preg_match('/^\d+$/', $formData['status'])) {
							 | 
						|
												$order_list = $order_list->where('status', $formData['status']);
							 | 
						|
											} else {
							 | 
						|
												$order_list = $order_list->whereIn('status', explode(',', $formData['status']));
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$order_list = $order_list->select('id', 'agent_product_id', 'product_id', 'title', 'picture', 'price', 'num', 'status', 'timeout', 'created_at')
							 | 
						|
											->orderBy('id', 'DESC')
							 | 
						|
											->simplePaginate(15)
							 | 
						|
											->toArray();
							 | 
						|
								
							 | 
						|
										$time = time();
							 | 
						|
								
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										foreach ($order_list['data'] as &$v) {
							 | 
						|
											//图片加上域名
							 | 
						|
											if (strpos($v['picture'], $prefix) === false) {
							 | 
						|
												$v['picture'] = $prefix . $v['picture'];
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if (strpos($v['picture'], $prefix) === false) {
							 | 
						|
												$v['picture'] = $prefix . $v['picture'];
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if (!empty($v['pictures']) && is_array($v['pictures'])) {
							 | 
						|
												$v['pictures'] = array_map(function($item) use ($prefix) {
							 | 
						|
													return strpos($item, $prefix) === false ? $prefix . $item : $item;
							 | 
						|
												}, $v['pictures']);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//未付款订单提示剩余付款时间
							 | 
						|
											if ($v['timeout'] !== null) {
							 | 
						|
												$second = strtotime($v['timeout']) - $time;
							 | 
						|
								
							 | 
						|
												if ($second > 0) {
							 | 
						|
													$text_arr = [
							 | 
						|
														Status::UNPAID => '付款',
							 | 
						|
														Status::OFFLINE_UNPAID => '线下付款',
							 | 
						|
														Status::PAY_EARNEST => '付尾款',
							 | 
						|
													];
							 | 
						|
													if (isset($text_arr[$v['status']])) {
							 | 
						|
														$v['status_text'] = '请在' . ceil($second / 60) . "分钟内" . ($text_arr[$v['status']] ?? '付款');
							 | 
						|
													}
							 | 
						|
												} else if ($second < 0 && $v['status'] == Status::PAY_EARNEST) {
							 | 
						|
													$v['status_text'] = '尾款支付已超时';
							 | 
						|
												} /*else { //此部分由定时处理
							 | 
						|
													$timeout_ids[] = $v['id'];
							 | 
						|
													$v['status'] = Status::CANCEL;
							 | 
						|
													$v['status_text'] = '已取消';
							 | 
						|
													//此部分已由定时任务处理
							 | 
						|
													Product::query()->find($v['product_id'])->increment('stock', $v['num']);
							 | 
						|
												}*/
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										return $this->success($order_list);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//提交订单
							 | 
						|
									public function create(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->input();
							 | 
						|
										$formData = array_map(fn($v) => is_string($v) ? trim($v) : $v, $formData); //过滤,删除首尾空
							 | 
						|
								
							 | 
						|
										//表单验证
							 | 
						|
										$pay_type_values = join(',', array_keys(PayType::array()));
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => ['required', 'regex:/^\d+$/'],
							 | 
						|
								//			'name' => ['required', 'between:2,20'],
							 | 
						|
								//			'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'],
							 | 
						|
											'pay_type' => ['required', 'in:' . $pay_type_values],
							 | 
						|
											'num' => ['required', 'min:1'],
							 | 
						|
											'spec_id' => ['required', 'array'],
							 | 
						|
											'info' => ['array'],
							 | 
						|
										], [
							 | 
						|
											'id.required' => '未指定产品ID',
							 | 
						|
								//			'name.required' => '请输入联系人姓名',
							 | 
						|
								//			'mobile.required' => '请输入联系手机号',
							 | 
						|
											'id.regex' => '产品ID错误',
							 | 
						|
								//			'name.between' => '联系人姓名在2~20字符之间',
							 | 
						|
								//			'mobile.regex' => '请输入11位手机号',
							 | 
						|
											'pay_type.required' => '请选择支付方式',
							 | 
						|
											'pay_type.in' => '不存在此支付方式',
							 | 
						|
											'num.required' => '请输入购买数量',
							 | 
						|
											'num.min' => '购买数量输入错误',
							 | 
						|
											'spec_id.required' => '请选择产品规格',
							 | 
						|
											'spec_id.array' => '产品规格必须是数组',
							 | 
						|
											'info.array' => '订单信息必须是数组',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$ap = AgentProduct::with(['coupon', 'product.diyForm.fields', 'agentCloudProduct:id,price'])
							 | 
						|
											->where('stock', '>=', $formData['num'])
							 | 
						|
											->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单
							 | 
						|
											->whereDoesntHave('agentProductItem', function ($query) {
							 | 
						|
												return $query->whereHas('product', function ($query) {
							 | 
						|
													return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
							 | 
						|
												});
							 | 
						|
											})
							 | 
						|
											->first();
							 | 
						|
										if (!$ap || !$ap->product) {
							 | 
						|
											return $this->error('产品已下架或库存不足');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$order_info = $formData['info'] ?? [];
							 | 
						|
										if (!empty($ap->product->diyForm->fields) && !$ap->product->diyForm->fields->isEmpty()) {
							 | 
						|
											$fields = array_column($ap->product->diyForm->fields->toArray(), null, 'field');
							 | 
						|
											foreach ($fields as &$field) {
							 | 
						|
												if ($field['required'] && !isset($order_info[$field['field']])) { //判断是否必填
							 | 
						|
													return $this->error($field['field'] . '不能为空');
							 | 
						|
												}
							 | 
						|
												$field['value'] = $order_info[$field['field']] ?? '';
							 | 
						|
											}
							 | 
						|
											$order_info = $fields;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//0:单品销售;1:组合销售
							 | 
						|
										if ($ap->type == 0) {
							 | 
						|
											$spec = AgentProductSpec::with('productSpec')
							 | 
						|
												->where('agent_product_id', $formData['id'])
							 | 
						|
												->whereIn('id', $formData['spec_id'])
							 | 
						|
												->get();
							 | 
						|
											if ($spec->isEmpty()) {
							 | 
						|
												return $this->error('你选择的产品规格不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$spec = $spec->toArray();
							 | 
						|
											$spec = array_map(function ($v) {
							 | 
						|
												$v = array_merge($v['product_spec'] ?? [], $v);
							 | 
						|
												unset($v['product_spec']);
							 | 
						|
												return $v;
							 | 
						|
											}, $spec);
							 | 
						|
								
							 | 
						|
											//订单总价
							 | 
						|
											$order_price = 0;
							 | 
						|
											foreach ($spec as $v) {
							 | 
						|
												$order_price += $v['price'] * $formData['num'];
							 | 
						|
											}
							 | 
						|
											$order_price = round($order_price, 2);
							 | 
						|
								
							 | 
						|
											//0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type')
							 | 
						|
											switch ($ap->product->type) {
							 | 
						|
												case 0:
							 | 
						|
												case 2:
							 | 
						|
													if (empty($formData['departure_time']) || !strtotime($formData['departure_time'])) {
							 | 
						|
														return $this->error('请选择出发时间');
							 | 
						|
													}
							 | 
						|
													//旅游线路->出发时间;景区->入园时间
							 | 
						|
													$key = [0 => '出发时间', 2 => '入园时间'];
							 | 
						|
													if (isset($key[$ap->product->type])) {
							 | 
						|
														$order_info[$key[$ap->product->type]] = $formData['departure_time'];
							 | 
						|
													}
							 | 
						|
													break;
							 | 
						|
												case 1:
							 | 
						|
													if (empty($formData['check_in_time']) || !strtotime($formData['check_in_time'])) {
							 | 
						|
														return $this->error('请选择入住时间');
							 | 
						|
													}
							 | 
						|
													if (empty($formData['check_out_time']) || !strtotime($formData['check_out_time'])) {
							 | 
						|
														return $this->error('请选择离店时间');
							 | 
						|
													}
							 | 
						|
													if (empty($formData['arrival_time']) || !strtotime($formData['arrival_time'])) {
							 | 
						|
														return $this->error('请选择到店时间');
							 | 
						|
													}
							 | 
						|
													$order_info['入住时间'] = $formData['check_in_time']; //入住时间
							 | 
						|
													$order_info['离店时间'] = $formData['check_out_time']; //离店时间
							 | 
						|
													$order_info['到店时间'] = $formData['arrival_time']; //到店时间
							 | 
						|
													break;
							 | 
						|
											}
							 | 
						|
										} else {
							 | 
						|
											return $this->error('不存在此类型产品');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//支付小程序的产品不允许购买
							 | 
						|
										if (AdminSetting::val('payee_appid') == Agent::where('id', $this->agent_id)->value('appid')) {
							 | 
						|
											return $this->error('系统出错了,购买失败~~');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$coupon_ids = [];
							 | 
						|
										if ($ap->coupon) {
							 | 
						|
											foreach ($ap->coupon as $v) {
							 | 
						|
												$coupon_ids[] = $v['id'];
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										DB::beginTransaction();
							 | 
						|
										try {
							 | 
						|
											//供应商产品表减库存
							 | 
						|
											$product_ids = explode(',', $ap->product_ids);
							 | 
						|
											$affect_row = Product::query()
							 | 
						|
												->where('stock', '>=', $formData['num']) //乐观锁
							 | 
						|
												->whereIn('id', $product_ids)
							 | 
						|
												->decrement('stock', $formData['num']);
							 | 
						|
											if ($affect_row != count($product_ids)) {
							 | 
						|
												throw new \Exception('供应产品库存不足');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//供应商产品规格减表减库存
							 | 
						|
											$product_spec_ids = array_column($spec, 'product_spec_id');
							 | 
						|
											$affect_row = ProductSpec::query()
							 | 
						|
												->where('stock', '>=', $formData['num']) //乐观锁
							 | 
						|
												->whereIn('id', $product_spec_ids)
							 | 
						|
												->decrement('stock', $formData['num']);
							 | 
						|
											if ($affect_row != count($product_spec_ids)) {
							 | 
						|
												throw new \Exception('你选择的供应产品规格库存不足');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//代理商产品表减库存
							 | 
						|
											$affect_row = AgentProduct::query()
							 | 
						|
												->where('stock', '>=', $formData['num']) //乐观锁
							 | 
						|
												->where('id', $formData['id'])
							 | 
						|
												->decrement('stock', $formData['num']);
							 | 
						|
											if (!$affect_row) {
							 | 
						|
												throw new \Exception('商户产品库存不足');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//代理商产品规格表减库存
							 | 
						|
											$agent_product_spec_ids = array_column($spec, 'id');
							 | 
						|
											$affect_row = AgentProductSpec::query()
							 | 
						|
												->where('stock', '>=', $formData['num']) //乐观锁
							 | 
						|
												->whereIn('id', $agent_product_spec_ids)
							 | 
						|
												->decrement('stock', $formData['num']);
							 | 
						|
											if ($affect_row != count($agent_product_spec_ids)) {
							 | 
						|
												throw new \Exception('你选择的商户产品规格库存不足');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//未付款时定金/订金的超时时间依然使用默认的订单超时时间,付定金/订金之后才使用定金超时时间
							 | 
						|
											$order_timeout = AgentSetting::val($this->agent_id, 'order_timeout') ?? 60; //默认60分钟
							 | 
						|
											$timeout = date('Y-m-d H:i:s', time() + $order_timeout * 60); //60 * 分钟转为秒
							 | 
						|
								
							 | 
						|
											//处理预付金额
							 | 
						|
											if ($formData['pay_type'] == PayType::DEPOSIT_PAY) {
							 | 
						|
												//订金支付
							 | 
						|
												$prepayPrice = round(count($spec) * $ap->deposit * $formData['num'], 2);
							 | 
						|
												$prepayTimeout = $ap->deposit_timeout;
							 | 
						|
											} else if($formData['pay_type'] == PayType::EARNEST_PAY) {
							 | 
						|
												//定金支付
							 | 
						|
												$prepayPrice = round(count($spec) * $ap->earnest * $formData['num'], 2);
							 | 
						|
												$prepayTimeout = $ap->earnest_timeout;
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$orderNo = $this->getOrderNo();
							 | 
						|
											// 存入订单表
							 | 
						|
											$order = Order::query()->create([
							 | 
						|
												'user_id' => $this->user_id,
							 | 
						|
												'agent_id' => $this->agent_id,
							 | 
						|
												'order_no' => $orderNo,
							 | 
						|
												'num' => $formData['num'],
							 | 
						|
												'price' => $order_price,
							 | 
						|
												'name' => $formData['name'] ?? '',
							 | 
						|
												'mobile' => $formData['mobile'] ?? '',
							 | 
						|
												'title' => $ap->title,
							 | 
						|
												'picture' => $ap->picture,
							 | 
						|
												'agent_product_id' => $ap->id,
							 | 
						|
												'product_id' => $ap->product_id,
							 | 
						|
												'product_ids' => $ap->product->product_ids ?? $ap->product_id,
							 | 
						|
												'status' => $formData['pay_type'] == PayType::OFFLINE ? Status::OFFLINE_UNPAID : Status::UNPAID,
							 | 
						|
												'pay_type' => $formData['pay_type'],
							 | 
						|
												'coupon_id' => join(',', $coupon_ids),
							 | 
						|
												'guide_id' => $ap->guide_id,
							 | 
						|
												'guide_price' => $ap->guide_price,
							 | 
						|
												'timeout' => $timeout,
							 | 
						|
												'agent_cloud_pid' => $ap->agent_cloud_pid,
							 | 
						|
												'agent_cloud_price' => $ap->agentCloudProduct->price ?? 0,
							 | 
						|
												'prepay_price' => $prepayPrice ?? 0,
							 | 
						|
												'prepay_timeout' => $prepayTimeout ?? 0,
							 | 
						|
												'single_price' => SystemSetting::val('single', 'price'),
							 | 
						|
												'info' => $order_info,
							 | 
						|
											]);
							 | 
						|
								
							 | 
						|
											//存入订单产品表
							 | 
						|
											OrderProductItem::insert([
							 | 
						|
												'order_id' => $order->id,
							 | 
						|
												'agent_id' => $this->agent_id,
							 | 
						|
												'agent_product_id' => $ap->id,
							 | 
						|
												'supplier_id' => $ap->product->supplier_id,
							 | 
						|
												'product_id' => $ap->product->id,
							 | 
						|
												'num' => $formData['num'],
							 | 
						|
												'price' => $ap->product->price * $formData['num'],
							 | 
						|
												'agent_product_spec_id' => json_encode($agent_product_spec_ids),
							 | 
						|
												'product_type' => $ap->product->type,
							 | 
						|
												'product_spec_id' => json_encode($product_spec_ids),
							 | 
						|
												'single_deposit' => $ap->product->single_deposit,
							 | 
						|
											]);
							 | 
						|
								
							 | 
						|
											//订单超时
							 | 
						|
											OrderTimeout::dispatch($orderNo,$order_timeout * 60);
							 | 
						|
								
							 | 
						|
											DB::commit();
							 | 
						|
										} catch (\Exception $e) {
							 | 
						|
											DB::rollBack();
							 | 
						|
											return $this->error($e->getMessage());
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if ($formData['pay_type'] == PayType::OFFLINE) { //线下支付
							 | 
						|
											return $this->success(['id' => $order->id], '操作成功,请及时联系客服付款');
							 | 
						|
										} else { //在线支付或定金支付
							 | 
						|
											/*$config = $this->payConfig($order, $price);
							 | 
						|
											if (!empty($config['paySign'])) {
							 | 
						|
												return $this->success($config);
							 | 
						|
											} else {
							 | 
						|
												return $this->error($config['err_code_des'] ?? join(',', $config));
							 | 
						|
											}*/
							 | 
						|
											// 跳转支付专用
							 | 
						|
											return $this->success(['id' => $order->id, 'jump' => true, 'jump_appid' => AdminSetting::val('payee_appid')]);
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//申请退款
							 | 
						|
									public function refund(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['id', 'desc', 'pictures']);
							 | 
						|
								
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => 'required|integer',
							 | 
						|
											'desc' => 'required|string',
							 | 
						|
											'pictures' => 'nullable|array',
							 | 
						|
										], [
							 | 
						|
											'*.required' => '内容输入不完整',
							 | 
						|
											'pictures.array' => '图片必须是数组',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										//去掉图片地址前的域名
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										foreach ($formData['pictures'] as &$v) {
							 | 
						|
											$v = str_replace($prefix, '', $v);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$order = Order::firstWhere(['id' => $formData['id'], 'user_id' => $this->user_id]);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单不存在');
							 | 
						|
										}
							 | 
						|
										//订金/定金/首付款不允许退款,只有付全款才能申请退款
							 | 
						|
										if (!in_array($order->status, [Status::PAID, Status::PAID_RETAINAGE])) {
							 | 
						|
											return $this->error('当前订单状态不允许退款');
							 | 
						|
										}
							 | 
						|
										$order->refund_info = [
							 | 
						|
											'desc' => strip_tags($formData['desc']),
							 | 
						|
											'refund_no' => $this->getOrderNo(), //退款单号
							 | 
						|
											'pictures' => $formData['pictures'] ?? [],
							 | 
						|
											'old_status' => $order->status,
							 | 
						|
										];
							 | 
						|
										$order->status = Status::REFUNDING;
							 | 
						|
										$order->save();
							 | 
						|
								
							 | 
						|
										return $this->success();
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//获取应付金额及相关产品信息
							 | 
						|
									public function getPrice(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['id', 'num', 'pay_type', 'spec_id']);
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => 'required|integer',
							 | 
						|
											'num' => 'required|integer',
							 | 
						|
											'pay_type' => 'required|integer',
							 | 
						|
											'spec_id' => 'required|array',
							 | 
						|
										], [
							 | 
						|
											'*.required' => '参数缺失',
							 | 
						|
											'*.integer' => '参数类型错误',
							 | 
						|
											'spec_id.array' => '产品规格必须是数组',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										if (!$formData['num'] || $formData['num'] < 1) {
							 | 
						|
											return $this->error('未指定产品数量');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$ap = AgentProduct::with(['coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at'])
							 | 
						|
											->has('product')
							 | 
						|
											->find($formData['id'], ['id', 'price', 'original_price', 'product_id', 'title', 'pictures', 'earnest', 'earnest_timeout', 'deposit', 'deposit_timeout']);
							 | 
						|
								
							 | 
						|
										if (!$ap) {
							 | 
						|
											return $this->error('产品信息不存在');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										$ap->pictures = array_map(fn($v) => $prefix . $v, $ap->pictures);
							 | 
						|
								
							 | 
						|
										//如果是线下支付,显示的价格跟在线全款支付价格一样
							 | 
						|
								
							 | 
						|
										$spec = AgentProductSpec::where('agent_product_id', $formData['id'])
							 | 
						|
											->whereIn('id', $formData['spec_id'])
							 | 
						|
											->get();
							 | 
						|
								
							 | 
						|
										if ($spec->isEmpty()) {
							 | 
						|
											return $this->error('你选择的产品规格不存在');
							 | 
						|
										}
							 | 
						|
										$spec = $spec->toArray();
							 | 
						|
								
							 | 
						|
										$ap->final_price = 0;
							 | 
						|
										if ($formData['pay_type'] == PayType::DEPOSIT_PAY) { //订金支付
							 | 
						|
											$ap->final_price = count($spec) * $ap->deposit * $formData['num'];
							 | 
						|
										}
							 | 
						|
										else if($formData['pay_type'] == PayType::EARNEST_PAY) { //定金支付
							 | 
						|
											$ap->final_price = count($spec) * $ap->earnest * $formData['num'];
							 | 
						|
										} else {
							 | 
						|
											foreach ($spec as $v) {
							 | 
						|
												$ap->final_price += $v['price'] * $formData['num'];
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$ap->final_price = round($ap->final_price, 2);
							 | 
						|
										$ap->num = $formData['num'];
							 | 
						|
								
							 | 
						|
										return $this->success($ap);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//订单支付(在订单列表发起)
							 | 
						|
									public function pay(Request $request)
							 | 
						|
									{
							 | 
						|
										$id = (int)request()->input('id');
							 | 
						|
								
							 | 
						|
										//订单信息
							 | 
						|
										$order = Order::query()
							 | 
						|
											->with('agentProduct')
							 | 
						|
											->where(['user_id' => $this->user_id, 'agent_id' => $this->agent_id])
							 | 
						|
											->whereRaw('`timeout` >= NOW()')
							 | 
						|
											->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])
							 | 
						|
											->find($id);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单已支付或已超时');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										// 跳转支付专用
							 | 
						|
										return $this->success(['id' => $id, 'jump' => true, 'jump_appid' => AdminSetting::val('payee_appid')]);
							 | 
						|
								
							 | 
						|
										/*$ap = AgentProduct::with('coupon')->find($order->agent_product_id);
							 | 
						|
								
							 | 
						|
										//如果已经付定金或首付款,则仅支付尾款
							 | 
						|
										if ($order->status == Status::PAY_EARNEST) {
							 | 
						|
											$price = $order->price - $order->paid_money;
							 | 
						|
										} else {
							 | 
						|
											$price = $this->calc($order->price, $order->num, $order->pay_type, $ap);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$config = $this->payConfig($order, $price);
							 | 
						|
										if (!empty($config['paySign'])) {
							 | 
						|
											return $this->success($config);
							 | 
						|
										} else {
							 | 
						|
											return $this->error($config['err_code_des'] ?? join(',', $config));
							 | 
						|
										}*/
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//获取支付配置信息
							 | 
						|
									private function payConfig($order, $price)
							 | 
						|
									{
							 | 
						|
										//用户openid
							 | 
						|
										$openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
							 | 
						|
								
							 | 
						|
										//代理商信息
							 | 
						|
										$agent = Agent::query()->find($this->agent_id);
							 | 
						|
								
							 | 
						|
										$config = config('wechat.payment.default');
							 | 
						|
										$config = array_merge($config, [
							 | 
						|
											'app_id' => $agent->appid,
							 | 
						|
											'mch_id' => $agent->mchid,
							 | 
						|
											'key' => $agent->mchkey,
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$app = Factory::payment($config);
							 | 
						|
										try {
							 | 
						|
											$result = $app->order->unify([
							 | 
						|
												'body' => mb_strcut($order->title, 0, 127),
							 | 
						|
												'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
							 | 
						|
												'total_fee' => round($price * 100), //支付金额单位为分
							 | 
						|
												'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
							 | 
						|
												'trade_type' => 'JSAPI',
							 | 
						|
												'openid' => $openid,
							 | 
						|
												'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
							 | 
						|
											]);
							 | 
						|
										} catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
							 | 
						|
											return ['error' => $e->getMessage(), 'file' => basename($e->getFile()), 'line' => $e->getLine()];
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if (empty($result['prepay_id'])) {
							 | 
						|
											return $result;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$jssdk = $app->jssdk;
							 | 
						|
										return $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//订单详情
							 | 
						|
									public function show()
							 | 
						|
									{
							 | 
						|
										$id = (int)request()->input('id');
							 | 
						|
								
							 | 
						|
										$fields = ['id', 'agent_id', 'order_no', 'agent_product_id', 'num', 'price', 'name', 'mobile', 'title', 'picture', 'status',
							 | 
						|
											'pay_type', 'coupon_id', 'paid_money', 'paid_at', 'refund_info', 'verify_code', 'info', 'created_at'];
							 | 
						|
										$order = Order::with('agent:id,appid,appsecret')
							 | 
						|
											->where('user_id', $this->user_id)
							 | 
						|
											->find($id, $fields);
							 | 
						|
								
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单不存在');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//订单ID和核销码拼接,查询时通过订单ID和核销码来查询,这样核销码不用建索引
							 | 
						|
										$order->verify_code = $order->verify_code ? $order->id . '-' . $order->verify_code : '';
							 | 
						|
								
							 | 
						|
										//如果有核销码,生成核销二维码
							 | 
						|
										if ($order->verify_code) {
							 | 
						|
											$app = new OpenPlatform();
							 | 
						|
											$refreshToken = $app->refreshToken($order->agent->appid);
							 | 
						|
											if (!$refreshToken) {
							 | 
						|
												return $this->error('获取refresh_token失败');
							 | 
						|
											}
							 | 
						|
											$app = $app->miniProgram($order->agent->appid, $refreshToken);
							 | 
						|
								
							 | 
						|
											//由于参数最多只能32个字符,故通过下面这种方式传参
							 | 
						|
											//pt表示使用普通订单,使用api/verification/verify接口核销;
							 | 
						|
											//hy表示行业产品订单,使用api/verification/industry_verify接口核销
							 | 
						|
											$response = $app->app_code->getUnlimit('pt' . $order->verify_code, ['page' => 'pages/verification/index']);
							 | 
						|
								
							 | 
						|
											if ($response instanceof StreamResponse) {
							 | 
						|
												$filename = $response->saveAs(storage_path('app/public/verify_code'), $order->verify_code);
							 | 
						|
												$order->verify_qrcode = Storage::disk('public')->url('verify_code/' . $filename);
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										unset($order->agent, $order->agent_id); //必须unset掉$order->agent,否则会造成appsecret泄漏
							 | 
						|
								
							 | 
						|
										$order->coupon = Coupon::query()
							 | 
						|
											->whereIn('id', $order->coupon_id)
							 | 
						|
											->where(['agent_id' => $this->agent_id, 'agent_product_id' => $order->agent_product_id,])
							 | 
						|
											->get(['tag']);
							 | 
						|
								
							 | 
						|
										//info数组排序
							 | 
						|
										if (!empty($order->info) && is_array($order->info)) {
							 | 
						|
											$info = $order->info;
							 | 
						|
											uksort($info, function ($a1, $a2) use ($info) {
							 | 
						|
												$sort = ['入住时间' => 1, '离店时间' => 2, '到店时间' => 3, '出发时间' => 4, '入园时间' => 4]; //这些字段排在最前
							 | 
						|
												if (isset($sort[$a1]) && !isset($sort[$a2])) {
							 | 
						|
													return -1;
							 | 
						|
												} else if (!isset($sort[$a1]) && isset($sort[$a2])) {
							 | 
						|
													return 1;
							 | 
						|
												} else if (isset($sort[$a1]) && isset($sort[$a2])) {
							 | 
						|
													return $sort[$a1] > $sort[$a2] ? 1 : -1;
							 | 
						|
												} else if (isset($info[$a1]['sort'], $info[$a2]['sort'])) {
							 | 
						|
													return $info[$a1]['sort'] > $info[$a2]['sort'] ? 1 : -1;
							 | 
						|
												}
							 | 
						|
												return 0;
							 | 
						|
											});
							 | 
						|
											$order->info = $info;
							 | 
						|
										}
							 | 
						|
										return $this->success($order);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									// 生成订单号
							 | 
						|
									private function getOrderNo(): string
							 | 
						|
									{
							 | 
						|
										list($micro, $sec) = explode(' ', microtime());
							 | 
						|
										$micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
							 | 
						|
										return date('ymdHis', $sec) . $micro . mt_rand(1000, 9999);
							 | 
						|
									}
							 | 
						|
								}
							 |