Skip to content

Commit 5d928ab

Browse files
Merge pull request #49 from pagopa/fixes-api
2 parents 174e0e5 + cd061ff commit 5d928ab

File tree

4 files changed

+144
-163
lines changed

4 files changed

+144
-163
lines changed

src/main/java/it/gov/pagopa/paymentupdater/consumer/MessageKafkaConsumer.java

+36-36
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,50 @@
2222
@Slf4j
2323
public class MessageKafkaConsumer {
2424

25-
@Autowired
26-
PaymentService paymentService;
25+
@Autowired
26+
PaymentService paymentService;
2727

28-
@Autowired
29-
PaymentServiceImpl paymentServiceImpl;
28+
@Autowired
29+
PaymentServiceImpl paymentServiceImpl;
3030

31-
private CountDownLatch latch = new CountDownLatch(1);
32-
private String payload = null;
31+
private CountDownLatch latch = new CountDownLatch(1);
32+
private String payload = null;
3333

34-
@KafkaListener(topics = "${kafka.message}", groupId = "consumer-message", containerFactory = "kafkaListenerContainerFactory", autoStartup = "${message.auto.start}")
35-
public void messageKafkaListener(Payment paymentMessage)
36-
throws JsonProcessingException, InterruptedException, ExecutionException {
37-
log.debug("Processing messageId=" + paymentMessage.getId() + " time=" + new Date().toString()
38-
+ " paymentMessageContentType= " + paymentMessage.getContent_type());
39-
if (Objects.nonNull(paymentMessage.getContent_type())
40-
&& paymentMessage.getContent_type().equals(MessageContentType.PAYMENT)) {
41-
log.debug("Received message with id: {} ", paymentMessage.getId());
42-
checkNullInMessage(paymentMessage);
43-
payload = paymentMessage.toString();
34+
@KafkaListener(topics = "${kafka.message}", groupId = "consumer-message", containerFactory = "kafkaListenerContainerFactory", autoStartup = "${message.auto.start}")
35+
public void messageKafkaListener(Payment paymentMessage)
36+
throws JsonProcessingException, InterruptedException, ExecutionException {
37+
log.debug("Processing messageId=" + paymentMessage.getId() + " time=" + new Date().toString()
38+
+ " paymentMessageContentType= " + paymentMessage.getContent_type());
39+
if (Objects.nonNull(paymentMessage.getContent_type())
40+
&& paymentMessage.getContent_type().equals(MessageContentType.PAYMENT)) {
41+
log.debug("Received message with id: {} ", paymentMessage.getId());
42+
checkNullInMessage(paymentMessage);
43+
payload = paymentMessage.toString();
4444

45-
if (paymentService.countById(paymentMessage.getId()) == 0) {
45+
if (paymentService.countById(paymentMessage.getId()) == 0) {
4646

47-
String rptId = paymentMessage.getContent_paymentData_payeeFiscalCode()
48-
.concat(paymentMessage.getContent_paymentData_noticeNumber());
49-
paymentMessage.setRptId(rptId);
50-
ProxyResponse proxyResponse = paymentServiceImpl.checkPayment(paymentMessage);
51-
if (!proxyResponse.isPaid()) {
52-
PaymentUtil.checkDueDateForPayment(proxyResponse.getDueDate(), paymentMessage);
53-
} else {
54-
paymentMessage.setPaidFlag(proxyResponse.isPaid());
47+
String rptId = paymentMessage.getContent_paymentData_payeeFiscalCode()
48+
.concat(paymentMessage.getContent_paymentData_noticeNumber());
49+
paymentMessage.setRptId(rptId);
50+
ProxyResponse proxyResponse = paymentServiceImpl.checkPayment(paymentMessage);
51+
if (proxyResponse.isPaid()) {
52+
paymentMessage.setPaidFlag(true);
53+
} else {
54+
PaymentUtil.checkDueDateForPayment(proxyResponse.getDueDate(), paymentMessage);
5555
}
56-
paymentService.save(paymentMessage);
57-
}
58-
}
56+
paymentService.save(paymentMessage);
57+
}
58+
}
5959

60-
this.latch.countDown();
61-
}
60+
this.latch.countDown();
61+
}
6262

63-
public CountDownLatch getLatch() {
64-
return latch;
65-
}
63+
public CountDownLatch getLatch() {
64+
return latch;
65+
}
6666

67-
public String getPayload() {
68-
return payload;
69-
}
67+
public String getPayload() {
68+
return payload;
69+
}
7070

7171
}

src/main/java/it/gov/pagopa/paymentupdater/dto/request/ProxyPaymentResponse.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import lombok.Getter;
44
import lombok.Setter;
55

6-
@Getter @Setter
6+
@Getter
7+
@Setter
78
public class ProxyPaymentResponse {
8-
9-
private String importoSingoloVersamento;
10-
private String codiceContestoPagamento;
11-
private String type;
12-
private String title;
13-
private int status;
14-
private String detail;
15-
private String detail_v2;
16-
private String instance;
17-
private String duedate;
9+
10+
private String type;
11+
private String title;
12+
private int status;
13+
private String detail;
14+
private String detailV2;
15+
private String instance;
1816

1917
}

src/main/java/it/gov/pagopa/paymentupdater/service/PaymentServiceImpl.java

+94-104
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import java.util.Optional;
88
import java.util.concurrent.ExecutionException;
99

10+
import it.gov.pagopa.paymentupdater.restclient.proxy.model.PaymentStatusFaultPaymentProblemJson;
1011
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.beans.factory.annotation.Qualifier;
1213
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.kafka.core.KafkaTemplate;
1516
import org.springframework.stereotype.Service;
1617
import org.springframework.transaction.annotation.Transactional;
18+
import org.springframework.web.client.HttpClientErrorException;
1719
import org.springframework.web.client.HttpServerErrorException;
20+
import org.springframework.web.client.HttpStatusCodeException;
1821
import org.springframework.web.client.RestTemplate;
1922

2023
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -26,10 +29,8 @@
2629
import it.gov.pagopa.paymentupdater.model.Payment;
2730
import it.gov.pagopa.paymentupdater.producer.PaymentProducer;
2831
import it.gov.pagopa.paymentupdater.repository.PaymentRepository;
29-
import it.gov.pagopa.paymentupdater.restclient.proxy.ApiClient;
3032
import it.gov.pagopa.paymentupdater.restclient.proxy.api.DefaultApi;
3133
import it.gov.pagopa.paymentupdater.restclient.proxy.model.PaymentRequestsGetResponse;
32-
import it.gov.pagopa.paymentupdater.util.Constants;
3334
import it.gov.pagopa.paymentupdater.util.PaymentUtil;
3435
import lombok.extern.slf4j.Slf4j;
3536

@@ -38,107 +39,96 @@
3839
@Slf4j
3940
public class PaymentServiceImpl implements PaymentService {
4041

41-
@Autowired
42-
PaymentRepository paymentRepository;
43-
@Autowired
44-
ObjectMapper mapper;
45-
@Autowired
46-
RestTemplate restTemplate;
47-
@Value("${kafka.paymentupdates}")
48-
private String topic;
49-
@Value("${enable_rest_key}")
50-
private boolean enableRestKey;
51-
@Value("${proxy_endpoint_subscription_key}")
52-
private String proxyEndpointKey;
53-
54-
@Autowired
55-
PaymentProducer producer;
56-
@Autowired
57-
DefaultApi defaultApi;
58-
59-
@Autowired
60-
@Qualifier("kafkaTemplatePayments")
61-
private KafkaTemplate<String, String> kafkaTemplatePayments;
62-
63-
@Override
64-
public void save(Payment reminder) {
65-
paymentRepository.save(reminder);
66-
log.info("Saved payment id: {}", reminder.getId());
67-
}
68-
69-
@Override
70-
71-
public ProxyResponse checkPayment(Payment payment)
72-
throws JsonProcessingException, InterruptedException, ExecutionException {
73-
ProxyResponse proxyResp = new ProxyResponse();
74-
try {
75-
ApiClient apiClient = new ApiClient();
76-
if (enableRestKey) {
77-
apiClient.addDefaultHeader("Ocp-Apim-Subscription-Key", proxyEndpointKey);
78-
}
79-
80-
defaultApi.setApiClient(apiClient);
81-
PaymentRequestsGetResponse resp = defaultApi.getPaymentInfo(payment.getRptId());
82-
83-
LocalDate dueDate = PaymentUtil.getLocalDateFromString(resp.getDueDate());
84-
proxyResp.setDueDate(dueDate);
85-
86-
return proxyResp;
87-
88-
}
89-
90-
catch (HttpServerErrorException errorException) {
91-
// the reminder is already paid
92-
ProxyPaymentResponse res = mapper.readValue(errorException.getResponseBodyAsString(),
93-
ProxyPaymentResponse.class);
94-
if (res.getDetail_v2() != null) {
95-
if (Arrays.asList("PAA_PAGAMENTO_DUPLICATO", "PPT_RPT_DUPLICATA").contains(res.getDetail_v2())
96-
&& errorException.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
97-
98-
List<Payment> payments = paymentRepository.getPaymentByRptId(payment.getRptId());
99-
payments.add(payment);
100-
for (Payment pay : payments) {
101-
pay.setPaidFlag(true);
102-
LocalDate proxyDate = PaymentUtil.getLocalDateFromString(res.getDuedate());
103-
PaymentUtil.checkDueDateForPayment(proxyDate, pay);
104-
paymentRepository.save(pay);
105-
106-
PaymentMessage message = new PaymentMessage();
107-
message.setMessageId(pay.getId());
108-
message.setFiscalCode(pay.getFiscalCode());
109-
message.setNoticeNumber(payment.getContent_paymentData_noticeNumber());
110-
message.setPayeeFiscalCode(payment.getContent_paymentData_payeeFiscalCode());
111-
message.setSource("payments");
112-
PaymentUtil.checkDueDateForPaymentMessage(res.getDuedate(), message);
113-
producer.sendPaymentUpdate(mapper.writeValueAsString(message), kafkaTemplatePayments, topic);
114-
}
115-
116-
proxyResp.setPaid(true);
117-
proxyResp.setDueDate(PaymentUtil.getLocalDateFromString(res.getDuedate()));
118-
return proxyResp;
119-
}
120-
proxyResp.setPaid(false);
121-
return proxyResp;
122-
} else {
123-
throw errorException;
124-
}
125-
}
126-
}
127-
128-
@Override
129-
public Optional<Payment> findById(String messageId) {
130-
return paymentRepository.findById(messageId);
131-
}
132-
133-
@Override
134-
public List<Payment> getPaymentsByRptid(String rptid) {
135-
List<Payment> payments = paymentRepository.getPaymentByRptId(rptid);
136-
return payments == null ? new ArrayList<>() : payments;
137-
}
138-
139-
@Override
140-
public int countById(String id) {
141-
return paymentRepository.countById(id);
142-
}
42+
@Autowired
43+
PaymentRepository paymentRepository;
44+
@Autowired
45+
ObjectMapper mapper;
46+
@Autowired
47+
RestTemplate restTemplate;
48+
@Value("${kafka.paymentupdates}")
49+
private String topic;
50+
@Value("${enable_rest_key}")
51+
private boolean enableRestKey;
52+
@Value("${proxy_endpoint_subscription_key}")
53+
private String proxyEndpointKey;
54+
55+
@Autowired
56+
PaymentProducer producer;
57+
@Autowired
58+
DefaultApi defaultApi;
59+
60+
@Autowired
61+
@Qualifier("kafkaTemplatePayments")
62+
private KafkaTemplate<String, String> kafkaTemplatePayments;
63+
64+
@Override
65+
public void save(Payment reminder) {
66+
paymentRepository.save(reminder);
67+
log.info("Saved payment id: {}", reminder.getId());
68+
}
69+
70+
@Override
71+
72+
public ProxyResponse checkPayment(Payment payment)
73+
throws JsonProcessingException, InterruptedException, ExecutionException {
74+
LocalDate paymentDueDate = payment.getDueDate() != null ? payment.getDueDate().toLocalDate() : null;
75+
ProxyResponse proxyResp = new ProxyResponse();
76+
try {
77+
if (enableRestKey) {
78+
defaultApi.getApiClient().addDefaultHeader("Ocp-Apim-Subscription-Key", proxyEndpointKey);
79+
}
80+
PaymentRequestsGetResponse resp = defaultApi.getPaymentInfo(payment.getRptId());
81+
LocalDate dueDate = PaymentUtil.getLocalDateFromString(resp.getDueDate());
82+
proxyResp.setDueDate(dueDate);
83+
return proxyResp;
84+
} catch (HttpStatusCodeException errorException) {
85+
PaymentStatusFaultPaymentProblemJson res = mapper.readValue(errorException.getResponseBodyAsString(),
86+
PaymentStatusFaultPaymentProblemJson.class);
87+
if (res.getDetailV2() != null) {
88+
if (Arrays.asList(HttpStatus.CONFLICT, HttpStatus.INTERNAL_SERVER_ERROR).contains(errorException.getStatusCode())
89+
&& Arrays.asList("PAA_PAGAMENTO_DUPLICATO", "PPT_RPT_DUPLICATA", "PPT_PAGAMENTO_DUPLICATO").contains(res.getDetailV2())) {
90+
// the payment message is already paid
91+
List<Payment> payments = paymentRepository.getPaymentByRptId(payment.getRptId());
92+
payments.add(payment);
93+
for (Payment pay : payments) {
94+
pay.setPaidFlag(true);
95+
paymentRepository.save(pay);
96+
97+
PaymentMessage message = new PaymentMessage();
98+
message.setMessageId(pay.getId());
99+
message.setFiscalCode(pay.getFiscalCode());
100+
message.setNoticeNumber(payment.getContent_paymentData_noticeNumber());
101+
message.setPayeeFiscalCode(payment.getContent_paymentData_payeeFiscalCode());
102+
message.setSource("payments");
103+
producer.sendPaymentUpdate(mapper.writeValueAsString(message), kafkaTemplatePayments, topic);
104+
}
105+
proxyResp.setPaid(true);
106+
proxyResp.setDueDate(paymentDueDate);
107+
return proxyResp;
108+
}
109+
proxyResp.setPaid(false);
110+
proxyResp.setDueDate(paymentDueDate);
111+
return proxyResp;
112+
} else {
113+
throw errorException;
114+
}
115+
}
116+
}
117+
118+
@Override
119+
public Optional<Payment> findById(String messageId) {
120+
return paymentRepository.findById(messageId);
121+
}
122+
123+
@Override
124+
public List<Payment> getPaymentsByRptid(String rptid) {
125+
List<Payment> payments = paymentRepository.getPaymentByRptId(rptid);
126+
return payments == null ? new ArrayList<>() : payments;
127+
}
128+
129+
@Override
130+
public int countById(String id) {
131+
return paymentRepository.countById(id);
132+
}
143133

144134
}

src/test/java/it/gov/pagopa/paymentupdater/AbstractMock.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,24 @@ public void mockGetPaymentInfo() {
119119
}
120120

121121
public void mockGetPaymentInfoIsPaidTrue() throws JsonProcessingException {
122-
HttpServerErrorException errorResponse = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
122+
HttpServerErrorException errorResponse = new HttpServerErrorException(HttpStatus.CONFLICT, "",
123123
mapper.writeValueAsString(getProxyResponse()).getBytes(), Charset.defaultCharset());
124124

125125
Mockito.when(mockDefaultApi.getPaymentInfo(Mockito.anyString())).thenThrow(errorResponse);
126126
}
127127

128128
public void mockGetPaymentInfoIsNotPaid(String responseDetail) throws JsonProcessingException {
129129
ProxyPaymentResponse proxyResponse = getProxyResponse();
130-
proxyResponse.setDetail_v2(responseDetail);
130+
proxyResponse.setDetailV2(responseDetail);
131131
HttpServerErrorException errorResponse = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
132132
mapper.writeValueAsString(proxyResponse).getBytes(), Charset.defaultCharset());
133133

134134
Mockito.when(mockDefaultApi.getPaymentInfo(Mockito.anyString())).thenThrow(errorResponse);
135135
}
136136

