org.vfdtech.implementations.CoreBankingOperations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utilities-and-generic-tools Show documentation
Show all versions of utilities-and-generic-tools Show documentation
A utilities service with generic tools implementation. Can be
plugged into your java project. This is designed for Java 1.8
package org.vfdtech.implementations;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.Credentials;
import okhttp3.Headers;
import org.vfdtech.MessageContent;
import org.vfdtech.exceptions.CustomException;
import org.vfdtech.interfaces.ICoreBankingProps;
import org.vfdtech.models.CoreBankingResponse;
import org.vfdtech.models.ResponseWrapper;
import org.vfdtech.models.cba.mifos.AccountClosure;
import org.vfdtech.models.cba.mifos.AccountLookup;
import org.vfdtech.models.cba.mifos.IPaymentRequest;
import org.vfdtech.models.cba.mifos.MifosResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import static org.vfdtech.implementations.ApiClient.HttpMethod.GET;
import static org.vfdtech.implementations.ApiClient.HttpMethod.POST;
@SuppressWarnings("unused")
public class CoreBankingOperations {
static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(
"dd MMMM yyyy"
);
private final ICoreBankingProps iCoreBankingProps;
private final ObjectMapper objectMapper;
private final ApiClient apiClient;
public CoreBankingOperations(ICoreBankingProps iCoreBankingProps, ObjectMapper objectMapper, ApiClient apiClient) {
this.iCoreBankingProps = iCoreBankingProps;
this.objectMapper = objectMapper;
this.apiClient = apiClient;
}
public static String getMifosDate(LocalDate localDate) {
return localDate.format(dateFormatter);
}
public static Headers headers(ICoreBankingProps iCoreBankingProps) {
String[] concatenatedCredentials = iCoreBankingProps
.getConcatenatedBasicAuthCredentials().split(":");
String basicAuthed = Credentials.basic(concatenatedCredentials[0], concatenatedCredentials[1]);
return Headers.of("Fineract-Platform-TenantId",
iCoreBankingProps.getFineractPlatformTenantId(),
"Authorization", basicAuthed);
}
public CoreBankingResponse closeAccount(AccountClosure accountClosure) throws CustomException {
try {
ResponseWrapper response = apiClient
.restExchange(POST.name(), String.format(iCoreBankingProps.getCbaUrl()
, "/savingsaccounts/%s?command=close", accountClosure.getAccountId()),
objectMapper.writeValueAsString(accountClosure), headers(iCoreBankingProps));
return getResponse(response);
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
}
}
public CoreBankingResponse doIntraTransfer(IPaymentRequest mifosPaymentRequest)
throws Exception {
try {
ResponseWrapper response = apiClient.restExchange(POST.name(),
iCoreBankingProps.getCbaUrl() + "/accounttransfers",
objectMapper.writeValueAsString(mifosPaymentRequest), headers(iCoreBankingProps));
return getResponse(response);
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public CoreBankingResponse placeOnPnd(IPaymentRequest mifosPaymentRequest)
throws Exception {
try {
ResponseWrapper response = apiClient.restExchange(POST.name(),
String.format("%s/savingsaccounts/%s?command=blockDebit",
iCoreBankingProps.getCbaUrl(), mifosPaymentRequest.getAccountId()),
objectMapper.writeValueAsString(mifosPaymentRequest), headers(iCoreBankingProps));
return getResponse(response);
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public CoreBankingResponse doAccountDebit(IPaymentRequest mifosPaymentRequest)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(POST.name(),
String.format(
String.format("%s/savingsaccounts/%%s/transactions?command=withdrawal",
iCoreBankingProps.getCbaUrl()),
mifosPaymentRequest.getAccountId()),
objectMapper.writeValueAsString(mifosPaymentRequest), headers(iCoreBankingProps));
return getResponse(response);
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public AccountLookup getBasicAccountDetailsAndBalance(String accountNumber) throws CustomException {
ResponseWrapper response = apiClient.restExchange(GET.name(),
String.format("%s/account/lookup/card?accountNo=%s&balanceNeeded=true",
iCoreBankingProps.getLookupUrl(),
accountNumber),
"", headers(iCoreBankingProps));
if (response.isSuccessful()) {
try {
return objectMapper.readValue(response.getResponseEntity().getValue(),
AccountLookup.class);
} catch (JsonProcessingException ignored) {
throw new CustomException(MessageContent.errorOccurred);
}
} else {
throw new CustomException(response.getResponseEntity().getKey());
}
}
public MifosResponse getAccountDetailsBy(String getBy, String accountParam) throws CustomException {
ResponseWrapper response;
if (getBy.equalsIgnoreCase("accountId")) {
response = apiClient.restExchange(GET.name(),
String.format("%s/savingsaccounts/%s?associations=all",
iCoreBankingProps.getCbaUrl(),
accountParam),
"", headers(iCoreBankingProps));
} else {
response = apiClient.restExchange(GET.name(),
String.format("%s/savingsaccounts/account/%s?associations=all",
iCoreBankingProps.getCbaUrl(),
accountParam),
"", headers(iCoreBankingProps));
}
if (response.isSuccessful()) {
LinkedHashMap result;
try {
result = objectMapper.readValue(response.getResponseEntity().getValue(),
LinkedHashMap.class);
} catch (JsonProcessingException ignored) {
throw new CustomException(MessageContent.errorOccurred);
}
return MifosResponse.builder()
.success(true)
.summary((HashMap)
result.getOrDefault("summary", new HashMap<>()))
.build();
} else {
throw new CustomException(response.getResponseEntity().getKey());
}
}
private CoreBankingResponse getResponse(ResponseWrapper responseWrapper) {
CoreBankingResponse coreBankingResponse;
try {
if (responseWrapper.isSuccessful()) {
coreBankingResponse = objectMapper.readValue(responseWrapper.getResponseEntity().getValue(),
CoreBankingResponse.class);
coreBankingResponse.setSuccess(true);
return coreBankingResponse;
} else {
return CoreBankingResponse.builder()
.success(false)
.message(responseWrapper.getResponseEntity().getKey())
.errorData(responseWrapper.getResponseEntity().getValue())
.build();
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy