Эх сурвалжийг харах

fix: harden OMG creation diagnostics

qmj 2 долоо хоног өмнө
parent
commit
a32c01752f

+ 1 - 1
ruoyi-admin/src/main/java/com/ruoyi/app/omgpay/OmgPaymentController.java

@@ -42,7 +42,7 @@ public class OmgPaymentController {
             OmgPaymentCreateOutcome outcome = createService.create(safeUserId, orderId);
             log.info("OMG payment create succeeded orderId={}, userId={}, storeId={}, "
                             + "attemptId={}, amount={}, status=CREATED, merchantTradeNo={}",
-                    outcome.orderId(), outcome.userId(), outcome.storeId(), outcome.attemptId(),
+                    safeLogOrderId(outcome.orderId()), outcome.userId(), outcome.storeId(), outcome.attemptId(),
                     outcome.amount(), outcome.maskedMerchantTradeNo());
             return AjaxResult.success(outcome.response());
         } catch (OmgPaymentBusinessException error) {

+ 16 - 1
ruoyi-admin/src/main/java/com/ruoyi/app/omgpay/OmgPaymentCreateService.java

@@ -48,7 +48,7 @@ public class OmgPaymentCreateService {
         PosStoreOmg credential = credentials.getEnabledCredential(order.getStoreId());
         validateCredential(credential, order.getStoreId());
         log.info("OMG payment validation passed orderId={}, userId={}, storeId={}",
-                order.getDdId(), userId, order.getStoreId());
+                safeLogOrderId(order.getDdId()), userId, order.getStoreId());
         return createWithBoundedTradeNumberRetries(order, userId, credential);
     }
 
@@ -135,6 +135,21 @@ public class OmgPaymentCreateService {
                 + merchantTradeNo.substring(merchantTradeNo.length() - 4);
     }
 
+    private static String safeLogOrderId(String orderId) {
+        if (orderId == null) {
+            return "<empty>";
+        }
+        StringBuilder safe = new StringBuilder();
+        for (int index = 0; index < orderId.length() && safe.length() < 64; index++) {
+            char value = orderId.charAt(index);
+            if ((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')
+                    || (value >= '0' && value <= '9') || value == '-' || value == '_') {
+                safe.append(value);
+            }
+        }
+        return safe.isEmpty() ? "<empty>" : safe.toString();
+    }
+
     private static OmgPaymentBusinessException business(OmgPaymentErrorCode code) {
         return new OmgPaymentBusinessException(code);
     }

+ 14 - 3
ruoyi-admin/src/test/java/com/ruoyi/app/omgpay/OmgPaymentCreateServiceTest.java

@@ -160,7 +160,6 @@ class OmgPaymentCreateServiceTest {
         wrongType.setPayType("1");
         OmgPaymentOrderSnapshot zeroAmount = payableOrder();
         zeroAmount.setAmount(0);
-        OmgPaymentOrderSnapshot missingCredential = payableOrder();
         return Stream.of(
                 Arguments.of("missing order", null, ORDER_NOT_AVAILABLE),
                 Arguments.of("other owner", otherOwner, ORDER_NOT_AVAILABLE),
@@ -170,8 +169,20 @@ class OmgPaymentCreateServiceTest {
                 Arguments.of("state 4", stateFour, ORDER_STATE_NOT_PAYABLE),
                 Arguments.of("already paid", paid, ORDER_ALREADY_PAID),
                 Arguments.of("wrong payment type", wrongType, PAYMENT_TYPE_INVALID),
-                Arguments.of("nonpositive amount", zeroAmount, ORDER_AMOUNT_INVALID),
-                Arguments.of("missing credential", missingCredential, STORE_CREDENTIAL_UNAVAILABLE));
+                Arguments.of("nonpositive amount", zeroAmount, ORDER_AMOUNT_INVALID));
+    }
+
+    @Test
+    void rejectsMissingCredentialBeforeGenerationOrInsert() {
+        when(attempts.selectOrderForUpdate("DD-1")).thenReturn(payableOrder());
+        when(credentials.getEnabledCredential(77L)).thenReturn(null);
+
+        OmgPaymentBusinessException error = assertThrows(OmgPaymentBusinessException.class,
+                () -> service.create(5L, "DD-1"));
+
+        assertEquals(STORE_CREDENTIAL_UNAVAILABLE, error.getCode());
+        verifyNoInteractions(generator, formFactory);
+        verify(attempts, never()).createCreated(anyString(), anyString(), anyLong(), anyString(), anyInt());
     }
 
     private static OmgPaymentOrderSnapshot payableOrder() {