From abb14b6d3aa089d56fb03cbc5cd9588e82b9b15e Mon Sep 17 00:00:00 2001 From: sanchit-0 Date: Tue, 16 Jul 2024 14:30:39 +0530 Subject: [PATCH] Create mock Airtel APIs. The response codes in the `application.yml` file have been added from the Airtel documentation. Similarly, the responses returned by the API are based on the Airtel documentation. --- build.gradle | 2 +- .../api/definition/AirtelMockApi.java | 36 ++++ .../api/implementation/AirtelMockService.java | 154 ++++++++++++++++++ .../mockairtel/utils/TransactionStatus.java | 5 + .../mockairtel/utils/TransferStatus.java | 5 + src/main/resources/application.yaml | 13 ++ 6 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/mifos/connector/airtel/mockairtel/api/definition/AirtelMockApi.java create mode 100644 src/main/java/org/mifos/connector/airtel/mockairtel/api/implementation/AirtelMockService.java create mode 100644 src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransactionStatus.java create mode 100644 src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransferStatus.java diff --git a/build.gradle b/build.gradle index ff39427..c9a0ebd 100644 --- a/build.gradle +++ b/build.gradle @@ -41,7 +41,7 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - implementation 'org.mifos:ph-ee-connector-common:1.9.1-SNAPSHOT' + implementation 'org.mifos:ph-ee-connector-common:1.10.0-SNAPSHOT' implementation 'org.json:json:20210307' checkstyle 'com.puppycrawl.tools:checkstyle:10.9.3' checkstyle 'com.github.sevntu-checkstyle:sevntu-checks:1.44.1' diff --git a/src/main/java/org/mifos/connector/airtel/mockairtel/api/definition/AirtelMockApi.java b/src/main/java/org/mifos/connector/airtel/mockairtel/api/definition/AirtelMockApi.java new file mode 100644 index 0000000..359ed0a --- /dev/null +++ b/src/main/java/org/mifos/connector/airtel/mockairtel/api/definition/AirtelMockApi.java @@ -0,0 +1,36 @@ +package org.mifos.connector.airtel.mockairtel.api.definition; + +import org.mifos.connector.airtel.mockairtel.api.implementation.AirtelMockService; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelEnquiryResponseDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelPaymentRequestDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelPaymentResponseDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AirtelMockApi { + + @Autowired + private AirtelMockService airtelMockService; + + protected Logger logger = LoggerFactory.getLogger(this.getClass()); + + @GetMapping(value = "/standard/v1/payments/{transactionId}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity airtelTransactionEnquiry(@PathVariable String transactionId) { + return airtelMockService.getTransactionStatus(transactionId); + } + + @PostMapping("/merchant/v2/payments/") + public ResponseEntity getAuthorization(@RequestBody AirtelPaymentRequestDTO airtelPaymentRequestDTO) { + return airtelMockService.initiateTransaction(airtelPaymentRequestDTO); + } + +} diff --git a/src/main/java/org/mifos/connector/airtel/mockairtel/api/implementation/AirtelMockService.java b/src/main/java/org/mifos/connector/airtel/mockairtel/api/implementation/AirtelMockService.java new file mode 100644 index 0000000..e8f518c --- /dev/null +++ b/src/main/java/org/mifos/connector/airtel/mockairtel/api/implementation/AirtelMockService.java @@ -0,0 +1,154 @@ +package org.mifos.connector.airtel.mockairtel.api.implementation; + +import java.util.UUID; +import org.mifos.connector.airtel.mockairtel.utils.TransactionStatus; +import org.mifos.connector.airtel.mockairtel.utils.TransferStatus; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelEnquiryResponseDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelEnquiryResponseDataDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelEnquiryResponseDataTransactionDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelPaymentRequestDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelPaymentResponseDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelPaymentResponseDataDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelPaymentResponseDataTransactionDTO; +import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelResponseStatusDTO; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +@Service +public class AirtelMockService { + + @Value("${mock-airtel.MSISDN_SUCCESSFUL}") + private String msisdnSuccessful; + + @Value("${mock-airtel.MSISDN_FAILED}") + private String msisdnFailed; + + @Value("${mock-airtel.TRANSACTION_ID_SUCCESSFUL}") + private String transactionIdSuccessful; + + @Value("${mock-airtel.TRANSACTION_ID_FAILED}") + private String transactionIdFailed; + + @Value("${mock-airtel.SUCCESS_RESPONSE_CODE}") + private String successResponseCode; + + @Value("${mock-airtel.FAILED_RESPONSE_CODE}") + private String failedResponseCode; + + @Value("${mock-airtel.PENDING_RESPONSE_CODE}") + private String pendingResponseCode; + + @Value("${mock-airtel.RESULT_CODE}") + private String resultCode; + + public ResponseEntity getTransactionStatus(String transactionId) { + String airtelMoneyId = UUID.randomUUID().toString().replace("-", ""); + String message; + String status; + String code; + String responseCode; + boolean success; + HttpStatus httpStatus; + + if (transactionId.equals(transactionIdSuccessful)) { + message = TransferStatus.SUCCESSFUL.name(); + status = TransactionStatus.TS.name(); + code = HttpStatus.OK.toString(); + responseCode = successResponseCode; + success = true; + httpStatus = HttpStatus.OK; + } + + else if (transactionId.equals(transactionIdFailed)) { + message = TransferStatus.FAILED.name(); + status = TransactionStatus.TF.name(); + code = HttpStatus.BAD_REQUEST.toString(); + responseCode = failedResponseCode; + success = false; + httpStatus = HttpStatus.BAD_REQUEST; + } + + else { + message = TransferStatus.IN_PROGRESS.name(); + status = TransactionStatus.TP.name(); + code = HttpStatus.ACCEPTED.toString(); + responseCode = pendingResponseCode; + success = true; + httpStatus = HttpStatus.ACCEPTED; + } + + return airtelEnquiryResponse(airtelMoneyId, transactionId, message, status, code, responseCode, success, httpStatus); + } + + public ResponseEntity initiateTransaction(AirtelPaymentRequestDTO airtelPaymentRequestDTO) { + String message; + String status; + String code; + String responseCode; + boolean success; + HttpStatus httpStatus; + + String msisdn = airtelPaymentRequestDTO.getSubscriber().getMsisdn(); + if (msisdn.equals(msisdnSuccessful)) { + message = TransferStatus.SUCCESSFUL.name(); + status = TransferStatus.SUCCESSFUL.name(); + code = HttpStatus.OK.toString(); + responseCode = successResponseCode; + success = true; + httpStatus = HttpStatus.OK; + } + + else if (msisdn.equals(msisdnFailed)) { + message = TransferStatus.FAILED.name(); + status = TransferStatus.FAILED.name(); + code = HttpStatus.BAD_REQUEST.toString(); + responseCode = failedResponseCode; + success = false; + httpStatus = HttpStatus.BAD_REQUEST; + } + + else { + message = TransferStatus.IN_PROGRESS.name(); + status = TransferStatus.IN_PROGRESS.name(); + code = HttpStatus.ACCEPTED.toString(); + responseCode = pendingResponseCode; + success = true; + httpStatus = HttpStatus.ACCEPTED; + } + + return airtelPaymentRespone(message, status, code, responseCode, success, httpStatus); + + } + + private ResponseEntity airtelEnquiryResponse(String airtelMoneyId, String transactionId, String message, + String status, String code, String responseCode, Boolean success, HttpStatus httpStatus) { + String id = transactionId; + + AirtelEnquiryResponseDataTransactionDTO airtelEnquiryResponseDataTransactionDTO = new AirtelEnquiryResponseDataTransactionDTO( + airtelMoneyId, id, message, status); + AirtelEnquiryResponseDataDTO airtelResponseDataDTO = new AirtelEnquiryResponseDataDTO(airtelEnquiryResponseDataTransactionDTO); + + AirtelResponseStatusDTO airtelResponseStatusDTO = new AirtelResponseStatusDTO(code, message, resultCode, responseCode, success); + + AirtelEnquiryResponseDTO responseEntity = new AirtelEnquiryResponseDTO(airtelResponseDataDTO, airtelResponseStatusDTO); + return ResponseEntity.status(httpStatus).body(responseEntity); + + } + + private ResponseEntity airtelPaymentRespone(String message, String status, String code, String responseCode, + Boolean success, HttpStatus httpStatus) { + boolean id = false; + + AirtelPaymentResponseDataTransactionDTO airtelPaymentResponseDataTransactionDTO = new AirtelPaymentResponseDataTransactionDTO(id, + status); + AirtelPaymentResponseDataDTO airtelResponseDataDTO = new AirtelPaymentResponseDataDTO(airtelPaymentResponseDataTransactionDTO); + + AirtelResponseStatusDTO airtelResponseStatusDTO = new AirtelResponseStatusDTO(code, message, resultCode, responseCode, success); + + AirtelPaymentResponseDTO responseEntity = new AirtelPaymentResponseDTO(airtelResponseDataDTO, airtelResponseStatusDTO); + return ResponseEntity.status(httpStatus).body(responseEntity); + } + +} diff --git a/src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransactionStatus.java b/src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransactionStatus.java new file mode 100644 index 0000000..76e87af --- /dev/null +++ b/src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransactionStatus.java @@ -0,0 +1,5 @@ +package org.mifos.connector.airtel.mockairtel.utils; + +public enum TransactionStatus { + TS, TF, TP +} diff --git a/src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransferStatus.java b/src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransferStatus.java new file mode 100644 index 0000000..bba38e3 --- /dev/null +++ b/src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransferStatus.java @@ -0,0 +1,5 @@ +package org.mifos.connector.airtel.mockairtel.utils; + +public enum TransferStatus { + COMPLETED, FAILED, IN_PROGRESS, UNKNOWN, SUCCESSFUL +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 1f976fe..e9ebb09 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,5 +1,8 @@ dfspids: "DFSPID" +server: + port: 8080 + transaction-id-length: -1 timer: "PT45S" @@ -26,3 +29,13 @@ ams: logging: level: root: INFO + +mock-airtel: + MSISDN_SUCCESSFUL: "0001" + MSISDN_FAILED: "0002" + TRANSACTION_ID_SUCCESSFUL: "0004" + TRANSACTION_ID_FAILED: "0005" + FAILED_RESPONSE_CODE: "DP00800001005" + SUCCESS_RESPONSE_CODE: "DP00800001001" + PENDING_RESPONSE_CODE: "DP00800001006" + RESULT_CODE: "ESB000010" \ No newline at end of file