Skip to content

Commit

Permalink
Create mock Airtel APIs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sanchit-0 committed Jul 22, 2024
1 parent fa62a65 commit 75432d3
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.mifos.connector.airtel.mockairtel.api.definition;

import org.mifos.connector.airtel.mockairtel.api.implementation.AirtelMockController;
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 AirtelMockController airtelMockController;

protected Logger logger = LoggerFactory.getLogger(this.getClass());

@GetMapping(value = "/standard/v1/payments/{transactionId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AirtelEnquiryResponseDTO> airtelTransactionEnquiry(@PathVariable String transactionId) {
return airtelMockController.getTransactionStatus(transactionId);
}

@PostMapping("/merchant/v2/payments/")
public ResponseEntity<AirtelPaymentResponseDTO> getAuthorization(@RequestBody AirtelPaymentRequestDTO airtelPaymentRequestDTO) {
return airtelMockController.initiateTransaction(airtelPaymentRequestDTO);
}

}
Original file line number Diff line number Diff line change
@@ -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 AirtelMockController {

@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<AirtelEnquiryResponseDTO> 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<AirtelPaymentResponseDTO> 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<AirtelEnquiryResponseDTO> 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<AirtelPaymentResponseDTO> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mifos.connector.airtel.mockairtel.utils;

public enum TransactionStatus {
TS, TF, TP
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mifos.connector.airtel.mockairtel.utils;

public enum TransferStatus {
COMPLETED, FAILED, IN_PROGRESS, UNKNOWN, SUCCESSFUL
}
13 changes: 13 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
dfspids: "DFSPID"

server:
port: 8080

transaction-id-length: -1

timer: "PT45S"
Expand All @@ -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"

0 comments on commit 75432d3

Please sign in to comment.