Added intent service

This commit is contained in:
Inal Arsanukaev 2019-06-19 20:12:31 +03:00
parent 4c82c710b4
commit e60e9145da
3 changed files with 60 additions and 1 deletions

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.rbkmoney</groupId>
<artifactId>adapter-bank-payout-spring-boot-starter</artifactId>
<version>0.0.5-SNAPSHOT</version>
<version>0.0.6-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Adapter-bank-payout-spring-boot-starter</name>

View File

@ -0,0 +1,14 @@
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.service;
import com.rbkmoney.adapter.bank.payout.spring.boot.starter.model.EntryStateModel;
import com.rbkmoney.adapter.bank.payout.spring.boot.starter.model.ExitStateModel;
import com.rbkmoney.damsel.withdrawals.provider_adapter.Intent;
import java.time.Instant;
public interface IntentService {
Intent getFailure(ExitStateModel exitStateModel);
Intent getSuccess(ExitStateModel exitStateModel);
Intent getSleep(ExitStateModel exitStateModel);
Instant getMaxDateTimeInstant(EntryStateModel entryStateModel);
}

View File

@ -0,0 +1,45 @@
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.service;
import com.rbkmoney.adapter.bank.payout.spring.boot.starter.config.properties.TimerProperties;
import com.rbkmoney.adapter.bank.payout.spring.boot.starter.model.EntryStateModel;
import com.rbkmoney.adapter.bank.payout.spring.boot.starter.model.ExitStateModel;
import com.rbkmoney.damsel.base.Timer;
import com.rbkmoney.damsel.domain.Failure;
import com.rbkmoney.damsel.domain.TransactionInfo;
import com.rbkmoney.damsel.withdrawals.provider_adapter.*;
import com.rbkmoney.error.mapping.ErrorMapping;
import com.rbkmoney.java.damsel.utils.extractors.OptionsExtractors;
import lombok.RequiredArgsConstructor;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import static com.rbkmoney.java.damsel.utils.extractors.OptionsExtractors.extractMaxTimePolling;
@RequiredArgsConstructor
public class IntentServiceImpl implements IntentService {
private final ErrorMapping errorMapping;
private final TimerProperties timerProperties;
public Intent getFailure(ExitStateModel exitStateModel) {
return Intent.finish(new FinishIntent(FinishStatus.failure(errorMapping.getFailureByCodeAndDescription(exitStateModel.getErrorCode(), exitStateModel.getErrorMessage()))));
}
public Intent getSuccess(ExitStateModel exitStateModel) {
return Intent.finish(new FinishIntent(FinishStatus.success(new Success(new TransactionInfo().setId(exitStateModel.getNextState().getTrxInfo().getTrxId())))));
}
public Intent getSleep(ExitStateModel exitStateModel) {
if (exitStateModel.getNextState().getMaxDateTimePolling().getEpochSecond() < Instant.now().getEpochSecond()) {
return Intent.finish(new FinishIntent(FinishStatus.failure(new Failure())));
}
int timerPollingDelay = OptionsExtractors.extractPollingDelay(exitStateModel.getEntryStateModel().getOptions(), timerProperties.getPollingDelay());
return Intent.sleep(new SleepIntent(new Timer(Timer.timeout(timerPollingDelay))));
}
public Instant getMaxDateTimeInstant(EntryStateModel entryStateModel) {
int maxTimePolling = extractMaxTimePolling(entryStateModel.getOptions(), timerProperties.getMaxTimePolling());
return Instant.now().plus(maxTimePolling, ChronoUnit.MINUTES);
}
}