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);
28229 amit.gupta 30
	private static final String SMS_GATEWAY = "http://sms.smartdukaan.com/blank/sms/user/urlsms.php?username=smart@123&pass=Smartdukaan@123&senderid=SMTDKN&dest_mobileno=%s";
21285 kshitij.so 31
	private static final int len = 5;
26087 tejbeer 32
	private static final String numbers = "0123456789";
28228 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.\n";
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
 
26657 amit.gupta 135
	private 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 {
28225 amit.gupta 140
			this.sendSmsNew(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
 
26656 amit.gupta 147
	public void sendSms(String text, String mobileNumber) throws Exception {
26706 amit.gupta 148
		Map<String, String> headersMap = new HashMap<>();
149
		headersMap.put("Content-Type", "application/json");
150
		headersMap.put("apikey", "b866f7-c6c483-682ff5-054420-ad9e2c");
151
		SmsMessage smsMessage = new SmsMessage();
26711 amit.gupta 152
 
26706 amit.gupta 153
		Message message = new Message();
154
		message.setNumber(mobileNumber);
155
 
156
		Map<String, String> map = new HashMap<>();
26709 amit.gupta 157
		map.put("F1", text);
26706 amit.gupta 158
		message.setText(map);
26711 amit.gupta 159
 
26706 amit.gupta 160
		List<Message> messages = new ArrayList<>();
161
		messages.add(message);
26711 amit.gupta 162
 
26706 amit.gupta 163
		smsMessage.setMessage(messages);
26711 amit.gupta 164
 
26712 amit.gupta 165
		String response = restClient.postJson(SMS_GATEWAY, smsMessage, headersMap);
166
		LOGGER.info(response);
26783 amit.gupta 167
 
26656 amit.gupta 168
	}
26711 amit.gupta 169
 
28225 amit.gupta 170
	public void sendSmsNew(String message, String mobileNumber) throws Exception {
28229 amit.gupta 171
		Map<String, String> obj = new HashMap<>();
172
		obj.put("message", message);
173
		String response = restClient.get(String.format(SMS_GATEWAY, mobileNumber), null, null);
28225 amit.gupta 174
		LOGGER.info(response);
175
	}
176
 
26706 amit.gupta 177
	class SmsMessage {
26707 amit.gupta 178
		private String sender = "SMTDKN";
26711 amit.gupta 179
		@JsonProperty(value = "templateid")
26706 amit.gupta 180
		private String templateId = "54";
26783 amit.gupta 181
 
26706 amit.gupta 182
		private List<Message> message;
26711 amit.gupta 183
 
184
		@JsonProperty(value = "messagetype")
185
		private String messageType = "TXT";
186
 
26706 amit.gupta 187
		@Override
188
		public String toString() {
26711 amit.gupta 189
			return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
190
					+ ", messageType=" + messageType + "]";
26706 amit.gupta 191
		}
26711 amit.gupta 192
 
193
		public String getMessageType() {
194
			return messageType;
195
		}
196
 
197
		public void setMessageType(String messageType) {
198
			this.messageType = messageType;
199
		}
200
 
26706 amit.gupta 201
		public String getSender() {
202
			return sender;
203
		}
26711 amit.gupta 204
 
26706 amit.gupta 205
		public void setSender(String sender) {
206
			this.sender = sender;
207
		}
26711 amit.gupta 208
 
26706 amit.gupta 209
		public String getTemplateId() {
210
			return templateId;
211
		}
26711 amit.gupta 212
 
26706 amit.gupta 213
		public void setTemplateId(String templateId) {
214
			this.templateId = templateId;
215
		}
26711 amit.gupta 216
 
26706 amit.gupta 217
		public List<Message> getMessage() {
218
			return message;
219
		}
26711 amit.gupta 220
 
26706 amit.gupta 221
		public void setMessage(List<Message> message) {
222
			this.message = message;
223
		}
26711 amit.gupta 224
 
26706 amit.gupta 225
	}
26711 amit.gupta 226
 
26706 amit.gupta 227
	class Message {
228
		private String number;
26711 amit.gupta 229
 
26706 amit.gupta 230
		public String getNumber() {
231
			return number;
232
		}
26711 amit.gupta 233
 
26706 amit.gupta 234
		public void setNumber(String number) {
235
			this.number = number;
236
		}
26711 amit.gupta 237
 
26706 amit.gupta 238
		private Map<String, String> text;
26711 amit.gupta 239
 
26706 amit.gupta 240
		@Override
241
		public String toString() {
26711 amit.gupta 242
			return "Message [number=" + number + ", text=" + text + "]";
26706 amit.gupta 243
		}
26711 amit.gupta 244
 
26706 amit.gupta 245
		public Map<String, String> getText() {
246
			return text;
247
		}
26711 amit.gupta 248
 
26706 amit.gupta 249
		public void setText(Map<String, String> text) {
250
			this.text = text;
251
		}
26711 amit.gupta 252
 
26706 amit.gupta 253
	}
21285 kshitij.so 254
 
255
}