mirror of
https://github.com/valitydev/cashreg-adapter-atol.git
synced 2024-11-06 01:15:20 +00:00
PROX-458: added polling (#8)
This commit is contained in:
parent
4ac10b6977
commit
90bbc7fa46
2
pom.xml
2
pom.xml
@ -39,7 +39,7 @@
|
||||
<testcontainers.version>1.10.2</testcontainers.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<adapter-cashreg-spring-boot-starter.version>1.0.2</adapter-cashreg-spring-boot-starter.version>
|
||||
<adapter-cashreg-spring-boot-starter.version>1.0.3</adapter-cashreg-spring-boot-starter.version>
|
||||
<spring-boot-starter-metrics-statsd.version>1.1.2</spring-boot-starter-metrics-statsd.version>
|
||||
<hibernate-validator.version>6.1.5.Final</hibernate-validator.version>
|
||||
<ipaddress.version>5.0.1</ipaddress.version>
|
||||
|
@ -0,0 +1,18 @@
|
||||
package com.rbkmoney.adapter.atol.configuration;
|
||||
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.config.properties.TimerProperties;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.service.IntentService;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.service.IntentServiceImpl;
|
||||
import com.rbkmoney.error.mapping.ErrorMapping;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
public IntentService intentService(ErrorMapping errorMapping, TimerProperties timerProperties) {
|
||||
return new IntentServiceImpl(errorMapping, timerProperties);
|
||||
}
|
||||
|
||||
}
|
@ -27,11 +27,11 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
public class CtxToEntryModelConverter implements Converter<CashregContext, EntryStateModel> {
|
||||
|
||||
private final CashregAdapterContextConverter cashRegAdapterContextConveter;
|
||||
private final CashregAdapterContextConverter cashregAdapterContextConverter;
|
||||
|
||||
@Override
|
||||
public EntryStateModel convert(CashregContext context) {
|
||||
AdapterState adapterState = cashRegAdapterContextConveter.convert(context);
|
||||
AdapterState adapterState = cashregAdapterContextConverter.convert(context);
|
||||
|
||||
Map<String, String> options = context.getOptions();
|
||||
EntryStateModel.EntryStateModelBuilder builder = EntryStateModel.builder();
|
||||
|
@ -1,59 +1,49 @@
|
||||
package com.rbkmoney.adapter.atol.converter.exit;
|
||||
|
||||
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.config.properties.TimerProperties;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.model.AdapterState;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.model.EntryStateModel;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.model.ExitStateModel;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.model.Step;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.service.IntentService;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.state.serializer.AdapterSerializer;
|
||||
import com.rbkmoney.adapter.cashreg.spring.boot.starter.utils.creators.CashregAdapterCreators;
|
||||
import com.rbkmoney.adapter.common.model.PollingInfo;
|
||||
import com.rbkmoney.damsel.cashreg.adapter.CashregResult;
|
||||
import com.rbkmoney.damsel.cashreg.adapter.Intent;
|
||||
import com.rbkmoney.damsel.domain.Failure;
|
||||
import com.rbkmoney.error.mapping.ErrorMapping;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.rbkmoney.adapter.cashreg.spring.boot.starter.utils.extractors.OptionsExtractors.extractMaxTimePolling;
|
||||
import static com.rbkmoney.adapter.cashreg.spring.boot.starter.utils.extractors.OptionsExtractors.extractPollingDelay;
|
||||
import static java.time.temporal.ChronoUnit.MINUTES;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ExitModelToProxyResultConverter implements Converter<ExitStateModel, CashregResult> {
|
||||
|
||||
private final ErrorMapping errorMapping;
|
||||
private final TimerProperties timerProperties;
|
||||
private final AdapterSerializer serializer;
|
||||
private final IntentService intentService;
|
||||
|
||||
@Override
|
||||
public CashregResult convert(ExitStateModel exitStateModel) {
|
||||
EntryStateModel entryStateModel = exitStateModel.getEntryStateModel();
|
||||
AdapterState adapterState = entryStateModel.getState().getAdapterContext();
|
||||
if (adapterState.getMaxDateTimePolling() == null) {
|
||||
int timerMaxTimePolling = extractMaxTimePolling(entryStateModel.getOptions(), timerProperties.getMaxTimePolling());
|
||||
Instant maxDateTime = Instant.now().plus(timerMaxTimePolling, MINUTES);
|
||||
adapterState.setMaxDateTimePolling(maxDateTime);
|
||||
|
||||
PollingInfo pollingInfo = new PollingInfo();
|
||||
if (adapterState.getPollingInfo() == null) {
|
||||
pollingInfo.setStartDateTimePolling(Instant.now());
|
||||
pollingInfo.setMaxDateTimePolling(intentService.extractMaxDateTimeInstant(entryStateModel));
|
||||
adapterState.setPollingInfo(pollingInfo);
|
||||
}
|
||||
|
||||
Intent intent = CashregAdapterCreators.createFinishIntentSuccess();
|
||||
if (exitStateModel.getErrorCode() != null) {
|
||||
Failure failure = errorMapping.mapFailure(
|
||||
exitStateModel.getErrorCode(),
|
||||
exitStateModel.getErrorMessage()
|
||||
);
|
||||
intent = CashregAdapterCreators.createFinishIntentFailure(failure);
|
||||
intent = intentService.getFailureByCodeAndDesc(exitStateModel);
|
||||
return new CashregResult().setIntent(intent).setState(serializer.writeByte(adapterState));
|
||||
}
|
||||
|
||||
if (exitStateModel.getInfo() == null) {
|
||||
Map<String, String> options = entryStateModel.getOptions();
|
||||
intent = CashregAdapterCreators.createIntentWithSleepIntent(extractPollingDelay(options, timerProperties.getPollingDelay()));
|
||||
intent = intentService.getSleep(exitStateModel);
|
||||
}
|
||||
|
||||
if (Step.CREATE == adapterState.getNextStep()) {
|
||||
|
@ -12,6 +12,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static com.rbkmoney.adapter.cashreg.spring.boot.starter.constant.Error.*;
|
||||
import static com.rbkmoney.adapter.cashreg.spring.boot.starter.constant.Error.SLEEP_TIMEOUT;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ErrorProcessor implements Processor<ExitStateModel, EntryStateModel, CommonResponse> {
|
||||
@ -31,15 +34,14 @@ public class ErrorProcessor implements Processor<ExitStateModel, EntryStateModel
|
||||
com.rbkmoney.adapter.atol.service.atol.model.Error error = response.getError();
|
||||
exitStateModel.setErrorCode(error.getCode().toString());
|
||||
exitStateModel.setErrorMessage(error.getText());
|
||||
} else if (adapterState.getMaxDateTimePolling().getEpochSecond() < currentTime.getEpochSecond()) {
|
||||
} else if (adapterState.getPollingInfo().getMaxDateTimePolling().getEpochSecond() < currentTime.getEpochSecond()) {
|
||||
log.error("Sleep Timeout for response: {}!", response);
|
||||
exitStateModel.setErrorCode(com.rbkmoney.adapter.cashreg.spring.boot.starter.constant.Error.SLEEP_TIMEOUT.getCode());
|
||||
exitStateModel.setErrorMessage(com.rbkmoney.adapter.cashreg.spring.boot.starter.constant.Error.SLEEP_TIMEOUT.getMessage());
|
||||
|
||||
exitStateModel.setErrorCode(SLEEP_TIMEOUT.getCode());
|
||||
exitStateModel.setErrorMessage(SLEEP_TIMEOUT.getMessage());
|
||||
} else {
|
||||
log.error("Unknown result code for response: {}!", response);
|
||||
exitStateModel.setErrorCode(com.rbkmoney.adapter.cashreg.spring.boot.starter.constant.Error.UNKNOWN.getCode());
|
||||
exitStateModel.setErrorMessage(com.rbkmoney.adapter.cashreg.spring.boot.starter.constant.Error.UNKNOWN.getMessage());
|
||||
exitStateModel.setErrorCode(UNKNOWN.getCode());
|
||||
exitStateModel.setErrorMessage(UNKNOWN.getMessage());
|
||||
}
|
||||
return exitStateModel;
|
||||
}
|
||||
|
@ -19,35 +19,35 @@ public class SuccessProcessor implements Processor<ExitStateModel, EntryStateMod
|
||||
|
||||
@Override
|
||||
public ExitStateModel process(CommonResponse response, EntryStateModel entryStateModel) {
|
||||
if (!ErrorUtils.hasError(response)) {
|
||||
ExitStateModel exitStateModel = new ExitStateModel();
|
||||
exitStateModel.setEntryStateModel(entryStateModel);
|
||||
|
||||
AdapterState adapterState = entryStateModel.getState().getAdapterContext();
|
||||
if (response.getUuid() != null) {
|
||||
adapterState.setReceiptId(response.getUuid());
|
||||
}
|
||||
adapterState.setCashregId(entryStateModel.getCashRegId());
|
||||
|
||||
if (Step.CHECK_STATUS.equals(entryStateModel.getState().getAdapterContext().getNextStep())) {
|
||||
if (Status.DONE.getValue().equalsIgnoreCase(response.getStatus())) {
|
||||
ReceiptInfo receiptInfo = new ReceiptInfo()
|
||||
.setReceiptId(response.getUuid())
|
||||
.setCallbackUrl(response.getCallbackUrl())
|
||||
.setDaemonCode(response.getDaemonCode())
|
||||
.setDeviceCode(response.getDeviceCode())
|
||||
.setGroupCode(response.getGroupCode())
|
||||
.setTimestamp(response.getTimestamp());
|
||||
exitStateModel.setInfo(receiptInfo);
|
||||
}
|
||||
} else if (Step.CREATE.equals(entryStateModel.getState().getAdapterContext().getNextStep())) {
|
||||
adapterState.setNextStep(Step.CHECK_STATUS);
|
||||
}
|
||||
exitStateModel.setAdapterContext(adapterState);
|
||||
return exitStateModel;
|
||||
if (ErrorUtils.hasError(response)) {
|
||||
return nextProcessor.process(response, entryStateModel);
|
||||
}
|
||||
|
||||
return nextProcessor.process(response, entryStateModel);
|
||||
ExitStateModel exitStateModel = new ExitStateModel();
|
||||
exitStateModel.setEntryStateModel(entryStateModel);
|
||||
|
||||
AdapterState adapterState = entryStateModel.getState().getAdapterContext();
|
||||
if (response.getUuid() != null) {
|
||||
adapterState.setReceiptId(response.getUuid());
|
||||
}
|
||||
adapterState.setCashregId(entryStateModel.getCashRegId());
|
||||
|
||||
if (Step.CHECK_STATUS.equals(entryStateModel.getState().getAdapterContext().getNextStep())) {
|
||||
if (Status.DONE.getValue().equalsIgnoreCase(response.getStatus())) {
|
||||
ReceiptInfo receiptInfo = new ReceiptInfo()
|
||||
.setReceiptId(response.getUuid())
|
||||
.setCallbackUrl(response.getCallbackUrl())
|
||||
.setDaemonCode(response.getDaemonCode())
|
||||
.setDeviceCode(response.getDeviceCode())
|
||||
.setGroupCode(response.getGroupCode())
|
||||
.setTimestamp(response.getTimestamp());
|
||||
exitStateModel.setInfo(receiptInfo);
|
||||
}
|
||||
} else if (Step.CREATE.equals(entryStateModel.getState().getAdapterContext().getNextStep())) {
|
||||
adapterState.setNextStep(Step.CHECK_STATUS);
|
||||
}
|
||||
exitStateModel.setAdapterContext(adapterState);
|
||||
return exitStateModel;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user