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
package org.vfdtech.implementations;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.Credentials;
import okhttp3.Headers;
import org.apache.commons.lang3.StringUtils;
import org.vfdtech.MessageContent;
import org.vfdtech.enums.CBACommand;
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.CoreMifosParams;
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(String accountId, IPaymentRequest mifosPaymentRequest)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(POST.name(),
String.format(
"%s/savingsaccounts/%s/transactions?command=withdrawal",
iCoreBankingProps.getCbaUrl(),
accountId != null? accountId : 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);
}
}
public CoreBankingResponse doAccountCredit(String accountId, IPaymentRequest mifosPaymentRequest)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(
POST.name(),
String.format(
"%s/savingsaccounts/%s/transactions?command=deposit",
iCoreBankingProps.getCbaUrl(),
accountId != null? accountId : mifosPaymentRequest.getAccountId()
),
objectMapper.writeValueAsString(mifosPaymentRequest),
headers(iCoreBankingProps)
);
CoreBankingResponse res = getResponse(response);
if (!res.isSuccess()) throw new CustomException(res.getMessage());
return res;
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public CoreBankingResponse undoTransaction(String accountId, String transactionId)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(
POST.name(),
String.format(
"%s/savingsaccounts/%s/transactions/%s?command=undo",
iCoreBankingProps.getCbaUrl(),
accountId,
transactionId
),
objectMapper.writeValueAsString(new HashMap<>()),
headers(iCoreBankingProps)
);
CoreBankingResponse res = getResponse(response);
if (!res.isSuccess()) throw new CustomException(res.getMessage());
return res;
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public CoreBankingResponse sendTransactionCommand(String accountId, CoreMifosParams load, CBACommand command)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(
POST.name(),
String.format(
"%s/savingsaccounts/%s/transactions?command=%s",
iCoreBankingProps.getCbaUrl(),
accountId,
command.getText()
),
objectMapper.writeValueAsString(load),
headers(iCoreBankingProps)
);
CoreBankingResponse res = getResponse(response);
if (!res.isSuccess()) throw new CustomException(res.getMessage());
return res;
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public CoreBankingResponse sendTransactionCommand(String accountId, CoreMifosParams load, String transactionId, CBACommand command)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(
POST.name(),
String.format(
"%s/savingsaccounts/%s/transactions/%s?command=%s",
iCoreBankingProps.getCbaUrl(),
accountId,
transactionId,
command.getText()
),
objectMapper.writeValueAsString(load),
headers(iCoreBankingProps)
);
CoreBankingResponse res = getResponse(response);
if (!res.isSuccess()) throw new CustomException(res.getMessage());
return res;
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public CoreBankingResponse sendCommand(String accountId, CBACommand command)
throws Exception {
try {
ResponseWrapper response = apiClient
.restExchange(
POST.name(),
String.format(
"%s/savingsaccounts/%s?command=%s",
iCoreBankingProps.getCbaUrl(),
accountId,
command.getText()
),
objectMapper.writeValueAsString(new HashMap<>()),
headers(iCoreBankingProps)
);
CoreBankingResponse res = getResponse(response);
if (!res.isSuccess()) throw new CustomException(res.getMessage());
return res;
} catch (JsonProcessingException e) {
throw new CustomException(MessageContent.badRequest);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public AccountLookup getBasicAccountDetails(String accountNumber, String amount) throws CustomException {
String lookupUrl = String.format("%s/account/lookup?accountNo=%s",
iCoreBankingProps.getLookupUrl(),
accountNumber);
if (StringUtils.isNotBlank(amount)) {
lookupUrl += "&amount=" + amount;
}
ResponseWrapper response = apiClient.restExchange(GET.name(),
lookupUrl,
"", 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());
}
}
}