Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
26712 amit.gupta 10
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.Logger;
21285 kshitij.so 12
import org.springframework.beans.factory.annotation.Autowired;
26649 amit.gupta 13
import org.springframework.beans.factory.annotation.Value;
26657 amit.gupta 14
import org.springframework.mail.javamail.JavaMailSender;
21285 kshitij.so 15
import org.springframework.stereotype.Component;
16
 
26709 amit.gupta 17
import com.fasterxml.jackson.annotation.JsonProperty;
21285 kshitij.so 18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26656 amit.gupta 19
import com.spice.profitmandi.common.web.client.RestClient;
21735 ashik.ali 20
import com.spice.profitmandi.dao.entity.dtr.Otp;
21
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
22
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
26712 amit.gupta 23
import com.spice.profitmandi.service.scheme.SchemeServiceImpl;
21285 kshitij.so 24
import com.spice.profitmandi.web.res.OTPResponse;
25
 
26
@Component
26087 tejbeer 27
public class OtpProcessor {
21285 kshitij.so 28
 
26712 amit.gupta 29
	private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
28234 amit.gupta 30
	private static final String SMS_GATEWAY = "http://api.pinnacle.in/index.php/sms/send";
21285 kshitij.so 31
	private static final int len = 5;
26087 tejbeer 32
	private static final String numbers = "0123456789";
28258 amit.gupta 33
	private static final String OTP_TEMPLATE = "Dear Customer, %s is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone";
21285 kshitij.so 34
 
26783 amit.gupta 35
	// This should remain unused reference template id is -->
36
	private static final String orderConfirmationText = "Dear customer, your order of <productname> <+3 more pcs> has been  successfully placed with SmartDukaan. We will get in touch with you soon.\n"
37
			+ "SmartDukaan Team";
38
 
21285 kshitij.so 39
	@Autowired
40
	OtpRepository otpRepository;
26711 amit.gupta 41
 
42
	@Autowired
43
	JavaMailSender mailSender;
44
 
26649 amit.gupta 45
	@Value("${prod}")
46
	private boolean prodEnv;
21285 kshitij.so 47
 
26711 amit.gupta 48
	@Autowired
49
	RestClient restClient;
26649 amit.gupta 50
 
26087 tejbeer 51
	private String getOtp() {
21285 kshitij.so 52
		Random rndm_method = new Random();
53
		char[] otp = new char[len];
54
 
26087 tejbeer 55
		for (int i = 0; i < len; i++) {
21285 kshitij.so 56
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
57
		}
58
		return String.valueOf(otp);
59
	}
60
 
26783 amit.gupta 61
	public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
21285 kshitij.so 62
		OTPResponse otpResponse = new OTPResponse();
26783 amit.gupta 63
		List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
64
		String otpCode = null;
26087 tejbeer 65
		if (otps.size() >= 5) {
21285 kshitij.so 66
			otpResponse.setReference_id(0);
67
			otpResponse.setResult(false);
68
			otpResponse.setMessage("Maximum limit reached for the day");
69
			return otpResponse;
70
		}
26087 tejbeer 71
		if (!otps.isEmpty()) {
72
			if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
21285 kshitij.so 73
				otpResponse.setMessage("OTP generated less than 2 minutes ago");
74
				otpResponse.setReference_id(otps.get(0).getId());
75
				otpResponse.setResult(true);
21299 kshitij.so 76
				otpResponse.setOtp(otps.get(0).getOtp());
21285 kshitij.so 77
				return otpResponse;
26087 tejbeer 78
			} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
26783 amit.gupta 79
				otpCode = otps.get(0).getOtp();
26087 tejbeer 80
			} else {
21285 kshitij.so 81
				;
82
			}
83
		}
26783 amit.gupta 84
		if (otpCode == null || otpCode.isEmpty()) {
85
			otpCode = getOtp();
21285 kshitij.so 86
		}
26087 tejbeer 87
 
