| 21285 |
kshitij.so |
1 |
package com.spice.profitmandi.web.processor;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 26706 |
amit.gupta |
4 |
import java.util.ArrayList;
|
| 26656 |
amit.gupta |
5 |
import java.util.HashMap;
|
| 21285 |
kshitij.so |
6 |
import java.util.List;
|
| 26656 |
amit.gupta |
7 |
import java.util.Map;
|
| 21285 |
kshitij.so |
8 |
import java.util.Random;
|
|
|
9 |
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 26649 |
amit.gupta |
11 |
import org.springframework.beans.factory.annotation.Value;
|
| 26657 |
amit.gupta |
12 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 21285 |
kshitij.so |
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 21499 |
kshitij.so |
16 |
import com.spice.profitmandi.common.util.Utils;
|
| 26656 |
amit.gupta |
17 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 21735 |
ashik.ali |
18 |
import com.spice.profitmandi.dao.entity.dtr.Otp;
|
|
|
19 |
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
|
|
|
20 |
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
|
| 21285 |
kshitij.so |
21 |
import com.spice.profitmandi.web.res.OTPResponse;
|
|
|
22 |
|
|
|
23 |
@Component
|
| 26087 |
tejbeer |
24 |
public class OtpProcessor {
|
| 21285 |
kshitij.so |
25 |
|
| 26706 |
amit.gupta |
26 |
private static final String SMS_GATEWAY = "http://sms.smartukaan.com/sms/templatesms";
|
| 21285 |
kshitij.so |
27 |
private static final int len = 5;
|
| 26087 |
tejbeer |
28 |
private static final String numbers = "0123456789";
|
| 23309 |
amit.gupta |
29 |
private static final String text = "Dear Customer, {0} is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone.";
|
| 21285 |
kshitij.so |
30 |
|
|
|
31 |
@Autowired
|
|
|
32 |
OtpRepository otpRepository;
|
| 26649 |
amit.gupta |
33 |
|
| 26657 |
amit.gupta |
34 |
@Autowired JavaMailSender mailSender;
|
|
|
35 |
|
| 26649 |
amit.gupta |
36 |
@Value("${prod}")
|
|
|
37 |
private boolean prodEnv;
|
| 26656 |
amit.gupta |
38 |
|
|
|
39 |
@Autowired RestClient restClient;
|
| 21285 |
kshitij.so |
40 |
|
| 26649 |
amit.gupta |
41 |
|
| 26087 |
tejbeer |
42 |
private String getOtp() {
|
| 21285 |
kshitij.so |
43 |
Random rndm_method = new Random();
|
|
|
44 |
char[] otp = new char[len];
|
|
|
45 |
|
| 26087 |
tejbeer |
46 |
for (int i = 0; i < len; i++) {
|
| 21285 |
kshitij.so |
47 |
otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
|
|
|
48 |
}
|
|
|
49 |
return String.valueOf(otp);
|
|
|
50 |
}
|
|
|
51 |
|
| 26087 |
tejbeer |
52 |
public OTPResponse generateOtp(String email, String phone, OtpType otpType)
|
|
|
53 |
throws Exception, ProfitMandiBusinessException {
|
| 21285 |
kshitij.so |
54 |
OTPResponse otpResponse = new OTPResponse();
|
| 23273 |
ashik.ali |
55 |
List<Otp> otps = otpRepository.selectAllByEmailWithTime(email);
|
| 21285 |
kshitij.so |
56 |
String otp = null;
|
| 26087 |
tejbeer |
57 |
if (otps.size() >= 5) {
|
| 21285 |
kshitij.so |
58 |
otpResponse.setReference_id(0);
|
|
|
59 |
otpResponse.setResult(false);
|
|
|
60 |
otpResponse.setMessage("Maximum limit reached for the day");
|
|
|
61 |
return otpResponse;
|
|
|
62 |
}
|
| 26087 |
tejbeer |
63 |
if (!otps.isEmpty()) {
|
|
|
64 |
if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
|
| 21285 |
kshitij.so |
65 |
otpResponse.setMessage("OTP generated less than 2 minutes ago");
|
|
|
66 |
otpResponse.setReference_id(otps.get(0).getId());
|
|
|
67 |
otpResponse.setResult(true);
|
| 21299 |
kshitij.so |
68 |
otpResponse.setOtp(otps.get(0).getOtp());
|
| 21285 |
kshitij.so |
69 |
return otpResponse;
|
| 26087 |
tejbeer |
70 |
} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
|
| 21285 |
kshitij.so |
71 |
otp = otps.get(0).getOtp();
|
| 26087 |
tejbeer |
72 |
} else {
|
| 21285 |
kshitij.so |
73 |
;
|
|
|
74 |
}
|
|
|
75 |
}
|
| 26087 |
tejbeer |
76 |
if (otp == null || otp.isEmpty()) {
|
| 21285 |
kshitij.so |
77 |
otp = getOtp();
|
|
|
78 |
}
|
| 26087 |
tejbeer |
79 |
|
| 23273 |
ashik.ali |
80 |
Otp otp_bean = new Otp();
|
|
|
81 |
otp_bean.setEmail(email);
|
|
|
82 |
otp_bean.setMobile(phone);
|
|
|
83 |
otp_bean.setOtp(otp);
|
|
|
84 |
otp_bean.setOtpType(otpType);
|
|
|
85 |
otp_bean.setCreatedOn(LocalDateTime.now());
|
|
|
86 |
otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
|
|
|
87 |
otpRepository.persist(otp_bean);
|
| 26657 |
amit.gupta |
88 |
if(prodEnv) {
|
|
|
89 |
sendOtp(otp_bean);
|
|
|
90 |
}
|
| 26087 |
tejbeer |
91 |
|
| 23273 |
ashik.ali |
92 |
otpResponse.setReference_id(otp_bean.getId());
|
| 21285 |
kshitij.so |
93 |
otpResponse.setMessage("OTP generated successfully");
|
|
|
94 |
otpResponse.setResult(true);
|
| 23273 |
ashik.ali |
95 |
otpResponse.setOtp(otp_bean.getOtp());
|
| 21285 |
kshitij.so |
96 |
return otpResponse;
|
|
|
97 |
}
|
|
|
98 |
|
| 26087 |
tejbeer |
99 |
public OTPResponse validateOtp(String email, int reference_id, String otp_number)
|
|
|
100 |
throws Exception, ProfitMandiBusinessException {
|
| 21285 |
kshitij.so |
101 |
OTPResponse otpResponse = new OTPResponse();
|
| 23420 |
ashik.ali |
102 |
Otp otp = otpRepository.selectById(reference_id);
|
| 26087 |
tejbeer |
103 |
otp.setTryCount(otp.getTryCount() + 1);
|
| 21285 |
kshitij.so |
104 |
otpResponse.setReference_id(reference_id);
|
| 26087 |
tejbeer |
105 |
if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)) {
|
| 21285 |
kshitij.so |
106 |
otpResponse.setMessage("Invalid otp");
|
|
|
107 |
otpResponse.setResult(false);
|
| 23273 |
ashik.ali |
108 |
otpRepository.persist(otp);
|
| 21285 |
kshitij.so |
109 |
return otpResponse;
|
|
|
110 |
}
|
| 26087 |
tejbeer |
111 |
if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
|
| 21285 |
kshitij.so |
112 |
otpResponse.setMessage("OTP expired");
|
|
|
113 |
otpResponse.setResult(false);
|
|
|
114 |
return otpResponse;
|
|
|
115 |
}
|
| 26087 |
tejbeer |
116 |
if (otp.getTryCount() > 5) {
|
| 21285 |
kshitij.so |
117 |
otpResponse.setMessage("Maximum try count reached");
|
|
|
118 |
otpResponse.setResult(false);
|
|
|
119 |
return otpResponse;
|
|
|
120 |
}
|
|
|
121 |
otp.setExpired(true);
|
|
|
122 |
otp.setVerified(true);
|
| 23273 |
ashik.ali |
123 |
otpRepository.persist(otp);
|
| 21285 |
kshitij.so |
124 |
otpResponse.setMessage("OTP validated successfully");
|
|
|
125 |
otpResponse.setResult(true);
|
|
|
126 |
return otpResponse;
|
|
|
127 |
}
|
| 26087 |
tejbeer |
128 |
|
| 26657 |
amit.gupta |
129 |
private void sendOtp(Otp otp) throws Exception {
|
|
|
130 |
String msg = java.text.MessageFormat.format(text, otp.getOtp());
|
| 26706 |
amit.gupta |
131 |
String otpString = otp.getOtp();
|
| 26657 |
amit.gupta |
132 |
Utils.sendMailWithAttachments(mailSender, otp.getEmail(), null, "One Time Password - Smartdukaan", msg, null);
|
| 26706 |
amit.gupta |
133 |
try {
|
|
|
134 |
this.sendSms(otpString, otp.getMobile());
|
| 26087 |
tejbeer |
135 |
} catch (Exception e) {
|
| 21499 |
kshitij.so |
136 |
e.printStackTrace();
|
| 26706 |
amit.gupta |
137 |
}
|
| 26657 |
amit.gupta |
138 |
|
| 26087 |
tejbeer |
139 |
|
| 21499 |
kshitij.so |
140 |
}
|
| 26656 |
amit.gupta |
141 |
|
|
|
142 |
public void sendSms(String text, String mobileNumber) throws Exception {
|
| 26706 |
amit.gupta |
143 |
Map<String, String> headersMap = new HashMap<>();
|
|
|
144 |
headersMap.put("Content-Type", "application/json");
|
|
|
145 |
headersMap.put("apikey", "b866f7-c6c483-682ff5-054420-ad9e2c");
|
|
|
146 |
SmsMessage smsMessage = new SmsMessage();
|
|
|
147 |
|
|
|
148 |
Message message = new Message();
|
|
|
149 |
message.setNumber(mobileNumber);
|
|
|
150 |
|
|
|
151 |
Map<String, String> map = new HashMap<>();
|
|
|
152 |
map.put("OTP", text);
|
|
|
153 |
message.setText(map);
|
|
|
154 |
|
|
|
155 |
List<Message> messages = new ArrayList<>();
|
|
|
156 |
messages.add(message);
|
|
|
157 |
|
|
|
158 |
smsMessage.setMessage(messages);
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
restClient.postJson(SMS_GATEWAY, smsMessage, headersMap);
|
| 26656 |
amit.gupta |
163 |
}
|
| 26706 |
amit.gupta |
164 |
|
|
|
165 |
class SmsMessage {
|
|
|
166 |
private String sender = "150423";
|
|
|
167 |
private String templateId = "54";
|
|
|
168 |
private List<Message> message;
|
|
|
169 |
@Override
|
|
|
170 |
public String toString() {
|
|
|
171 |
return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message + "]";
|
|
|
172 |
}
|
|
|
173 |
public String getSender() {
|
|
|
174 |
return sender;
|
|
|
175 |
}
|
|
|
176 |
public void setSender(String sender) {
|
|
|
177 |
this.sender = sender;
|
|
|
178 |
}
|
|
|
179 |
public String getTemplateId() {
|
|
|
180 |
return templateId;
|
|
|
181 |
}
|
|
|
182 |
public void setTemplateId(String templateId) {
|
|
|
183 |
this.templateId = templateId;
|
|
|
184 |
}
|
|
|
185 |
public List<Message> getMessage() {
|
|
|
186 |
return message;
|
|
|
187 |
}
|
|
|
188 |
public void setMessage(List<Message> message) {
|
|
|
189 |
this.message = message;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
class Message {
|
|
|
197 |
private String number;
|
|
|
198 |
public String getNumber() {
|
|
|
199 |
return number;
|
|
|
200 |
}
|
|
|
201 |
public void setNumber(String number) {
|
|
|
202 |
this.number = number;
|
|
|
203 |
}
|
|
|
204 |
private Map<String, String> text;
|
|
|
205 |
private String messageType = "TXT";
|
|
|
206 |
@Override
|
|
|
207 |
public String toString() {
|
|
|
208 |
return "Message [number=" + number + ", text=" + text + ", messageType=" + messageType + "]";
|
|
|
209 |
}
|
|
|
210 |
public Map<String, String> getText() {
|
|
|
211 |
return text;
|
|
|
212 |
}
|
|
|
213 |
public void setText(Map<String, String> text) {
|
|
|
214 |
this.text = text;
|
|
|
215 |
}
|
|
|
216 |
public String getMessageType() {
|
|
|
217 |
return messageType;
|
|
|
218 |
}
|
|
|
219 |
public void setMessageType(String messageType) {
|
|
|
220 |
this.messageType = messageType;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
}
|
| 21285 |
kshitij.so |
225 |
|
|
|
226 |
}
|