From dc5945960faac4a5b9838ba0a74c401e7d3d948a Mon Sep 17 00:00:00 2001 From: yin5th <541304803@qq.com> Date: Mon, 21 Aug 2023 17:14:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=86=85=E6=94=AF=E4=BB=98=E6=97=B6=EF=BC=8C=E4=B8=8B?= =?UTF-8?q?=E5=8D=95=E6=8E=A5=E5=8F=A3=E4=BC=A0=E9=80=92openid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/api/controller/Pay.php | 6 +++++- server/app/api/logic/LoginLogic.php | 4 +++- server/app/api/logic/PayLogic.php | 4 ++-- server/app/common/server/WeChatPayServer.php | 7 +++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server/app/api/controller/Pay.php b/server/app/api/controller/Pay.php index 4b788e04..bcac28c8 100644 --- a/server/app/api/controller/Pay.php +++ b/server/app/api/controller/Pay.php @@ -74,7 +74,11 @@ class Pay extends Api $result = PayLogic::balancePay($post['order_id'], $post['from']); break; case OrderEnum::PAY_WAY_WECHAT://微信支付 - $result = PayLogic::wechatPay($post['order_id'], $post['from'], $this->client); + if (empty($post['openid'])) { + //微信支付时必须传openid过来 + return JsonServer::error('缺少openid'); + } + $result = PayLogic::wechatPay($post['order_id'], $post['from'], $this->client, $post['openid']); break; case OrderEnum::PAY_WAY_ALIPAY://支付宝支付 $result = PayLogic::aliPay($post['order_id'], $post['from'],$this->client); diff --git a/server/app/api/logic/LoginLogic.php b/server/app/api/logic/LoginLogic.php index 61a34eef..90ee9f6b 100644 --- a/server/app/api/logic/LoginLogic.php +++ b/server/app/api/logic/LoginLogic.php @@ -68,11 +68,13 @@ class LoginLogic extends Logic return false; } + // 将openid返回给前端缓存 支付的时候降openid传给后端 if (empty($user_id)) { //系统中没有用户-调用authlogin接口生成新用户 - return []; + return ['openid' => $response['openid'] ?? '']; } else { $user_info = UserServer::updateUser($response, Client_::mnp, $user_id); + $user_info['openid'] = $response['openid'] ?? ''; } //验证用户信息 diff --git a/server/app/api/logic/PayLogic.php b/server/app/api/logic/PayLogic.php index 3520b8c9..6e4f81c8 100644 --- a/server/app/api/logic/PayLogic.php +++ b/server/app/api/logic/PayLogic.php @@ -296,7 +296,7 @@ class PayLogic extends Logic * @author suny * @date 2021/7/13 6:24 下午 */ - public static function wechatPay($order_id, $form, $client) + public static function wechatPay($order_id, $form, $client, $openid = '') { switch ($form) { case "trade": @@ -327,7 +327,7 @@ class PayLogic extends Logic return JsonServer::error('订单已支付'); } // 这里进行微信支付 - $res = WeChatPayServer::unifiedOrder($form, $order, $client); + $res = WeChatPayServer::unifiedOrder($form, $order, $client, $openid); if (false === $res) { return JsonServer::error(WeChatPayServer::getError()); } diff --git a/server/app/common/server/WeChatPayServer.php b/server/app/common/server/WeChatPayServer.php index 10d526b2..0500cea9 100644 --- a/server/app/common/server/WeChatPayServer.php +++ b/server/app/common/server/WeChatPayServer.php @@ -87,7 +87,7 @@ class WeChatPayServer * @throws \GuzzleHttp\Exception\GuzzleException * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException */ - public static function unifiedOrder($from, $order, $order_source) + public static function unifiedOrder($from, $order, $order_source, $openid = '') { try { @@ -99,11 +99,14 @@ class WeChatPayServer //jsapi需要验证openID $check_source = [Client_::mnp, Client_::oa]; if (!$auth && in_array($order_source, $check_source)) { - throw new Exception('授权信息失效'); +// throw new Exception('授权信息失效'); } $app = Factory::payment($config); $attributes = self::getAttributes($from, $order, $order_source, $auth, $notify_url); + if (empty($attributes['openid'])) { + $attributes['openid'] = $openid; + } $result = $app->order->unify($attributes); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {