Bläddra i källkod

fix: 按机构启用的收款渠道判定对账状态

yonge 1 vecka sedan
förälder
incheckning
54d4501e6b

+ 1 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/ReceiptBillDto.java

@@ -8,6 +8,7 @@ import java.util.List;
 
 
 @Data
 @Data
 public class ReceiptBillDto {
 public class ReceiptBillDto {
+    public static final String NOT_ENABLED = "NOT_ENABLED";
     private String channel;
     private String channel;
     private String status;
     private String status;
     private String message;
     private String message;

+ 8 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/impl/receipt/ReceiptLedger.java

@@ -32,7 +32,6 @@ public final class ReceiptLedger {
         for (String name : CHANNELS) {
         for (String name : CHANNELS) {
             ReceiptChannelVo channel = new ReceiptChannelVo();
             ReceiptChannelVo channel = new ReceiptChannelVo();
             channel.setChannel(name);
             channel.setChannel(name);
-            channels.put(name, channel);
             ReceiptBillDto bill = null;
             ReceiptBillDto bill = null;
             for (ReceiptBillDto candidate : bills) {
             for (ReceiptBillDto candidate : bills) {
                 if (name.equals(candidate.getChannel())) {
                 if (name.equals(candidate.getChannel())) {
@@ -42,6 +41,12 @@ public final class ReceiptLedger {
             }
             }
             channel.setStatus(bill == null ? "ERROR" : bill.getStatus());
             channel.setStatus(bill == null ? "ERROR" : bill.getStatus());
             channel.setMessage(bill == null ? "未取得账单" : bill.getMessage());
             channel.setMessage(bill == null ? "未取得账单" : bill.getMessage());
+            if (ReceiptBillDto.NOT_ENABLED.equals(channel.getStatus())) {
+                if (system.stream().noneMatch(row -> name.equals(row.getChannel()))) continue;
+                channel.setStatus("ERROR");
+                channel.setMessage("当日有该渠道的系统成功收款,但当前机构未启用该渠道,请核实收款账户");
+            }
+            channels.put(name, channel);
             if (bill != null) {
             if (bill != null) {
                 channel.setAvailableBalance(bill.getAvailableBalance());
                 channel.setAvailableBalance(bill.getAvailableBalance());
                 channel.setBalanceTime(bill.getBalanceTime());
                 channel.setBalanceTime(bill.getBalanceTime());
@@ -60,6 +65,7 @@ public final class ReceiptLedger {
             count += channel.getThirdCount();
             count += channel.getThirdCount();
             amount = amount.add(channel.getThirdAmount());
             amount = amount.add(channel.getThirdAmount());
         }
         }
+        complete = complete && !channels.isEmpty();
         for (ReceiptEntryDto row : system) {
         for (ReceiptEntryDto row : system) {
             ReceiptChannelVo channel = channels.computeIfAbsent(row.getChannel(), name -> {
             ReceiptChannelVo channel = channels.computeIfAbsent(row.getChannel(), name -> {
                 ReceiptChannelVo unknown = new ReceiptChannelVo();
                 ReceiptChannelVo unknown = new ReceiptChannelVo();
@@ -118,7 +124,7 @@ public final class ReceiptLedger {
             } else if (group.getThird() == null) {
             } else if (group.getThird() == null) {
                 if (!complete) continue;
                 if (!complete) continue;
                 group.setType("SYSTEM_ONLY");
                 group.setType("SYSTEM_ONLY");
-                group.setReason("三个渠道当日账单均未找到对应收款");
+                group.setReason("该机构已启用的收款渠道当日账单中未找到对应收款");
             } else {
             } else {
                 boolean sameChannel = Objects.equals(group.getSystem().getChannel(), group.getThird().getChannel());
                 boolean sameChannel = Objects.equals(group.getSystem().getChannel(), group.getThird().getChannel());
                 boolean sameAmount = group.getSystem().getAmount().compareTo(group.getThird().getAmount()) == 0;
                 boolean sameAmount = group.getSystem().getAmount().compareTo(group.getThird().getAmount()) == 0;

+ 5 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/receipt/gateway/ReceiptBillGateway.java

@@ -25,6 +25,11 @@ public class ReceiptBillGateway {
                 !("HUIFU".equals(channel) || "ALIPAY".equals(channel) || "WECHAT".equals(channel))) return bill;
                 !("HUIFU".equals(channel) || "ALIPAY".equals(channel) || "WECHAT".equals(channel))) return bill;
         try {
         try {
             BillCredentials credentials = accounts.resolve(tenantId, channel);
             BillCredentials credentials = accounts.resolve(tenantId, channel);
+            if (credentials == null) {
+                bill.setStatus(ReceiptBillDto.NOT_ENABLED);
+                bill.setMessage("该机构未启用此收款渠道");
+                return bill;
+            }
             try (BillHttp http = transport()) {
             try (BillHttp http = transport()) {
                 ProviderBills provider = new ProviderBills(channel, credentials, http);
                 ProviderBills provider = new ProviderBills(channel, credentials, http);
                 try {
                 try {

+ 27 - 14
mec-biz/src/main/java/com/ym/mec/biz/service/impl/receipt/gateway/ReceiptPaymentAccounts.java

@@ -39,19 +39,22 @@ class ReceiptPaymentAccounts {
         this.environment = environment;
         this.environment = environment;
     }
     }
 
 
+    // 仅在确认渠道未开通或已停用时返回空;配置异常继续报错。
     BillCredentials resolve(String tenant, String channel) throws Exception {
     BillCredentials resolve(String tenant, String channel) throws Exception {
         if ("HUIFU".equals(channel)) return huifuCredentials(tenant);
         if ("HUIFU".equals(channel)) return huifuCredentials(tenant);
         String vendor = "WECHAT".equals(channel) ? "WXPAY" : "ALIPAY";
         String vendor = "WECHAT".equals(channel) ? "WXPAY" : "ALIPAY";
         String route = vendor + "_" + tenant;
         String route = vendor + "_" + tenant;
-        BasePaymentService service;
-        try {
-            service = payments.getPaymentService(route);
-        } catch (com.microsvc.toolkit.middleware.payment.common.exception.PaymentException missing) {
-            throw missingRoute(vendor, route);
+        Map<String, ? extends BasePaymentConfig> routes = "WECHAT".equals(channel)
+                ? configured.getWxPayConfigs() : configured.getAliPayConfigs();
+        if (disabled(routes.get(route))) return null;
+        BasePaymentService service = registeredService(route);
+        if (service == null) {
+            if (routes.containsKey(route)) throw new BillHttp.Failure("有直连账户配置,但支付服务未注册该实例(" + route + ")");
+            return null;
         }
         }
-        if (service == null) throw missingRoute(vendor, route);
         if (!route.equals(service.venderName())) throw unknown();
         if (!route.equals(service.venderName())) throw unknown();
         BasePaymentConfig account = service.getPaymentConfig();
         BasePaymentConfig account = service.getPaymentConfig();
+        if (disabled(account)) return null;
         if ("ALIPAY".equals(vendor) && account instanceof AliPayConfig) {
         if ("ALIPAY".equals(vendor) && account instanceof AliPayConfig) {
             AliPayConfig ali = (AliPayConfig)account;
             AliPayConfig ali = (AliPayConfig)account;
             String app = required(ali.getAppId(), "支付宝应用 ID");
             String app = required(ali.getAppId(), "支付宝应用 ID");
@@ -101,10 +104,14 @@ class ReceiptPaymentAccounts {
         }
         }
         String vendor = PaymentChannelEnum.ADAPAY.getCode();
         String vendor = PaymentChannelEnum.ADAPAY.getCode();
         HfMerchantConfig stored = huifu.queryByTenantId(tenantId, vendor);
         HfMerchantConfig stored = huifu.queryByTenantId(tenantId, vendor);
-        if (stored == null || !tenant.equals(String.valueOf(stored.getTenantId()))) throw unknown();
+        String route = vendor + "_" + tenant;
+        if (stored == null) {
+            if (huifu.adapayAccountOwners().containsKey(route) || configured.getAdapayPayConfigs().containsKey(route)) throw unknown();
+            return null;
+        }
+        if (!tenant.equals(String.valueOf(stored.getTenantId()))) throw unknown();
         String app = required(stored.getAppId(), "汇付应用 ID");
         String app = required(stored.getAppId(), "汇付应用 ID");
         String merchantKey = required(stored.getMerKey(), "汇付商户标识");
         String merchantKey = required(stored.getMerKey(), "汇付商户标识");
-        String route = vendor + "_" + tenant;
         checkOthers(route, vendor, app, configured.getAdapayPayConfigs(), BasePaymentConfig::getAppId);
         checkOthers(route, vendor, app, configured.getAdapayPayConfigs(), BasePaymentConfig::getAppId);
         for (Map.Entry<String, Set<String>> item : huifu.adapayAccountOwners().entrySet()) {
         for (Map.Entry<String, Set<String>> item : huifu.adapayAccountOwners().entrySet()) {
             if (route.equals(item.getKey())) {
             if (route.equals(item.getKey())) {
@@ -130,12 +137,18 @@ class ReceiptPaymentAccounts {
         }
         }
     }
     }
 
 
-    private BillHttp.Failure missingRoute(String vendor, String route) {
-        boolean configuredHere = "WXPAY".equals(vendor) ? configured.getWxPayConfigs().containsKey(route)
-                : configured.getAliPayConfigs().containsKey(route);
-        String name = "WXPAY".equals(vendor) ? "微信" : "支付宝";
-        String cause = configuredHere ? "有直连账户配置,但支付服务未注册该实例" : "未找到该机构的直连支付实例";
-        return new BillHttp.Failure(name + cause + "(" + route + "),当日交易未确认");
+    private BasePaymentService registeredService(String route) {
+        try {
+            return payments.getPaymentService(route);
+        } catch (com.microsvc.toolkit.middleware.payment.common.exception.PaymentException missing) {
+            return null;
+        }
+    }
+
+    private static boolean disabled(BasePaymentConfig account) {
+        if (account instanceof AliPayConfig) return Boolean.FALSE.equals(((AliPayConfig) account).getEnable());
+        return account instanceof PayConfigProperties.WxPayConfig
+                && Boolean.FALSE.equals(((PayConfigProperties.WxPayConfig) account).getEnable());
     }
     }
 
 
     private static BillHttp.Failure unknown() { return new BillHttp.Failure("该机构的渠道账户未确认,不能判定为零笔"); }
     private static BillHttp.Failure unknown() { return new BillHttp.Failure("该机构的渠道账户未确认,不能判定为零笔"); }

+ 34 - 0
mec-biz/src/test/java/com/ym/mec/biz/service/impl/receipt/ReceiptLedgerTest.java

@@ -69,6 +69,40 @@ public class ReceiptLedgerTest {
         assertTrue(result.getDifferences().isEmpty());
         assertTrue(result.getDifferences().isEmpty());
     }
     }
 
 
+    private ReceiptBillDto inactive(String channel) {
+        ReceiptBillDto result = bill(channel);
+        result.setStatus(ReceiptBillDto.NOT_ENABLED);
+        return result;
+    }
+
+    @Test public void onlyHuifuIsRequiredAndFinalDifferencesAreAvailable() {
+        ReceiptEntryDto order = entry("HUIFU", "001", "tx", "17.20");
+        List<ReceiptBillDto> bills = Arrays.asList(bill("HUIFU", order), inactive("ALIPAY"), inactive("WECHAT"));
+        ReceiptReconcileResultVo result = ReceiptLedger.compare("13", day, Collections.singletonList(order), bills);
+        assertEquals("MATCHED", result.getStatus());
+        assertEquals(1, result.getChannels().size());
+        assertEquals(order.getAmount(), result.getThirdAmount());
+        assertEquals("HUIFU", result.getChannels().get(0).getChannel());
+
+        assertEquals("THIRD_ONLY", ReceiptLedger.compare("13", day, Collections.emptyList(), bills).getDifferences().get(0).getType());
+        bills.get(0).getEntries().clear();
+        result = ReceiptLedger.compare("13", day, Collections.singletonList(order), bills);
+        assertEquals("DIFFERENT", result.getStatus());
+        assertEquals("SYSTEM_ONLY", result.getDifferences().get(0).getType());
+    }
+
+    @Test public void noEnabledAccountsOrOrdersWithoutAnActiveAccountNeedReview() {
+        List<ReceiptBillDto> bills = Arrays.asList(inactive("HUIFU"), inactive("ALIPAY"), inactive("WECHAT"));
+        ReceiptReconcileResultVo empty = ReceiptLedger.compare("13", day, Collections.emptyList(), bills);
+        assertEquals("INCOMPLETE", empty.getStatus());
+        assertTrue(empty.getChannels().isEmpty());
+        ReceiptReconcileResultVo conflict = ReceiptLedger.compare("13", day,
+                Collections.singletonList(entry("WECHAT", "old", "wx", "10")), bills);
+        assertEquals("INCOMPLETE", conflict.getStatus());
+        assertEquals(1, conflict.getSystemCount());
+        assertEquals("ERROR", conflict.getChannels().get(0).getStatus());
+    }
+
     @Test public void preservesDuplicateAndConflictingCandidatesAsOneGroup() {
     @Test public void preservesDuplicateAndConflictingCandidatesAsOneGroup() {
         ReceiptReconcileResultVo result = compare(Arrays.asList(entry("ALIPAY", "01", "a", "10"), entry("ALIPAY", "02", "b", "10")),
         ReceiptReconcileResultVo result = compare(Arrays.asList(entry("ALIPAY", "01", "a", "10"), entry("ALIPAY", "02", "b", "10")),
                 entry("ALIPAY", "01", "b", "10"));
                 entry("ALIPAY", "01", "b", "10"));

+ 14 - 7
mec-biz/src/test/java/com/ym/mec/biz/service/impl/receipt/gateway/ReceiptBillGatewayTest.java

@@ -98,8 +98,8 @@ public class ReceiptBillGatewayTest {
         runtimeAccount.setApiKey(stored.getApiKey());
         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);
-        wx.setMerchantId("10001"); wx.setEnable(false);
+        ali.setPrivateKey(key); ali.setPublicKey(pub); ali.setAppId("ali-app"); ali.setEnable(true);
+        wx.setMerchantId("10001"); wx.setEnable(true);
         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);
@@ -417,7 +417,15 @@ public class ReceiptBillGatewayTest {
     @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)"));
-        assertTrue(fetch("102", "WECHAT", DAY).getMessage().contains("未找到该机构的直连支付实例(WXPAY_102)"));
+        assertEquals(ReceiptBillDto.NOT_ENABLED, fetch("102", "WECHAT", DAY).getStatus());
+        assertEquals(0, requests);
+    }
+
+    @Test public void disabledDirectAccountsDoNotRequestStatementsOrBalances() throws Exception {
+        wx.setEnable(false);
+        ali.setEnable(false);
+        assertEquals(ReceiptBillDto.NOT_ENABLED, fetch("101", "WECHAT", DAY).getStatus());
+        assertEquals(ReceiptBillDto.NOT_ENABLED, fetch("101", "ALIPAY", DAY).getStatus());
         assertEquals(0, requests);
         assertEquals(0, requests);
     }
     }
 
 
@@ -434,8 +442,7 @@ public class ReceiptBillGatewayTest {
         assertEquals(0, requests);
         assertEquals(0, requests);
     }
     }
 
 
-    @Test public void missingConfigOrSecretsNotTreatedAsPendingEvenToday() throws Exception {
-        assertEquals("ERROR", fetch("unconfigured", "WECHAT", DAY.plusDays(1)).getStatus());
+    @Test public void missingSecretsNotTreatedAsPendingEvenToday() throws Exception {
         stored.setApiKey(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);
@@ -445,7 +452,7 @@ public class ReceiptBillGatewayTest {
         supplements.put("mec.receipt.gateway.tenants.102.ALIPAY.appId", "ali-app");
         supplements.put("mec.receipt.gateway.tenants.102.ALIPAY.appId", "ali-app");
         for (String channel : ReceiptLedger.CHANNELS) {
         for (String channel : ReceiptLedger.CHANNELS) {
             ReceiptBillDto bill = fetch("102", channel, DAY);
             ReceiptBillDto bill = fetch("102", channel, DAY);
-            assertEquals("ERROR", bill.getStatus()); assertTrue(bill.getMessage().contains("未确认"));
+            assertEquals(ReceiptBillDto.NOT_ENABLED, bill.getStatus());
         }
         }
         org.mockito.Mockito.verify(payments, never()).getPaymentService();
         org.mockito.Mockito.verify(payments, never()).getPaymentService();
         assertEquals(0, requests);
         assertEquals(0, requests);
@@ -496,7 +503,7 @@ public class ReceiptBillGatewayTest {
         assertTrue(fetch("101", "ALIPAY", DAY).getMessage().contains("商户 PID"));
         assertTrue(fetch("101", "ALIPAY", DAY).getMessage().contains("商户 PID"));
         supplements.put("app-pay.ali-pay-configs[ALIPAY_101].seller-id", PID);
         supplements.put("app-pay.ali-pay-configs[ALIPAY_101].seller-id", PID);
         assertEquals("READY", fetch("101", "ALIPAY", DAY).getStatus());
         assertEquals("READY", fetch("101", "ALIPAY", DAY).getStatus());
-        assertTrue(fetch("102", "ALIPAY", DAY).getMessage().contains("未确认"));
+        assertEquals(ReceiptBillDto.NOT_ENABLED, fetch("102", "ALIPAY", DAY).getStatus());
     }
     }
 
 
     @Test public void wechatRejectsInvalidDatesAndWaitsBeforeCutoff() throws Exception {
     @Test public void wechatRejectsInvalidDatesAndWaitsBeforeCutoff() throws Exception {