26783 amit.gupta 88
		Otp otp = new Otp();
89
		otp.setMobile(mobile);
90
		otp.setOtp(otpCode);
91
		otp.setOtpType(otpType);
92
		otp.setCreatedOn(LocalDateTime.now());
93
		otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
94
		otpRepository.persist(otp);
26711 amit.gupta 95
		if (prodEnv) {
26783 amit.gupta 96
			sendOtp(otp);
26657 amit.gupta 97
		}
26087 tejbeer 98
 
26783 amit.gupta 99
		otpResponse.setReference_id(otp.getId());
21285 kshitij.so 100
		otpResponse.setMessage("OTP generated successfully");
101
		otpResponse.setResult(true);
26783 amit.gupta 102
		otpResponse.setOtp(otp.getOtp());
21285 kshitij.so 103
		return otpResponse;
104
	}
105
 
26783 amit.gupta 106
	public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
26087 tejbeer 107
			throws Exception, ProfitMandiBusinessException {
21285 kshitij.so 108
		OTPResponse otpResponse = new OTPResponse();
26783 amit.gupta 109
		Otp otp = otpRepository.selectById(referenceId);
26087 tejbeer 110
		otp.setTryCount(otp.getTryCount() + 1);
26783 amit.gupta 111
		otpResponse.setReference_id(referenceId);
112
		if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
21285 kshitij.so 113
			otpResponse.setMessage("Invalid otp");
114
			otpResponse.setResult(false);
115
			return otpResponse;
116
		}
26087 tejbeer 117
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
21285 kshitij.so 118
			otpResponse.setMessage("OTP expired");
119
			otpResponse.setResult(false);
120
			return otpResponse;
121
		}
26087 tejbeer 122
		if (otp.getTryCount() > 5) {
21285 kshitij.so 123
			otpResponse.setMessage("Maximum try count reached");
124
			otpResponse.setResult(false);
125
			return otpResponse;
126
		}
127
		otp.setExpired(true);
128
		otp.setVerified(true);
23273 ashik.ali 129
		otpRepository.persist(otp);
21285 kshitij.so 130
		otpResponse.setMessage("OTP validated successfully");
131
		otpResponse.setResult(true);
132
		return otpResponse;
133
	}
26087 tejbeer 134
 
28234 amit.gupta 135
	public void sendOtp(Otp otp) throws Exception {
28225 amit.gupta 136
		// In case of Cant receive SMS?
137
		// String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
26706 amit.gupta 138
		String otpString = otp.getOtp();
139
		try {
28234 amit.gupta 140
			this.sendSms(String.format(OTP_TEMPLATE, otpString), otp.getMobile());
26087 tejbeer 141
		} catch (Exception e) {
21499 kshitij.so 142
			e.printStackTrace();
26706 amit.gupta 143
		}
26087 tejbeer 144
 
21499 kshitij.so 145
	}
26711 amit.gupta 146
 
28234 amit.gupta 147
	public void sendSms(String message, String mobileNumber) throws Exception {
26706 amit.gupta 148
		Map<String, String> map = new HashMap<>();
28234 amit.gupta 149
 
150
		map.put("sender", "SMTDKN");
151
		map.put("numbers", "91"+mobileNumber);
152
		map.put("message", message);
153
		map.put("messagetype", "TXT");
28259 amit.gupta 154
		map.put("apikey", "b866f7-c6c483-682ff5-054420-ad9e2c");
28236 amit.gupta 155
		LOGGER.info("Message {}", message);
28235 amit.gupta 156
		//OTP Message Template
28258 amit.gupta 157
		map.put("dlttempid", "1507161889822750240");
28234 amit.gupta 158
		//SD
28235 amit.gupta 159
		//map.put("dltentityid", "1501589330000013091");
28234 amit.gupta 160
		//SMTDKN
28235 amit.gupta 161
		//map.put("dltheaderid", "1505159255691251261");
28234 amit.gupta 162
		//Consent Template
28235 amit.gupta 163
		//map.put("dlttagid", "");
28234 amit.gupta 164
		//Speqtra Innov - Our SMS Provider
28235 amit.gupta 165
		//map.put("tmid", "1502472910000014692");
28234 amit.gupta 166
 
26711 amit.gupta 167
 
28260 amit.gupta 168
		String response = restClient.post(SMS_GATEWAY, map, new HashMap<>());
26712 amit.gupta 169
		LOGGER.info(response);
26783 amit.gupta 170
 
26656 amit.gupta 171
	}
