Subversion Repositories SmartDukaan

Rev

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

Rev 23568 Rev 26087
Line 1... Line 1...
1
package com.spice.profitmandi.web.processor;
1
package com.spice.profitmandi.web.processor;
2
 
2
 
3
 
-
 
4
import java.io.IOException;
3
import java.io.IOException;
5
import java.net.URISyntaxException;
4
import java.net.URISyntaxException;
6
import java.time.LocalDateTime;
5
import java.time.LocalDateTime;
7
import java.util.List;
6
import java.util.List;
8
import java.util.Random;
7
import java.util.Random;
Line 16... Line 15...
16
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
15
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
17
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
16
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
18
import com.spice.profitmandi.web.res.OTPResponse;
17
import com.spice.profitmandi.web.res.OTPResponse;
19
 
18
 
20
@Component
19
@Component
21
public class OtpProcessor{
20
public class OtpProcessor {
22
 
21
 
23
	private static final int len = 5;
22
	private static final int len = 5;
24
	private static final String numbers ="0123456789";
23
	private static final String numbers = "0123456789";
25
	private static final String text = "Dear Customer, {0} is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone.";
24
	private static final String text = "Dear Customer, {0} is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone.";
26
 
25
 
27
	@Autowired
26
	@Autowired
28
	OtpRepository otpRepository;
27
	OtpRepository otpRepository;
29
 
28
 
30
	private String getOtp(){
29
	private String getOtp() {
31
		Random rndm_method = new Random();
30
		Random rndm_method = new Random();
32
		char[] otp = new char[len];
31
		char[] otp = new char[len];
33
 
32
 
34
		for (int i = 0; i < len; i++)
33
		for (int i = 0; i < len; i++) {
35
		{
-
 
36
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
34
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
37
		}
35
		}
38
		return String.valueOf(otp);
36
		return String.valueOf(otp);
39
	}
37
	}
40
 
38
 
41
	public OTPResponse generateOtp(String email, String phone, OtpType otpType) throws Exception, ProfitMandiBusinessException{
39
	public OTPResponse generateOtp(String email, String phone, OtpType otpType)
-
 
40
			throws Exception, ProfitMandiBusinessException {
42
		OTPResponse otpResponse = new OTPResponse();
41
		OTPResponse otpResponse = new OTPResponse();
43
		List<Otp> otps = otpRepository.selectAllByEmailWithTime(email);
42
		List<Otp> otps = otpRepository.selectAllByEmailWithTime(email);
44
		String otp = null;
43
		String otp = null;
45
		if (otps.size() >=5){
44
		if (otps.size() >= 5) {
46
			otpResponse.setReference_id(0);
45
			otpResponse.setReference_id(0);
47
			otpResponse.setResult(false);
46
			otpResponse.setResult(false);
48
			otpResponse.setMessage("Maximum limit reached for the day");
47
			otpResponse.setMessage("Maximum limit reached for the day");
49
			return otpResponse;
48
			return otpResponse;
50
		}
49
		}
51
		if (!otps.isEmpty()){
50
		if (!otps.isEmpty()) {
52
			if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))){
51
			if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
53
				otpResponse.setMessage("OTP generated less than 2 minutes ago");
52
				otpResponse.setMessage("OTP generated less than 2 minutes ago");
54
				otpResponse.setReference_id(otps.get(0).getId());
53
				otpResponse.setReference_id(otps.get(0).getId());
55
				otpResponse.setResult(true);
54
				otpResponse.setResult(true);
56
				otpResponse.setOtp(otps.get(0).getOtp());
55
				otpResponse.setOtp(otps.get(0).getOtp());
57
				return otpResponse;
56
				return otpResponse;
58
			}
-
 
59
			else if(otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))){
57
			} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
60
				otp = otps.get(0).getOtp();
58
				otp = otps.get(0).getOtp();
61
			}
-
 
62
			else{
59
			} else {
63
				;
60
				;
64
			}
61
			}
65
		}
62
		}
66
		if (otp == null || otp.isEmpty()){
63
		if (otp == null || otp.isEmpty()) {
67
			otp = getOtp();
64
			otp = getOtp();
68
		}
65
		}
69
		sendOtp(otp, phone);
66
		sendOtp(otp, phone);
70
		
67
 
71
		Otp otp_bean = new Otp();
68
		Otp otp_bean = new Otp();
72
		otp_bean.setEmail(email);
