Skip to content

Commit

Permalink
feat: Changed retrieval logic of PSP configuration from DB to API (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsabacn authored Mar 6, 2023
1 parent ded5b01 commit ae6b01c
Show file tree
Hide file tree
Showing 35 changed files with 11,697 additions and 567 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
<version>4.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
<version>1.72</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public final class ErrorCode {
// integration error node - rest client
public static final String ERROR_CALLING_NODE_REST_SERVICES = MODULE_ID + "000029";

public static final String ERROR_CALLING_MIL_REST_SERVICES = MODULE_ID + "00002A";


private ErrorCode() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.gov.pagopa.swclient.mil.paymentnotice.client;

import io.smallrye.mutiny.Uni;
import it.gov.pagopa.swclient.mil.paymentnotice.client.bean.AcquirerConfiguration;
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
* Reactive rest client for the REST APIs exposed by the MIL APIM
*/
@RegisterRestClient(configKey = "mil-rest-api")
public interface MilRestService {

/**
* Retrieves the psp configuration
* @param requestId the id of the request
* @param acquirerId the acquirer id passed in request
* @return the psp configuration for the acquirer id
*/
@GET
@Path("/mil-acquirer-conf/confs/{acquirerId}/psp")
@ClientHeaderParam(name = "Ocp-Apim-Subscription-Key", value = "${mil-rest-client.apim-subscription-key}", required = false)
@ClientHeaderParam(name = "Version", value = "${mil-rest-client.mil-acquirer-conf.version}", required = false)
Uni<AcquirerConfiguration> getPspConfiguration(@HeaderParam(value = "RequestId") String requestId, @PathParam(value = "acquirerId") String acquirerId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package it.gov.pagopa.swclient.mil.paymentnotice.client.bean;

/**
*
*/
public class AcquirerConfiguration {

/**
* The psp configuration to be used when calling the "verify" and "activate" APIs of the node
*/
private PspConfiguration pspConfigForVerifyAndActivate;

/**
* The psp configuration to be used when calling the "close" API of the node and the "getFee" api of GEC
*/
private PspConfiguration pspConfigForGetFeeAndClosePayment;

/**
* Gets pspConfigForVerifyAndActivate
*
* @return value of pspConfigForVerifyAndActivate
*/
public PspConfiguration getPspConfigForVerifyAndActivate() {
return pspConfigForVerifyAndActivate;
}

/**
* Sets pspConfigForVerifyAndActivate
*
* @param pspConfigForVerifyAndActivate value of pspConfigForVerifyAndActivate
*/
public void setPspConfigForVerifyAndActivate(PspConfiguration pspConfigForVerifyAndActivate) {
this.pspConfigForVerifyAndActivate = pspConfigForVerifyAndActivate;
}

/**
* Gets pspConfigForGetFeeAndClosePayment
*
* @return value of pspConfigForGetFeeAndClosePayment
*/
public PspConfiguration getPspConfigForGetFeeAndClosePayment() {
return pspConfigForGetFeeAndClosePayment;
}

/**
* Sets pspConfigForGetFeeAndClosePayment
*
* @param pspConfigForGetFeeAndClosePayment value of pspConfigForGetFeeAndClosePayment
*/
public void setPspConfigForGetFeeAndClosePayment(PspConfiguration pspConfigForGetFeeAndClosePayment) {
this.pspConfigForGetFeeAndClosePayment = pspConfigForGetFeeAndClosePayment;
}


@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PspConfiguration{");
sb.append("pspConfigForVerifyAndActivate=").append(pspConfigForVerifyAndActivate);
sb.append(", pspConfigForGetFeeAndClosePayment=").append(pspConfigForGetFeeAndClosePayment);
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package it.gov.pagopa.swclient.mil.paymentnotice.client.bean;

/**
* Configuration of a PSP used when connecting to the node
*/
public class PspConfiguration {

/**
* Identifier of the PSP, assigned by PagoPA
*/
private String psp;

/**
* Identifier of the broker, assigned by PagoPA
*/
private String broker;

/**
* Identifier of the channel used for the payment transaction.
* Is assigned by PagoPA and is unique for the psp
*/
private String channel;

/**
* Channel's password, assigned by PagoPA
*/
private String password;


/**
* Gets psp
* @return value of psp
*/
public String getPsp() {
return psp;
}

/**
* Sets psp
* @param psp value of psp
*/
public void setPsp(String psp) {
this.psp = psp;
}

/**
* Gets broker
* @return value of broker
*/
public String getBroker() {
return broker;
}

/**
* Sets broker
* @param broker value of broker
*/
public void setBroker(String broker) {
this.broker = broker;
}

/**
* Gets channel
* @return value of channel
*/
public String getChannel() {
return channel;
}

/**
* Sets channel
* @param channel value of channel
*/
public void setChannel(String channel) {
this.channel = channel;
}

/**
* Gets password
* @return value of password
*/
public String getPassword() {
return password;
}

/**
* Sets password
* @param password value of password
*/
public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PspConfiguration{");
sb.append("psp='").append(psp).append('\'');
sb.append(", broker='").append(broker).append('\'');
sb.append(", channel='").append(channel).append('\'');
sb.append(", password='").append(password).append('\'');
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoRepository;

/**
* MongoDB repository to access PPS configuration data, reactive flavor
* MongoDB repository to access PSP configuration data, reactive flavor
*/
@ApplicationScoped
public class PspConfRepository implements ReactivePanacheMongoRepository<PspConfEntity> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PspConfiguration {
private String idChannel;

/**
* Channel's password, , assigned by PagoPA
* Channel's password, assigned by PagoPA
*/
private String pspPassword;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import it.gov.pagopa.swclient.mil.paymentnotice.client.bean.PspConfiguration;
import it.gov.pagopa.swclient.mil.paymentnotice.utils.NodeApi;
import it.gov.pagopa.swclient.mil.paymentnotice.utils.QrCode;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand All @@ -34,7 +36,6 @@
import it.gov.pagopa.swclient.mil.paymentnotice.bean.ActivatePaymentNoticeRequest;
import it.gov.pagopa.swclient.mil.paymentnotice.bean.ActivatePaymentNoticeResponse;
import it.gov.pagopa.swclient.mil.paymentnotice.bean.Outcome;
import it.gov.pagopa.swclient.mil.paymentnotice.dao.PspConfiguration;
import it.gov.pagopa.swclient.mil.paymentnotice.bean.Transfer;
import it.gov.pagopa.swclient.mil.paymentnotice.utils.PaymentNoticeConstants;

Expand Down Expand Up @@ -71,7 +72,7 @@ public Uni<Response> activateByQrCode(@Valid @BeanParam CommonHeader headers,
// parse qr-code to retrieve the notice number and the PA tax code
QrCode parsedQrCode = QrCode.parse(qrCode);

return retrievePSPConfiguration(headers.getAcquirerId()).
return retrievePSPConfiguration(headers.getRequestId(), headers.getAcquirerId(), NodeApi.ACTIVATE).
chain(pspConf -> callNodeActivatePaymentNotice(parsedQrCode.getPaTaxCode(), parsedQrCode.getNoticeNumber(),
pspConf, activatePaymentNoticeRequest));

Expand Down Expand Up @@ -103,7 +104,7 @@ public Uni<Response> activateByTaxCodeAndNoticeNumber(@Valid @BeanParam CommonHe
Log.debugf("activateByTaxCodeAndNoticeNumber - Input parameters: %s, paTaxCode: %s, noticeNumber, body: %s",
headers, paTaxCode, noticeNumber, activatePaymentNoticeRequest);

return retrievePSPConfiguration(headers.getAcquirerId()).
return retrievePSPConfiguration(headers.getRequestId(), headers.getAcquirerId(), NodeApi.VERIFY).
chain(pspConf -> callNodeActivatePaymentNotice(paTaxCode, noticeNumber, pspConf, activatePaymentNoticeRequest));
}

Expand All @@ -125,10 +126,10 @@ private Uni<Response> callNodeActivatePaymentNotice(String paTaxCode, String not
ctQrCode.setNoticeNumber(noticeNumber);

ActivatePaymentNoticeV2Request nodeActivateRequest = new ActivatePaymentNoticeV2Request();
nodeActivateRequest.setIdPSP(pspConfiguration.getPspId());
nodeActivateRequest.setIdBrokerPSP(pspConfiguration.getPspBroker());
nodeActivateRequest.setIdChannel(pspConfiguration.getIdChannel());
nodeActivateRequest.setPassword(pspConfiguration.getPspPassword());
nodeActivateRequest.setIdPSP(pspConfiguration.getPsp());
nodeActivateRequest.setIdBrokerPSP(pspConfiguration.getBroker());
nodeActivateRequest.setIdChannel(pspConfiguration.getChannel());
nodeActivateRequest.setPassword(pspConfiguration.getPassword());

nodeActivateRequest.setIdempotencyKey(activatePaymentNoticeRequest.getIdempotencyKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.swclient.mil.bean.Errors;
import it.gov.pagopa.swclient.mil.paymentnotice.ErrorCode;
import it.gov.pagopa.swclient.mil.paymentnotice.client.MilRestService;
import it.gov.pagopa.swclient.mil.paymentnotice.client.NodeForPspWrapper;
import it.gov.pagopa.swclient.mil.paymentnotice.dao.PspConfRepository;
import it.gov.pagopa.swclient.mil.paymentnotice.dao.PspConfiguration;
import it.gov.pagopa.swclient.mil.paymentnotice.client.bean.PspConfiguration;
import it.gov.pagopa.swclient.mil.paymentnotice.utils.NodeErrorMapping;
import it.gov.pagopa.swclient.mil.paymentnotice.utils.NodeApi;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.ClientWebApplicationException;

import javax.inject.Inject;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -20,45 +22,56 @@
public class BasePaymentResource {

/**
* The configuration object cotaining the mapping between
* The configuration object containing the mapping between
*/
@Inject
NodeErrorMapping nodeErrorMapping;

/**
* The async wrapper for the Node SOAP client
*/
@Inject
NodeForPspWrapper nodeWrapper;

@Inject
PspConfRepository pspConfRepository;

/**
* The reactive REST client for the MIL REST interfaces
*/
@RestClient
MilRestService milRestService;


/**
* Retrieves the PSP configuration from the database by acquirer id, and emits it as a Uni
* Retrieves the PSP configuration by acquirer id, and emits it as a Uni
*
* @param requestId the id of the request passed in request
* @param acquirerId the id of the acquirer
* @param api a {@link NodeApi} used to choose which of the two PspConfiguration return
* @return the {@link Uni} emitting a {@link PspConfiguration}
*/
protected Uni<PspConfiguration> retrievePSPConfiguration(String acquirerId) {
Log.debugf("retrievePSPConfiguration - acquirerId: %s ", acquirerId);
protected Uni<PspConfiguration> retrievePSPConfiguration(String requestId, String acquirerId, NodeApi api) {
Log.debugf("retrievePSPConfiguration - requestId: %s acquirerId: %s ", requestId, acquirerId);

return pspConfRepository.findByIdOptional(acquirerId)
.onFailure().transform(t -> {
Log.errorf(t, "[%s] Error retrieving data from the db", ErrorCode.ERROR_RETRIEVING_DATA_FROM_MONGO);
return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(List.of(ErrorCode.ERROR_RETRIEVING_DATA_FROM_MONGO)))
.build());
})
.onItem().transform(o -> o.orElseThrow(NotFoundException::new))
return milRestService.getPspConfiguration(requestId, acquirerId)
.onFailure().transform(t -> {
Log.errorf(t, "[%s] Error retrieving data from the db", ErrorCode.UNKNOWN_ACQUIRER_ID);
return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(List.of(ErrorCode.UNKNOWN_ACQUIRER_ID)))
.build());
if (t instanceof ClientWebApplicationException webEx && webEx.getResponse().getStatus() == 404) {
Log.errorf(t, "[%s] Missing psp configuration for acquirerId", ErrorCode.UNKNOWN_ACQUIRER_ID);
return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(List.of(ErrorCode.UNKNOWN_ACQUIRER_ID)))
.build());
}
else {
Log.errorf(t, "[%s] Error retrieving the psp configuration", ErrorCode.ERROR_CALLING_MIL_REST_SERVICES);
return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(List.of(ErrorCode.ERROR_CALLING_MIL_REST_SERVICES)))
.build());
}
})
.map(t -> t.pspConfiguration);

.map(acquirerConfiguration -> switch (api) {
case ACTIVATE, VERIFY -> acquirerConfiguration.getPspConfigForVerifyAndActivate();
case CLOSE -> acquirerConfiguration.getPspConfigForGetFeeAndClosePayment();
});
}


Expand All @@ -74,7 +87,7 @@ protected String remapNodeFaultToOutcome(String faultCode, String originalFaultC
Integer outcomeErrorId = nodeErrorMapping.map().
get(Stream.of(faultCode, originalFaultCode)
.filter(s -> s != null && !s.isEmpty())
.collect(Collectors.joining(",")));
.collect(Collectors.joining("-")));
if (outcomeErrorId == null) {
Log.errorf("Could not find configured mapping for faultCode %s originalFaultCode %s, defaulting to UNEXPECTED_ERROR",
faultCode, originalFaultCode);
Expand Down
Loading

0 comments on commit ae6b01c

Please sign in to comment.