Skip to content

Commit 26db4e1

Browse files
authored
Add check for version invoice snapshot (#84)
1 parent 2c0d26b commit 26db4e1

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<dependency>
6363
<groupId>dev.vality</groupId>
6464
<artifactId>damsel</artifactId>
65-
<version>1.674-368f05b</version>
65+
<version>1.678-09e4c72</version>
6666
</dependency>
6767
<dependency>
6868
<groupId>dev.vality.geck</groupId>

src/main/java/dev/vality/hooker/service/InvoicingEventService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import dev.vality.hooker.utils.TimeUtils;
1717
import dev.vality.swag_webhook_events.model.*;
1818
import lombok.RequiredArgsConstructor;
19+
import lombok.extern.slf4j.Slf4j;
1920
import org.apache.thrift.TException;
2021
import org.springframework.stereotype.Service;
2122

23+
@Slf4j
2224
@Service
2325
@RequiredArgsConstructor
2426
public class InvoicingEventService
@@ -51,7 +53,14 @@ public InvoicePaymentAdjustment getAdjustmentByMessage(InvoicingMessage message,
5153
@Override
5254
public dev.vality.damsel.payment_processing.Invoice getInvoiceByMessage(InvoicingMessage message) {
5355
try {
54-
return invoicingClient.get(message.getSourceId(), getEventRange(message));
56+
dev.vality.damsel.payment_processing.Invoice invoice =
57+
invoicingClient.get(message.getSourceId(), getEventRange(message));
58+
if (invoice.getLatestEventId() != message.getSequenceId().intValue()) {
59+
log.warn("Event not need version: {} but come: {}", message.getSequenceId(),
60+
invoice.getLatestEventId());
61+
throw new RemoteHostException("Event not need version!");
62+
}
63+
return invoice;
5564
} catch (InvoiceNotFound e) {
5665
throw new NotFoundException("Invoice not found, invoiceId=" + message.getSourceId());
5766
} catch (TException e) {
@@ -157,7 +166,7 @@ private static InvoicePaymentAdjustment findAdjustmentInPayment(InvoicingMessage
157166
}
158167
return invoicePayment.getAdjustments().stream()
159168
.filter(invoicePaymentAdjustment -> invoicePaymentAdjustment.isSetId()
160-
&& invoicePaymentAdjustment.getId().equals(adjustmentId))
169+
&& invoicePaymentAdjustment.getId().equals(adjustmentId))
161170
.findFirst()
162171
.orElseThrow(() -> new NotFoundException(
163172
String.format("Adjustment not found, invoiceId=%s, paymentId=%s, adjustmentId=%s",

src/test/java/dev/vality/hooker/service/InvoicingEventServiceTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void setUp() throws Exception {
5656
void testRefundSucceeded() {
5757
InvoicingMessage message = random(InvoicingMessage.class, "userInteraction");
5858
message.setPaymentId("1");
59+
message.setSequenceId(1L);
5960
message.setRefundId("1");
6061
message.setType(InvoicingMessageEnum.REFUND);
6162
message.setEventTime("2016-03-22T06:12:27Z");
@@ -75,6 +76,7 @@ void testRefundSucceeded() {
7576
void testJson() throws JsonProcessingException {
7677
InvoicingMessage message = random(InvoicingMessage.class, "userInteraction");
7778
message.setPaymentId("1");
79+
message.setSequenceId(1L);
7880
message.setType(InvoicingMessageEnum.PAYMENT);
7981
message.setEventTime("2016-03-22T06:12:27Z");
8082
message.setEventType(EventType.INVOICE_PAYMENT_STATUS_CHANGED);
@@ -127,6 +129,7 @@ private static InvoicingMessage createInvloiceMessage(String adjustmentId) {
127129
message.setEventTime("2016-03-22T06:12:27Z");
128130
message.setEventType(EventType.INVOICE_PAYMENT_STATUS_CHANGED);
129131
message.setPaymentStatus(PaymentStatusEnum.CAPTURED);
132+
message.setSequenceId(1L);
130133
return message;
131134
}
132135

@@ -230,6 +233,7 @@ private static InvoicingMessage createDefaultInvoicingMessage() {
230233
message.setSequenceId(123L);
231234
message.setType(InvoicingMessageEnum.PAYMENT);
232235
message.setEventTime("2016-03-22T06:12:27Z");
236+
message.setSequenceId(1L);
233237
return message;
234238
}
235239
}

src/test/java/dev/vality/hooker/utils/BuildUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static dev.vality.damsel.payment_processing.Invoice buildInvoice(String p
7171
payer.getPaymentResource().getResource()
7272
.setPaymentTool(paymentTool);
7373
}
74+
invoice.setLatestEventId(1);
7475
return invoice;
7576
}
7677

0 commit comments

Comments
 (0)