Skip to content

Commit 6aaa963

Browse files
authored
Paybill validation (#20)
* paybilll validation * changed logger levels * changed application yaml
1 parent af5c024 commit 6aaa963

File tree

2 files changed

+105
-20
lines changed

2 files changed

+105
-20
lines changed

src/main/java/org/mifos/connector/ams/pesacore/camel/route/PesaRouteBuilder.java

+71-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.json.JSONObject;
88
import org.mifos.connector.ams.pesacore.pesacore.dto.PesacoreRequestDTO;
99
import org.mifos.connector.ams.pesacore.util.ConnectionUtils;
10+
import org.mifos.connector.ams.pesacore.util.PesacoreUtils;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213
import org.springframework.beans.factory.annotation.Value;
@@ -55,6 +56,29 @@ public void configure() {
5556
})
5657
.to("direct:transfer-settlement-base");
5758

59+
from("rest:POST:/api/v1/paybill/validate/roster")
60+
.id("validate-user")
61+
.log(LoggingLevel.INFO, "## Roster user validation")
62+
.setBody(e -> {
63+
String body=e.getIn().getBody(String.class);
64+
logger.debug("Body : {}",body);
65+
e.setProperty("dfspId",e.getProperty("dfspId"));
66+
return body;
67+
})
68+
.to("direct:transfer-validation-base")
69+
.process(e->{
70+
String transactionId= e.getProperty(TRANSACTION_ID).toString();
71+
logger.debug("Transaction Id : "+transactionId);
72+
logger.debug("Response received from validation base : {}",e.getIn().getBody());
73+
// Building the response
74+
JSONObject responseObject=new JSONObject();
75+
responseObject.put("reconciled", e.getProperty(PARTY_LOOKUP_FAILED).equals(false));
76+
responseObject.put("AMS", "roster");
77+
responseObject.put("transaction_id", transactionId);
78+
logger.debug("response object "+responseObject);
79+
e.getIn().setBody(responseObject.toString());
80+
});
81+
5882
from("direct:transfer-validation-base")
5983
.id("transfer-validation-base")
6084
.log(LoggingLevel.INFO, "## Starting transfer Validation base route")
@@ -65,12 +89,16 @@ public void configure() {
6589
.process(exchange -> {
6690
// processing success case
6791
exchange.setProperty(PARTY_LOOKUP_FAILED, false);
92+
exchange.setProperty("dfspId",exchange.getProperty("dfspId"));
93+
logger.debug("Pesacore Validation Success");
6894
})
6995
.otherwise()
7096
.log(LoggingLevel.ERROR, "Validation unsuccessful")
7197
.process(exchange -> {
7298
// processing unsuccessful case
7399
exchange.setProperty(PARTY_LOOKUP_FAILED, true);
100+
exchange.setProperty("dfspId",exchange.getProperty("dfspId"));
101+
logger.debug("Pesacore Validation Failure");
74102
});
75103

76104
from("direct:transfer-validation")
@@ -81,14 +109,26 @@ public void configure() {
81109
.setHeader("Content-Type", constant("application/json"))
82110
.setHeader("Authorization", simple("Token " + authHeader))
83111
.setBody(exchange -> {
84-
JSONObject channelRequest = (JSONObject) exchange.getProperty(CHANNEL_REQUEST);
85-
String transactionId = exchange.getProperty(TRANSACTION_ID, String.class);
86-
87-
PesacoreRequestDTO verificationRequestDTO = buildPesacoreDtoFromChannelRequest(channelRequest,
88-
transactionId);
89-
90-
logger.info("Validation request DTO: \n\n\n" + verificationRequestDTO);
91-
return verificationRequestDTO;
112+
if(exchange.getProperty(CHANNEL_REQUEST)!=null) {
113+
JSONObject channelRequest = (JSONObject) exchange.getProperty(CHANNEL_REQUEST);
114+
String transactionId = exchange.getProperty(TRANSACTION_ID, String.class);
115+
116+
PesacoreRequestDTO verificationRequestDTO = buildPesacoreDtoFromChannelRequest(channelRequest,
117+
transactionId);
118+
logger.debug("Validation request DTO: \n\n\n" + verificationRequestDTO);
119+
return verificationRequestDTO;
120+
}
121+
else {
122+
JSONObject paybillRequest = new JSONObject(exchange.getIn().getBody(String.class));
123+
PesacoreRequestDTO pesacoreRequestDTO = PesacoreUtils.convertPaybillPayloadToAmsPesacorePayload(paybillRequest);
124+
125+
String transactionId = pesacoreRequestDTO.getRemoteTransactionId();
126+
log.info(pesacoreRequestDTO.toString());
127+
exchange.setProperty(TRANSACTION_ID, transactionId);
128+
exchange.setProperty("dfspId",exchange.getProperty("dfspId"));
129+
logger.debug("Validation request DTO: \n\n\n" + pesacoreRequestDTO);
130+
return pesacoreRequestDTO;
131+
}
92132
})
93133
.marshal().json(JsonLibrary.Jackson)
94134
.toD(getVerificationEndpoint()+ "?bridgeEndpoint=true&throwExceptionOnFailure=false&"+
@@ -129,18 +169,28 @@ public void configure() {
129169
.setHeader("Content-Type", constant("application/json"))
130170
.setHeader("Authorization", simple("Token " + authHeader))
131171
.setBody(exchange -> {
132-
133-
JSONObject channelRequest = (JSONObject) exchange.getProperty(CHANNEL_REQUEST);
134-
String transactionId = exchange.getProperty(TRANSACTION_ID, String.class);
135-
String mpesaReceiptNumber = exchange.getProperty(EXTERNAL_ID, String.class);
136-
137-
PesacoreRequestDTO confirmationRequestDTO = buildPesacoreDtoFromChannelRequest(channelRequest,
138-
mpesaReceiptNumber);
139-
confirmationRequestDTO.setStatus("successful");
140-
confirmationRequestDTO.setReceiptId(mpesaReceiptNumber);
141-
142-
logger.info("Confirmation request DTO: \n\n\n" + confirmationRequestDTO);
143-
return confirmationRequestDTO;
172+
if(exchange.getProperty(CHANNEL_REQUEST).toString().contains("customData")){
173+
JSONObject channelRequest = (JSONObject) exchange.getProperty(CHANNEL_REQUEST);
174+
String transactionId = exchange.getProperty(TRANSACTION_ID, String.class);
175+
String mpesaReceiptNumber = exchange.getProperty(EXTERNAL_ID, String.class);
176+
177+
PesacoreRequestDTO confirmationRequestDTO = buildPesacoreDtoFromChannelRequest(channelRequest,
178+
mpesaReceiptNumber);
179+
confirmationRequestDTO.setStatus("successful");
180+
confirmationRequestDTO.setReceiptId(mpesaReceiptNumber);
181+
182+
logger.info("Confirmation request DTO: \n\n\n" + confirmationRequestDTO);
183+
return confirmationRequestDTO;
184+
}else {
185+
JSONObject paybillRequest = new JSONObject(exchange.getIn().getBody(String.class));
186+
PesacoreRequestDTO pesacoreRequestDTO = PesacoreUtils.convertPaybillPayloadToAmsPesacorePayload(paybillRequest);
187+
188+
String transactionId = pesacoreRequestDTO.getRemoteTransactionId();
189+
log.debug(pesacoreRequestDTO.toString());
190+
exchange.setProperty(TRANSACTION_ID, transactionId);
191+
logger.debug("Confirmation request DTO: {}" ,pesacoreRequestDTO);
192+
return pesacoreRequestDTO;
193+
}
144194
})
145195
.marshal().json(JsonLibrary.Jackson)
146196
.toD(getConfirmationEndpoint() + "?bridgeEndpoint=true&throwExceptionOnFailure=false&"+
@@ -149,6 +199,7 @@ public void configure() {
149199

150200
}
151201

202+
152203
// returns the complete URL for verification request
153204
private String getVerificationEndpoint() {
154205
return pesacoreBaseUrl + verificationEndpoint;

src/main/java/org/mifos/connector/ams/pesacore/util/PesacoreUtils.java

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import com.google.gson.Gson;
44
import org.apache.camel.util.json.JsonObject;
5+
import org.json.JSONArray;
6+
import org.json.JSONObject;
7+
import org.mifos.connector.ams.pesacore.pesacore.dto.PesacoreRequestDTO;
8+
9+
import static org.apache.camel.model.dataformat.JsonLibrary.Gson;
510

611
public class PesacoreUtils {
712

@@ -24,4 +29,33 @@ public static String parseErrorDescriptionFromJsonPayload(String errorJson) {
2429
return "Internal Server Error";
2530
}
2631

32+
public static PesacoreRequestDTO convertPaybillPayloadToAmsPesacorePayload(JSONObject payload) {
33+
String transactionId = convertCustomData(payload.getJSONArray("customData"), "transactionId");
34+
String currency = convertCustomData(payload.getJSONArray("customData"), "currency");
35+
String wallet_msisdn=payload.getJSONObject("secondaryIdentifier").getString("value");
36+
String accountID=payload.getJSONObject("primaryIdentifier").getString("value");
37+
PesacoreRequestDTO validationRequestDTO = new PesacoreRequestDTO();
38+
validationRequestDTO.setAccount(accountID);
39+
validationRequestDTO.setAmount(1L);
40+
validationRequestDTO.setCurrency(currency);
41+
validationRequestDTO.setRemoteTransactionId(transactionId);
42+
validationRequestDTO.setPhoneNumber(wallet_msisdn);
43+
validationRequestDTO.setStatus(null);
44+
return validationRequestDTO;
45+
}
46+
public static String convertCustomData(JSONArray customData, String key)
47+
{
48+
for(Object obj: customData)
49+
{
50+
JSONObject item = (JSONObject) obj;
51+
try {
52+
String filter = item.getString("key");
53+
if (filter != null && filter.equalsIgnoreCase(key)) {
54+
return item.getString("value");
55+
}
56+
} catch (Exception e){
57+
}
58+
}
59+
return null;
60+
}
2761
}

0 commit comments

Comments
 (0)