实现支持一个商户号对应多个appId的功能
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
This commit is contained in:
@@ -78,6 +78,15 @@ public interface WxPayService {
|
||||
*/
|
||||
boolean switchover(String mchId, String appId);
|
||||
|
||||
/**
|
||||
* 仅根据商户号进行切换.
|
||||
* 适用于一个商户号对应多个appId的场景,切换时会匹配第一个符合该商户号的配置.
|
||||
*
|
||||
* @param mchId 商户标识
|
||||
* @return 切换是否成功 boolean
|
||||
*/
|
||||
boolean switchover(String mchId);
|
||||
|
||||
/**
|
||||
* 进行相应的商户切换.
|
||||
*
|
||||
@@ -87,6 +96,15 @@ public interface WxPayService {
|
||||
*/
|
||||
WxPayService switchoverTo(String mchId, String appId);
|
||||
|
||||
/**
|
||||
* 仅根据商户号进行切换.
|
||||
* 适用于一个商户号对应多个appId的场景,切换时会匹配第一个符合该商户号的配置.
|
||||
*
|
||||
* @param mchId 商户标识
|
||||
* @return 切换成功 ,则返回当前对象,方便链式调用,否则抛出异常
|
||||
*/
|
||||
WxPayService switchoverTo(String mchId);
|
||||
|
||||
/**
|
||||
* 发送post请求,得到响应字节数组.
|
||||
*
|
||||
|
||||
@@ -212,6 +212,28 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean switchover(String mchId) {
|
||||
// 先尝试精确匹配(针对只有mchId没有appId的配置)
|
||||
if (this.configMap.containsKey(mchId)) {
|
||||
WxPayConfigHolder.set(mchId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 尝试前缀匹配(查找以 mchId_ 开头的配置)
|
||||
String prefix = mchId + "_";
|
||||
for (String key : this.configMap.keySet()) {
|
||||
if (key.startsWith(prefix)) {
|
||||
WxPayConfigHolder.set(key);
|
||||
log.debug("根据mchId=【{}】找到配置key=【{}】", mchId, key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
log.error("无法找到对应mchId=【{}】的商户号配置信息,请核实!", mchId);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayService switchoverTo(String mchId, String appId) {
|
||||
String configKey = this.getConfigKey(mchId, appId);
|
||||
@@ -222,6 +244,27 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
||||
throw new WxRuntimeException(String.format("无法找到对应mchId=【%s】,appId=【%s】的商户号配置信息,请核实!", mchId, appId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayService switchoverTo(String mchId) {
|
||||
// 先尝试精确匹配(针对只有mchId没有appId的配置)
|
||||
if (this.configMap.containsKey(mchId)) {
|
||||
WxPayConfigHolder.set(mchId);
|
||||
return this;
|
||||
}
|
||||
|
||||
// 尝试前缀匹配(查找以 mchId_ 开头的配置)
|
||||
String prefix = mchId + "_";
|
||||
for (String key : this.configMap.keySet()) {
|
||||
if (key.startsWith(prefix)) {
|
||||
WxPayConfigHolder.set(key);
|
||||
log.debug("根据mchId=【{}】找到配置key=【{}】", mchId, key);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
throw new WxRuntimeException(String.format("无法找到对应mchId=【%s】的商户号配置信息,请核实!", mchId));
|
||||
}
|
||||
|
||||
public String getConfigKey(String mchId, String appId) {
|
||||
return mchId + "_" + appId;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 手动验证多appId切换功能
|
||||
*/
|
||||
public class MultiAppIdSwitchoverManualTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
WxPayService payService = new WxPayServiceImpl();
|
||||
|
||||
String testMchId = "1234567890";
|
||||
String testAppId1 = "wx1111111111111111";
|
||||
String testAppId2 = "wx2222222222222222";
|
||||
String testAppId3 = "wx3333333333333333";
|
||||
|
||||
// 配置同一个商户号,三个不同的appId
|
||||
WxPayConfig config1 = new WxPayConfig();
|
||||
config1.setMchId(testMchId);
|
||||
config1.setAppId(testAppId1);
|
||||
config1.setMchKey("test_key_1");
|
||||
|
||||
WxPayConfig config2 = new WxPayConfig();
|
||||
config2.setMchId(testMchId);
|
||||
config2.setAppId(testAppId2);
|
||||
config2.setMchKey("test_key_2");
|
||||
|
||||
WxPayConfig config3 = new WxPayConfig();
|
||||
config3.setMchId(testMchId);
|
||||
config3.setAppId(testAppId3);
|
||||
config3.setMchKey("test_key_3");
|
||||
|
||||
Map<String, WxPayConfig> configMap = new HashMap<>();
|
||||
configMap.put(testMchId + "_" + testAppId1, config1);
|
||||
configMap.put(testMchId + "_" + testAppId2, config2);
|
||||
configMap.put(testMchId + "_" + testAppId3, config3);
|
||||
|
||||
payService.setMultiConfig(configMap);
|
||||
|
||||
// 测试1: 使用 mchId + appId 精确切换
|
||||
System.out.println("=== 测试1: 使用 mchId + appId 精确切换 ===");
|
||||
boolean success = payService.switchover(testMchId, testAppId1);
|
||||
System.out.println("切换结果: " + success);
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert success : "切换应该成功";
|
||||
assert testAppId1.equals(payService.getConfig().getAppId()) : "AppId应该是 " + testAppId1;
|
||||
System.out.println("✓ 测试1通过\n");
|
||||
|
||||
// 测试2: 仅使用 mchId 切换
|
||||
System.out.println("=== 测试2: 仅使用 mchId 切换 ===");
|
||||
success = payService.switchover(testMchId);
|
||||
System.out.println("切换结果: " + success);
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert success : "仅使用mchId切换应该成功";
|
||||
assert testMchId.equals(payService.getConfig().getMchId()) : "MchId应该是 " + testMchId;
|
||||
System.out.println("✓ 测试2通过\n");
|
||||
|
||||
// 测试3: 使用 switchoverTo 链式调用(精确匹配)
|
||||
System.out.println("=== 测试3: 使用 switchoverTo 链式调用(精确匹配) ===");
|
||||
WxPayService result = payService.switchoverTo(testMchId, testAppId2);
|
||||
System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例"));
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert result == payService : "应该返回同一实例";
|
||||
assert testAppId2.equals(payService.getConfig().getAppId()) : "AppId应该是 " + testAppId2;
|
||||
System.out.println("✓ 测试3通过\n");
|
||||
|
||||
// 测试4: 使用 switchoverTo 链式调用(仅mchId)
|
||||
System.out.println("=== 测试4: 使用 switchoverTo 链式调用(仅mchId) ===");
|
||||
result = payService.switchoverTo(testMchId);
|
||||
System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例"));
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert result == payService : "应该返回同一实例";
|
||||
assert testMchId.equals(payService.getConfig().getMchId()) : "MchId应该是 " + testMchId;
|
||||
System.out.println("✓ 测试4通过\n");
|
||||
|
||||
// 测试5: 切换到不存在的商户号
|
||||
System.out.println("=== 测试5: 切换到不存在的商户号 ===");
|
||||
success = payService.switchover("nonexistent_mch_id");
|
||||
System.out.println("切换结果: " + success);
|
||||
assert !success : "切换到不存在的商户号应该失败";
|
||||
System.out.println("✓ 测试5通过\n");
|
||||
|
||||
// 测试6: 切换到不存在的 appId
|
||||
System.out.println("=== 测试6: 切换到不存在的 appId ===");
|
||||
success = payService.switchover(testMchId, "wx9999999999999999");
|
||||
System.out.println("切换结果: " + success);
|
||||
assert !success : "切换到不存在的appId应该失败";
|
||||
System.out.println("✓ 测试6通过\n");
|
||||
|
||||
// 测试7: 添加新配置后切换
|
||||
System.out.println("=== 测试7: 添加新配置后切换 ===");
|
||||
String newAppId = "wx4444444444444444";
|
||||
WxPayConfig newConfig = new WxPayConfig();
|
||||
newConfig.setMchId(testMchId);
|
||||
newConfig.setAppId(newAppId);
|
||||
newConfig.setMchKey("test_key_4");
|
||||
payService.addConfig(testMchId, newAppId, newConfig);
|
||||
|
||||
success = payService.switchover(testMchId, newAppId);
|
||||
System.out.println("切换结果: " + success);
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert success : "切换到新添加的配置应该成功";
|
||||
assert newAppId.equals(payService.getConfig().getAppId()) : "AppId应该是 " + newAppId;
|
||||
System.out.println("✓ 测试7通过\n");
|
||||
|
||||
System.out.println("==================");
|
||||
System.out.println("所有测试通过! ✓");
|
||||
System.out.println("==================");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* 测试一个商户号配置多个appId的场景
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
public class MultiAppIdSwitchoverTest {
|
||||
|
||||
private WxPayService payService;
|
||||
private String testMchId = "1234567890";
|
||||
private String testAppId1 = "wx1111111111111111";
|
||||
private String testAppId2 = "wx2222222222222222";
|
||||
private String testAppId3 = "wx3333333333333333";
|
||||
|
||||
@BeforeMethod
|
||||
public void setup() {
|
||||
payService = new WxPayServiceImpl();
|
||||
|
||||
// 配置同一个商户号,三个不同的appId
|
||||
WxPayConfig config1 = new WxPayConfig();
|
||||
config1.setMchId(testMchId);
|
||||
config1.setAppId(testAppId1);
|
||||
config1.setMchKey("test_key_1");
|
||||
|
||||
WxPayConfig config2 = new WxPayConfig();
|
||||
config2.setMchId(testMchId);
|
||||
config2.setAppId(testAppId2);
|
||||
config2.setMchKey("test_key_2");
|
||||
|
||||
WxPayConfig config3 = new WxPayConfig();
|
||||
config3.setMchId(testMchId);
|
||||
config3.setAppId(testAppId3);
|
||||
config3.setMchKey("test_key_3");
|
||||
|
||||
Map<String, WxPayConfig> configMap = new HashMap<>();
|
||||
configMap.put(testMchId + "_" + testAppId1, config1);
|
||||
configMap.put(testMchId + "_" + testAppId2, config2);
|
||||
configMap.put(testMchId + "_" + testAppId3, config3);
|
||||
|
||||
payService.setMultiConfig(configMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试使用 mchId + appId 精确切换(原有功能,确保向后兼容)
|
||||
*/
|
||||
@Test
|
||||
public void testSwitchoverWithMchIdAndAppId() {
|
||||
// 切换到第一个配置
|
||||
boolean success = payService.switchover(testMchId, testAppId1);
|
||||
assertTrue(success);
|
||||
assertEquals(payService.getConfig().getAppId(), testAppId1);
|
||||
assertEquals(payService.getConfig().getMchKey(), "test_key_1");
|
||||
|
||||
// 切换到第二个配置
|
||||
success = payService.switchover(testMchId, testAppId2);
|
||||
assertTrue(success);
|
||||
assertEquals(payService.getConfig().getAppId(), testAppId2);
|
||||
assertEquals(payService.getConfig().getMchKey(), "test_key_2");
|
||||
|
||||
// 切换到第三个配置
|
||||
success = payService.switchover(testMchId, testAppId3);
|
||||
assertTrue(success);
|
||||
assertEquals(payService.getConfig().getAppId(), testAppId3);
|
||||
assertEquals(payService.getConfig().getMchKey(), "test_key_3");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试仅使用 mchId 切换(新功能)
|
||||
* 应该能够成功切换到该商户号的某个配置
|
||||
*/
|
||||
@Test
|
||||
public void testSwitchoverWithMchIdOnly() {
|
||||
// 仅使用商户号切换,应该能够成功切换到该商户号的某个配置
|
||||
boolean success = payService.switchover(testMchId);
|
||||
assertTrue(success, "应该能够通过mchId切换配置");
|
||||
|
||||
// 验证配置确实是该商户号的配置之一
|
||||
WxPayConfig currentConfig = payService.getConfig();
|
||||
assertNotNull(currentConfig);
|
||||
assertEquals(currentConfig.getMchId(), testMchId);
|
||||
|
||||
// appId应该是三个中的一个
|
||||
String currentAppId = currentConfig.getAppId();
|
||||
assertTrue(
|
||||
testAppId1.equals(currentAppId) || testAppId2.equals(currentAppId) || testAppId3.equals(currentAppId),
|
||||
"当前appId应该是配置的appId之一"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 switchoverTo 方法(带链式调用,使用 mchId + appId)
|
||||
*/
|
||||
@Test
|
||||
public void testSwitchoverToWithMchIdAndAppId() {
|
||||
WxPayService result = payService.switchoverTo(testMchId, testAppId2);
|
||||
assertNotNull(result);
|
||||
assertEquals(result, payService, "switchoverTo应该返回当前服务实例,支持链式调用");
|
||||
assertEquals(payService.getConfig().getAppId(), testAppId2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 switchoverTo 方法(带链式调用,仅使用 mchId)
|
||||
*/
|
||||
@Test
|
||||
public void testSwitchoverToWithMchIdOnly() {
|
||||
WxPayService result = payService.switchoverTo(testMchId);
|
||||
assertNotNull(result);
|
||||
assertEquals(result, payService, "switchoverTo应该返回当前服务实例,支持链式调用");
|
||||
assertEquals(payService.getConfig().getMchId(), testMchId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试切换到不存在的商户号
|
||||
*/
|
||||
@Test
|
||||
public void testSwitchoverToNonexistentMchId() {
|
||||
boolean success = payService.switchover("nonexistent_mch_id");
|
||||
assertFalse(success, "切换到不存在的商户号应该失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 switchoverTo 切换到不存在的商户号(应该抛出异常)
|
||||
*/
|
||||
@Test(expectedExceptions = WxRuntimeException.class)
|
||||
public void testSwitchoverToNonexistentMchIdThrowsException() {
|
||||
payService.switchoverTo("nonexistent_mch_id");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试切换到不存在的 mchId + appId 组合
|
||||
*/
|
||||
@Test
|
||||
public void testSwitchoverToNonexistentAppId() {
|
||||
boolean success = payService.switchover(testMchId, "wx9999999999999999");
|
||||
assertFalse(success, "切换到不存在的appId应该失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试添加配置后能够正常切换
|
||||
*/
|
||||
@Test
|
||||
public void testAddConfigAndSwitchover() {
|
||||
String newAppId = "wx4444444444444444";
|
||||
|
||||
// 动态添加一个新的配置
|
||||
WxPayConfig newConfig = new WxPayConfig();
|
||||
newConfig.setMchId(testMchId);
|
||||
newConfig.setAppId(newAppId);
|
||||
newConfig.setMchKey("test_key_4");
|
||||
|
||||
payService.addConfig(testMchId, newAppId, newConfig);
|
||||
|
||||
// 切换到新添加的配置
|
||||
boolean success = payService.switchover(testMchId, newAppId);
|
||||
assertTrue(success);
|
||||
assertEquals(payService.getConfig().getAppId(), newAppId);
|
||||
assertEquals(payService.getConfig().getMchKey(), "test_key_4");
|
||||
|
||||
// 使用仅mchId切换也应该能够找到配置
|
||||
success = payService.switchover(testMchId);
|
||||
assertTrue(success);
|
||||
assertEquals(payService.getConfig().getMchId(), testMchId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试移除配置后切换
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveConfigAndSwitchover() {
|
||||
// 移除一个配置
|
||||
payService.removeConfig(testMchId, testAppId1);
|
||||
|
||||
// 切换到已移除的配置应该失败
|
||||
boolean success = payService.switchover(testMchId, testAppId1);
|
||||
assertFalse(success);
|
||||
|
||||
// 但仍然能够切换到其他配置
|
||||
success = payService.switchover(testMchId, testAppId2);
|
||||
assertTrue(success);
|
||||
|
||||
// 使用仅mchId切换应该仍然有效(因为还有其他appId的配置)
|
||||
success = payService.switchover(testMchId);
|
||||
assertTrue(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试单个配置的场景(确保向后兼容)
|
||||
*/
|
||||
@Test
|
||||
public void testSingleConfig() {
|
||||
WxPayService singlePayService = new WxPayServiceImpl();
|
||||
WxPayConfig singleConfig = new WxPayConfig();
|
||||
singleConfig.setMchId("single_mch_id");
|
||||
singleConfig.setAppId("single_app_id");
|
||||
singleConfig.setMchKey("single_key");
|
||||
|
||||
singlePayService.setConfig(singleConfig);
|
||||
|
||||
// 直接获取配置应该成功
|
||||
assertEquals(singlePayService.getConfig().getMchId(), "single_mch_id");
|
||||
assertEquals(singlePayService.getConfig().getAppId(), "single_app_id");
|
||||
|
||||
// 使用精确匹配切换
|
||||
boolean success = singlePayService.switchover("single_mch_id", "single_app_id");
|
||||
assertTrue(success);
|
||||
|
||||
// 使用仅mchId切换
|
||||
success = singlePayService.switchover("single_mch_id");
|
||||
assertTrue(success);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user