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