|
|
@@ -3,7 +3,10 @@ package com.ym.mec.biz.service.impl.receipt.gateway;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
import com.github.binarywang.wxpay.v3.auth.Verifier;
|
|
|
+import com.huifu.adapay.Adapay;
|
|
|
import com.huifu.adapay.core.AdapayCore;
|
|
|
+import com.huifu.adapay.core.util.AdapaySign;
|
|
|
+import com.huifu.adapay.model.MerConfig;
|
|
|
import com.microsvc.toolkit.middleware.payment.common.api.*;
|
|
|
import com.microsvc.toolkit.middleware.payment.impl.WxPaymentServicePlugin;
|
|
|
import com.microsvc.toolkit.middleware.payment.properties.PayConfigProperties;
|
|
|
@@ -12,6 +15,7 @@ import com.ym.mec.biz.dal.dto.ReceiptBillDto;
|
|
|
import com.ym.mec.biz.dal.vo.ReceiptChannelVo;
|
|
|
import com.ym.mec.biz.dal.vo.ReceiptReconcileResultVo;
|
|
|
import com.ym.mec.biz.service.HfMerchantConfigService;
|
|
|
+import com.ym.mec.thirdparty.adapay.entity.HfMerchantConfig;
|
|
|
import com.ym.mec.biz.service.impl.receipt.ReceiptLedger;
|
|
|
import org.junit.*;
|
|
|
import static org.mockito.Mockito.*;
|
|
|
@@ -52,6 +56,8 @@ public class ReceiptBillGatewayTest {
|
|
|
private final AliPayConfig ali = new AliPayConfig();
|
|
|
private final PayConfigProperties.WxPayConfig wx = new PayConfigProperties.WxPayConfig();
|
|
|
private String mode = "ok", originalPlatformKey;
|
|
|
+ private static final String MERCHANT_KEY = "receipt-test-merchant";
|
|
|
+ private MerConfig originalAccount;
|
|
|
private int requests, downloads, tickets;
|
|
|
private LocalDateTime clock = DAY.plusDays(1).atTime(10, 0);
|
|
|
|
|
|
@@ -72,6 +78,12 @@ public class ReceiptBillGatewayTest {
|
|
|
String pub = Base64.getEncoder().encodeToString(platform.getPublic().getEncoded());
|
|
|
originalPlatformKey = AdapayCore.PUBLIC_KEY; AdapayCore.PUBLIC_KEY = pub;
|
|
|
ada.setKeyPrivate(key); ada.setMerchantId("offline-api"); ada.setAppId("ada");
|
|
|
+ ada.setMerchantKey(MERCHANT_KEY);
|
|
|
+ originalAccount = Adapay.getConfig(MERCHANT_KEY);
|
|
|
+ MerConfig runtimeAccount = new MerConfig();
|
|
|
+ runtimeAccount.setApiKey(ada.getMerchantId());
|
|
|
+ runtimeAccount.setRSAPrivateKey(key);
|
|
|
+ Adapay.addMerConfig(runtimeAccount, MERCHANT_KEY);
|
|
|
ali.setPrivateKey(key); ali.setPublicKey(pub); ali.setAppId("ali-app"); ali.setEnable(false);
|
|
|
wx.setMerchantId("10001"); wx.setEnable(false);
|
|
|
register("ADAPAY_101", ada, mock(BasePaymentService.class));
|
|
|
@@ -95,7 +107,10 @@ public class ReceiptBillGatewayTest {
|
|
|
accounts = new ReceiptPaymentAccounts(payments, shared, huifu, environment);
|
|
|
}
|
|
|
|
|
|
- @After public void restorePlatformKey() { AdapayCore.PUBLIC_KEY = originalPlatformKey; }
|
|
|
+ @After public void restoreAccounts() throws Exception {
|
|
|
+ AdapayCore.PUBLIC_KEY = originalPlatformKey;
|
|
|
+ Adapay.addMerConfig(originalAccount, MERCHANT_KEY);
|
|
|
+ }
|
|
|
|
|
|
private ReceiptBillDto fetch(String tenant, String channel, LocalDate day) throws Exception {
|
|
|
return new ReceiptBillGateway(accounts) {
|
|
|
@@ -143,6 +158,7 @@ public class ReceiptBillGatewayTest {
|
|
|
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]);
|
|
|
@@ -212,6 +228,32 @@ public class ReceiptBillGatewayTest {
|
|
|
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()));
|
|
|
+ assertEquals("READY", fetch("101", "HUIFU", DAY).getStatus());
|
|
|
+ assertEquals(1, tickets);
|
|
|
+
|
|
|
+ updated.setAppId("another-app");
|
|
|
+ ReceiptBillDto rejected = fetch("101", "HUIFU", DAY);
|
|
|
+ assertEquals("ERROR", rejected.getStatus());
|
|
|
+ assertTrue(rejected.getMessage().contains("与本机构商户记录不一致"));
|
|
|
+ assertEquals(1, tickets);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test public void huifuDoesNotFallBackToStartupKeysForMissingSdkAccount() throws Exception {
|
|
|
+ ada.setMerchantKey("receipt-test-unregistered");
|
|
|
+ assertTrue(fetch("101", "HUIFU", DAY).getMessage().contains("未注册该机构"));
|
|
|
+ assertEquals(0, requests);
|
|
|
+ }
|
|
|
+
|
|
|
@Test public void huifuReportsActualHttpStageAndSafeReason() throws Exception {
|
|
|
mode = "ticket401";
|
|
|
ReceiptBillDto bill = fetch("101", "HUIFU", DAY);
|