Browse Source

added permanent flow and service, controller ,pages and message

master
yoganandhan 8 months ago
parent
commit
57d1c27ccd
  1. 4
      src/main/java/com/gcc/flagpoles/service/PaymentService.java
  2. 53
      src/main/java/com/gcc/flagpoles/service/RequestService.java
  3. 225
      src/main/java/com/gcc/flagpoles/service/SMSService.java
  4. 314
      src/main/java/com/gcc/flagpoles/service/WhatsappMessageService.java
  5. 41
      src/main/java/com/gcc/flagpoles/util/MessageUtility.java

4
src/main/java/com/gcc/flagpoles/service/PaymentService.java

@ -37,8 +37,8 @@ public class PaymentService {
@Autowired
private RequestService requestService;
@Autowired
private WhatsappMessageService whatsappMessageService;
// @Autowired
// private WhatsappMessageService whatsappMessageService;
@Transactional(transactionManager = "mysqlgccFlagPoleTransactionManager")
public int saveorderLog(

53
src/main/java/com/gcc/flagpoles/service/RequestService.java

@ -546,9 +546,62 @@ public class RequestService {
WHERE temp_refid = ?
""", tempRefId);
// ================= SMS SECTION =================
String mobileNo = mysqlJdbcTemplate.queryForObject("""
SELECT mobile_number
FROM user_request_details
WHERE id = ?
""", String.class, newId);
if (mobileNo != null && !mobileNo.isBlank()) {
try {
smsService.sendBookingSuccesssms(mobileNo, finalRefId);
} catch (Exception e) {
System.err.println("SMS failed for requestId: " + finalRefId);
e.printStackTrace();
}
}
return finalRefId;
}
@Transactional(transactionManager = "mysqlgccFlagPoleTransactionManager")
public void sendApprovedSmsIfEligible(String requestId) {
String sql = """
SELECT mobile_number, final_status
FROM user_request_details
WHERE refid = ?
""";
Map<String, Object> row = mysqlJdbcTemplate.queryForMap(sql, requestId);
if (row == null || row.isEmpty()) {
System.out.println("No record found for requestId: " + requestId);
return;
}
String mobileNo = (String) row.get("mobile_number");
String finalStatus = (String) row.get("final_status");
// Send SMS only if APPROVED
if ("APPROVED".equalsIgnoreCase(finalStatus)
&& mobileNo != null
&& !mobileNo.isBlank()) {
try {
smsService.userrequestApprovedsms(mobileNo, requestId);
} catch (Exception e) {
System.err.println("Approved SMS failed for requestId: " + requestId);
e.printStackTrace();
}
} else {
System.out.println("SMS not sent. Current Status = " + finalStatus);
}
}
@Transactional(transactionManager = "mysqlgccFlagPoleTransactionManager")
public void updateFinePaymentStatus(String refId) {

225
src/main/java/com/gcc/flagpoles/service/SMSService.java

@ -1,39 +1,226 @@
package com.gcc.flagpoles.service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.net.HttpURLConnection;
import java.net.URL;
import com.gcc.flagpoles.configuration.AppConfig;
import com.gcc.flagpoles.util.MessageUtility;
@Service
public class SMSService {
public String sendMsg(String msgUrl) {
System.out.println("Whatsapp URL :-" + msgUrl);
String response = "";
@Autowired
private AppConfig appConfig;
@Autowired
private MessageUtility messageUtility;
private final String key = "pfTEYN6H";
public String sendBookingSuccesssms(String mobileNo, String requestId) {
String message = "Your Flag Pole permission request has been submitted successfully. "
+ "Request ID: " + requestId
+ ". You will be notified once the status is updated. By GCC";
// Encode message and fix colon issue (gateway not decoding %3A)
String encodedMsg = URLEncoder.encode(message, StandardCharsets.UTF_8)
.replace("%3A", ":");
String urlString = appConfig.getGetOtpUrl()
+ "from=GCCCRP"
+ "&key=" + key
+ "&sender=GCCCRP"
+ "&to=" + mobileNo
+ "&body=" + encodedMsg
+ "&entityid=1401572690000011081"
+ "&templateid=1407176959297472163";
System.out.println("SMS URL:- " + urlString);
String response = messageUtility.sendSMS(urlString);
System.out.println("SMS RESPONSE:- " + response);
return response;
}
public String userrequestApprovedsms(String mobileNo, String requestId) {
String message = "Your Flag Pole permission request has been approved. "
+ "Request ID: " + requestId
+ ". Please proceed as per the permitted guidelines. By GCC";
// Encode message and fix colon issue (gateway not decoding %3A)
String encodedMsg = URLEncoder.encode(message, StandardCharsets.UTF_8)
.replace("%3A", ":");
String urlString = appConfig.getGetOtpUrl()
+ "from=GCCCRP"
+ "&key=" + key
+ "&sender=GCCCRP"
+ "&to=" + mobileNo
+ "&body=" + encodedMsg
+ "&entityid=1401572690000011081"
+ "&templateid=1407176959297472163";
System.out.println("SMS URL:- " + urlString);
String response = messageUtility.sendSMS(urlString);
System.out.println("SMS RESPONSE:- " + response);
return response;
}
public String fineforunauthorizedflagpoleSms(String mobileNo,
String location,
String fineAmount,
String requestId) {
String message = "An unauthorized flag pole installation was found at " + location + ". "
+ "Fine amount: Rs." + fineAmount
+ " to be paid at https://gccservices.in/flagpolesregistration/paymentform?requestId="
+ requestId
+ " to avoid further action.\n"
+ "By GCC";
// Encode message and fix colon issue (gateway not decoding %3A)
String encodedMsg = URLEncoder.encode(message, StandardCharsets.UTF_8)
.replace("%3A", ":");
String urlString = appConfig.getGetOtpUrl()
+ "from=GCCCRP"
+ "&key=" + key
+ "&sender=GCCCRP"
+ "&to=" + mobileNo
+ "&body=" + encodedMsg
+ "&entityid=1401572690000011081"
+ "&templateid=1407176984622316740"; // <-- replace with correct template id
System.out.println("SMS URL:- " + urlString);
String response = messageUtility.sendSMS(urlString);
System.out.println("SMS RESPONSE:- " + response);
return response;
}
public String restorationflagpoleSms(String mobileNo,
String streetName,
String amount,
String requestId) {
try {
HttpURLConnection connection = null;
URL url = new URL(msgUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
String message = "Your flagpole installation at " + streetName
+ " caused damage to the public property. "
+ "Restoration charges of RS." + amount
+ " to be paid at https://gccservices.in/flagpolesregistration/paymentform?requestId="
+ requestId
+ " to avoid further action.\r\n"
+ "By GCC";
String encodedMsg = URLEncoder.encode(message, StandardCharsets.UTF_8);
String urlString = appConfig.getGetOtpUrl()
+ "from=GCCCRP"
+ "&key=" + key
+ "&sender=GCCCRP"
+ "&to=" + mobileNo
+ "&body=" + encodedMsg
+ "&entityid=1401572690000011081"
+ "&templateid=1407176984612580095";
System.out.println("FINAL SMS URL:- " + urlString);
URL apiUrl = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
int responseCode = connection.getResponseCode();
response = responseCode + "";
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
conn.disconnect();
System.out.println("DIRECT SMS RESPONSE:- " + content.toString());
return content.toString();
} catch (Exception e) {
e.printStackTrace();
return "FAILED";
}
return response;
}
public String sendSMS(String SMS_Url) {
System.out.println("SMS URL:- " + SMS_Url);
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(SMS_Url, String.class);
return response;
public String unauthorizedFlagPoleSms(String mobileNo,
String streetName,
String fineAmount,
String requestId) {
try {
String message = "An unauthorized flag pole installation was found at " + streetName + ". "
+ "Fine amount: Rs." + fineAmount
+ " to be paid at https://gccservices.in/flagpolesregistration/paymentform?requestId="
+ requestId
+ " to avoid further action.\r\n"
+ "By GCC";
String encodedMsg = URLEncoder.encode(message, StandardCharsets.UTF_8);
String urlString = appConfig.getGetOtpUrl()
+ "from=GCCCRP"
+ "&key=" + key
+ "&sender=GCCCRP"
+ "&to=" + mobileNo
+ "&body=" + encodedMsg
+ "&entityid=1401572690000011081"
+ "&templateid=1407176984622316740";
System.out.println("FINAL SMS URL:- " + urlString);
URL apiUrl = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
conn.disconnect();
System.out.println("DIRECT SMS RESPONSE:- " + content.toString());
return content.toString();
} catch (Exception e) {
e.printStackTrace();
return "FAILED";
}
}
}

314
src/main/java/com/gcc/flagpoles/service/WhatsappMessageService.java

@ -1,147 +1,167 @@
package com.gcc.flagpoles.service;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class WhatsappMessageService {
@Autowired
private SMSService smsService;
// c & d waste template after user register
public void sendrequestraisedWhatsappMessage(String refId, String mobileNumber) {
try {
String apiUrl = "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507&password=h2YjFNcJ&send_to="
+ mobileNumber
+ "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE&msg=Thank+you%21+Your+request+has+been+successfully+submitted.%0ARequest+Id+%3A+"
+ refId
+ "&isTemplate=true&header=C%26D+Waste+Collection+Request+Registered&footer=Greater+Chennai+Corporation";
System.out.println("WhatsApp API URL: " + apiUrl); // Optional debug
smsService.sendMsg(apiUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
// payment complete template
public void sendPaymentCompleteWhatsappMessage(String refId, double actualWeight, double totalCharge,
String mobileNumber) {
try {
String message = String.format(
"Your request (Request ID: *%s*) has been processed. Please proceed with the payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
refId, actualWeight, totalCharge);
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment Request", StandardCharsets.UTF_8);
String encodedFooter = URLEncoder.encode("Greater Chennai Corporation", StandardCharsets.UTF_8);
String redirectUrlParam = URLEncoder.encode(
"http://localhost:8077/flagpolesregistration/paymentform?requestId=" + refId,
StandardCharsets.UTF_8);
String apiUrl = "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
"&password=h2YjFNcJ" +
"&send_to=" + mobileNumber +
"&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
"&msg=" + encodedMessage +
"&isTemplate=true" +
"&header=" + encodedHeader +
"&footer=" + encodedFooter +
"&buttonUrlParam=" + redirectUrlParam;
// Optional: log URL before hitting for debugging
System.out.println("WhatsApp API URL: " + apiUrl);
// Make API call
// restTemplate.getForObject(apiUrl, String.class);
smsService.sendMsg(apiUrl);
} catch (Exception e) {
e.printStackTrace();
// Optionally log or alert failure
}
}
// for fine collection
public void sendFineCollectionWhatsappMessage(String refId, double actualWeight, double totalCharge,
String mobileNumber) {
try {
String message = String.format(
"Your request (Request ID: *%s*) has been processed. Please proceed with the payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
refId, actualWeight, totalCharge);
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment Request", StandardCharsets.UTF_8);
String encodedFooter = URLEncoder.encode("Greater Chennai Corporation", StandardCharsets.UTF_8);
String redirectUrlParam = URLEncoder.encode(
"http://localhost:8077/flagpolesregistration/paymentform?requestId=" + refId,
StandardCharsets.UTF_8);
String apiUrl = "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
"&password=h2YjFNcJ" +
"&send_to=" + mobileNumber +
"&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
"&msg=" + encodedMessage +
"&isTemplate=true" +
"&header=" + encodedHeader +
"&footer=" + encodedFooter +
"&buttonUrlParam=" + redirectUrlParam;
// Optional: log URL before hitting for debugging
System.out.println("WhatsApp API URL: " + apiUrl);
// Make API call
// restTemplate.getForObject(apiUrl, String.class);
smsService.sendMsg(apiUrl);
} catch (Exception e) {
e.printStackTrace();
// Optionally log or alert failure
}
}
// restoration message
public void sendRestorationWhatsappMessage(String refId, double actualWeight, double totalCharge,
String mobileNumber) {
try {
String message = String.format(
"Your request (Request ID: *%s*) has been processed. Please proceed with the payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
refId, actualWeight, totalCharge);
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment Request", StandardCharsets.UTF_8);
String encodedFooter = URLEncoder.encode("Greater Chennai Corporation", StandardCharsets.UTF_8);
String redirectUrlParam = URLEncoder.encode(
"http://localhost:8077/flagpolesregistration/paymentform?requestId=" + refId,
StandardCharsets.UTF_8);
String apiUrl = "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
"&password=h2YjFNcJ" +
"&send_to=" + mobileNumber +
"&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
"&msg=" + encodedMessage +
"&isTemplate=true" +
"&header=" + encodedHeader +
"&footer=" + encodedFooter +
"&buttonUrlParam=" + redirectUrlParam;
// Optional: log URL before hitting for debugging
System.out.println("WhatsApp API URL: " + apiUrl);
// Make API call
// restTemplate.getForObject(apiUrl, String.class);
smsService.sendMsg(apiUrl);
} catch (Exception e) {
e.printStackTrace();
// Optionally log or alert failure
}
}
}
// package com.gcc.flagpoles.service;
// import java.net.URLEncoder;
// import java.nio.charset.StandardCharsets;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.stereotype.Service;
// import com.gcc.flagpoles.util.MessageUtility;
// @Service
// public class WhatsappMessageService {
// @Autowired
// private MessageUtility messageUtility;
// // c & d waste template after user register
// public void sendrequestraisedWhatsappMessage(String refId, String
// mobileNumber) {
// try {
// String apiUrl =
// "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507&password=h2YjFNcJ&send_to="
// + mobileNumber
// +
// "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE&msg=Thank+you%21+Your+request+has+been+successfully+submitted.%0ARequest+Id+%3A+"
// + refId
// +
// "&isTemplate=true&header=C%26D+Waste+Collection+Request+Registered&footer=Greater+Chennai+Corporation";
// System.out.println("WhatsApp API URL: " + apiUrl); // Optional debug
// messageUtility.sendMsg(apiUrl);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// // payment complete template
// public void sendPaymentCompleteWhatsappMessage(String refId, double
// actualWeight, double totalCharge,
// String mobileNumber) {
// try {
// String message = String.format(
// "Your request (Request ID: *%s*) has been processed. Please proceed with the
// payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
// refId, actualWeight, totalCharge);
// String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
// String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment
// Request", StandardCharsets.UTF_8);
// String encodedFooter = URLEncoder.encode("Greater Chennai Corporation",
// StandardCharsets.UTF_8);
// String redirectUrlParam = URLEncoder.encode(
// "http://localhost:8077/flagpolesregistration/paymentform?requestId=" + refId,
// StandardCharsets.UTF_8);
// String apiUrl =
// "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
// "&password=h2YjFNcJ" +
// "&send_to=" + mobileNumber +
// "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
// "&msg=" + encodedMessage +
// "&isTemplate=true" +
// "&header=" + encodedHeader +
// "&footer=" + encodedFooter +
// "&buttonUrlParam=" + redirectUrlParam;
// // Optional: log URL before hitting for debugging
// System.out.println("WhatsApp API URL: " + apiUrl);
// // Make API call
// // restTemplate.getForObject(apiUrl, String.class);
// messageUtility.sendMsg(apiUrl);
// } catch (Exception e) {
// e.printStackTrace();
// // Optionally log or alert failure
// }
// }
// // for fine collection
// public void sendFineCollectionWhatsappMessage(String refId, double
// actualWeight, double totalCharge,
// String mobileNumber) {
// try {
// String message = String.format(
// "Your request (Request ID: *%s*) has been processed. Please proceed with the
// payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
// refId, actualWeight, totalCharge);
// String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
// String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment
// Request", StandardCharsets.UTF_8);
// String encodedFooter = URLEncoder.encode("Greater Chennai Corporation",
// StandardCharsets.UTF_8);
// String redirectUrlParam = URLEncoder.encode(
// "http://localhost:8077/flagpolesregistration/paymentform?requestId=" + refId,
// StandardCharsets.UTF_8);
// String apiUrl =
// "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
// "&password=h2YjFNcJ" +
// "&send_to=" + mobileNumber +
// "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
// "&msg=" + encodedMessage +
// "&isTemplate=true" +
// "&header=" + encodedHeader +
// "&footer=" + encodedFooter +
// "&buttonUrlParam=" + redirectUrlParam;
// // Optional: log URL before hitting for debugging
// System.out.println("WhatsApp API URL: " + apiUrl);
// // Make API call
// // restTemplate.getForObject(apiUrl, String.class);
// messageUtility.sendMsg(apiUrl);
// } catch (Exception e) {
// e.printStackTrace();
// // Optionally log or alert failure
// }
// }
// // restoration message
// public void sendRestorationWhatsappMessage(String refId, double actualWeight,
// double totalCharge,
// String mobileNumber) {
// try {
// String message = String.format(
// "Your request (Request ID: *%s*) has been processed. Please proceed with the
// payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
// refId, actualWeight, totalCharge);
// String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
// String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment
// Request", StandardCharsets.UTF_8);
// String encodedFooter = URLEncoder.encode("Greater Chennai Corporation",
// StandardCharsets.UTF_8);
// String redirectUrlParam = URLEncoder.encode(
// "http://localhost:8077/flagpolesregistration/paymentform?requestId=" + refId,
// StandardCharsets.UTF_8);
// String apiUrl =
// "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
// "&password=h2YjFNcJ" +
// "&send_to=" + mobileNumber +
// "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
// "&msg=" + encodedMessage +
// "&isTemplate=true" +
// "&header=" + encodedHeader +
// "&footer=" + encodedFooter +
// "&buttonUrlParam=" + redirectUrlParam;
// // Optional: log URL before hitting for debugging
// System.out.println("WhatsApp API URL: " + apiUrl);
// // Make API call
// // restTemplate.getForObject(apiUrl, String.class);
// messageUtility.sendMsg(apiUrl);
// } catch (Exception e) {
// e.printStackTrace();
// // Optionally log or alert failure
// }
// }
// }

41
src/main/java/com/gcc/flagpoles/util/MessageUtility.java

@ -0,0 +1,41 @@
package com.gcc.flagpoles.util;
import java.net.HttpURLConnection;
import java.net.URL;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class MessageUtility {
public String sendMsg(String msgUrl) {
System.out.println("Whatsapp URL :-" + msgUrl);
String response = "";
try {
HttpURLConnection connection = null;
URL url = new URL(msgUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
response = responseCode + "";
System.out.println("Response Code: " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String sendSMS(String SMS_Url) {
// System.out.println("SMS URL:- " + SMS_Url);
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(SMS_Url, String.class);
return response;
}
}
Loading…
Cancel
Save