-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PANC-71] Create mock Airtel Api. #3
Open
sanchit-0
wants to merge
1
commit into
fynarfin:develop
Choose a base branch
from
sanchit-0:mock-airtel-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, update this to the released version after the jfrog issue is fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 0.0.0 as this PR has not been merged and new version is not released.