-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
339 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.mifos.connector.airtel; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/mifos/connector/airtel/api/definition/CallBackApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.mifos.connector.airtel.api.definition; | ||
|
||
import org.mifos.connector.airtel.api.implementation.CallBackController; | ||
import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelCallBackRequestDTO; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class CallBackApi { | ||
|
||
@Autowired | ||
CallBackController callBackController; | ||
|
||
@PostMapping("/callback") | ||
public ResponseEntity<AirtelCallBackRequestDTO> getCallBack(@RequestBody AirtelCallBackRequestDTO requestBody) { | ||
return callBackController.handleCallBackRequest(requestBody); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/mifos/connector/airtel/api/implementation/CallBackController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.mifos.connector.airtel.api.implementation; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelCallBackRequestDTO; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CallBackController { | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
public ResponseEntity<AirtelCallBackRequestDTO> handleCallBackRequest(AirtelCallBackRequestDTO requestBody) { | ||
try { | ||
logger.info("CallBack Request: {}", objectMapper.writeValueAsString(requestBody)); | ||
return ResponseEntity.status(HttpStatus.OK).body(requestBody); | ||
} catch (Exception ex) { | ||
throw new RuntimeException("Invalid response!", ex); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/mifos/connector/airtel/mockairtel/api/definition/AirtelMockApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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.AirtelCallBackRequestDTO; | ||
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.RequestHeader; | ||
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(@RequestHeader(value = "X-Country") String country, | ||
@RequestHeader(value = "X-Currency") String currency, @RequestHeader(value = "Authorization") String authorization, | ||
@RequestHeader(value = "x-signature") String signature, @RequestHeader(value = "x-key") String key, | ||
@RequestBody AirtelPaymentRequestDTO airtelPaymentRequestDTO) { | ||
return airtelMockController.initiateTransaction(airtelPaymentRequestDTO); | ||
} | ||
|
||
@PostMapping("/sendcallback") | ||
public ResponseEntity<AirtelCallBackRequestDTO> sendCallback(@RequestBody String transactionId) { | ||
return airtelMockController.sendCallBack(transactionId); | ||
} | ||
} |
197 changes: 197 additions & 0 deletions
197
...n/java/org/mifos/connector/airtel/mockairtel/api/implementation/AirtelMockController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package org.mifos.connector.airtel.mockairtel.api.implementation; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.UUID; | ||
import org.mifos.connector.airtel.mockairtel.utils.TransferStatus; | ||
import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelCallBackRequestDTO; | ||
import org.mifos.connector.common.mobilemoney.airtel.dto.AirtelCallBackRequestTransactionDTO; | ||
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.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class AirtelMockController { | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Value("${airtel.endpoints.contact-point}") | ||
public String airtelContactPoint; | ||
|
||
@Value("${server.port}") | ||
public String port; | ||
|
||
@Value("${airtel.endpoints.call-back}") | ||
public String callBackEndpoint; | ||
|
||
@Value("${airtel.endpoints.send-call-back}") | ||
public String sendCallBackEndpoint; | ||
|
||
@Value("${mock-airtel.MSISDN_FAILED}") | ||
private String msisdnFailed; | ||
|
||
@Value("${mock-airtel.CLIENT_CORRELATION_ID_SUCCESSFUL}") | ||
private String transactionIdSuccessful; | ||
|
||
@Value("${mock-airtel.CLIENT_CORRELATION_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; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
public ResponseEntity<AirtelEnquiryResponseDTO> getTransactionStatus(String transactionId) { | ||
String airtelMoneyId = UUID.nameUUIDFromBytes(transactionId.getBytes()).toString().replace("-", ""); | ||
String message; | ||
String status; | ||
String code; | ||
String responseCode; | ||
boolean success; | ||
HttpStatus httpStatus; | ||
|
||
if (transactionId.equals(transactionIdSuccessful)) { | ||
message = TransferStatus.SUCCESS.name(); | ||
status = TransferStatus.TS.name(); | ||
code = HttpStatus.OK.toString(); | ||
responseCode = successResponseCode; | ||
success = true; | ||
httpStatus = HttpStatus.OK; | ||
} | ||
|
||
else if (transactionId.equals(transactionIdFailed)) { | ||
message = TransferStatus.FAILED.name(); | ||
status = TransferStatus.TF.name(); | ||
code = HttpStatus.BAD_REQUEST.toString(); | ||
responseCode = failedResponseCode; | ||
success = false; | ||
httpStatus = HttpStatus.BAD_REQUEST; | ||
} | ||
|
||
else { | ||
message = TransferStatus.IN_PROGRESS.name(); | ||
status = TransferStatus.TIP.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 responseCode; | ||
boolean success; | ||
String code = HttpStatus.OK.name(); | ||
HttpStatus httpStatus = HttpStatus.OK; | ||
|
||
String msisdn = airtelPaymentRequestDTO.getSubscriber().getMsisdn(); | ||
|
||
if (msisdn.equals(msisdnFailed)) { | ||
message = TransferStatus.FAILED.name(); | ||
responseCode = failedResponseCode; | ||
success = false; | ||
AirtelResponseStatusDTO airtelResponseStatusDTO = new AirtelResponseStatusDTO(code, message, resultCode, responseCode, success); | ||
AirtelPaymentResponseDTO responseEntity = new AirtelPaymentResponseDTO(null, airtelResponseStatusDTO); | ||
return ResponseEntity.status(httpStatus).body(responseEntity); | ||
} | ||
|
||
else { | ||
message = TransferStatus.IN_PROGRESS.name(); | ||
status = TransferStatus.IN_PROGRESS.name(); | ||
responseCode = pendingResponseCode; | ||
success = true; | ||
|
||
String url = airtelContactPoint + ":" + port + sendCallBackEndpoint; | ||
|
||
String transactionId = airtelPaymentRequestDTO.getTransaction().getId(); | ||
restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(transactionId, new HttpHeaders()), AirtelCallBackRequestDTO.class); | ||
} | ||
|
||
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); | ||
} | ||
|
||
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); | ||
} | ||
|
||
@Async | ||
public ResponseEntity<AirtelCallBackRequestDTO> sendCallBack(String transactionId) { | ||
String url = airtelContactPoint + ":" + port + callBackEndpoint; | ||
HttpHeaders headers = new HttpHeaders(); | ||
String airtelMoneyId = UUID.nameUUIDFromBytes(transactionId.getBytes()).toString().replace("-", ""); | ||
String statusCode = TransferStatus.TF.name(); | ||
|
||
if (transactionId.equals(transactionIdSuccessful)) { | ||
statusCode = TransferStatus.TS.name(); | ||
} | ||
|
||
AirtelCallBackRequestTransactionDTO transactionDTO = new AirtelCallBackRequestTransactionDTO(); | ||
transactionDTO.setId(transactionId); | ||
transactionDTO.setMessage("Paid amount x"); | ||
transactionDTO.setStatusCode(statusCode); | ||
transactionDTO.setAirtelMoneyId(airtelMoneyId); | ||
|
||
AirtelCallBackRequestDTO callBackRequestDTO = new AirtelCallBackRequestDTO(); | ||
callBackRequestDTO.setTransaction(transactionDTO); | ||
try { | ||
// Sleep for 1 second before sending callback | ||
Thread.sleep(1000); | ||
ResponseEntity<AirtelCallBackRequestDTO> result = restTemplate.exchange(url, HttpMethod.POST, | ||
new HttpEntity<>(callBackRequestDTO, headers), AirtelCallBackRequestDTO.class); | ||
HttpStatus statusCode2 = result.getStatusCode(); | ||
logger.info("Response code from sendcallback: {}", statusCode2.value()); | ||
logger.info("Response sendcallback: {}", objectMapper.writeValueAsString(result)); | ||
return result; | ||
} catch (Exception ex) { | ||
throw new RuntimeException("Invalid response!", ex); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/org/mifos/connector/airtel/mockairtel/utils/TransferStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, SUCCESS, TS, TF, TIP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters