Subversion Repositories SmartDukaan

Rev

Rev 28375 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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