feat 增加code 动态获取openid的逻辑

debug 删除调试信息
fix 终端类型少了H5而导致发起支付失败的bug
This commit is contained in:
damonyuan 2024-11-10 15:08:18 +08:00
parent db5e362cbf
commit 942769780e
5 changed files with 27 additions and 6 deletions

View File

@ -68,6 +68,7 @@ public class PayController {
Integer payWay = requestObj.getPayWay();
Integer orderId = requestObj.getOrderId();
Integer terminal = LikeFrontThreadLocal.getTerminal();
String code = requestObj.getCode();
requestObj.setTerminal(terminal);
// 订单处理
@ -98,7 +99,7 @@ public class PayController {
throw new OperateException("订单已支付");
}
// 发起支付
Object result = iPayService.prepay(requestObj, terminal);
Object result = iPayService.prepay(requestObj, terminal, code);
return AjaxResult.success(result);
}

View File

@ -41,7 +41,7 @@ public interface IPayService {
* @param terminal 终端
* @return Object
*/
Object prepay(PaymentValidate params, Integer terminal);
Object prepay(PaymentValidate params, Integer terminal, String code);
/**
* 支付回调处理

View File

@ -1,5 +1,7 @@
package com.mdd.front.service.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
@ -25,6 +27,7 @@ import com.mdd.common.mapper.setting.DevPayConfigMapper;
import com.mdd.common.mapper.setting.DevPayWayMapper;
import com.mdd.common.mapper.user.UserAuthMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.plugin.wechat.WxMnpDriver;
import com.mdd.common.plugin.wechat.WxPayDriver;
import com.mdd.common.plugin.wechat.request.PaymentRequestV3;
import com.mdd.common.util.*;
@ -34,6 +37,8 @@ import com.mdd.front.vo.pay.PayStatusVo;
import com.mdd.front.vo.pay.PayWayInfoVo;
import com.mdd.front.vo.pay.PayWayListVo;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -163,7 +168,7 @@ public class PayServiceImpl implements IPayService {
* @param terminal 终端
* @return Object
*/
public Object prepay(PaymentValidate params, Integer terminal) {
public Object prepay(PaymentValidate params, Integer terminal, String code) {
try {
params.setTerminal(terminal);
String openId = null;
@ -174,6 +179,20 @@ public class PayServiceImpl implements IPayService {
if (StringUtils.isNotNull(userAuth)) {
openId = userAuth.getOpenid();
} else {
if(terminal.intValue() != ClientEnum.PC.getCode()) {
if (StringUtils.isNotEmpty(code)) {
if (ClientEnum.OA.getCode() == terminal.intValue()) {
WxMpService wxMpService = WxMnpDriver.oa();
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
openId = wxOAuth2AccessToken.getOpenId();
} else if (ClientEnum.MNP.getCode() == terminal.intValue()) {
WxMaService wxMaService = WxMnpDriver.mnp();
WxMaJscode2SessionResult sessionResult = wxMaService.getUserService().getSessionInfo(code);
openId = sessionResult.getOpenid();
}
}
}
}
switch (params.getPayWay()) {
@ -191,12 +210,10 @@ public class PayServiceImpl implements IPayService {
requestV3.setOrderAmount(params.getOrderAmount());
requestV3.setDescription(params.getDescription());
Object result = WxPayDriver.unifiedOrder(requestV3);
System.out.println(terminal);
if (terminal == ClientEnum.H5.getCode()) {
Assert.notNull(params.getRedirect(), "redirectUrl参数缺失");
JSONObject ret = new JSONObject();
String h5Url = result.toString();
System.out.println("aaaaaaaaaa");
ret.put("config", WxPayDriver.unifiedOrder(requestV3));
ret.put("pay_way", 2);
return ret;

View File

@ -70,7 +70,7 @@ public class UserServiceImpl implements IUserService {
}
vo.setIsAuth(false);
if (terminal.equals(ClientEnum.OA.getCode()) || terminal.equals(ClientEnum.MNP.getCode())) {
if (terminal.equals(ClientEnum.OA.getCode()) || terminal.equals(ClientEnum.MNP.getCode()) || terminal.equals(ClientEnum.H5.getCode())) {
UserAuth userAuth = userAuthMapper.selectOne(new QueryWrapper<UserAuth>()
.select("id,openid,terminal")
.eq("user_id", userId)

View File

@ -47,4 +47,7 @@ public class PaymentValidate implements Serializable {
@ApiModelProperty(value = "Terminal")
private Integer Terminal;
@ApiModelProperty(value = "微信授权code")
private String code;
}