feat: 微信小程序内支付时,下单接口传递openid

master
yin5th 2023-08-21 17:14:07 +08:00
parent a14bd86005
commit dc5945960f
4 changed files with 15 additions and 6 deletions

View File

@ -74,7 +74,11 @@ class Pay extends Api
$result = PayLogic::balancePay($post['order_id'], $post['from']); $result = PayLogic::balancePay($post['order_id'], $post['from']);
break; break;
case OrderEnum::PAY_WAY_WECHAT://微信支付 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; break;
case OrderEnum::PAY_WAY_ALIPAY://支付宝支付 case OrderEnum::PAY_WAY_ALIPAY://支付宝支付
$result = PayLogic::aliPay($post['order_id'], $post['from'],$this->client); $result = PayLogic::aliPay($post['order_id'], $post['from'],$this->client);

View File

@ -68,11 +68,13 @@ class LoginLogic extends Logic
return false; return false;
} }
// 将openid返回给前端缓存 支付的时候降openid传给后端
if (empty($user_id)) { if (empty($user_id)) {
//系统中没有用户-调用authlogin接口生成新用户 //系统中没有用户-调用authlogin接口生成新用户
return []; return ['openid' => $response['openid'] ?? ''];
} else { } else {
$user_info = UserServer::updateUser($response, Client_::mnp, $user_id); $user_info = UserServer::updateUser($response, Client_::mnp, $user_id);
$user_info['openid'] = $response['openid'] ?? '';
} }
//验证用户信息 //验证用户信息

View File

@ -296,7 +296,7 @@ class PayLogic extends Logic
* @author suny * @author suny
* @date 2021/7/13 6:24 下午 * @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) { switch ($form) {
case "trade": case "trade":
@ -327,7 +327,7 @@ class PayLogic extends Logic
return JsonServer::error('订单已支付'); return JsonServer::error('订单已支付');
} }
// 这里进行微信支付 // 这里进行微信支付
$res = WeChatPayServer::unifiedOrder($form, $order, $client); $res = WeChatPayServer::unifiedOrder($form, $order, $client, $openid);
if (false === $res) { if (false === $res) {
return JsonServer::error(WeChatPayServer::getError()); return JsonServer::error(WeChatPayServer::getError());
} }

View File

@ -87,7 +87,7 @@ class WeChatPayServer
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/ */
public static function unifiedOrder($from, $order, $order_source) public static function unifiedOrder($from, $order, $order_source, $openid = '')
{ {
try { try {
@ -99,11 +99,14 @@ class WeChatPayServer
//jsapi需要验证openID //jsapi需要验证openID
$check_source = [Client_::mnp, Client_::oa]; $check_source = [Client_::mnp, Client_::oa];
if (!$auth && in_array($order_source, $check_source)) { if (!$auth && in_array($order_source, $check_source)) {
throw new Exception('授权信息失效'); // throw new Exception('授权信息失效');
} }
$app = Factory::payment($config); $app = Factory::payment($config);
$attributes = self::getAttributes($from, $order, $order_source, $auth, $notify_url); $attributes = self::getAttributes($from, $order, $order_source, $auth, $notify_url);
if (empty($attributes['openid'])) {
$attributes['openid'] = $openid;
}
$result = $app->order->unify($attributes); $result = $app->order->unify($attributes);
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {