diff --git a/README.md b/README.md index 6bb6c8c..0d421d9 100644 --- a/README.md +++ b/README.md @@ -1 +1,21 @@ -# adapter-bank-spring-boot-starter \ No newline at end of file +### adapter-bank-spring-boot-starter + +**Adapter-bank-spring-boot-starter** - это подключаемый Spring Boot модуль для стандартных +компонентов proxy-алаптеров. + +Для корректной работы Вашего адаптера необходимо: +* подключить необходимые properties (AdapterProperties, TimerProperties). Если поля родителя + не покрывают всех необходимых бизнес-полей, то необходимо унаследоваться от родителя и + при помощи аннотаций @Configuration и @ConfigurationProperties создать соответствующий + класс конфигураций .Если поля родителя полностью покрывают case, то можно не создавать + класс-наследник, а создать соответствующий bean в конфигурации. Например: + + + @Bean + @Primary + @ConfigurationProperties("time.config") + public TimerProperties timerProperties() { + return new TimerProperties(); + } + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1c0b7b7..94fef0e 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,11 @@ hellgate-adapter-client ${hellgate-adapter-client.version} + + com.rbkmoney + adapter-common-lib + 0.0.4-SNAPSHOT + diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/AppConfig.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/AppConfig.java new file mode 100644 index 0000000..e43efc2 --- /dev/null +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/AppConfig.java @@ -0,0 +1,39 @@ +package com.rbkmoney.adapter.bank.spring.boot.starter.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.rbkmoney.adapter.common.component.NetworkFilterComponent; +import com.rbkmoney.adapter.common.mapper.SimpleErrorMapping; +import com.rbkmoney.adapter.common.mapper.SimpleObjectMapper; +import com.rbkmoney.error.mapping.ErrorMapping; +import org.apache.catalina.connector.Connector; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.server.ServletWebServerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; + +import java.io.IOException; + +@Configuration +public class AppConfig { + + @Value("${error-mapping.file}") + private Resource errorMappingFilePath; + + @Value("${error-mapping.patternReason:\"'%s' - '%s'\"}") + private String errorMappingPattern; + + @Bean + public ErrorMapping errorMapping() throws IOException { + return new SimpleErrorMapping(errorMappingFilePath, errorMappingPattern).getErrorMapping(); + } + + @Bean + public ObjectMapper objectMapper(){ + return new SimpleObjectMapper().getSimpleObjectMapper(); + } + + +} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/ErrorMappingConfiguration.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/ErrorMappingConfiguration.java deleted file mode 100644 index 754839f..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/ErrorMappingConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.config; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.error.mapping.ErrorMapping; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.Resource; - -import java.io.IOException; - -@Configuration -public class ErrorMappingConfiguration { - - @Value("${error-mapping.file}") - private Resource filePath; - - @Value("${error-mapping.patternReason:\"'%s' - '%s'\"}") - private String patternReason; - - @Bean - ErrorMapping errorMapping() throws IOException { - ObjectMapper mapper = new ObjectMapper(); - mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); - - ErrorMapping errorMapping = new ErrorMapping(filePath.getInputStream(), patternReason, mapper); - errorMapping.validateMappingFormat(); - return errorMapping; - } - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/FilterConfiguration.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/FilterConfiguration.java deleted file mode 100644 index 0bde5a1..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/FilterConfiguration.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.config; - -import com.rbkmoney.woody.api.flow.WFlow; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.filter.OncePerRequestFilter; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -@Configuration -public class FilterConfiguration { - - public static final String HEALTH = "/actuator/health"; - - @Value("${server.rest.port}") - private int restPort; - - @Value("/${server.rest.endpoint}/") - private String restEndpoint; - - @Bean - public FilterRegistrationBean externalPortRestrictingFilter() { - Filter filter = new OncePerRequestFilter() { - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, - FilterChain filterChain) throws ServletException, IOException { - String servletPath = request.getServletPath(); - if ((request.getLocalPort() == restPort) - && !(servletPath.startsWith(restEndpoint) || servletPath.startsWith(HEALTH))) { - response.sendError(404, "Unknown address"); - return; - } - filterChain.doFilter(request, response); - } - }; - - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); - filterRegistrationBean.setFilter(filter); - filterRegistrationBean.setOrder(-100); - filterRegistrationBean.setName("httpPortFilter"); - filterRegistrationBean.addUrlPatterns("/*"); - return filterRegistrationBean; - } - - @Bean - public FilterRegistrationBean woodyFilter() { - WFlow wFlow = new WFlow(); - Filter filter = new OncePerRequestFilter() { - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, - FilterChain filterChain) throws ServletException, IOException { - if ((request.getLocalPort() == restPort) - && request.getServletPath().startsWith(restEndpoint)) { - wFlow.createServiceFork(() -> { - try { - filterChain.doFilter(request, response); - } catch (IOException | ServletException e) { - sneakyThrow(e); - } - }).run(); - return; - } - filterChain.doFilter(request, response); - } - - private T sneakyThrow(Throwable t) throws E { - throw (E) t; - } - }; - - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); - filterRegistrationBean.setFilter(filter); - filterRegistrationBean.setOrder(-50); - filterRegistrationBean.setName("woodyFilter"); - filterRegistrationBean.addUrlPatterns(restEndpoint + "*"); - return filterRegistrationBean; - } -} \ No newline at end of file diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/LoggingInterceptor.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/LoggingInterceptor.java deleted file mode 100644 index e4b5d55..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/LoggingInterceptor.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.config; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpRequest; -import org.springframework.http.HttpStatus; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.util.StreamUtils; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.atomic.AtomicInteger; - -@Slf4j -public class LoggingInterceptor implements ClientHttpRequestInterceptor { - - private AtomicInteger requestNumberSequence = new AtomicInteger(0); - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - int requestNumber = requestNumberSequence.incrementAndGet(); - logRequest(requestNumber, request, body); - ClientHttpResponse response = execution.execute(request, body); - response = new BufferedClientHttpResponse(response); - logResponse(requestNumber, response); - return response; - } - - private void logRequest(int requestNumber, HttpRequest request, byte[] body) { - if (log.isDebugEnabled()) { - String prefix = requestNumber + " > "; - log.debug("{} Request: {} {}", prefix, request.getMethod(), request.getURI()); - log.debug("{} Headers: {}", prefix, request.getHeaders()); - if (body.length > 0) { - log.debug("{} Body: \n{}", prefix, new String(body, StandardCharsets.UTF_8)); - } - } - } - - private void logResponse(int requestNumber, ClientHttpResponse response) throws IOException { - if (log.isDebugEnabled()) { - String prefix = requestNumber + " < "; - log.debug("{} Response: {} {} {}", prefix, response.getStatusCode(), response.getStatusCode().name(), response.getStatusText()); - log.debug("{} Headers: {}", prefix, response.getHeaders()); - String body = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); - if (body.length() > 0) { - log.debug("{} Body: \n{}", prefix, body); - } - } - } - - /** - * Wrapper around ClientHttpResponse, buffers the body so it can be read repeatedly (for logging & consuming the result). - */ - private class BufferedClientHttpResponse implements ClientHttpResponse { - - private final ClientHttpResponse response; - private byte[] body; - - public BufferedClientHttpResponse(ClientHttpResponse response) { - this.response = response; - } - - @Override - public HttpStatus getStatusCode() throws IOException { - return response.getStatusCode(); - } - - @Override - public int getRawStatusCode() throws IOException { - return response.getRawStatusCode(); - } - - @Override - public String getStatusText() throws IOException { - return response.getStatusText(); - } - - @Override - public void close() { - response.close(); - } - - @Override - public InputStream getBody() throws IOException { - if (body == null) { - body = StreamUtils.copyToByteArray(response.getBody()); - } - return new ByteArrayInputStream(body); - } - - @Override - public HttpHeaders getHeaders() { - return response.getHeaders(); - } - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/NetworkConfig.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/NetworkConfig.java new file mode 100644 index 0000000..6a1cfa6 --- /dev/null +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/NetworkConfig.java @@ -0,0 +1,40 @@ +package com.rbkmoney.adapter.bank.spring.boot.starter.config; + +import com.rbkmoney.adapter.common.component.NetworkFilterComponent; +import org.apache.catalina.connector.Connector; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.server.ServletWebServerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class NetworkConfig { + + @Value("${server.rest.port}") + private int restPort; + + @Value("/${server.rest.endpoint}/") + private String restEndpoint; + + @Bean + public FilterRegistrationBean externalPortRestrictingFilter() { + return new NetworkFilterComponent().externalPortRestrictingFilter(restPort, restEndpoint); + } + + @Bean + public FilterRegistrationBean woodyFilter() { + return new NetworkFilterComponent().woodyFilter(restPort, restEndpoint); + } + + @Bean + public ServletWebServerFactory servletContainer() { + TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); + Connector connector = new Connector(); + connector.setPort(restPort); + tomcat.addAdditionalTomcatConnectors(connector); + return tomcat; + } + +} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/SerializerConfig.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/SerializerConfig.java new file mode 100644 index 0000000..1f601e7 --- /dev/null +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/SerializerConfig.java @@ -0,0 +1,30 @@ +package com.rbkmoney.adapter.bank.spring.boot.starter.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.rbkmoney.adapter.common.serializer.AdapterSerializer; +import com.rbkmoney.adapter.common.serializer.CallbackSerializer; +import com.rbkmoney.adapter.common.serializer.RecTokenSerializer; +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@RequiredArgsConstructor +public class SerializerConfig { + + @Bean + public CallbackSerializer callbackSerializer(ObjectMapper objectMapper) { + return new CallbackSerializer(objectMapper); + } + + @Bean + public AdapterSerializer adapterSerializer(ObjectMapper objectMapper) { + return new AdapterSerializer(objectMapper); + } + + @Bean + public RecTokenSerializer recTokenSerializer(ObjectMapper objectMapper) { + return new RecTokenSerializer(objectMapper); + } + +} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/properties/AdapterProperties.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/properties/AdapterProperties.java deleted file mode 100644 index d2c2443..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/properties/AdapterProperties.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.config.properties; - -import lombok.Getter; -import lombok.Setter; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import org.springframework.validation.annotation.Validated; - -import javax.validation.constraints.NotEmpty; - -@Getter -@Setter -@Configuration -@ConfigurationProperties("adapter") -@Validated -public class AdapterProperties { - - @NotEmpty - private String url; - - @NotEmpty - private String callbackUrl; - - @NotEmpty - private String pathCallbackUrl; - - @NotEmpty - private String pathRecurrentCallbackUrl; - - @NotEmpty - private String tagPrefix; - -} \ No newline at end of file diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/properties/TimerProperties.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/properties/TimerProperties.java deleted file mode 100644 index bc5f475..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/config/properties/TimerProperties.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.config.properties; - -import lombok.Getter; -import lombok.Setter; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import org.springframework.validation.annotation.Validated; - -import javax.validation.constraints.NotNull; - -@Configuration -@ConfigurationProperties("time.config") -@Validated -@Getter -@Setter -public class TimerProperties { - - @NotNull - private int redirectTimeout; - - @NotNull - private int maxTimePolling; - - @NotNull - private int pollingDelay; - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/constants/TargetStatus.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/constants/TargetStatus.java deleted file mode 100644 index ea5f84f..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/constants/TargetStatus.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.constants; - -public enum TargetStatus { - - PROCESSED, - CAPTURED, - CANCELLED, - REFUNDED, - AUTH_RECURRENT - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/constants/ThreeDsFields.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/constants/ThreeDsFields.java deleted file mode 100644 index d2c727d..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/constants/ThreeDsFields.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.constants; - -public class ThreeDsFields { - - public static final String MD = "MD"; - public static final String PA_REQ = "PaReq"; - public static final String PA_RES = "PaRes"; - public static final String TERM_URL = "TermUrl"; - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/controller/AdapterController.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/controller/AdapterController.java deleted file mode 100644 index a759857..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/controller/AdapterController.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.controller; - -import com.rbkmoney.adapter.bank.spring.boot.starter.model.Callback; -import com.rbkmoney.adapter.bank.spring.boot.starter.serializer.CallbackSerializer; -import com.rbkmoney.adapter.helpers.hellgate.HellgateAdapterClient; -import com.rbkmoney.adapter.helpers.hellgate.exception.HellgateException; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.function.BiFunction; - - -/** - * Handler callback 3DS - */ -@Slf4j -@RequiredArgsConstructor -@RestController -@RequestMapping("/${server.rest.endpoint}") -public class AdapterController { - - private final HellgateAdapterClient hgClient; - private final CallbackSerializer callbackSerializer; - - @PostMapping(value = "term_url") - public String receivePaymentIncomingParameters(HttpServletRequest request, HttpServletResponse servletResponse) throws IOException { - return processCallback(request, servletResponse, hgClient::processPaymentCallback); - } - - @PostMapping(value = "rec_term_url") - public String receiveRecurrentIncomingParameters(HttpServletRequest request, HttpServletResponse servletResponse) throws IOException { - return processCallback(request, servletResponse, hgClient::processRecurrentTokenCallback); - } - - private String processCallback(HttpServletRequest servletRequest, HttpServletResponse servletResponse, BiFunction hgFunction) throws IOException { - String resp = ""; - Callback callbackObj = callbackSerializer.read(servletRequest); - log.info("ProcessCallback {}", callbackObj); - try { - String tag = callbackObj.getMd(); - ByteBuffer callback = ByteBuffer.wrap(callbackSerializer.writeByte(callbackObj)); - ByteBuffer response = hgFunction.apply(tag, callback); - resp = new String(response.array(), StandardCharsets.UTF_8); - } catch (HellgateException e) { - log.warn("Failed handle callback for recurrent", e); - } catch (Exception e) { - log.error("Failed handle callback for recurrent", e); - } - if (StringUtils.hasText(callbackObj.getTermUrl())) { - servletResponse.sendRedirect(callbackObj.getTermUrl()); - } - log.info("ProcessCallback response {}", resp); - return resp; - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/controller/AdapterControllerDecorator.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/controller/AdapterControllerDecorator.java new file mode 100644 index 0000000..5784dbb --- /dev/null +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/controller/AdapterControllerDecorator.java @@ -0,0 +1,39 @@ +package com.rbkmoney.adapter.bank.spring.boot.starter.controller; + +import com.rbkmoney.adapter.common.controller.AdapterController; +import com.rbkmoney.adapter.common.serializer.CallbackSerializer; +import com.rbkmoney.adapter.helpers.hellgate.HellgateAdapterClient; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** Handler callback 3DS */ +@Slf4j +@RestController +@RequestMapping("/${server.rest.endpoint}") +public class AdapterControllerDecorator { + + private AdapterController adapterController; + + public AdapterControllerDecorator(HellgateAdapterClient hgClient, CallbackSerializer callbackSerializer) { + adapterController = new AdapterController(hgClient, callbackSerializer); + } + + @PostMapping(value = "term_url") + public String receivePaymentIncomingParameters(HttpServletRequest servletRequest, + HttpServletResponse servletResponse) throws IOException { + return adapterController.receivePaymentIncomingParameters(servletRequest, servletResponse); + } + + @PostMapping(value = "rec_term_url") + public String receiveRecurrentIncomingParameters(HttpServletRequest servletRequest, + HttpServletResponse servletResponse) throws IOException { + return adapterController.receiveRecurrentIncomingParameters(servletRequest, servletResponse); + } + +} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/exception/UnknownTargetStatusException.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/exception/UnknownTargetStatusException.java deleted file mode 100644 index 9d34125..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/exception/UnknownTargetStatusException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.exception; - -public class UnknownTargetStatusException extends RuntimeException { - - public UnknownTargetStatusException() { - super("Unknown target status!"); - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/exception/UnsupportedMethodException.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/exception/UnsupportedMethodException.java deleted file mode 100644 index a5b5321..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/exception/UnsupportedMethodException.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.exception; - -import org.apache.thrift.TException; - -public class UnsupportedMethodException extends TException { - - public UnsupportedMethodException() { - super("Unsupported method"); - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImpl.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImpl.java index 6519011..376589d 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImpl.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImpl.java @@ -1,10 +1,9 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.flow; import com.rbkmoney.adapter.bank.spring.boot.starter.model.*; -import org.springframework.stereotype.Component; +import com.rbkmoney.adapter.common.enums.Step; -@Component -public class DefaultStepResolverImpl implements StepResolver { +public class DefaultStepResolverImpl implements StepResolver { @Override public Step resolveEntry(StateModel stateModel) { @@ -17,8 +16,6 @@ public class DefaultStepResolverImpl implements StepResolver { +public interface StepResolver { Step resolveEntry(T stateModel); - Step resolveExit(R stateModel); + Step resolveExit(T stateModel); } diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/TargetStatusResolver.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/TargetStatusResolver.java deleted file mode 100644 index c473799..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/TargetStatusResolver.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.flow; - -import com.rbkmoney.adapter.bank.spring.boot.starter.constants.TargetStatus; -import com.rbkmoney.adapter.bank.spring.boot.starter.exception.UnknownTargetStatusException; -import com.rbkmoney.damsel.domain.TargetInvoicePaymentStatus; -import org.springframework.stereotype.Component; - -@Component -public class TargetStatusResolver { - - public TargetStatus resolve(TargetInvoicePaymentStatus target) { - if (target != null) { - if (target.isSetCaptured()) { - return TargetStatus.CAPTURED; - } else if (target.isSetProcessed()) { - return TargetStatus.PROCESSED; - } else if (target.isSetRefunded()) { - return TargetStatus.REFUNDED; - } else if (target.isSetCancelled()) { - return TargetStatus.CANCELLED; - } - } - throw new UnknownTargetStatusException(); - } - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/CommonHandler.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/CommonHandler.java deleted file mode 100644 index e84f0f0..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/CommonHandler.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.handler; - -import com.rbkmoney.adapter.bank.spring.boot.starter.model.EntryStateModel; -import org.apache.thrift.TException; - -public interface CommonHandler { - - boolean isHandler(final E entryStateModel); - - T handle(E context) throws TException; - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/CommonHandlerImpl.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/CommonHandlerImpl.java deleted file mode 100644 index ed8e433..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/CommonHandlerImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.handler; - -import com.rbkmoney.adapter.bank.spring.boot.starter.model.EntryStateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.processor.Processor; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.core.convert.converter.Converter; - -import java.util.function.Function; - - -@Slf4j -@RequiredArgsConstructor -public abstract class CommonHandlerImpl implements CommonHandler { - - private final Function requestFunction; - private final Converter converter; - private final Processor processor; - - @Override - public T handle(E entryStateModel) { - P request = converter.convert(entryStateModel); - R response = requestFunction.apply(request); - return processor.process(response, entryStateModel); - } - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/ServerHandlerLogDecorator.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/ServerHandlerLogDecorator.java deleted file mode 100644 index 6c6971b..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/handler/ServerHandlerLogDecorator.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.handler; - -import com.rbkmoney.damsel.proxy_provider.*; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.thrift.TException; - -import java.nio.ByteBuffer; - -import static com.rbkmoney.java.damsel.utils.extractors.ProxyProviderPackageExtractors.*; -import static com.rbkmoney.java.damsel.utils.verification.ProxyProviderVerification.isUndefinedResultOrUnavailable; - -@Slf4j -@RequiredArgsConstructor -public class ServerHandlerLogDecorator implements ProviderProxySrv.Iface { - - private final ProviderProxySrv.Iface handler; - - @Override - public RecurrentTokenProxyResult generateToken(RecurrentTokenContext context) throws TException { - String recurrentId = extractRecurrentId(context); - log.info("Generate token started with recurrentId {}", recurrentId); - try { - RecurrentTokenProxyResult proxyResult = handler.generateToken(context); - log.info("Generate token finished {} with recurrentId {}", proxyResult, recurrentId); - return proxyResult; - } catch (Exception ex) { - String message = "Failed handle generate token with recurrentId " + recurrentId; - logMessage(ex, message); - throw ex; - } - } - - @Override - public RecurrentTokenCallbackResult handleRecurrentTokenCallback(ByteBuffer byteBuffer, RecurrentTokenContext context) throws TException { - String recurrentId = context.getTokenInfo().getPaymentTool().getId(); - log.info("handleRecurrentTokenCallback: start with recurrentId {}", recurrentId); - RecurrentTokenCallbackResult result = handler.handleRecurrentTokenCallback(byteBuffer, context); - log.info("handleRecurrentTokenCallback end {} with recurrentId {}", result, recurrentId); - return result; - } - - @Override - public PaymentProxyResult processPayment(PaymentContext context) throws TException { - String invoiceId = extractInvoiceId(context); - String invoicePaymentStatus = extractTargetInvoicePaymentStatus(context); - log.info("Process payment handle {} start with invoiceId {}", invoicePaymentStatus, invoiceId); - try { - PaymentProxyResult proxyResult = handler.processPayment(context); - log.info("Process payment handle {} finished with invoiceId {} and proxyResult {}", invoicePaymentStatus, invoiceId, proxyResult); - return proxyResult; - } catch (Exception e) { - String message = String.format("Failed handle %s process payment for operation with invoiceId %s", invoicePaymentStatus, invoiceId); - logMessage(e, message); - throw e; - } - } - - @Override - public PaymentCallbackResult handlePaymentCallback(ByteBuffer byteBuffer, PaymentContext context) throws TException { - String invoiceId = context.getPaymentInfo().getInvoice().getId(); - log.info("handlePaymentCallback start with invoiceId {}", invoiceId); - PaymentCallbackResult result = handler.handlePaymentCallback(byteBuffer, context); - log.info("handlePaymentCallback finish {} with invoiceId {}", result, invoiceId); - return result; - } - - private static void logMessage(Exception ex, String message) { - if (isUndefinedResultOrUnavailable(ex)) { - log.warn(message, ex); - } else { - log.error(message, ex); - } - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/AdapterContext.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/AdapterContext.java deleted file mode 100644 index f8897c9..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/AdapterContext.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -import java.time.Instant; - - -@Getter -@Setter -@ToString -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) -public class AdapterContext { - - @JsonProperty(value = "max_date_time_polling") - private Instant maxDateTimePolling; - private Step nextStep; - @JsonProperty(value = "pares") - private String paRes; - private String md; - @JsonProperty(value = "trx_id") - private String trxId; - -} \ No newline at end of file diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/Callback.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/Callback.java deleted file mode 100644 index 0fda8c7..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/Callback.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.model; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Data; - -@Data -public class Callback { - - @JsonProperty(value = "MD") - private String md; - @JsonProperty(value = "PaRes") - private String paRes; - @JsonProperty(value = "termination_uri") - private String termUrl; - -} \ No newline at end of file diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/EntryStateModel.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/GeneralEntryStateModel.java similarity index 86% rename from src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/EntryStateModel.java rename to src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/GeneralEntryStateModel.java index 737d53f..22ac12d 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/EntryStateModel.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/GeneralEntryStateModel.java @@ -5,7 +5,7 @@ import lombok.Data; @Data @Builder -public class EntryStateModel { +public class GeneralEntryStateModel { private OperationModel operationModel; private StateModel stateModel; diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/ExitStateModel.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/GeneralExitStateModel.java similarity index 68% rename from src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/ExitStateModel.java rename to src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/GeneralExitStateModel.java index adf6370..71fb502 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/ExitStateModel.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/GeneralExitStateModel.java @@ -1,5 +1,7 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.model; +import com.rbkmoney.adapter.common.enums.Step; +import com.rbkmoney.adapter.common.model.AdapterContext; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -11,17 +13,14 @@ import java.util.Map; @Builder @NoArgsConstructor @AllArgsConstructor -public class ExitStateModel { +public class GeneralExitStateModel { private Step nextStep; private String errorCode; private String errorMessage; private AdapterContext adapterContext; - private EntryStateModel entryStateModel; - private String paReq; - private String md; - private String acsUrl; - private String trxId; + private GeneralEntryStateModel generalEntryStateModel; + private Map trxExtra; } diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/RecurrentContext.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/RecurrentContext.java deleted file mode 100644 index 5a09ca6..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/RecurrentContext.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - - -@Getter -@Setter -@ToString -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) -public class RecurrentContext { - - private String recToken; - -} \ No newline at end of file diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/StateModel.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/StateModel.java index e468b6d..24b3bd9 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/StateModel.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/StateModel.java @@ -1,6 +1,8 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.model; -import com.rbkmoney.adapter.bank.spring.boot.starter.constants.TargetStatus; +import com.rbkmoney.adapter.common.enums.Step; +import com.rbkmoney.adapter.common.enums.TargetStatus; +import com.rbkmoney.adapter.common.model.AdapterContext; import lombok.Builder; import lombok.Data; @@ -8,11 +10,12 @@ import lombok.Data; @Builder public class StateModel { - private String recToken; - private TargetStatus targetStatus; - private boolean makeRecurrent; private AdapterContext adapterContext; - + private TargetStatus targetStatus; private Step step; + private Step nextStep; + + private String recToken; + private boolean makeRecurrent; } diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/Step.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/Step.java deleted file mode 100644 index 9a3e1cd..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/Step.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.model; - -public enum Step { - - PRE_AUTH, - AUTH, - RECURRENT, - AUTH_RECURRENT, - CANCEL, - CHECK_STATUS, - FINISH_THREE_DS, - GENERATE_TOKEN_FINISH_THREE_DS, - GENERATE_TOKEN_CHECK_STATUS, - GENERATE_TOKEN_CAPTURE, - GENERATE_TOKEN_REFUND, - GENERATE_TOKEN_FINISH, - CAPTURE, - REFUND - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/processor/Processor.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/processor/Processor.java deleted file mode 100644 index d9f0dd8..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/processor/Processor.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.processor; - -import com.rbkmoney.adapter.bank.spring.boot.starter.model.EntryStateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel; - -public interface Processor { - - T process(R response, E context); - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/AdapterSerializer.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/AdapterSerializer.java deleted file mode 100644 index d5b170f..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/AdapterSerializer.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.serializer; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.AdapterContext; -import com.rbkmoney.damsel.proxy_provider.PaymentContext; -import com.rbkmoney.damsel.proxy_provider.RecurrentTokenContext; -import org.springframework.stereotype.Component; - -import java.io.IOException; - -@Component -public class AdapterSerializer extends StateSerializer { - - public AdapterSerializer(ObjectMapper mapper) { - super(mapper); - } - - public AdapterContext read(byte[] data) { - if (data == null) { - return new AdapterContext(); - } - try { - return getMapper().readValue(data, AdapterContext.class); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - } - - public AdapterContext getAdapterContext(Object context) { - AdapterContext adapterContext = new AdapterContext(); - byte[] state = getState(context); - if (state != null && state.length > 0) { - return this.read(state); - } - return adapterContext; - } - - public static byte[] getState(Object context) { - if (context instanceof RecurrentTokenContext) { - if (((RecurrentTokenContext) context).getSession() == null) { - return new byte[0]; - } - return ((RecurrentTokenContext) context).getSession().getState(); - } - return ((PaymentContext) context).getSession().getState(); - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/CallbackSerializer.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/CallbackSerializer.java deleted file mode 100644 index 734f9df..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/CallbackSerializer.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.serializer; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.Callback; -import org.springframework.stereotype.Component; - -import javax.servlet.http.HttpServletRequest; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -@Component -public class CallbackSerializer extends StateSerializer { - - public CallbackSerializer(ObjectMapper mapper) { - super(mapper); - } - - @Override - public Callback read(byte[] data) { - if (data == null) { - return new Callback(); - } - try { - return getMapper().readValue(data, Callback.class); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - } - - public Callback read(HttpServletRequest request) { - Map stringMap = Optional.ofNullable(request.getParameterMap()) - .orElseGet(HashMap::new) - .entrySet().stream() - .collect(Collectors.toMap(k -> k.getKey().trim(), - v -> v.getValue()[0])); - return mapper.convertValue(stringMap, Callback.class); - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/RecurrentContextSerializer.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/RecurrentContextSerializer.java deleted file mode 100644 index 4f49b02..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/RecurrentContextSerializer.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.serializer; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.RecurrentContext; -import org.springframework.stereotype.Component; - -import java.io.IOException; - -@Component -public class RecurrentContextSerializer extends StateSerializer { - - public RecurrentContextSerializer(ObjectMapper mapper) { - super(mapper); - } - - public RecurrentContext read(byte[] data) { - if (data == null) { - return new RecurrentContext(); - } - try { - return getMapper().readValue(data, RecurrentContext.class); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - } - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/Serializer.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/Serializer.java deleted file mode 100644 index 0f2c626..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/Serializer.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.serializer; - -public interface Serializer { - - byte[] writeByte(Object obj); - - String writeString(Object obj); - - TState read(byte[] data); - - TState read(String data); - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/StateSerializer.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/StateSerializer.java deleted file mode 100644 index a4533f9..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/serializer/StateSerializer.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.serializer; - -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.Setter; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.util.Base64; - -@Component -@Getter -@Setter -@AllArgsConstructor -public abstract class StateSerializer implements Serializer { - - protected final ObjectMapper mapper; - - @Override - public byte[] writeByte(Object obj) { - try { - return mapper.writeValueAsBytes(obj); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - } - - @Override - public String writeString(Object obj) { - try { - return Base64.getEncoder().encodeToString(getMapper().writeValueAsBytes(obj)); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - } - - @Override - public T read(byte[] data) { - throw new RuntimeException("Not supported"); - } - - @Override - public T read(String data) { - throw new RuntimeException("Not supported"); - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesService.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesService.java index 39426a3..cbc4d97 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesService.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesService.java @@ -1,11 +1,12 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.service; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel; +import com.rbkmoney.adapter.bank.spring.boot.starter.model.GeneralExitStateModel; import java.util.Map; public interface ThreeDsPropertiesService { - Map initProperties(ExitStateModel exitStateModel); + Map initProperties(GeneralExitStateModel generalExitStateModel); + } diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesServiceImpl.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesServiceImpl.java index 441bb0a..6de28fb 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesServiceImpl.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/service/ThreeDsPropertiesServiceImpl.java @@ -1,9 +1,10 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.service; -import com.rbkmoney.adapter.bank.spring.boot.starter.config.properties.AdapterProperties; -import com.rbkmoney.adapter.bank.spring.boot.starter.constants.ThreeDsFields; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.utils.RedirectUtils; +import com.rbkmoney.adapter.bank.spring.boot.starter.model.GeneralExitStateModel; +import com.rbkmoney.adapter.common.constants.ThreeDsFields; +import com.rbkmoney.adapter.common.model.AdapterContext; +import com.rbkmoney.adapter.common.properties.CommonAdapterProperties; +import com.rbkmoney.adapter.common.utils.converter.RedirectUtils; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -14,14 +15,18 @@ import java.util.Map; @RequiredArgsConstructor public class ThreeDsPropertiesServiceImpl implements ThreeDsPropertiesService { - private final AdapterProperties adapterProperties; + private final CommonAdapterProperties adapterProperties; @Override - public Map initProperties(ExitStateModel exitStateModel) { + public Map initProperties(GeneralExitStateModel generalExitStateModel) { Map params = new HashMap<>(); - params.put(ThreeDsFields.PA_REQ, exitStateModel.getPaReq()); - params.put(ThreeDsFields.MD, exitStateModel.getMd()); - params.put(ThreeDsFields.TERM_URL, RedirectUtils.getCallbackUrl(adapterProperties.getCallbackUrl(), adapterProperties.getPathCallbackUrl())); + AdapterContext adapterContext = generalExitStateModel.getAdapterContext(); + params.put(ThreeDsFields.PA_REQ, adapterContext.getPaReq()); + params.put(ThreeDsFields.MD, adapterContext.getMd()); + params.put(ThreeDsFields.TERM_URL, RedirectUtils.getCallbackUrl( + adapterProperties.getCallbackUrl(), + adapterProperties.getPathCallbackUrl()) + ); return params; } diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java deleted file mode 100644 index 9232507..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.utils; - -import com.rbkmoney.damsel.cds.ExpDate; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.math.BigDecimal; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class CardDateConverter { - - public static String getYearFromExpDate(ExpDate expDate) { - return String.format("%1$02d", expDate.getYear() % 100); - } - - public static String getYearFromShort(short year) { - return String.format("%1$02d", year % 100); - } - - public static String getMonthFromExpDate(ExpDate expDate) { - return String.format("%02d", expDate.getMonth()); - } - - public static String getMonthFromByte(byte month) { - return String.format("%02d", month); - } - - public static BigDecimal getFormattedAmount(long amount) { - return new BigDecimal(amount).movePointLeft(2); - } - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/RedirectUtils.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/RedirectUtils.java deleted file mode 100644 index bea802f..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/RedirectUtils.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.utils; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.util.UriComponentsBuilder; - -@Slf4j -public class RedirectUtils { - - public static String getCallbackUrl(String callbackUrl, String path) { - return UriComponentsBuilder.fromUriString(callbackUrl) - .path(path) - .build() - .toUriString(); - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtils.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtils.java deleted file mode 100644 index 75aa969..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtils.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.utils; - -import com.rbkmoney.damsel.cds.SessionData; - -public class SessionDataUtils { - - public static String getCvv2(SessionData sessionData) { - if (sessionData == null - || sessionData.getAuthData() == null - || !sessionData.getAuthData().isSetCardSecurityCode()) { - return null; - } - return sessionData.getAuthData().getCardSecurityCode().getValue(); - } - -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGenerator.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGenerator.java deleted file mode 100644 index 9199bb3..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGenerator.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.utils; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.codec.binary.Hex; -import org.springframework.util.MultiValueMap; - -import javax.crypto.Mac; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; - -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class SignGenerator { - - public static String prepareDataForHmac(String[] fields, MultiValueMap params) { - StringBuilder dataHmac = new StringBuilder(); - Arrays.asList(fields) - .forEach(field -> { - if (params.get(field) != null - && !params.get(field).isEmpty() - && params.get(field).get(0) != null - && !params.get(field).get(0).isEmpty() - ) { - dataHmac.append(params.get(field).get(0).length()); - dataHmac.append(params.get(field).get(0)); - } else { - dataHmac.append("-"); - } - } - ); - - return dataHmac.toString(); - } - - public static String sign(String[] fieldsForSign, MultiValueMap params, String key, String algorithm) { - String dataHmac = prepareDataForHmac(fieldsForSign, params); - String pSign = sign(dataHmac, key, algorithm); - return pSign.toUpperCase(); - } - - public static String sign(String data, String key, String algorithm) { - try { - byte[] decodedKey = Hex.decodeHex(key.toCharArray()); - SecretKeySpec keySpec = new SecretKeySpec(decodedKey, algorithm); - Mac mac = Mac.getInstance(algorithm); - mac.init(keySpec); - - byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); - byte[] signatureBytes = mac.doFinal(dataBytes); - - return new String(new Hex().encode(signatureBytes)); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } -} diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/validator/Validator.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/validator/Validator.java deleted file mode 100644 index fb3fd23..0000000 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/validator/Validator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.validator; - -public interface Validator { - - void validate(T request); - -} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 233bdf4..cbf1b95 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,12 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.rbkmoney.adapter.bank.spring.boot.starter.config.ErrorMappingConfiguration,\ -com.rbkmoney.adapter.bank.spring.boot.starter.config.properties.TimerProperties,\ -com.rbkmoney.adapter.bank.spring.boot.starter.config.properties.AdapterProperties,\ -com.rbkmoney.adapter.bank.spring.boot.starter.config.FilterConfiguration,\ -com.rbkmoney.adapter.bank.spring.boot.starter.flow.TargetStatusResolver,\ -com.rbkmoney.adapter.bank.spring.boot.starter.serializer.AdapterSerializer,\ -com.rbkmoney.adapter.bank.spring.boot.starter.serializer.CallbackSerializer,\ -com.rbkmoney.adapter.bank.spring.boot.starter.serializer.RecurrentContextSerializer,\ -com.rbkmoney.adapter.bank.spring.boot.starter.controller.AdapterController,\ -com.rbkmoney.adapter.bank.spring.boot.starter.service.ThreeDsPropertiesServiceImpl,\ -com.rbkmoney.adapter.bank.spring.boot.starter.flow.DefaultStepResolverImpl +com.rbkmoney.adapter.bank.spring.boot.starter.config.AppConfig,\ +com.rbkmoney.adapter.bank.spring.boot.starter.config.SerializerConfig,\ +com.rbkmoney.adapter.bank.spring.boot.starter.controller.AdapterControllerDecorator,\ +com.rbkmoney.adapter.bank.spring.boot.starter.service.ThreeDsPropertiesServiceImpl diff --git a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImplTest.java b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImplTest.java deleted file mode 100644 index 2664e4c..0000000 --- a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/DefaultStepResolverImplTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.flow; - -import com.rbkmoney.adapter.bank.spring.boot.starter.constants.TargetStatus; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.EntryStateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.StateModel; -import com.rbkmoney.adapter.bank.spring.boot.starter.model.Step; -import org.junit.Assert; -import org.junit.Test; - -public class DefaultStepResolverImplTest { - - private DefaultStepResolverImpl defaultStepResolver = new DefaultStepResolverImpl(); - - @Test - public void resolveEntry() { - StateModel stateModel = StateModel.builder() - .targetStatus(TargetStatus.CANCELLED) - .build(); - Step step = defaultStepResolver.resolveEntry(stateModel); - Assert.assertEquals(Step.CANCEL, step); - - stateModel.setTargetStatus(TargetStatus.CAPTURED); - step = defaultStepResolver.resolveEntry(stateModel); - Assert.assertEquals(Step.CAPTURE, step); - - stateModel.setTargetStatus(TargetStatus.PROCESSED); - step = defaultStepResolver.resolveEntry(stateModel); - Assert.assertEquals(Step.AUTH, step); - - stateModel.setTargetStatus(TargetStatus.AUTH_RECURRENT); - step = defaultStepResolver.resolveEntry(stateModel); - Assert.assertEquals(Step.AUTH_RECURRENT, step); - - stateModel.setTargetStatus(TargetStatus.REFUNDED); - step = defaultStepResolver.resolveEntry(stateModel); - Assert.assertEquals(Step.REFUND, step); - } - - @Test - public void resolveExit() { - EntryStateModel entryStateModel = EntryStateModel.builder() - .stateModel(StateModel.builder() - .step(Step.AUTH) - .build()) - .build(); - ExitStateModel exitStateModel = ExitStateModel.builder() - .entryStateModel(entryStateModel) - .nextStep(Step.FINISH_THREE_DS) - .build(); - Step step = defaultStepResolver.resolveExit(exitStateModel); - Assert.assertEquals(Step.FINISH_THREE_DS, step); - - entryStateModel.setStateModel(StateModel.builder() - .step(Step.AUTH_RECURRENT).build()); - step = defaultStepResolver.resolveExit(exitStateModel); - Assert.assertEquals(Step.GENERATE_TOKEN_FINISH_THREE_DS, step); - - exitStateModel.setNextStep(Step.GENERATE_TOKEN_CAPTURE); - step = defaultStepResolver.resolveExit(exitStateModel); - Assert.assertEquals(Step.GENERATE_TOKEN_CAPTURE, step); - - entryStateModel.setStateModel(StateModel.builder() - .step(Step.GENERATE_TOKEN_FINISH_THREE_DS).build()); - step = defaultStepResolver.resolveExit(exitStateModel); - Assert.assertEquals(Step.GENERATE_TOKEN_CAPTURE, step); - - entryStateModel.setStateModel(StateModel.builder() - .step(Step.GENERATE_TOKEN_CAPTURE).build()); - step = defaultStepResolver.resolveExit(exitStateModel); - Assert.assertEquals(Step.GENERATE_TOKEN_REFUND, step); - - entryStateModel.setStateModel(StateModel.builder() - .step(Step.GENERATE_TOKEN_REFUND).build()); - step = defaultStepResolver.resolveExit(exitStateModel); - Assert.assertEquals(Step.GENERATE_TOKEN_FINISH, step); - } - -} \ No newline at end of file diff --git a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/TargetStatusResolverTest.java b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/TargetStatusResolverTest.java deleted file mode 100644 index 403abdc..0000000 --- a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/flow/TargetStatusResolverTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.rbkmoney.adapter.bank.spring.boot.starter.flow; - -import com.rbkmoney.adapter.bank.spring.boot.starter.constants.TargetStatus; -import com.rbkmoney.adapter.bank.spring.boot.starter.exception.UnknownTargetStatusException; -import com.rbkmoney.damsel.domain.*; -import org.junit.Assert; -import org.junit.Test; - -public class TargetStatusResolverTest { - - private TargetStatusResolver targetStatusResolver = new TargetStatusResolver(); - - @Test - public void resolve() { - TargetStatus resolve = targetStatusResolver.resolve(TargetInvoicePaymentStatus.cancelled(new InvoicePaymentCancelled())); - Assert.assertEquals(TargetStatus.CANCELLED, resolve); - - resolve = targetStatusResolver.resolve(TargetInvoicePaymentStatus.captured(new InvoicePaymentCaptured())); - Assert.assertEquals(TargetStatus.CAPTURED, resolve); - - resolve = targetStatusResolver.resolve(TargetInvoicePaymentStatus.processed(new InvoicePaymentProcessed())); - Assert.assertEquals(TargetStatus.PROCESSED, resolve); - - resolve = targetStatusResolver.resolve(TargetInvoicePaymentStatus.refunded(new InvoicePaymentRefunded())); - Assert.assertEquals(TargetStatus.REFUNDED, resolve); - } - - @Test(expected = UnknownTargetStatusException.class) - public void resolveException() { - TargetStatus resolve = targetStatusResolver.resolve(null); - } - -} \ No newline at end of file diff --git a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtilsTest.java b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtilsTest.java index 979ceb4..22015b5 100644 --- a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtilsTest.java +++ b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SessionDataUtilsTest.java @@ -1,6 +1,6 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.utils; - +import com.rbkmoney.adapter.common.utils.converter.CardDataUtils; import com.rbkmoney.damsel.cds.AuthData; import com.rbkmoney.damsel.cds.CardSecurityCode; import com.rbkmoney.damsel.cds.SessionData; @@ -14,25 +14,25 @@ public class SessionDataUtilsTest { @Test public void getCvv2() { SessionData sessionData = new SessionData(); - String cvv2 = SessionDataUtils.getCvv2(sessionData); + String cvv2 = CardDataUtils.getCvv2(sessionData); Assert.assertNull(cvv2); AuthData authData = new AuthData(); sessionData.setAuthData(authData); - cvv2 = SessionDataUtils.getCvv2(sessionData); + cvv2 = CardDataUtils.getCvv2(sessionData); Assert.assertNull(cvv2); CardSecurityCode cardSecurityCode = new CardSecurityCode(); authData.setCardSecurityCode(cardSecurityCode); - cvv2 = SessionDataUtils.getCvv2(sessionData); + cvv2 = CardDataUtils.getCvv2(sessionData); Assert.assertNull(cvv2); cardSecurityCode.setValue(CARD_SECURITY_CODE); - cvv2 = SessionDataUtils.getCvv2(sessionData); + cvv2 = CardDataUtils.getCvv2(sessionData); Assert.assertEquals(CARD_SECURITY_CODE, cvv2); } diff --git a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java index 9df5f66..68e9c7a 100644 --- a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java +++ b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java @@ -1,5 +1,6 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.utils; +import com.rbkmoney.adapter.common.utils.encryption.HmacEncryption; import org.junit.Assert; import org.junit.Test; @@ -7,13 +8,11 @@ import java.security.NoSuchAlgorithmException; public class SignGeneratorTest { - public static final String HMAC_SHA_1 = "HmacSHA1"; - @Test public void sign() throws NoSuchAlgorithmException { String test = "511.483USD677144616IT Books. Qty: 217Books Online Inc.14www.sample.com1512345678901234589999999919pgw@mail.sample.com11--142003010515302116F2B2DD7E603A7ADA33https://www.sample.com/shop/reply"; - String s = SignGenerator.sign(test, "00112233445566778899AABBCCDDEEFF", HMAC_SHA_1); + String s = HmacEncryption.calculateHMacSha1(test, "00112233445566778899AABBCCDDEEFF"); Assert.assertEquals("FACC882CA67E109E409E3974DDEDA8AAB13A5E48", s.toUpperCase()); } } \ No newline at end of file