|
@@ -6,11 +6,13 @@ import com.zanxiang.common.constant.Constants;
|
|
import com.zanxiang.common.domain.ResultMap;
|
|
import com.zanxiang.common.domain.ResultMap;
|
|
import com.zanxiang.common.exception.BaseException;
|
|
import com.zanxiang.common.exception.BaseException;
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
|
+import com.zanxiang.module.util.bean.BeanUtil;
|
|
import com.zanxiang.module.web.util.IpUtil;
|
|
import com.zanxiang.module.web.util.IpUtil;
|
|
import com.zanxiang.mybatis.entity.PayApplication;
|
|
import com.zanxiang.mybatis.entity.PayApplication;
|
|
import com.zanxiang.mybatis.entity.PayMerchant;
|
|
import com.zanxiang.mybatis.entity.PayMerchant;
|
|
import com.zanxiang.mybatis.mapper.PayApplicationMapper;
|
|
import com.zanxiang.mybatis.mapper.PayApplicationMapper;
|
|
import com.zanxiang.sdk.constant.WxPayConstants;
|
|
import com.zanxiang.sdk.constant.WxPayConstants;
|
|
|
|
+import com.zanxiang.sdk.domain.dto.PayApplicationDTO;
|
|
import com.zanxiang.sdk.domain.params.MiniAppPayParam;
|
|
import com.zanxiang.sdk.domain.params.MiniAppPayParam;
|
|
import com.zanxiang.sdk.service.PayApplicationService;
|
|
import com.zanxiang.sdk.service.PayApplicationService;
|
|
import com.zanxiang.sdk.service.PayMerchantService;
|
|
import com.zanxiang.sdk.service.PayMerchantService;
|
|
@@ -47,17 +49,24 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
@Autowired
|
|
@Autowired
|
|
private PayMerchantService payMerchantService;
|
|
private PayMerchantService payMerchantService;
|
|
|
|
|
|
-
|
|
|
|
private static final String WX_PAY_JSAPI = "JSAPI";
|
|
private static final String WX_PAY_JSAPI = "JSAPI";
|
|
|
|
|
|
private static final String SIGN_TYPE = "MD5";
|
|
private static final String SIGN_TYPE = "MD5";
|
|
|
|
|
|
- /**
|
|
|
|
- * 微信回调接口
|
|
|
|
- */
|
|
|
|
@Value("${payConfig.wx.returnUrl}")
|
|
@Value("${payConfig.wx.returnUrl}")
|
|
private String notifyUrl;
|
|
private String notifyUrl;
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public PayApplicationDTO getPayApplication(String appId) {
|
|
|
|
+ PayApplication payApplication = super.getOne(new LambdaQueryWrapper<PayApplication>()
|
|
|
|
+ .eq(PayApplication::getAppId, appId));
|
|
|
|
+ if (payApplication == null) {
|
|
|
|
+ log.error("参数错误, 支付商城小程序信息不存在, appId : {}", appId);
|
|
|
|
+ throw new BaseException("参数错误, 支付商城小程序信息不存在");
|
|
|
|
+ }
|
|
|
|
+ return BeanUtil.copy(payApplication, PayApplicationDTO.class);
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public ResultMap miniAppPay(MiniAppPayParam payParam, HttpServletRequest request) {
|
|
public ResultMap miniAppPay(MiniAppPayParam payParam, HttpServletRequest request) {
|
|
//查询商城小程序信息
|
|
//查询商城小程序信息
|
|
@@ -67,7 +76,14 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
throw new BaseException("参数错误, 支付商城小程序信息不存在");
|
|
throw new BaseException("参数错误, 支付商城小程序信息不存在");
|
|
}
|
|
}
|
|
//查询商户信息
|
|
//查询商户信息
|
|
- PayMerchant payMerchant = payMerchantService.getById(4L);
|
|
|
|
|
|
+ PayMerchant payMerchant = payMerchantService.getById(3L);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
Map<String, String> payConfigMap = JsonUtil.toMap(payMerchant.getPayConfig(), Map.class, String.class);
|
|
Map<String, String> payConfigMap = JsonUtil.toMap(payMerchant.getPayConfig(), Map.class, String.class);
|
|
//获取用户openId
|
|
//获取用户openId
|
|
String openId = wxApiService.getAppletOpenId(payParam.getCode(), payApplication.getAppId(), payApplication.getAppSecret());
|
|
String openId = wxApiService.getAppletOpenId(payParam.getCode(), payApplication.getAppId(), payApplication.getAppSecret());
|
|
@@ -84,7 +100,7 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
paramMap.put("ip", IpUtil.getRealIp(request));
|
|
paramMap.put("ip", IpUtil.getRealIp(request));
|
|
paramMap.put("openId", openId);
|
|
paramMap.put("openId", openId);
|
|
paramMap.put("apiKey", payConfigMap.get("apiKey"));
|
|
paramMap.put("apiKey", payConfigMap.get("apiKey"));
|
|
- Map<String, String> attachMap = new HashMap<>(2);
|
|
|
|
|
|
+ Map<String, String> attachMap = new HashMap<>(3);
|
|
attachMap.put("appId", payApplication.getAppId());
|
|
attachMap.put("appId", payApplication.getAppId());
|
|
attachMap.put("orderId", orderId);
|
|
attachMap.put("orderId", orderId);
|
|
attachMap.put("merchantNo", payMerchant.getMerchantNo());
|
|
attachMap.put("merchantNo", payMerchant.getMerchantNo());
|
|
@@ -92,37 +108,44 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
log.error("下单参数, paramMap : {}", JsonUtil.toString(paramMap));
|
|
log.error("下单参数, paramMap : {}", JsonUtil.toString(paramMap));
|
|
//下单
|
|
//下单
|
|
Map<String, String> successMap = this.unifiedOrder(paramMap);
|
|
Map<String, String> successMap = this.unifiedOrder(paramMap);
|
|
- //支付id
|
|
|
|
- String prepayId = successMap.get("prepay_id");
|
|
|
|
- // 时间戳转化成字符串,否则前端wx.requestPayment方法会报签名错误
|
|
|
|
- String createTime = String.valueOf(System.currentTimeMillis() / 1000);
|
|
|
|
- // 随机字符串
|
|
|
|
- String nonceStr = WxPayUtil.generateNonceStr();
|
|
|
|
- // 获取统一下单回调结果, 二次签名
|
|
|
|
- HashMap<String, String> map = new HashMap<>(5);
|
|
|
|
- map.put("appId", payApplication.getAppId());
|
|
|
|
- map.put("nonceStr", nonceStr);
|
|
|
|
- map.put("timeStamp", createTime);
|
|
|
|
- map.put("package", "prepay_id=" + prepayId);
|
|
|
|
- map.put("signType", SIGN_TYPE);
|
|
|
|
- // 再次签名sign,这个签名用于小程序端调用支付方法
|
|
|
|
- String sign = WxPayUtil.generateSignature(map, payConfigMap.get("apiKey"));
|
|
|
|
- //组装返回前端支付参数
|
|
|
|
- Map<Object, Object> miniMap = new HashMap<>(5);
|
|
|
|
- miniMap.put("paySign", sign);
|
|
|
|
- miniMap.put("signType", SIGN_TYPE);
|
|
|
|
- miniMap.put("timeStamp", createTime);
|
|
|
|
- miniMap.put("package", successMap);
|
|
|
|
- miniMap.put("nonceStr", nonceStr);
|
|
|
|
- miniMap.put("orderId", orderId);
|
|
|
|
|
|
+ //构造支付参数
|
|
|
|
+ Map<Object, Object> payParamMap = this.payParamMap(orderId, payApplication.getAppId(), payConfigMap.get("apiKey"), successMap);
|
|
//返回
|
|
//返回
|
|
- return ResultMap.ok(miniMap);
|
|
|
|
|
|
+ return ResultMap.ok(payParamMap);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("微信小程序支付通信异常, 订单号:{}, e : {}", orderId, e.getMessage());
|
|
log.error("微信小程序支付通信异常, 订单号:{}, e : {}", orderId, e.getMessage());
|
|
return ResultMap.error();
|
|
return ResultMap.error();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private Map<Object, Object> payParamMap(String orderId, String appId, String apiKey, Map<String, String> successMap) {
|
|
|
|
+ //支付id
|
|
|
|
+ String prepayId = successMap.get("prepay_id");
|
|
|
|
+ // 时间戳转化成字符串,否则前端wx.requestPayment方法会报签名错误
|
|
|
|
+ String createTime = String.valueOf(System.currentTimeMillis() / 1000);
|
|
|
|
+ // 随机字符串
|
|
|
|
+ String nonceStr = WxPayUtil.generateNonceStr();
|
|
|
|
+ // 获取统一下单回调结果, 二次签名
|
|
|
|
+ HashMap<String, String> map = new HashMap<>(5);
|
|
|
|
+ map.put("appId", appId);
|
|
|
|
+ map.put("nonceStr", nonceStr);
|
|
|
|
+ map.put("timeStamp", createTime);
|
|
|
|
+ map.put("package", "prepay_id=" + prepayId);
|
|
|
|
+ map.put("signType", SIGN_TYPE);
|
|
|
|
+ // 再次签名sign,这个签名用于小程序端调用支付方法
|
|
|
|
+ String sign = WxPayUtil.generateSignature(map, apiKey);
|
|
|
|
+ //组装返回前端支付参数
|
|
|
|
+ Map<Object, Object> payParamMap = new HashMap<>(5);
|
|
|
|
+ payParamMap.put("paySign", sign);
|
|
|
|
+ payParamMap.put("signType", SIGN_TYPE);
|
|
|
|
+ payParamMap.put("timeStamp", createTime);
|
|
|
|
+ payParamMap.put("package", successMap);
|
|
|
|
+ payParamMap.put("nonceStr", nonceStr);
|
|
|
|
+ payParamMap.put("orderId", orderId);
|
|
|
|
+ //返回数据
|
|
|
|
+ return payParamMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
private String getOrderNum(String openId) {
|
|
private String getOrderNum(String openId) {
|
|
//时间(精确到毫秒)
|
|
//时间(精确到毫秒)
|
|
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
|
|
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
|