completed initial scaffolding for services

This commit is contained in:
2025-06-16 23:12:38 +02:00
parent eee746a5d8
commit b214d42d2f
24 changed files with 305 additions and 88 deletions

View File

@@ -1,11 +0,0 @@
package zw.qantra.tm.domain.services;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import zw.qantra.tm.domain.repositories.ConfirmTransactionRepository;
@Service
@RequiredArgsConstructor
public class ConfirmTransactionService {
private final ConfirmTransactionRepository confirmTransactionRepository;
}

View File

@@ -2,10 +2,15 @@ package zw.qantra.tm.domain.services;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import zw.qantra.tm.domain.enums.Status;
import zw.qantra.tm.domain.models.PaymentProcessor;
import zw.qantra.tm.domain.models.Transaction;
import zw.qantra.tm.domain.repositories.TransactionRepository;
import zw.qantra.tm.domain.services.factories.PaymentProcessorFactory;
import zw.qantra.tm.domain.services.processors.TransactionProcessorInterface;
import zw.qantra.tm.domain.services.processors.handlers.CalculateChargesHandler;
import zw.qantra.tm.domain.services.processors.handlers.LogicPipeline;
import zw.qantra.tm.domain.services.processors.handlers.ValidationHandler;
@Service
@RequiredArgsConstructor
@@ -13,9 +18,26 @@ public class TransactionService {
private final TransactionRepository transactionRepository;
private final PaymentProcessorFactory paymentProcessorFactory;
private Transaction bootstrap(Transaction transaction) {
String label = transaction.getType() + "_" + transaction.getPaymentProcessorLabel();
// PaymentProcessor paymentProcessor = paymentProcessorFactory.getPaymentProcessor(label);
public Transaction execute(Transaction transaction) {
LogicPipeline<Transaction, Transaction> transactionLogic =
new LogicPipeline<>(new CalculateChargesHandler())
.addHandler(new ValidationHandler());
transaction = transactionLogic.execute(transaction);
String label = transaction.getType() + "_" + transaction.getIntegrationProcessorLabel();
TransactionProcessorInterface transactionProcessorInterface =
(TransactionProcessorInterface) paymentProcessorFactory.getPaymentProcessor(label);
try {
transactionProcessorInterface.process(transaction);
}catch (Exception e){
e.printStackTrace();
transaction.setStatus(Status.FAILED);
transaction.setResponseCode("01");
transaction.setErrorMessage(e.getMessage());
transactionRepository.save(transaction);
return transaction;
}
return transaction;
}
}

View File

@@ -9,6 +9,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import zw.qantra.tm.exceptions.ApiException;
@Component
@RequiredArgsConstructor
@@ -28,11 +29,13 @@ public class PaymentProcessorFactory implements ApplicationContextAware {
try {
paymentProcessor = applicationContext.getBean(paymentProcessorName);
} catch (NoSuchBeanDefinitionException exception) {
exception.printStackTrace();
logger.error("Couldn't find payment processor: {}", exception.getMessage());
return null;
throw new ApiException("Couldn't find payment processor");
} catch (BeansException e) {
e.printStackTrace();
logger.error("Error retrieving payment processor bean: {}", e.getMessage());
return null;
throw new ApiException("Error retrieving payment processor bean");
}
return paymentProcessor;

View File

@@ -3,6 +3,6 @@ package zw.qantra.tm.domain.services.processors;
import zw.qantra.tm.domain.models.Transaction;
public interface TransactionProcessorInterface {
Object process(Transaction transaction);
Object process(Transaction transaction) throws Exception;
Object reverse(Transaction transaction);
}

View File

@@ -1,21 +1,25 @@
package zw.qantra.tm.domain.services.processors.confirmations;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import zw.qantra.tm.domain.models.Transaction;
import zw.qantra.tm.domain.services.processors.TransactionProcessorInterface;
import zw.qantra.tm.rest.RestService;
@Service("CONFIRM_POWERTEL_ZESA")
@Service("CONFIRM_ZESA")
@RequiredArgsConstructor
public class ZesaConfirmationProcessor implements TransactionProcessorInterface {
private final RestService restService;
@Value("${sbz.aggregator.url}")
private String aggregatorUrl;
@Override
public Object process(Transaction transaction) {
return transaction;
public Object process(Transaction transaction) throws Exception {
System.out.println("processing transaction");
throw new Exception("Challenge encountered, please contact support.");
// return transaction;
}
@Override

View File

@@ -0,0 +1,11 @@
package zw.qantra.tm.domain.services.processors.handlers;
import zw.qantra.tm.domain.models.Transaction;
public class CalculateChargesHandler implements LogicHandler<Transaction, Transaction> {
@Override
public Transaction process(Transaction transaction) {
System.out.println("processing charges");
return transaction;
}
}

View File

@@ -0,0 +1,5 @@
package zw.qantra.tm.domain.services.processors.handlers;
interface LogicHandler<I, O> {
O process(I input);
}

View File

@@ -0,0 +1,20 @@
package zw.qantra.tm.domain.services.processors.handlers;
import org.springframework.stereotype.Service;
public class LogicPipeline<I, O> {
private final LogicHandler<I, O> currentHandler;
public LogicPipeline(LogicHandler<I, O> currentHandler) {
this.currentHandler = currentHandler;
}
public <K> LogicPipeline<I, K> addHandler(LogicHandler<O, K> newHandler) {
return new LogicPipeline<>(input -> newHandler.process(currentHandler.process(input)));
}
public O execute(I input) {
return currentHandler.process(input);
}
}

View File

@@ -0,0 +1,11 @@
package zw.qantra.tm.domain.services.processors.handlers;
import zw.qantra.tm.domain.models.Transaction;
public class ValidationHandler implements LogicHandler<Transaction, Transaction> {
@Override
public Transaction process(Transaction transaction) {
System.out.println("processing validation");
return transaction;
}
}

View File

@@ -0,0 +1,28 @@
package zw.qantra.tm.domain.services.processors.integrations;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import zw.qantra.tm.domain.models.Transaction;
import zw.qantra.tm.domain.services.processors.TransactionProcessorInterface;
import zw.qantra.tm.rest.RestService;
@Service("REQUEST_ZESA")
@RequiredArgsConstructor
public class ZesaIntegrationProcessor implements TransactionProcessorInterface {
private final RestService restService;
@Value("${sbz.aggregator.url}")
private String aggregatorUrl;
@Override
public Object process(Transaction transaction) throws Exception {
return transaction;
}
@Override
public Object reverse(Transaction transaction) {
return transaction;
}
}

View File

@@ -0,0 +1,24 @@
package zw.qantra.tm.domain.services.processors.payments;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import zw.qantra.tm.domain.models.Transaction;
import zw.qantra.tm.domain.services.processors.TransactionProcessorInterface;
import zw.qantra.tm.rest.RestService;
@Service("STEWARD_WEB")
@RequiredArgsConstructor
public class StewardWebPaymentProcessor implements TransactionProcessorInterface {
private final RestService restService;
@Override
public Object process(Transaction transaction) throws Exception {
return transaction;
}
@Override
public Object reverse(Transaction transaction) {
return transaction;
}
}