|
@@ -5,7 +5,8 @@ import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
import com.github.binarywang.wxpay.v3.auth.Verifier;
|
|
import com.github.binarywang.wxpay.v3.auth.Verifier;
|
|
|
import com.huifu.adapay.Adapay;
|
|
import com.huifu.adapay.Adapay;
|
|
|
import com.huifu.adapay.core.AdapayCore;
|
|
import com.huifu.adapay.core.AdapayCore;
|
|
|
-import com.huifu.adapay.core.util.AdapaySign;
|
|
|
|
|
|
|
+import com.sun.net.httpserver.HttpExchange;
|
|
|
|
|
+import com.sun.net.httpserver.HttpServer;
|
|
|
import com.huifu.adapay.model.MerConfig;
|
|
import com.huifu.adapay.model.MerConfig;
|
|
|
import com.microsvc.toolkit.middleware.payment.common.api.*;
|
|
import com.microsvc.toolkit.middleware.payment.common.api.*;
|
|
|
import com.microsvc.toolkit.middleware.payment.impl.WxPaymentServicePlugin;
|
|
import com.microsvc.toolkit.middleware.payment.impl.WxPaymentServicePlugin;
|
|
@@ -52,7 +53,10 @@ public class ReceiptBillGatewayTest {
|
|
|
private final HfMerchantConfigService huifu = mock(HfMerchantConfigService.class);
|
|
private final HfMerchantConfigService huifu = mock(HfMerchantConfigService.class);
|
|
|
private final StandardEnvironment environment = new StandardEnvironment();
|
|
private final StandardEnvironment environment = new StandardEnvironment();
|
|
|
private final Map<String,Object> supplements = new HashMap<>();
|
|
private final Map<String,Object> supplements = new HashMap<>();
|
|
|
- private final AdapayPayConfig ada = new AdapayPayConfig();
|
|
|
|
|
|
|
+ private final HfMerchantConfig stored = new HfMerchantConfig();
|
|
|
|
|
+ private HttpServer huifuServer;
|
|
|
|
|
+ private String originalApiBase;
|
|
|
|
|
+ private volatile Throwable handlerFailure;
|
|
|
private final AliPayConfig ali = new AliPayConfig();
|
|
private final AliPayConfig ali = new AliPayConfig();
|
|
|
private final PayConfigProperties.WxPayConfig wx = new PayConfigProperties.WxPayConfig();
|
|
private final PayConfigProperties.WxPayConfig wx = new PayConfigProperties.WxPayConfig();
|
|
|
private String mode = "ok", originalPlatformKey;
|
|
private String mode = "ok", originalPlatformKey;
|
|
@@ -77,16 +81,24 @@ public class ReceiptBillGatewayTest {
|
|
|
String key = Base64.getEncoder().encodeToString(merchant.getPrivate().getEncoded());
|
|
String key = Base64.getEncoder().encodeToString(merchant.getPrivate().getEncoded());
|
|
|
String pub = Base64.getEncoder().encodeToString(platform.getPublic().getEncoded());
|
|
String pub = Base64.getEncoder().encodeToString(platform.getPublic().getEncoded());
|
|
|
originalPlatformKey = AdapayCore.PUBLIC_KEY; AdapayCore.PUBLIC_KEY = pub;
|
|
originalPlatformKey = AdapayCore.PUBLIC_KEY; AdapayCore.PUBLIC_KEY = pub;
|
|
|
- ada.setKeyPrivate(key); ada.setMerchantId("offline-api"); ada.setAppId("ada");
|
|
|
|
|
- ada.setMerchantKey(MERCHANT_KEY);
|
|
|
|
|
|
|
+ originalApiBase = AdapayCore.apiBase;
|
|
|
|
|
+ huifuServer = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
+ huifuServer.createContext("/", this::handleHuifu);
|
|
|
|
|
+ huifuServer.start();
|
|
|
|
|
+ AdapayCore.apiBase = "http://127.0.0.1:" + huifuServer.getAddress().getPort();
|
|
|
|
|
+ stored.setTenantId(101);
|
|
|
|
|
+ stored.setMerKey(MERCHANT_KEY);
|
|
|
|
|
+ stored.setAppId("ada");
|
|
|
|
|
+ stored.setApiKey("offline-api");
|
|
|
|
|
+ stored.setRsaPrivateKey(key);
|
|
|
|
|
+ when(huifu.queryByTenantId(101, "ADAPAY")).thenReturn(stored);
|
|
|
originalAccount = Adapay.getConfig(MERCHANT_KEY);
|
|
originalAccount = Adapay.getConfig(MERCHANT_KEY);
|
|
|
MerConfig runtimeAccount = new MerConfig();
|
|
MerConfig runtimeAccount = new MerConfig();
|
|
|
- runtimeAccount.setApiKey(ada.getMerchantId());
|
|
|
|
|
|
|
+ runtimeAccount.setApiKey(stored.getApiKey());
|
|
|
runtimeAccount.setRSAPrivateKey(key);
|
|
runtimeAccount.setRSAPrivateKey(key);
|
|
|
Adapay.addMerConfig(runtimeAccount, MERCHANT_KEY);
|
|
Adapay.addMerConfig(runtimeAccount, MERCHANT_KEY);
|
|
|
ali.setPrivateKey(key); ali.setPublicKey(pub); ali.setAppId("ali-app"); ali.setEnable(false);
|
|
ali.setPrivateKey(key); ali.setPublicKey(pub); ali.setAppId("ali-app"); ali.setEnable(false);
|
|
|
wx.setMerchantId("10001"); wx.setEnable(false);
|
|
wx.setMerchantId("10001"); wx.setEnable(false);
|
|
|
- register("ADAPAY_101", ada, mock(BasePaymentService.class));
|
|
|
|
|
register("ALIPAY_101", ali, mock(BasePaymentService.class));
|
|
register("ALIPAY_101", ali, mock(BasePaymentService.class));
|
|
|
WxPaymentServicePlugin plugin = mock(WxPaymentServicePlugin.class);
|
|
WxPaymentServicePlugin plugin = mock(WxPaymentServicePlugin.class);
|
|
|
register("WXPAY_101", wx, plugin);
|
|
register("WXPAY_101", wx, plugin);
|
|
@@ -108,8 +120,11 @@ public class ReceiptBillGatewayTest {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@After public void restoreAccounts() throws Exception {
|
|
@After public void restoreAccounts() throws Exception {
|
|
|
|
|
+ if (huifuServer != null) huifuServer.stop(0);
|
|
|
|
|
+ AdapayCore.apiBase = originalApiBase;
|
|
|
AdapayCore.PUBLIC_KEY = originalPlatformKey;
|
|
AdapayCore.PUBLIC_KEY = originalPlatformKey;
|
|
|
Adapay.addMerConfig(originalAccount, MERCHANT_KEY);
|
|
Adapay.addMerConfig(originalAccount, MERCHANT_KEY);
|
|
|
|
|
+ if (handlerFailure != null) throw new AssertionError(handlerFailure);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private ReceiptBillDto fetch(String tenant, String channel, LocalDate day) throws Exception {
|
|
private ReceiptBillDto fetch(String tenant, String channel, LocalDate day) throws Exception {
|
|
@@ -152,23 +167,6 @@ public class ReceiptBillGatewayTest {
|
|
|
assertEquals("billdownload.cloud.alipay.com", uri.getHost());
|
|
assertEquals("billdownload.cloud.alipay.com", uri.getHost());
|
|
|
return new BillHttp.Reply(200, zip((mode.equals("foreignName") ? OTHER : PID) + "_业务明细.csv", mode.equals("foreignHeader") ? ALI_CSV.replace(PID, OTHER) : ALI_CSV, Charset.forName("GBK")));
|
|
return new BillHttp.Reply(200, zip((mode.equals("foreignName") ? OTHER : PID) + "_业务明细.csv", mode.equals("foreignHeader") ? ALI_CSV.replace(PID, OTHER) : ALI_CSV, Charset.forName("GBK")));
|
|
|
}
|
|
}
|
|
|
- if (uri.getHost().equals("api.adapay.tech")) {
|
|
|
|
|
- tickets++; assertEquals("20261119", JSON.readTree(body).path("bill_date").asText());
|
|
|
|
|
- assertEquals("/v1/bill/download", uri.getPath());
|
|
|
|
|
- assertEquals(uri.getPath(), headers.get("api_name"));
|
|
|
|
|
- assertEquals("offline-api", headers.get("Authorization"));
|
|
|
|
|
- verify("SHA1withRSA", "https://api.adapay.tech" + uri.getPath() + body, headers.get("Signature"));
|
|
|
|
|
- assertEquals(AdapaySign.sign(uri.toString() + body, Adapay.getConfig(MERCHANT_KEY).getRSAPrivateKey()), headers.get("Signature"));
|
|
|
|
|
- if (mode.equals("ticket401")) return new BillHttp.Reply(401, JSON.writeValueAsBytes(Collections.singletonMap("data",
|
|
|
|
|
- "{\"error_code\":\"security_exception\",\"error_msg\":\"private-file\"}")));
|
|
|
|
|
- if (mode.equals("empty401")) return new BillHttp.Reply(401, new byte[0]);
|
|
|
|
|
- if (mode.equals("plain401")) return new BillHttp.Reply(401, "Invalid API key".getBytes(UTF_8));
|
|
|
|
|
- String result = "{\"status\":\"succeeded\",\"prod_mode\":true,\"bill_download_url\":\"https://jarvisfile.oss-cn-shanghai.aliyuncs.com/test\"}";
|
|
|
|
|
- Map<String,String> envelope = new HashMap<>();
|
|
|
|
|
- envelope.put("data", result);
|
|
|
|
|
- envelope.put("signature", sign("SHA1withRSA", result));
|
|
|
|
|
- return new BillHttp.Reply(200, JSON.writeValueAsBytes(envelope));
|
|
|
|
|
- }
|
|
|
|
|
assertEquals("openapi.alipay.com", uri.getHost());
|
|
assertEquals("openapi.alipay.com", uri.getHost());
|
|
|
Map<String,String> fields = new TreeMap<>();
|
|
Map<String,String> fields = new TreeMap<>();
|
|
|
for (String pair : body.split("&")) { String[] split = pair.split("=", 2); fields.put(split[0], URLDecoder.decode(split[1], "UTF-8")); }
|
|
for (String pair : body.split("&")) { String[] split = pair.split("=", 2); fields.put(split[0], URLDecoder.decode(split[1], "UTF-8")); }
|
|
@@ -188,6 +186,47 @@ public class ReceiptBillGatewayTest {
|
|
|
return new BillHttp.Reply(200, envelope.getBytes(UTF_8));
|
|
return new BillHttp.Reply(200, envelope.getBytes(UTF_8));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void handleHuifu(HttpExchange exchange) throws IOException {
|
|
|
|
|
+ int status = 200;
|
|
|
|
|
+ byte[] response;
|
|
|
|
|
+ try {
|
|
|
|
|
+ tickets++;
|
|
|
|
|
+ requests++;
|
|
|
|
|
+ assertEquals("POST", exchange.getRequestMethod());
|
|
|
|
|
+ assertEquals("/v1/bill/download", exchange.getRequestURI().toString());
|
|
|
|
|
+ assertEquals("/v1/bill/download", exchange.getRequestHeaders().getFirst("API_Name"));
|
|
|
|
|
+ assertEquals("offline-api", exchange.getRequestHeaders().getFirst("Authorization"));
|
|
|
|
|
+ String body = new String(BillHttp.read(exchange.getRequestBody(), BillHttp.API_LIMIT), UTF_8);
|
|
|
|
|
+ assertEquals("20261119", JSON.readTree(body).path("bill_date").asText());
|
|
|
|
|
+ verify("SHA1withRSA", AdapayCore.apiBase + exchange.getRequestURI() + body,
|
|
|
|
|
+ exchange.getRequestHeaders().getFirst("Signature"));
|
|
|
|
|
+ String data = "{\"status\":\"succeeded\",\"prod_mode\":true,\"bill_download_url\":\"https://jarvisfile.oss-cn-shanghai.aliyuncs.com/test\"}";
|
|
|
|
|
+ if ("pending".equals(mode)) data = "{\"status\":\"pending\",\"prod_mode\":true}";
|
|
|
|
|
+ if ("ticket401".equals(mode)) {
|
|
|
|
|
+ status = 401;
|
|
|
|
|
+ data = "{\"status\":\"failed\",\"error_code\":\"security_exception\",\"error_msg\":\"private-file\"}";
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String,String> envelope = new HashMap<>();
|
|
|
|
|
+ envelope.put("data", data);
|
|
|
|
|
+ envelope.put("signature", sign("SHA1withRSA", "badTicketSignature".equals(mode) ? "changed" : data));
|
|
|
|
|
+ response = JSON.writeValueAsBytes(envelope);
|
|
|
|
|
+ if ("empty401".equals(mode) || "plain401".equals(mode)) {
|
|
|
|
|
+ status = 401;
|
|
|
|
|
+ response = "empty401".equals(mode) ? new byte[0] : "Invalid API key".getBytes(UTF_8);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Throwable failure) {
|
|
|
|
|
+ handlerFailure = failure;
|
|
|
|
|
+ status = 500;
|
|
|
|
|
+ response = new byte[0];
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ exchange.sendResponseHeaders(status, response.length);
|
|
|
|
|
+ exchange.getResponseBody().write(response);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ exchange.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private BillHttp.Reply wx(int status, String body) throws Exception {
|
|
private BillHttp.Reply wx(int status, String body) throws Exception {
|
|
|
String timestamp = Long.toString(Instant.now().getEpochSecond());
|
|
String timestamp = Long.toString(Instant.now().getEpochSecond());
|
|
|
BillHttp.Reply reply = new BillHttp.Reply(status, body.getBytes(UTF_8));
|
|
BillHttp.Reply reply = new BillHttp.Reply(status, body.getBytes(UTF_8));
|
|
@@ -228,20 +267,12 @@ public class ReceiptBillGatewayTest {
|
|
|
assertEquals(3, downloads); assertEquals(7, requests);
|
|
assertEquals(3, downloads); assertEquals(7, requests);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Test public void huifuUsesCurrentPaymentCredentialsAfterRotation() throws Exception {
|
|
|
|
|
- HfMerchantConfig updated = new HfMerchantConfig();
|
|
|
|
|
- updated.setTenantId(101);
|
|
|
|
|
- updated.setAppId(ada.getAppId());
|
|
|
|
|
- updated.setMerKey(MERCHANT_KEY);
|
|
|
|
|
- updated.setApiKey(ada.getMerchantId());
|
|
|
|
|
- updated.setRsaPrivateKey(ada.getKeyPrivate());
|
|
|
|
|
- when(huifu.queryByTenantId(101, "ADAPAY")).thenReturn(updated);
|
|
|
|
|
- ada.setMerchantId("previous-api");
|
|
|
|
|
- ada.setKeyPrivate(Base64.getEncoder().encodeToString(platform.getPrivate().getEncoded()));
|
|
|
|
|
|
|
+ @Test public void huifuUsesFinanceExportMerchantWithoutPaymentPlugin() throws Exception {
|
|
|
assertEquals("READY", fetch("101", "HUIFU", DAY).getStatus());
|
|
assertEquals("READY", fetch("101", "HUIFU", DAY).getStatus());
|
|
|
|
|
+ org.mockito.Mockito.verify(payments, never()).getPaymentService("ADAPAY_101");
|
|
|
assertEquals(1, tickets);
|
|
assertEquals(1, tickets);
|
|
|
|
|
|
|
|
- updated.setAppId("another-app");
|
|
|
|
|
|
|
+ stored.setApiKey("different-account");
|
|
|
ReceiptBillDto rejected = fetch("101", "HUIFU", DAY);
|
|
ReceiptBillDto rejected = fetch("101", "HUIFU", DAY);
|
|
|
assertEquals("ERROR", rejected.getStatus());
|
|
assertEquals("ERROR", rejected.getStatus());
|
|
|
assertTrue(rejected.getMessage().contains("与本机构商户记录不一致"));
|
|
assertTrue(rejected.getMessage().contains("与本机构商户记录不一致"));
|
|
@@ -249,7 +280,7 @@ public class ReceiptBillGatewayTest {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Test public void huifuDoesNotFallBackToStartupKeysForMissingSdkAccount() throws Exception {
|
|
@Test public void huifuDoesNotFallBackToStartupKeysForMissingSdkAccount() throws Exception {
|
|
|
- ada.setMerchantKey("receipt-test-unregistered");
|
|
|
|
|
|
|
+ stored.setMerKey("receipt-test-unregistered");
|
|
|
assertTrue(fetch("101", "HUIFU", DAY).getMessage().contains("未注册该机构"));
|
|
assertTrue(fetch("101", "HUIFU", DAY).getMessage().contains("未注册该机构"));
|
|
|
assertEquals(0, requests);
|
|
assertEquals(0, requests);
|
|
|
}
|
|
}
|
|
@@ -258,12 +289,14 @@ public class ReceiptBillGatewayTest {
|
|
|
mode = "ticket401";
|
|
mode = "ticket401";
|
|
|
ReceiptBillDto bill = fetch("101", "HUIFU", DAY);
|
|
ReceiptBillDto bill = fetch("101", "HUIFU", DAY);
|
|
|
assertEquals("ERROR", bill.getStatus());
|
|
assertEquals("ERROR", bill.getStatus());
|
|
|
- assertTrue(bill.getMessage(), bill.getMessage().contains("申请账单地址失败:HTTP 401,签名校验失败(security_exception)"));
|
|
|
|
|
|
|
+ assertTrue(bill.getMessage(), bill.getMessage().contains("申请账单地址失败:渠道拒绝请求:security_exception"));
|
|
|
assertFalse(bill.getMessage().contains("private-file"));
|
|
assertFalse(bill.getMessage().contains("private-file"));
|
|
|
- mode = "empty401";
|
|
|
|
|
- assertTrue(fetch("101", "HUIFU", DAY).getMessage().contains("申请账单地址失败:HTTP 401,认证未通过"));
|
|
|
|
|
- mode = "plain401";
|
|
|
|
|
- assertTrue(fetch("101", "HUIFU", DAY).getMessage().contains("申请账单地址失败:HTTP 401,API Key 无效"));
|
|
|
|
|
|
|
+ mode = "badTicketSignature";
|
|
|
|
|
+ bill = fetch("101", "HUIFU", DAY);
|
|
|
|
|
+ assertEquals("ERROR", bill.getStatus());
|
|
|
|
|
+ assertTrue(bill.getMessage(), bill.getMessage().contains("申请账单地址失败:汇付响应验签失败"));
|
|
|
|
|
+ mode = "pending";
|
|
|
|
|
+ assertEquals("NOT_READY", fetch("101", "HUIFU", DAY).getStatus());
|
|
|
assertEquals(0, downloads);
|
|
assertEquals(0, downloads);
|
|
|
mode = "file401";
|
|
mode = "file401";
|
|
|
bill = fetch("101", "HUIFU", DAY);
|
|
bill = fetch("101", "HUIFU", DAY);
|
|
@@ -274,6 +307,18 @@ public class ReceiptBillGatewayTest {
|
|
|
assertEquals(1, downloads);
|
|
assertEquals(1, downloads);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Test public void huifuUnsignedOrEmptyRejectionsStayAtTicketStage() throws Exception {
|
|
|
|
|
+ for (String scenario : Arrays.asList("empty401", "plain401")) {
|
|
|
|
|
+ mode = scenario;
|
|
|
|
|
+ ReceiptBillDto bill = fetch("101", "HUIFU", DAY);
|
|
|
|
|
+ assertEquals("ERROR", bill.getStatus());
|
|
|
|
|
+ assertTrue(bill.getMessage(), bill.getMessage().contains("汇付申请账单地址失败"));
|
|
|
|
|
+ assertFalse(bill.getMessage().contains("Invalid API key"));
|
|
|
|
|
+ }
|
|
|
|
|
+ assertEquals(2, tickets);
|
|
|
|
|
+ assertEquals(0, downloads);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Test public void missingRegistrationIsDistinctFromMissingDirectAccount() throws Exception {
|
|
@Test public void missingRegistrationIsDistinctFromMissingDirectAccount() throws Exception {
|
|
|
when(payments.getPaymentService("ALIPAY_101")).thenThrow(new com.microsvc.toolkit.middleware.payment.common.exception.PaymentException("missing"));
|
|
when(payments.getPaymentService("ALIPAY_101")).thenThrow(new com.microsvc.toolkit.middleware.payment.common.exception.PaymentException("missing"));
|
|
|
assertTrue(fetch("101", "ALIPAY", DAY).getMessage().contains("有直连账户配置,但支付服务未注册该实例(ALIPAY_101)"));
|
|
assertTrue(fetch("101", "ALIPAY", DAY).getMessage().contains("有直连账户配置,但支付服务未注册该实例(ALIPAY_101)"));
|
|
@@ -296,7 +341,7 @@ public class ReceiptBillGatewayTest {
|
|
|
|
|
|
|
|
@Test public void missingConfigOrSecretsNotTreatedAsPendingEvenToday() throws Exception {
|
|
@Test public void missingConfigOrSecretsNotTreatedAsPendingEvenToday() throws Exception {
|
|
|
assertEquals("ERROR", fetch("unconfigured", "WECHAT", DAY.plusDays(1)).getStatus());
|
|
assertEquals("ERROR", fetch("unconfigured", "WECHAT", DAY.plusDays(1)).getStatus());
|
|
|
- ada.setMerchantId(null);
|
|
|
|
|
|
|
+ stored.setApiKey(null);
|
|
|
ReceiptBillDto bill = fetch("101", "HUIFU", DAY.plusDays(1));
|
|
ReceiptBillDto bill = fetch("101", "HUIFU", DAY.plusDays(1));
|
|
|
assertEquals("ERROR", bill.getStatus()); assertNull(bill.getAvailableBalance()); assertEquals(0, requests);
|
|
assertEquals("ERROR", bill.getStatus()); assertNull(bill.getAvailableBalance()); assertEquals(0, requests);
|
|
|
}
|
|
}
|