Subversion Repositories SmartDukaan

Rev

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

Rev 26712 Rev 26783
Line 14... Line 14...
14
import org.springframework.mail.javamail.JavaMailSender;
14
import org.springframework.mail.javamail.JavaMailSender;
15
import org.springframework.stereotype.Component;
15
import org.springframework.stereotype.Component;
16
 
16
 
17
import com.fasterxml.jackson.annotation.JsonProperty;
17
import com.fasterxml.jackson.annotation.JsonProperty;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.util.Utils;
-
 
20
import com.spice.profitmandi.common.web.client.RestClient;
19
import com.spice.profitmandi.common.web.client.RestClient;
21
import com.spice.profitmandi.dao.entity.dtr.Otp;
20
import com.spice.profitmandi.dao.entity.dtr.Otp;
22
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
21
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
23
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
22
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
24
import com.spice.profitmandi.service.scheme.SchemeServiceImpl;
23
import com.spice.profitmandi.service.scheme.SchemeServiceImpl;
Line 29... Line 28...
29
 
28
 
30
	private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
29
	private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
31
	private static final String SMS_GATEWAY = "http://sms.smartdukaan.com/index.php/sms/templatesms";
30
	private static final String SMS_GATEWAY = "http://sms.smartdukaan.com/index.php/sms/templatesms";
32
	private static final int len = 5;
31
	private static final int len = 5;
33
	private static final String numbers = "0123456789";
32
	private static final String numbers = "0123456789";
34
	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.";
33
	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.\n";
-
 
34
 
-
 
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";
35
 
38
 
36
	@Autowired
39
	@Autowired
37
	OtpRepository otpRepository;
40
	OtpRepository otpRepository;
38
 
41
 
39
	@Autowired
42
	@Autowired
Line 53... Line 56...
53
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
56
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
54
		}
57
		}
55
		return String.valueOf(otp);
58
		return String.valueOf(otp);
56
	}
59
	}
57
 
60
 
58
	public OTPResponse generateOtp(String email, String phone, OtpType otpType)
61
	public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
59
			throws Exception, ProfitMandiBusinessException {
-
 
60
		OTPResponse otpResponse = new OTPResponse();
62
		OTPResponse otpResponse = new OTPResponse();
61
		List<Otp> otps = otpRepository.selectAllByEmailWithTime(email);
63
		List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
62
		String otp = null;
64
		String otpCode = null;
63
		if (otps.size() >= 5) {
65
		if (otps.size() >= 5) {
64
			otpResponse.setReference_id(0);
66
			otpResponse.setReference_id(0);
65
			otpResponse.setResult(false);
67
			otpResponse.setResult(false);
66
			otpResponse.setMessage("Maximum limit reached for the day");
68
			otpResponse.setMessage("Maximum limit reached for the day");
67
			return otpResponse;
69
			return otpResponse;
Line 72... Line 74...
72
				otpResponse.setReference_id(otps.get(0).getId());
74
				otpResponse.setReference_id(otps.get(0).getId());
73
				otpResponse.setResult(true);
75
				otpResponse.setResult(true);
74
				otpResponse.setOtp(otps.get(0).getOtp());
76
				otpResponse.setOtp(otps.get(0).getOtp());
75
				return otpResponse;
77
				return otpResponse;
76
			} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
78
			} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
77
				otp = otps.get(0).getOtp();
79
				otpCode = otps.get(0).getOtp();
78
			} else {
80
			} else {
79
				;
81
				;
80
			}
82
			}
81
		}
83
		}
82
		if (otp == null || otp.isEmpty()) {
84
		if (otpCode == null || otpCode.isEmpty()) {
83
			otp = getOtp();
85
			otpCode = getOtp();
84
		}
86
		}
85
 
87
 
86
		Otp otp_bean = new Otp();
88
		Otp otp = new Otp();
87
		otp_bean.setEmail(email);
-
 
88
		otp_bean.setMobile(phone);
89
		otp.setMobile(mobile);
89
		otp_bean.setOtp(otp);
90
		otp.setOtp(otpCode);
90
		otp_bean.setOtpType(otpType);
91
		otp.setOtpType(otpType);
91
		otp_bean.setCreatedOn(LocalDateTime.now());
92
		otp.setCreatedOn(LocalDateTime.now());
92
		otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
93
		otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
93
		otpRepository.persist(otp_bean);
94
		otpRepository.persist(otp);
94
		if (prodEnv) {
95
		if (prodEnv) {
95
			sendOtp(otp_bean);
96
			sendOtp(otp);
96
		}
97
		}
97
 
98
 
98
		otpResponse.setReference_id(otp_bean.getId());
99
		otpResponse.setReference_id(otp.getId());
