|
13 | 13 | import com.github.binarywang.wxpay.bean.request.*;
|
14 | 14 | import com.github.binarywang.wxpay.bean.result.*;
|
15 | 15 | import com.github.binarywang.wxpay.config.WxPayConfig;
|
| 16 | +import com.github.binarywang.wxpay.config.WxPayConfigHolder; |
16 | 17 | import com.github.binarywang.wxpay.constant.WxPayConstants;
|
17 | 18 | import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
|
18 | 19 | import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
|
|
21 | 22 | import com.github.binarywang.wxpay.util.SignUtils;
|
22 | 23 | import com.github.binarywang.wxpay.util.XmlConfig;
|
23 | 24 | import com.google.common.base.Joiner;
|
| 25 | +import com.google.common.collect.ImmutableMap; |
24 | 26 | import com.google.common.collect.Maps;
|
25 | 27 | import jodd.io.ZipUtil;
|
26 | 28 | import me.chanjar.weixin.common.error.WxRuntimeException;
|
@@ -65,7 +67,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
65 | 67 | private final MarketingMediaService marketingMediaService = new MarketingMediaServiceImpl(this);
|
66 | 68 | private final MarketingFavorService marketingFavorService = new MarketingFavorServiceImpl(this);
|
67 | 69 |
|
68 |
| - protected WxPayConfig config; |
| 70 | + protected Map<String, WxPayConfig> configMap; |
69 | 71 |
|
70 | 72 | @Override
|
71 | 73 | public EntPayService getEntPayService() {
|
@@ -119,12 +121,78 @@ public void setEntPayService(EntPayService entPayService) {
|
119 | 121 |
|
120 | 122 | @Override
|
121 | 123 | public WxPayConfig getConfig() {
|
122 |
| - return this.config; |
| 124 | + if (this.configMap.size() == 1) { |
| 125 | + // 只有一个商户号,直接返回其配置即可 |
| 126 | + return this.configMap.values().iterator().next(); |
| 127 | + } |
| 128 | + return this.configMap.get(WxPayConfigHolder.get()); |
123 | 129 | }
|
124 | 130 |
|
125 | 131 | @Override
|
126 | 132 | public void setConfig(WxPayConfig config) {
|
127 |
| - this.config = config; |
| 133 | + final String defaultMchId = config.getMchId(); |
| 134 | + this.setMultiConfig(ImmutableMap.of(defaultMchId, config), defaultMchId); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void addConfig(String mchId, WxPayConfig wxPayConfig) { |
| 139 | + synchronized (this) { |
| 140 | + if (this.configMap == null) { |
| 141 | + this.setConfig(wxPayConfig); |
| 142 | + } else { |
| 143 | + WxPayConfigHolder.set(mchId); |
| 144 | + this.configMap.put(mchId, wxPayConfig); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void removeConfig(String mchId) { |
| 151 | + synchronized (this) { |
| 152 | + if (this.configMap.size() == 1) { |
| 153 | + this.configMap.remove(mchId); |
| 154 | + log.warn("已删除最后一个商户号配置:{},须立即使用setConfig或setMultiConfig添加配置", mchId); |
| 155 | + return; |
| 156 | + } |
| 157 | + if (WxPayConfigHolder.get().equals(mchId)) { |
| 158 | + this.configMap.remove(mchId); |
| 159 | + final String defaultMpId = this.configMap.keySet().iterator().next(); |
| 160 | + WxPayConfigHolder.set(defaultMpId); |
| 161 | + log.warn("已删除默认商户号配置,商户号【{}】被设为默认配置", defaultMpId); |
| 162 | + return; |
| 163 | + } |
| 164 | + this.configMap.remove(mchId); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void setMultiConfig(Map<String, WxPayConfig> wxPayConfigs) { |
| 170 | + this.setMultiConfig(wxPayConfigs, wxPayConfigs.keySet().iterator().next()); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void setMultiConfig(Map<String, WxPayConfig> wxPayConfigs, String defaultMchId) { |
| 175 | + this.configMap = Maps.newHashMap(wxPayConfigs); |
| 176 | + WxPayConfigHolder.set(defaultMchId); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public boolean switchover(String mchId) { |
| 181 | + if (this.configMap.containsKey(mchId)) { |
| 182 | + WxPayConfigHolder.set(mchId); |
| 183 | + return true; |
| 184 | + } |
| 185 | + log.error("无法找到对应【{}】的商户号配置信息,请核实!", mchId); |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public WxPayService switchoverTo(String mchId) { |
| 191 | + if (this.configMap.containsKey(mchId)) { |
| 192 | + WxPayConfigHolder.set(mchId); |
| 193 | + return this; |
| 194 | + } |
| 195 | + throw new WxRuntimeException(String.format("无法找到对应【%s】的商户号配置信息,请核实!", mchId)); |
128 | 196 | }
|
129 | 197 |
|
130 | 198 | @Override
|
|
0 commit comments