69
		otp_bean.setEmail(email);
73
		otp_bean.setMobile(phone);
70
		otp_bean.setMobile(phone);
74
		otp_bean.setOtp(otp);
71
		otp_bean.setOtp(otp);
75
		otp_bean.setOtpType(otpType);
72
		otp_bean.setOtpType(otpType);
76
		otp_bean.setCreatedOn(LocalDateTime.now());
73
		otp_bean.setCreatedOn(LocalDateTime.now());
77
		otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
74
		otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
78
		otpRepository.persist(otp_bean);
75
		otpRepository.persist(otp_bean);
79
		
76
 
80
		otpResponse.setReference_id(otp_bean.getId());
77
		otpResponse.setReference_id(otp_bean.getId());
81
		otpResponse.setMessage("OTP generated successfully");
78
		otpResponse.setMessage("OTP generated successfully");
82
		otpResponse.setResult(true);
79
		otpResponse.setResult(true);
83
		otpResponse.setOtp(otp_bean.getOtp());
80
		otpResponse.setOtp(otp_bean.getOtp());
84
		return otpResponse;
81
		return otpResponse;
85
	}
82
	}
86
 
83
 
87
	public OTPResponse validateOtp(String email, int reference_id, String otp_number) throws Exception, ProfitMandiBusinessException{
84
	public OTPResponse validateOtp(String email, int reference_id, String otp_number)
-
 
85
			throws Exception, ProfitMandiBusinessException {
88
		OTPResponse otpResponse = new OTPResponse();
86
		OTPResponse otpResponse = new OTPResponse();
89
		Otp otp = otpRepository.selectById(reference_id);
87
		Otp otp = otpRepository.selectById(reference_id);
90
		otp.setTryCount(otp.getTryCount()+1);
88
		otp.setTryCount(otp.getTryCount() + 1);
91
		otpResponse.setReference_id(reference_id);
89
		otpResponse.setReference_id(reference_id);
92
		if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)){
90
		if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)) {
93
			otpResponse.setMessage("Invalid otp");
91
			otpResponse.setMessage("Invalid otp");
94
			otpResponse.setResult(false);
92
			otpResponse.setResult(false);
95
			otpRepository.persist(otp);
93
			otpRepository.persist(otp);
96
			return otpResponse;
94
			return otpResponse;
97
		}
95
		}
98
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())){
96
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
99
			otpResponse.setMessage("OTP expired");
97
			otpResponse.setMessage("OTP expired");
100
			otpResponse.setResult(false);
98
			otpResponse.setResult(false);
101
			return otpResponse;
99
			return otpResponse;
102
		}
100
		}
103
		if (otp.getTryCount() >5){
101
		if (otp.getTryCount() > 5) {
104
			otpResponse.setMessage("Maximum try count reached");
102
			otpResponse.setMessage("Maximum try count reached");
105
			otpResponse.setResult(false);
103
			otpResponse.setResult(false);
106
			return otpResponse;
104
			return otpResponse;
107
		}
105
		}
108
		otp.setExpired(true);
106
		otp.setExpired(true);
Line 110... Line 108...
110
		otpRepository.persist(otp);
108
		otpRepository.persist(otp);
111
		otpResponse.setMessage("OTP validated successfully");
109
		otpResponse.setMessage("OTP validated successfully");
112
		otpResponse.setResult(true);
110
		otpResponse.setResult(true);
113
		return otpResponse;
111
		return otpResponse;
114
	}
112
	}
115
	
113
 
116
	private void sendOtp(String otp_text, String phone){
114
	private void sendOtp(String otp_text, String phone) {
117
		String msg = java.text.MessageFormat.format(text, otp_text);
115
		String msg = java.text.MessageFormat.format(text, otp_text);
118
		try {
116
		try {
119
			Utils.sendSms(msg, phone);
117
			Utils.sendSms(msg, phone);
120
		} catch (URISyntaxException e) {
-
 
121
			// TODO Auto-generated catch block
-
 
122
			e.printStackTrace();
-
 
123
		} catch (IOException e) {
118
		} catch (Exception e) {
124
			// TODO Auto-generated catch block
-
 
125
			e.printStackTrace();
119
			e.printStackTrace();
126
		}
120
		}
127
		
121
 
128
	}
122
	}
129
	
-
 
130
 
123
 
131
	
-
 
132
	
-
 
133
}
124
}
134
125