| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -2,8 +2,10 @@ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					namespace App\Http\Controllers\Api; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Common\OrderStatus as Status; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Common\PayType; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Http\Controllers\Controller; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\Agent; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\AgentProduct; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\Order; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\User; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use EasyWeChat\Factory; | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -36,14 +38,24 @@ class SharePayController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$openid = $user_info['openid']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$this->agent_id = $user_info['agent_id']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order = Order::with(['agentProduct']) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->whereRaw('`timeout` >= NOW()') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->find($order_id); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (!$order) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								return $this->error('订单不存在或已支付'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								return $this->error('订单已支付或已超时'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order->pay_user_id = $this->user_id; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order->save(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							//如果已经付定金或首付款,则仅支付尾款
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if ($order->status == Status::PAY_EARNEST) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$price = $order->price - $order->paid_money; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} else { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$price = $this->calc($order->price, $order->num, $order->pay_type, $order->agent_product); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$config = config('wechat.payment.default'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$config = array_merge($config, [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'app_id' => 'wxb35ef055a4dd8ad4', | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -56,7 +68,7 @@ class SharePayController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$result = $app->order->unify([ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'body' => $order->title, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'total_fee' => round($price * 100), //支付金额单位为分
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'trade_type' => 'JSAPI', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'openid' => $openid, | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -71,10 +83,36 @@ class SharePayController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								return $result; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$jump_appid = Agent::where('id', $order->agent_id)->value('appid'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$jssdk = $app->jssdk; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$goback_appid = Agent::where('id', $order->agent_id)->value('appid'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) + | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								['id' => $order->id, 'order_no' => $order->order_no, 'jump_appid' => $goback_appid]; // 返回数组
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								['id' => $order->id, 'order_no' => $order->order_no, 'jump_appid' => $jump_appid]; // 返回数组
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return $this->success($payConfig); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						/** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * 计算最终价格 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * @param float $price 订单价格 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * @param float $num 购买数量 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * @param int $pay_type 支付方式 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * @param AgentProduct $agent_product 代理商产品 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * @return float | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 */ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						private function calc(float $price, float $num, int $pay_type, AgentProduct $agent_product): float | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							/** 修改需要同步修改Order里面的 */ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							//根据支付方式计算价格
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									return $agent_product->deposit; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								} else if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									return $agent_product->earnest; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$total_price = $price * $num; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return round($total_price, 2); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} |