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;
26656 amit.gupta 4
import java.util.HashMap;
21285 kshitij.so 5
import java.util.List;
26656 amit.gupta 6
import java.util.Map;
21285 kshitij.so 7
import java.util.Random;
8
 
26712 amit.gupta 9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
21285 kshitij.so 11
import org.springframework.beans.factory.annotation.Autowired;
26649 amit.gupta 12
import org.springframework.beans.factory.annotation.Value;
26657 amit.gupta 13
import org.springframework.mail.javamail.JavaMailSender;
21285 kshitij.so 14
import org.springframework.stereotype.Component;
15
 
26709 amit.gupta 16
import com.fasterxml.jackson.annotation.JsonProperty;
28305 amit.gupta 17
import com.google.common.collect.ImmutableMap;
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;
28305 amit.gupta 32
	private static final String SENDER = "SMTDKN";
33
 
34
 
26087 tejbeer 35
	private static final String numbers = "0123456789";
28258 amit.gupta 36
	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";
28305 amit.gupta 37
	private static final String OTP_TEMPLATE_ID = "1507161889822750240";
38
	public static final String SELF_CANCELLED_TEMPLATE = "Order Cancelled PO%s: Dear Customer, your order with SmartDukaan of %s has been cancelled on %s as per your request. Team SmartDukaan";
39
	public static final String SELF_CANCELLED_TEMPLATE_ID = "1507161943392646481";
28375 amit.gupta 40
	public static final String TEMPLATE_ORDER_CREATED = "Order Placed PO%d: Dear Customer, your order with SmartDukaan of %s worth Rs.%.0f has been placed successfully. Our team will get in touch with you soon. Team SmartDukaan";
41
	public static final String TEMPLATE_ORDER_CREATED_ID = "1507162021138742118";
21285 kshitij.so 42
 
43
	@Autowired
44
	OtpRepository otpRepository;
26711 amit.gupta 45
 
46
	@Autowired
47
	JavaMailSender mailSender;
48
 
26649 amit.gupta 49
	@Value("${prod}")
50
	private boolean prodEnv;
21285 kshitij.so 51
 
26711 amit.gupta 52
	@Autowired
53
	RestClient restClient;
26649 amit.gupta 54
 
26087 tejbeer 55
	private String getOtp() {
21285 kshitij.so 56
		Random rndm_method = new Random();
57
		char[] otp = new char[len];
58
 
26087 tejbeer 59
		for (int i = 0; i < len; i++) {
21285 kshitij.so 60
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
61
		}
62
		return String.valueOf(otp);
63
	}
64
 
26783 amit.gupta 65
	public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
21285 kshitij.so 66
		OTPResponse otpResponse = new OTPResponse();
26783 amit.gupta 67
		List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
68
		String otpCode = null;
26087 tejbeer 69
		if (otps.size() >= 5) {
21285 kshitij.so 70
			otpResponse.setReference_id(0);
71
			otpResponse.setResult(false);
72
			otpResponse.setMessage("Maximum limit reached for the day");
73
			return otpResponse;
74
		}
26087 tejbeer 75
		if (!otps.isEmpty()) {
76
			if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
21285 kshitij.so 77
				otpResponse.setMessage("OTP generated less than 2 minutes ago");
78
				otpResponse.setReference_id(otps.get(0).getId());
79
				otpResponse.setResult(true);
21299 kshitij.so 80
				otpResponse.setOtp(otps.get(0).getOtp());
21285 kshitij.so 81
				return otpResponse;
26087 tejbeer 82
			} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
26783 amit.gupta 83
				otpCode = otps.get(0).getOtp();
26087 tejbeer 84
			} else {
21285 kshitij.so 85
				;
86
			}
87
		}
26783 amit.gupta 88
		if (otpCode == null || otpCode.isEmpty()) {
89
			otpCode = getOtp();
21285 kshitij.so 90
		}
26087 tejbeer 91
 
26783 amit.gupta 92
		Otp otp = new Otp();
93
		otp.setMobile(mobile);
94
		otp.setOtp(otpCode);
95
		otp.setOtpType(otpType);
96
		otp.setCreatedOn(LocalDateTime.now());
97
		otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
98
		otpRepository.persist(otp);
26711 amit.gupta 99
		if (prodEnv) {
26783 amit.gupta 100
			sendOtp(otp);
26657 amit.gupta 101
		}
26087 tejbeer 102
 
26783 amit.gupta 103
		otpResponse.setReference_id(otp.getId());
21285 kshitij.so 104
		otpResponse.setMessage("OTP generated successfully");
105
		otpResponse.setResult(true);
26783 amit.gupta 106
		otpResponse.setOtp(otp.getOtp());
21285 kshitij.so 107
		return otpResponse;
108
	}
109
 
26783 amit.gupta 110
	public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
26087 tejbeer 111
			throws Exception, ProfitMandiBusinessException {
21285 kshitij.so 112
		OTPResponse otpResponse = new OTPResponse();
26783 amit.gupta 113
		Otp otp = otpRepository.selectById(referenceId);
26087 tejbeer 114
		otp.setTryCount(otp.getTryCount() + 1);
26783 amit.gupta 115
		otpResponse.setReference_id(referenceId);
116
		if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
21285 kshitij.so 117
			otpResponse.setMessage("Invalid otp");
118
			otpResponse.setResult(false);
119
			return otpResponse;
120
		}