137-
public void mockGetPaymentInfoIsNotPaid() throws JsonProcessingException {
138-
mockGetPaymentInfoIsNotPaid("PAA_PAGAMNETO_ANNULLATO");
139-
}
140-
141137
public void mockGetPaymentInfoError() throws JsonProcessingException {
142138
ProxyPaymentResponse proxyResponse = getProxyResponse();
143-
proxyResponse.setDetail_v2(null);
139+
proxyResponse.setDetailV2(null);
144140
HttpServerErrorException errorResponse = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
145141
mapper.writeValueAsString(proxyResponse).getBytes(), Charset.defaultCharset());
146142

@@ -149,8 +145,7 @@ public void mockGetPaymentInfoError() throws JsonProcessingException {
149145

150146
protected Payment selectReminderMockObject(String type, String id, String contentType, String fiscalCode,
151147
int numReminder, String rptId, String paymentDataNoticeNumber, String paymentDataFiscalCode) {
152-
Payment returnReminder1 = null;
153-
returnReminder1 = new Payment();
148+
Payment returnReminder1 = new Payment();
154149
returnReminder1.setId(id);
155150
returnReminder1.setContent_type(MessageContentType.valueOf(contentType));
156151
returnReminder1.setFiscalCode(fiscalCode);
@@ -202,9 +197,7 @@ protected String selectPaymentMessageObject(String type, String messageId, Strin
202197

203198
protected ProxyPaymentResponse getProxyResponse() {
204199
ProxyPaymentResponse paymentResponse = new ProxyPaymentResponse();
205-
paymentResponse.setCodiceContestoPagamento("");
206-
paymentResponse.setImportoSingoloVersamento("20");
207-
paymentResponse.setDetail_v2("PPT_RPT_DUPLICATA");
200+
paymentResponse.setDetailV2("PPT_RPT_DUPLICATA");
208201
paymentResponse.setDetail("");
209202
paymentResponse.setInstance("");
210203
paymentResponse.setStatus(500);

0 commit comments

Comments
 (0)