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.
		
		
		
		
		
			
		
			
				
					
					
						
							80 lines
						
					
					
						
							2.7 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							80 lines
						
					
					
						
							2.7 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Http\Controllers\Api;
							 | 
						|
								use App\Common\OrderStatus as Status;
							 | 
						|
								use App\Http\Controllers\Controller;
							 | 
						|
								use App\Models\Agent;
							 | 
						|
								use App\Models\Order;
							 | 
						|
								use App\Models\User;
							 | 
						|
								use EasyWeChat\Factory;
							 | 
						|
								use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
							 | 
						|
								use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
							 | 
						|
								use GuzzleHttp\Exception\GuzzleException;
							 | 
						|
								use Illuminate\Support\Facades\Cache;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 跳转支付
							 | 
						|
								 * Class SharePay
							 | 
						|
								 * @package App\Http\Controllers\Api
							 | 
						|
								 */
							 | 
						|
								class SharePayController extends Controller
							 | 
						|
								{
							 | 
						|
									public function pay()
							 | 
						|
									{
							 | 
						|
										$order_id = request()->input('id');
							 | 
						|
										if (!$order_id) {
							 | 
						|
											return $this->error('无效的ID');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$this->user_id = Cache::get(request()->header('Authentication'));
							 | 
						|
										if (!$this->user_id) {
							 | 
						|
											return $this->error('获取用户信息失败');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//用户openid
							 | 
						|
										$user_info = User::query()->where('id', $this->user_id)->first(['agent_id', 'openid']); //此处要用where,value()用find有BUG
							 | 
						|
										$openid = $user_info['openid'];
							 | 
						|
										$this->agent_id = $user_info['agent_id'];
							 | 
						|
								
							 | 
						|
										$order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单不存在或已支付');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$order->pay_user_id = $this->user_id;
							 | 
						|
										$order->save();
							 | 
						|
								
							 | 
						|
										$config = config('wechat.payment.default');
							 | 
						|
										$config = array_merge($config, [
							 | 
						|
											'app_id' => 'wxb35ef055a4dd8ad4',
							 | 
						|
											'mch_id' => '1606181693',
							 | 
						|
											'key' => 'lfyyhyz8888888888888888888888888',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$app = Factory::payment($config);
							 | 
						|
										try {
							 | 
						|
											$result = $app->order->unify([
							 | 
						|
												'body' => $order->title,
							 | 
						|
												'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
							 | 
						|
												'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分
							 | 
						|
												'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
							 | 
						|
												'trade_type' => 'JSAPI',
							 | 
						|
												'openid' => $openid,
							 | 
						|
												'attach' => $this->user_id, //保存支付用户的user_id
							 | 
						|
								//				'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
							 | 
						|
											]);
							 | 
						|
										} catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
							 | 
						|
											return ['error' => $e->getMessage(), 'line' => $e->getLine()];
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if (empty($result['prepay_id'])) {
							 | 
						|
											return $result;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$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]; // 返回数组
							 | 
						|
										return $this->success($payConfig);
							 | 
						|
									}
							 | 
						|
								}
							 |