99
		otpResponse.setMessage("OTP generated successfully");
100
		otpResponse.setMessage("OTP generated successfully");
100
		otpResponse.setResult(true);
101
		otpResponse.setResult(true);
101
		otpResponse.setOtp(otp_bean.getOtp());
102
		otpResponse.setOtp(otp.getOtp());
102
		return otpResponse;
103
		return otpResponse;
103
	}
104
	}
104
 
105
 
105
	public OTPResponse validateOtp(String email, int reference_id, String otp_number)
106
	public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
106
			throws Exception, ProfitMandiBusinessException {
107
			throws Exception, ProfitMandiBusinessException {
107
		OTPResponse otpResponse = new OTPResponse();
108
		OTPResponse otpResponse = new OTPResponse();
108
		Otp otp = otpRepository.selectById(reference_id);
109
		Otp otp = otpRepository.selectById(referenceId);
109
		otp.setTryCount(otp.getTryCount() + 1);
110
		otp.setTryCount(otp.getTryCount() + 1);
110
		otpResponse.setReference_id(reference_id);
111
		otpResponse.setReference_id(referenceId);
111
		if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)) {
112
		if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
112
			otpResponse.setMessage("Invalid otp");
113
			otpResponse.setMessage("Invalid otp");
113
			otpResponse.setResult(false);
114
			otpResponse.setResult(false);
114
			otpRepository.persist(otp);
-
 
115
			return otpResponse;
115
			return otpResponse;
116
		}
116
		}
117
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
117
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
118
			otpResponse.setMessage("OTP expired");
118
			otpResponse.setMessage("OTP expired");
119
			otpResponse.setResult(false);
119
			otpResponse.setResult(false);
Line 131... Line 131...
131
		otpResponse.setResult(true);
131
		otpResponse.setResult(true);
132
		return otpResponse;
132
		return otpResponse;
133
	}
133
	}
134
 
134
 
135
	private void sendOtp(Otp otp) throws Exception {
135
	private void sendOtp(Otp otp) throws Exception {
-
 
136
		//In case of Cant receive SMS?
136
		String msg = java.text.MessageFormat.format(text, otp.getOtp());
137
		//String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
137
		String otpString = otp.getOtp();
138
		String otpString = otp.getOtp();
138
		Utils.sendMailWithAttachments(mailSender, otp.getEmail(), null, "One Time Password - Smartdukaan", msg, null);
-
 
139
		try {
139
		try {
140
			this.sendSms(otpString, otp.getMobile());
140
			this.sendSms(otpString, otp.getMobile());
141
		} catch (Exception e) {
141
		} catch (Exception e) {
142
			e.printStackTrace();
142
			e.printStackTrace();
143
		}
143
		}
Line 162... Line 162...
162
 
162
 
163
		smsMessage.setMessage(messages);
163
		smsMessage.setMessage(messages);
164
 
164
 
165
		String response = restClient.postJson(SMS_GATEWAY, smsMessage, headersMap);
165
		String response = restClient.postJson(SMS_GATEWAY, smsMessage, headersMap);
166
		LOGGER.info(response);
166
		LOGGER.info(response);
167
		
167
 
168
	}
168
	}
169
 
169
 
170
	class SmsMessage {
170
	class SmsMessage {
171
		private String sender = "SMTDKN";
171
		private String sender = "SMTDKN";
172
		@JsonProperty(value = "templateid")
172
		@JsonProperty(value = "templateid")
173
		private String templateId = "54";
173
		private String templateId = "54";
174
		
174
 
175
		private List<Message> message;
175
		private List<Message> message;
176
 
176
 
177
		@JsonProperty(value = "messagetype")
177
		@JsonProperty(value = "messagetype")
178
		private String messageType = "TXT";
178
		private String messageType = "TXT";
179
 
179
 
180
		@Override
180
		@Override
181
		public String toString() {
181
		public String toString() {
182
			return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
182
			return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
183
					+ ", messageType=" + messageType + "]";
183
					+ ", messageType=" + messageType + "]";
184
		}
184
		}
185
		
-
 
186
		
-
 
187
 
185
 
188
		public String getMessageType() {
186
		public String getMessageType() {
189
			return messageType;
187
			return messageType;
190
		}
188
		}
191
 
189
 
192
 
-
 
193
 
-
 
194
		public void setMessageType(String messageType) {
190
		public void setMessageType(String messageType) {
195
			this.messageType = messageType;
191
			this.messageType = messageType;
196
		}
192
		}
197
 
193
 
198
 
-
 
199
 
-
 
200
		public String getSender() {
194
		public String getSender() {
201
			return sender;
195
			return sender;
202
		}
196
		}
203
 
197
 
204
		public void setSender(String sender) {
198
		public void setSender(String sender) {