|
|
@@ -91,6 +91,64 @@ class OmgPaymentQueryServiceTest {
|
|
|
verify(settlement).synchronizeVerifiedQuery(argThat(facts -> facts.resultCode() == 10200095));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ void acceptsSignedSparseUnpaidResponseWithoutOptionalFields() {
|
|
|
+ when(gateway.query(anyMap())).thenReturn(signedSparseResponse("0"));
|
|
|
+
|
|
|
+ OmgQueryPaymentResponse response = service.query(5L, "DD-1");
|
|
|
+
|
|
|
+ assertEquals("UNPAID", response.status());
|
|
|
+ assertEquals("0", response.tradeStatus());
|
|
|
+ assertEquals(100, response.amount());
|
|
|
+ verify(settlement, never()).synchronizeVerifiedQuery(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void acceptsSignedSparseFailureResponseWithoutSettlementFields() {
|
|
|
+ when(gateway.query(anyMap())).thenReturn(signedSparseResponse("10200095"));
|
|
|
+ when(settlement.synchronizeVerifiedQuery(any())).thenReturn(OmgPaymentSettlementResult.FAILED);
|
|
|
+
|
|
|
+ OmgQueryPaymentResponse response = service.query(5L, "DD-1");
|
|
|
+
|
|
|
+ assertEquals("FAILED", response.status());
|
|
|
+ verify(settlement).synchronizeVerifiedQuery(argThat(facts ->
|
|
|
+ facts.resultCode() == 10200095
|
|
|
+ && facts.tradeNo() == null
|
|
|
+ && facts.paymentType() == null
|
|
|
+ && facts.tradeDate() == null
|
|
|
+ && facts.paymentTypeChargeFee() == null));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void treatsSignedGatewayMissingTradeAsFailedAttempt() {
|
|
|
+ when(gateway.query(anyMap())).thenReturn(signedFields(gatewayMissingTradeFields()));
|
|
|
+ when(settlement.synchronizeVerifiedQuery(any())).thenReturn(OmgPaymentSettlementResult.FAILED);
|
|
|
+
|
|
|
+ OmgQueryPaymentResponse response = service.query(5L, "DD-1");
|
|
|
+
|
|
|
+ assertEquals("FAILED", response.status());
|
|
|
+ assertEquals("10200047", response.tradeStatus());
|
|
|
+ assertEquals(0, response.amount());
|
|
|
+ verify(settlement).synchronizeVerifiedQuery(argThat(facts ->
|
|
|
+ facts.resultCode() == 10200047
|
|
|
+ && facts.amount() == 100
|
|
|
+ && facts.tradeNo() == null
|
|
|
+ && facts.paymentType() == null));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void rejectsGatewayMissingTradeStatusWithNonzeroAmount() {
|
|
|
+ Map<String, String> fields = gatewayMissingTradeFields();
|
|
|
+ fields.put("TradeAmt", "100");
|
|
|
+ when(gateway.query(anyMap())).thenReturn(signedFields(fields));
|
|
|
+
|
|
|
+ OmgPaymentBusinessException exception = assertThrows(OmgPaymentBusinessException.class,
|
|
|
+ () -> service.query(5L, "DD-1"));
|
|
|
+
|
|
|
+ assertEquals(OmgPaymentErrorCode.PAYMENT_QUERY_FAILED, exception.getCode());
|
|
|
+ verifyNoInteractions(settlement);
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
void paidOrderQueriesItsPaidAttemptInsteadOfAnOldOrClientSelectedAttempt() {
|
|
|
OmgPaymentAttempt paid = attempt(1);
|
|
|
@@ -134,6 +192,25 @@ class OmgPaymentQueryServiceTest {
|
|
|
verify(settlement, never()).synchronizeVerifiedQuery(any());
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ void rejectsSparseResponseWithoutCoreIdentityAndPaidSettlementFields() {
|
|
|
+ Map<String, String> missingAmount = sparseFields("0");
|
|
|
+ missingAmount.remove("TradeAmt");
|
|
|
+ when(gateway.query(anyMap())).thenReturn(signedFields(missingAmount));
|
|
|
+
|
|
|
+ OmgPaymentBusinessException missingIdentity = assertThrows(OmgPaymentBusinessException.class,
|
|
|
+ () -> service.query(5L, "DD-1"));
|
|
|
+ assertEquals(OmgPaymentErrorCode.PAYMENT_QUERY_FAILED, missingIdentity.getCode());
|
|
|
+
|
|
|
+ reset(gateway, settlement);
|
|
|
+ when(gateway.query(anyMap())).thenReturn(signedSparseResponse("1"));
|
|
|
+
|
|
|
+ OmgPaymentBusinessException missingSettlement = assertThrows(OmgPaymentBusinessException.class,
|
|
|
+ () -> service.query(5L, "DD-1"));
|
|
|
+ assertEquals(OmgPaymentErrorCode.PAYMENT_QUERY_FAILED, missingSettlement.getCode());
|
|
|
+ verifyNoInteractions(settlement);
|
|
|
+ }
|
|
|
+
|
|
|
private String signedResponse(String tradeStatus, Map<String, String> extras) {
|
|
|
Map<String, String> fields = new LinkedHashMap<>();
|
|
|
fields.put("MerchantID", "1000031");
|
|
|
@@ -153,6 +230,40 @@ class OmgPaymentQueryServiceTest {
|
|
|
fields.put("CustomField3", "");
|
|
|
fields.put("CustomField4", "");
|
|
|
fields.putAll(extras);
|
|
|
+ return signedFields(fields);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String signedSparseResponse(String tradeStatus) {
|
|
|
+ return signedFields(sparseFields(tradeStatus));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> sparseFields(String tradeStatus) {
|
|
|
+ Map<String, String> fields = new LinkedHashMap<>();
|
|
|
+ fields.put("MerchantID", "1000031");
|
|
|
+ fields.put("MerchantTradeNo", "OMG123");
|
|
|
+ fields.put("TradeAmt", "100");
|
|
|
+ fields.put("TradeStatus", tradeStatus);
|
|
|
+ return fields;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> gatewayMissingTradeFields() {
|
|
|
+ Map<String, String> fields = new LinkedHashMap<>();
|
|
|
+ fields.put("MerchantID", "1000031");
|
|
|
+ fields.put("MerchantTradeNo", "OMG123");
|
|
|
+ fields.put("TradeNo", "");
|
|
|
+ fields.put("TradeAmt", "0");
|
|
|
+ fields.put("PaymentDate", "");
|
|
|
+ fields.put("PaymentType", "");
|
|
|
+ fields.put("HandlingCharge", "0");
|
|
|
+ fields.put("PaymentTypeChargeFee", "0");
|
|
|
+ fields.put("TradeDate", "");
|
|
|
+ fields.put("TradeStatus", "10200047");
|
|
|
+ fields.put("ItemName", "");
|
|
|
+ return fields;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String signedFields(Map<String, String> source) {
|
|
|
+ Map<String, String> fields = new LinkedHashMap<>(source);
|
|
|
fields.put("CheckMacValue", new OmgCheckMacSigner().sign(fields, HASH_KEY, HASH_IV));
|
|
|
return fields.entrySet().stream()
|
|
|
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue()))
|