Open sunyantao opened 1 month ago
集成微信支付时,配置多商户号,默认key是商户号。 已知商户号是可以绑定多个公众号/小程序的,那么配置过程中的key就是同一个,此种方式行不通。
如果配置key是appid时,支付通知等代码默认用的商户号key获取的配置信息,会出现错误!
以下是配置(通过appid作为key的方式): pay: configs:
Bean配置: @Bean public WxPayService wxService() { List configs = this.properties.getConfigs(); if (configs == null) { throw new WxRuntimeException("配置错误"); } String appId = configs.get(0).getAppId(); WxPayService wxService = new WxPayServiceImpl(); wxService.setMultiConfig( configs.stream() .map(a -> { WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(StringUtils.trimToNull(a.getAppId())); payConfig.setMchId(StringUtils.trimToNull(a.getMchId())); payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey())); payConfig.setSubAppId(StringUtils.trimToNull(a.getSubAppId())); payConfig.setSubMchId(StringUtils.trimToNull(a.getSubMchId())); payConfig.setKeyPath(StringUtils.trimToNull(a.getKeyPath())); payConfig.setNotifyUrl(StringUtils.trimToNull(a.getNotifyUrl())); // 可以指定是否使用沙箱环境 payConfig.setUseSandboxEnv(false); return payConfig; }).collect(Collectors.toMap(WxPayConfig::getAppId, a -> a, (o, n) -> o)),appId); return wxService; }
源码中处理通知: @Override public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException { try { log.debug("微信支付异步通知请求参数:{}", xmlData); WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData); if (signType == null) { if (result.getSignType() != null) { // 如果解析的通知对象中signType有值,则使用它进行验签 signType = result.getSignType(); ### } else if (configMap.get(result.getMchId()).getSignType() != null) {//此部分会报错 // 如果配置中signType有值,则使用它进行验签 signType = configMap.get(result.getMchId()).getSignType(); this.switchover(result.getMchId()); } }
log.debug("微信支付异步通知请求解析后的对象:{}", result); result.checkResult(this, signType, false); return result; } catch (WxPayException e) { throw e; } catch (Exception e) { throw new WxPayException("发生异常!", e); }
}
集成微信支付时,配置多商户号,默认key是商户号。 已知商户号是可以绑定多个公众号/小程序的,那么配置过程中的key就是同一个,此种方式行不通。
如果配置key是appid时,支付通知等代码默认用的商户号key获取的配置信息,会出现错误!
以下是配置(通过appid作为key的方式): pay: configs:
Bean配置: configs = this.properties.getConfigs();
if (configs == null) {
throw new WxRuntimeException("配置错误");
}
String appId = configs.get(0).getAppId();
WxPayService wxService = new WxPayServiceImpl();
wxService.setMultiConfig(
configs.stream()
.map(a -> {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(StringUtils.trimToNull(a.getAppId()));
payConfig.setMchId(StringUtils.trimToNull(a.getMchId()));
payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey()));
payConfig.setSubAppId(StringUtils.trimToNull(a.getSubAppId()));
payConfig.setSubMchId(StringUtils.trimToNull(a.getSubMchId()));
payConfig.setKeyPath(StringUtils.trimToNull(a.getKeyPath()));
payConfig.setNotifyUrl(StringUtils.trimToNull(a.getNotifyUrl()));
// 可以指定是否使用沙箱环境
payConfig.setUseSandboxEnv(false);
return payConfig;
}).collect(Collectors.toMap(WxPayConfig::getAppId, a -> a, (o, n) -> o)),appId);
return wxService;
}
@Bean public WxPayService wxService() { List
源码中处理通知: @Override public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException { try { log.debug("微信支付异步通知请求参数:{}", xmlData); WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData); if (signType == null) { if (result.getSignType() != null) { // 如果解析的通知对象中signType有值,则使用它进行验签 signType = result.getSignType(); ### } else if (configMap.get(result.getMchId()).getSignType() != null) {//此部分会报错 // 如果配置中signType有值,则使用它进行验签 signType = configMap.get(result.getMchId()).getSignType(); this.switchover(result.getMchId()); } }
}