26711 amit.gupta 172
 
28225 amit.gupta 173
	public void sendSmsNew(String message, String mobileNumber) throws Exception {
28229 amit.gupta 174
		Map<String, String> obj = new HashMap<>();
175
		obj.put("message", message);
176
		String response = restClient.get(String.format(SMS_GATEWAY, mobileNumber), null, null);
28225 amit.gupta 177
		LOGGER.info(response);
178
	}
179
 
26706 amit.gupta 180
	class SmsMessage {
26707 amit.gupta 181
		private String sender = "SMTDKN";
26711 amit.gupta 182
		@JsonProperty(value = "templateid")
26706 amit.gupta 183
		private String templateId = "54";
26783 amit.gupta 184
 
26706 amit.gupta 185
		private List<Message> message;
26711 amit.gupta 186
 
187
		@JsonProperty(value = "messagetype")
188
		private String messageType = "TXT";
189
 
26706 amit.gupta 190
		@Override
191
		public String toString() {
26711 amit.gupta 192
			return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
193
					+ ", messageType=" + messageType + "]";
26706 amit.gupta 194
		}
26711 amit.gupta 195
 
196
		public String getMessageType() {
197
			return messageType;
198
		}
199
 
200
		public void setMessageType(String messageType) {
201
			this.messageType = messageType;
202
		}
203
 
26706 amit.gupta 204
		public String getSender() {
205
			return sender;
206
		}
26711 amit.gupta 207
 
26706 amit.gupta 208
		public void setSender(String sender) {
209
			this.sender = sender;
210
		}
26711 amit.gupta 211
 
26706 amit.gupta 212
		public String getTemplateId() {
213
			return templateId;
214
		}
26711 amit.gupta 215
 
26706 amit.gupta 216
		public void setTemplateId(String templateId) {
217
			this.templateId = templateId;
218
		}
26711 amit.gupta 219
 
26706 amit.gupta 220
		public List<Message> getMessage() {
221
			return message;
222
		}
26711 amit.gupta 223
 
26706 amit.gupta 224
		public void setMessage(List<Message> message) {
225
			this.message = message;
226
		}
26711 amit.gupta 227
 
26706 amit.gupta 228
	}
26711 amit.gupta 229
 
26706 amit.gupta 230
	class Message {
231
		private String number;
26711 amit.gupta 232
 
26706 amit.gupta 233
		public String getNumber() {
234
			return number;
235
		}
26711 amit.gupta 236
 
26706 amit.gupta 237
		public void setNumber(String number) {
238
			this.number = number;
239
		}
26711 amit.gupta 240
 
26706 amit.gupta 241
		private Map<String, String> text;
26711 amit.gupta 242
 
26706 amit.gupta 243
		@Override
244
		public String toString() {
26711 amit.gupta 245
			return "Message [number=" + number + ", text=" + text + "]";
26706 amit.gupta 246
		}
26711 amit.gupta 247
 
26706 amit.gupta 248
		public Map<String, String> getText() {
249
			return text;
250
		}
26711 amit.gupta 251
 
26706 amit.gupta 252
		public void setText(Map<String, String> text) {
253
			this.text = text;
254
		}
26711 amit.gupta 255
 
26706 amit.gupta 256
	}
21285 kshitij.so 257
 
258
}