feat(payment): 完善微信支付类型并优化相关逻辑
- 新增 WechatPayType 类,定义微信支付的具体实现方式 - 在 PaymentType 枚举中添加获取微信支付类型的方法 - 在 ShopOrder 实体中增加微信支付子类型字段 - 优化 ShopOrderServiceImpl 中的微信支付逻辑,支持自动选择支付类型
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package com.gxwebsoft.payment.constants;
|
||||
|
||||
/**
|
||||
* 微信支付类型常量
|
||||
* 定义微信支付的具体实现方式
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-30
|
||||
*/
|
||||
public class WechatPayType {
|
||||
|
||||
/**
|
||||
* JSAPI支付 - 小程序/公众号内支付
|
||||
* 需要用户的openid
|
||||
*/
|
||||
public static final String JSAPI = "JSAPI";
|
||||
|
||||
/**
|
||||
* Native支付 - 扫码支付
|
||||
* 生成二维码供用户扫描支付
|
||||
*/
|
||||
public static final String NATIVE = "NATIVE";
|
||||
|
||||
/**
|
||||
* H5支付 - 手机网页支付
|
||||
* 在手机浏览器中调起微信支付
|
||||
*/
|
||||
public static final String H5 = "H5";
|
||||
|
||||
/**
|
||||
* APP支付 - 移动应用支付
|
||||
* 在APP中调起微信支付
|
||||
*/
|
||||
public static final String APP = "APP";
|
||||
|
||||
/**
|
||||
* 根据openid自动选择微信支付类型
|
||||
*
|
||||
* @param openid 用户openid
|
||||
* @return JSAPI 或 NATIVE
|
||||
*/
|
||||
public static String getAutoType(String openid) {
|
||||
return (openid != null && !openid.trim().isEmpty()) ? JSAPI : NATIVE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为有效的微信支付类型
|
||||
*
|
||||
* @param payType 支付类型
|
||||
* @return true表示有效
|
||||
*/
|
||||
public static boolean isValidType(String payType) {
|
||||
return JSAPI.equals(payType) || NATIVE.equals(payType) ||
|
||||
H5.equals(payType) || APP.equals(payType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付类型描述
|
||||
*
|
||||
* @param payType 支付类型
|
||||
* @return 描述文本
|
||||
*/
|
||||
public static String getDescription(String payType) {
|
||||
if (payType == null) {
|
||||
return "未知支付类型";
|
||||
}
|
||||
|
||||
switch (payType) {
|
||||
case JSAPI:
|
||||
return "小程序/公众号支付";
|
||||
case NATIVE:
|
||||
return "扫码支付";
|
||||
case H5:
|
||||
return "手机网页支付";
|
||||
case APP:
|
||||
return "移动应用支付";
|
||||
default:
|
||||
return "未知支付类型: " + payType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,20 @@ public enum PaymentType {
|
||||
return this == WECHAT || this == WECHAT_NATIVE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信支付的具体类型
|
||||
* @param openid 用户openid
|
||||
* @return JSAPI 或 NATIVE
|
||||
*/
|
||||
public String getWechatPayType(String openid) {
|
||||
if (!isWechatPay()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 有openid使用JSAPI,无openid使用Native
|
||||
return (openid != null && !openid.trim().isEmpty()) ? "JSAPI" : "NATIVE";
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为第三方支付
|
||||
*/
|
||||
|
||||
@@ -151,6 +151,9 @@ public class ShopOrder implements Serializable {
|
||||
@Schema(description = "支付方式:0余额支付,1微信支付,2支付宝支付,3银联支付,4现金支付,5POS机支付,6免费,7积分支付")
|
||||
private Integer payType;
|
||||
|
||||
@Schema(description = "微信支付子类型:JSAPI小程序支付,NATIVE扫码支付")
|
||||
private String wechatPayType;
|
||||
|
||||
@Schema(description = "代付支付方式:0余额支付,1微信支付,2支付宝支付,3银联支付,4现金支付,5POS机支付,6免费,7积分支付")
|
||||
private Integer friendPayType;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.gxwebsoft.common.system.service.PaymentService;
|
||||
import com.gxwebsoft.common.system.service.SettingService;
|
||||
import com.gxwebsoft.payment.constants.WechatPayType;
|
||||
import com.gxwebsoft.shop.mapper.ShopOrderMapper;
|
||||
import com.gxwebsoft.shop.param.ShopOrderParam;
|
||||
import com.gxwebsoft.common.core.web.PageParam;
|
||||
@@ -179,10 +180,14 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
if (order.getPayType().equals(102)) {
|
||||
// Native支付处理(兼容旧的102类型)
|
||||
System.out.println("⚠️ 检测到使用废弃的微信Native支付类型(102),建议升级到微信支付(1)");
|
||||
order.setWechatPayType(WechatPayType.NATIVE);
|
||||
return createNativePayOrder(order, payment);
|
||||
} else if (order.getPayType().equals(1)) {
|
||||
// 微信支付处理 - 根据是否有openid判断使用JSAPI还是Native
|
||||
if (order.getOpenid() != null && !order.getOpenid().trim().isEmpty()) {
|
||||
String wechatType = WechatPayType.getAutoType(order.getOpenid());
|
||||
order.setWechatPayType(wechatType);
|
||||
|
||||
if (WechatPayType.JSAPI.equals(wechatType)) {
|
||||
// 有openid,使用JSAPI支付
|
||||
return createJsapiPayOrder(order, payment);
|
||||
} else {
|
||||
@@ -284,7 +289,8 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
orderInfo.put("provider", "wxpay");
|
||||
orderInfo.put("codeUrl", response.getCodeUrl()); // Native支付返回二维码URL
|
||||
orderInfo.put("orderNo", order.getOrderNo());
|
||||
orderInfo.put("payType", "NATIVE");
|
||||
orderInfo.put("payType", WechatPayType.NATIVE);
|
||||
orderInfo.put("wechatPayType", WechatPayType.NATIVE);
|
||||
|
||||
return orderInfo;
|
||||
}
|
||||
@@ -366,6 +372,8 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
orderInfo.put("signType", "RSA");
|
||||
orderInfo.put("paySign", response.getPaySign());
|
||||
orderInfo.put("orderNo", order.getOrderNo());
|
||||
orderInfo.put("payType", WechatPayType.JSAPI);
|
||||
orderInfo.put("wechatPayType", WechatPayType.JSAPI);
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user