|
7 | 7 | import java.util.Optional;
|
8 | 8 | import java.util.concurrent.ExecutionException;
|
9 | 9 |
|
| 10 | +import it.gov.pagopa.paymentupdater.restclient.proxy.model.PaymentStatusFaultPaymentProblemJson; |
10 | 11 | import org.springframework.beans.factory.annotation.Autowired;
|
11 | 12 | import org.springframework.beans.factory.annotation.Qualifier;
|
12 | 13 | import org.springframework.beans.factory.annotation.Value;
|
13 | 14 | import org.springframework.http.HttpStatus;
|
14 | 15 | import org.springframework.kafka.core.KafkaTemplate;
|
15 | 16 | import org.springframework.stereotype.Service;
|
16 | 17 | import org.springframework.transaction.annotation.Transactional;
|
| 18 | +import org.springframework.web.client.HttpClientErrorException; |
17 | 19 | import org.springframework.web.client.HttpServerErrorException;
|
| 20 | +import org.springframework.web.client.HttpStatusCodeException; |
18 | 21 | import org.springframework.web.client.RestTemplate;
|
19 | 22 |
|
20 | 23 | import com.fasterxml.jackson.core.JsonProcessingException;
|
|
26 | 29 | import it.gov.pagopa.paymentupdater.model.Payment;
|
27 | 30 | import it.gov.pagopa.paymentupdater.producer.PaymentProducer;
|
28 | 31 | import it.gov.pagopa.paymentupdater.repository.PaymentRepository;
|
29 |
| -import it.gov.pagopa.paymentupdater.restclient.proxy.ApiClient; |
30 | 32 | import it.gov.pagopa.paymentupdater.restclient.proxy.api.DefaultApi;
|
31 | 33 | import it.gov.pagopa.paymentupdater.restclient.proxy.model.PaymentRequestsGetResponse;
|
32 |
| -import it.gov.pagopa.paymentupdater.util.Constants; |
33 | 34 | import it.gov.pagopa.paymentupdater.util.PaymentUtil;
|
34 | 35 | import lombok.extern.slf4j.Slf4j;
|
35 | 36 |
|
|
38 | 39 | @Slf4j
|
39 | 40 | public class PaymentServiceImpl implements PaymentService {
|
40 | 41 |
|
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 | + } |
143 | 133 |
|
144 | 134 | }
|
0 commit comments