26087 tejbeer 121
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
21285 kshitij.so 122
			otpResponse.setMessage("OTP expired");
123
			otpResponse.setResult(false);
124
			return otpResponse;
125
		}
26087 tejbeer 126
		if (otp.getTryCount() > 5) {
21285 kshitij.so 127
			otpResponse.setMessage("Maximum try count reached");
128
			otpResponse.setResult(false);
129
			return otpResponse;
130
		}
131
		otp.setExpired(true);
132
		otp.setVerified(true);
23273 ashik.ali 133
		otpRepository.persist(otp);
21285 kshitij.so 134
		otpResponse.setMessage("OTP validated successfully");
135
		otpResponse.setResult(true);
136
		return otpResponse;
137
	}
26087 tejbeer 138
 
28234 amit.gupta 139
	public void sendOtp(Otp otp) throws Exception {
28225 amit.gupta 140
		// In case of Cant receive SMS?
141
		// String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
26706 amit.gupta 142
		String otpString = otp.getOtp();
143
		try {
28305 amit.gupta 144
			this.sendSms(OTP_TEMPLATE_ID, String.format(OTP_TEMPLATE, otpString), otp.getMobile());
26087 tejbeer 145
		} catch (Exception e) {
21499 kshitij.so 146
			e.printStackTrace();
26706 amit.gupta 147
		}
26087 tejbeer 148
 
21499 kshitij.so 149
	}
26711 amit.gupta 150
 
28305 amit.gupta 151
	public void sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
26706 amit.gupta 152
		Map<String, String> map = new HashMap<>();
28234 amit.gupta 153
 
28305 amit.gupta 154
		map.put("sender", SENDER);
28234 amit.gupta 155
		map.put("messagetype", "TXT");
28259 amit.gupta 156
		map.put("apikey", "b866f7-c6c483-682ff5-054420-ad9e2c");
28305 amit.gupta 157
 
158
		map.put("numbers", "91"+mobileNumber);
28236 amit.gupta 159
		LOGGER.info("Message {}", message);
28235 amit.gupta 160
		//OTP Message Template
28305 amit.gupta 161
		map.put("message", message);
162
		map.put("dlttempid", dltTemplateId);
26711 amit.gupta 163
 
28260 amit.gupta 164
		String response = restClient.post(SMS_GATEWAY, map, new HashMap<>());
26712 amit.gupta 165
		LOGGER.info(response);
26783 amit.gupta 166
 
26656 amit.gupta 167
	}
26711 amit.gupta 168
 
26706 amit.gupta 169
	class SmsMessage {
26707 amit.gupta 170
		private String sender = "SMTDKN";
26711 amit.gupta 171
		@JsonProperty(value = "templateid")
26706 amit.gupta 172
		private String templateId = "54";
26783 amit.gupta 173
 
26706 amit.gupta 174
		private List<Message> message;
26711 amit.gupta 175
 
176
		@JsonProperty(value = "messagetype")
177
		private String messageType = "TXT";
178
 
26706 amit.gupta 179
		@Override
180
		public String toString() {
26711 amit.gupta 181
			return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
182
					+ ", messageType=" + messageType + "]";
26706 amit.gupta 183
		}
26711 amit.gupta 184
 
185
		public String getMessageType() {
186
			return messageType;
187
		}
188
 
189
		public void setMessageType(String messageType) {
190
			this.messageType = messageType;
191
		}
192
 
26706 amit.gupta 193
		public String getSender() {
194
			return sender;
195
		}
26711 amit.gupta 196
 
26706 amit.gupta 197
		public void setSender(String sender) {
198
			this.sender = sender;
199
		}
26711 amit.gupta 200
 
26706 amit.gupta 201
		public String getTemplateId() {
202
			return templateId;
203
		}
26711 amit.gupta 204
 
26706 amit.gupta 205
		public void setTemplateId(String templateId) {
206
			this.templateId = templateId;
207
		}
26711 amit.gupta 208
 
26706 amit.gupta 209
		public List<Message> getMessage() {
210
			return message;
211
		}
26711 amit.gupta 212
 
26706 amit.gupta 213
		public void setMessage(List<Message> message) {
214
			this.message = message;
215
		}
26711 amit.gupta 216
 
26706 amit.gupta 217
	}
26711 amit.gupta 218
 
26706 amit.gupta 219
	class Message {
220
		private String number;
26711 amit.gupta 221
 
26706 amit.gupta 222
		public String getNumber() {
223
			return number;
224
		}
26711 amit.gupta 225
 
26706 amit.gupta 226
		public void setNumber(String number) {
227
			this.number = number;
228
		}
26711 amit.gupta 229
 
26706 amit.gupta 230
		private Map<String, String> text;
26711 amit.gupta 231
 
26706 amit.gupta 232
		@Override
233
		public String toString() {
26711 amit.gupta 234
			return "Message [number=" + number + ", text=" + text + "]";
26706 amit.gupta 235
		}
26711 amit.gupta 236
 
26706 amit.gupta 237
		public Map<String, String> getText() {
238
			return text;
239
		}
26711 amit.gupta 240
 
26706 amit.gupta 241
		public void setText(Map<String, String> text) {
242
			this.text = text;
243
		}
26711 amit.gupta 244
 
26706 amit.gupta 245
	}
21285 kshitij.so 246
 
247
}