Subversion Repositories SmartDukaan

Rev

Rev 36399 | Rev 37632 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
33510 tejus.loha 1
package com.spice.profitmandi.dao.service;
21285 kshitij.so 2
 
33510 tejus.loha 3
import com.fasterxml.jackson.annotation.JsonProperty;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33577 tejus.loha 5
import com.spice.profitmandi.common.util.Utils;
33510 tejus.loha 6
import com.spice.profitmandi.common.web.client.RestClient;
7
import com.spice.profitmandi.dao.entity.dtr.Otp;
8
import com.spice.profitmandi.dao.entity.onBoarding.LoiForm;
9
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
10
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
11
import com.spice.profitmandi.dao.repository.user.LoiFormRepository;
35963 amit 12
import com.spice.profitmandi.service.mail.MailOutboxService;
26712 amit.gupta 13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
21285 kshitij.so 15
import org.springframework.beans.factory.annotation.Autowired;
26649 amit.gupta 16
import org.springframework.beans.factory.annotation.Value;
26657 amit.gupta 17
import org.springframework.mail.javamail.JavaMailSender;
21285 kshitij.so 18
import org.springframework.stereotype.Component;
19
 
33510 tejus.loha 20
import java.time.LocalDateTime;
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Random;
21285 kshitij.so 25
 
26
@Component
26087 tejbeer 27
public class OtpProcessor {
21285 kshitij.so 28
 
33510 tejus.loha 29
    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";
30
    public static final String SELF_CANCELLED_TEMPLATE_ID = "1507161943392646481";
31
    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";
32
    public static final String TEMPLATE_ORDER_CREATED_ID = "1507162021138742118";
33
    private static final Logger LOGGER = LogManager.getLogger(OtpProcessor.class);
31828 amit.gupta 34
    private static final String SMS_GATEWAY = "http://sms.speqtrainnov.com/api/v4/?api_key=A7897cd4627a8781e176fd31710b057a9";
35
    private static final int len = 5;
36
    private static final String SENDER = "SMTDKN";
37
    private static final String numbers = "0123456789";
38
    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";
39
    private static final String OTP_TEMPLATE_ID = "1507161889822750240";
33577 tejus.loha 40
    //LOI OTP
41
    public static final String LOI_ACCEPTANCE_OTP_TEMPLATE_ID = "1707171687739961361";
42
    public static final String DLT_PRINCIPLE_ENTITY_ID = "1501589330000013091";
43
    public static final String LOI_ACCEPTANCE_OTP_TEMPLATE = "Dear %s,\nThank you for showing your interest in the SmartDukaan franchise.\n%s is the OTP for your LOI acceptance.\nPlease read the LOI and provide your consent by sharing OTP with the SmartDukaan team.\nBest Regards,\nSmartDukaan Team.";
36240 vikas 44
    private static final String USER = "devkinandan.lal";
45
    private static final String PASS = "smartdukaan@123";
34014 amit.gupta 46
    private static final String SPECTRA_TELEMARKETER = "1502472910000014692";
36240 vikas 47
    //private static String NEXG_API_ENDPOINT = "https://api2.nexgplatforms.com/sms/1/text/query";
48
    private static String NEXG_API_ENDPOINT = "https://automate.nexgplatforms.com/api/v1/sentsms";
26711 amit.gupta 49
 
31828 amit.gupta 50
    @Autowired
51
    OtpRepository otpRepository;
26711 amit.gupta 52
 
31828 amit.gupta 53
    @Autowired
36399 amit 54
    JavaMailSender gmailRelaySender;
33510 tejus.loha 55
    @Autowired
35963 amit 56
    MailOutboxService mailOutboxService;
57
    @Autowired
33510 tejus.loha 58
    RestClient restClient;
59
    @Autowired
33867 tejus.loha 60
    LoiFormRepository loiFormRepository;
31828 amit.gupta 61
    @Value("${prod}")
62
    private boolean prodEnv;
26649 amit.gupta 63
 
31828 amit.gupta 64
    private String getOtp() {
65
        Random rndm_method = new Random();
66
        char[] otp = new char[len];
21285 kshitij.so 67
 
31828 amit.gupta 68
        for (int i = 0; i < len; i++) {
69
            otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
70
        }
71
        return String.valueOf(otp);
72
    }
26087 tejbeer 73
 
31828 amit.gupta 74
    public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
75
        LOGGER.info("Try generating otp for mobile -- {}", mobile);
76
        OTPResponse otpResponse = new OTPResponse();
77
        List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
78
        String otpCode = null;
33510 tejus.loha 79
        LOGGER.info("Try to size of otps -- {}", otps.size());
31828 amit.gupta 80
        if (otps.size() >= 5) {
81
            otpResponse.setReference_id(0);
82
            otpResponse.setResult(false);
83
            otpResponse.setMessage("Maximum limit reached for the day");
84
            return otpResponse;
85
        }
33510 tejus.loha 86
        LOGGER.info("Try to check otps isEmpty", otps.isEmpty());
31828 amit.gupta 87
        if (!otps.isEmpty()) {
88
            if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
89
                otpResponse.setMessage("OTP generated less than 2 minutes ago");
90
                otpResponse.setReference_id(otps.get(0).getId());
91
                otpResponse.setResult(true);
92
                otpResponse.setOtp(otps.get(0).getOtp());
93
                return otpResponse;
94
            } else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
95
                otpCode = otps.get(0).getOtp();
96
            } else {
97
                ;
98
            }
99
        }
33510 tejus.loha 100
        LOGGER.info("Try to check otp null-- {}", otpCode == null);
31828 amit.gupta 101
        if (otpCode == null || otpCode.isEmpty()) {
102
            otpCode = getOtp();
103
        }
26087 tejbeer 104
 
31828 amit.gupta 105
        Otp otp = new Otp();
106
        otp.setMobile(mobile);
107
        otp.setOtp(otpCode);
108
        otp.setOtpType(otpType);
109
        otp.setCreatedOn(LocalDateTime.now());
110
        otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
111
        otpRepository.persist(otp);
112
        //sendOtp(otp);
33510 tejus.loha 113
        if (otpType.equals(OtpType.REGISTRATION)) {
31828 amit.gupta 114
            sendOtp(otp);
37194 aman 115
            otpResponse.setSent(true);
33510 tejus.loha 116
        } else if (otpType.equals(OtpType.LOI_ACCEPTANCE)) {
117
            sendOtpForLoiAcceptance(otp);
37194 aman 118
            otpResponse.setSent(true);
31828 amit.gupta 119
        } else {
120
            otpResponse.setOtp(otp.getOtp());
33510 tejus.loha 121
 
31828 amit.gupta 122
        }
26087 tejbeer 123
 
33510 tejus.loha 124
 
31828 amit.gupta 125
        otpResponse.setReference_id(otp.getId());
126
        otpResponse.setMessage("OTP generated successfully");
127
        otpResponse.setResult(true);
128
        return otpResponse;
129
    }
26087 tejbeer 130
 
31828 amit.gupta 131
    public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
132
            throws Exception, ProfitMandiBusinessException {
33510 tejus.loha 133
        LOGGER.info("validate_call");
31828 amit.gupta 134
        OTPResponse otpResponse = new OTPResponse();
135
        Otp otp = otpRepository.selectById(referenceId);
136
        otp.setTryCount(otp.getTryCount() + 1);
137
        otpResponse.setReference_id(referenceId);
138
        if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
139
            otpResponse.setMessage("Invalid otp");
140
            otpResponse.setResult(false);
33510 tejus.loha 141
            LOGGER.info("Invalid otp");
31828 amit.gupta 142
            return otpResponse;
143
        }
144
        if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
145
            otpResponse.setMessage("OTP expired");
146
            otpResponse.setResult(false);
33510 tejus.loha 147
            LOGGER.info("OTP expired");
31828 amit.gupta 148
            return otpResponse;
149
        }
150
        if (otp.getTryCount() > 5) {
151
            otpResponse.setMessage("Maximum try count reached");
152
            otpResponse.setResult(false);
33510 tejus.loha 153
            LOGGER.info("Maximum try count reached");
31828 amit.gupta 154
            return otpResponse;
155
        }
156
        otp.setExpired(true);
157
        otp.setVerified(true);
158
        otpRepository.persist(otp);
159
        otpResponse.setMessage("OTP validated successfully");
160
        otpResponse.setResult(true);
33510 tejus.loha 161
        LOGGER.info("OTP validated successfully");
31828 amit.gupta 162
        return otpResponse;
163
    }
26711 amit.gupta 164
 
31828 amit.gupta 165
    public void sendOtp(Otp otp) throws Exception {
166
        // In case of Cant receive SMS?
167
        // String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
168
        String otpString = otp.getOtp();
169
        try {
170
            this.sendSms(OTP_TEMPLATE_ID, String.format(OTP_TEMPLATE, otpString), otp.getMobile());
171
        } catch (Exception e) {
172
            e.printStackTrace();
173
        }
28305 amit.gupta 174
 
31828 amit.gupta 175
    }
26711 amit.gupta 176
 
33510 tejus.loha 177
    //todo
178
    public void sendOtpForLoiAcceptance(Otp otp1) throws Exception {
179
        String otp = otp1.getOtp();
33867 tejus.loha 180
        LoiForm pod = loiFormRepository.selectByEmailOrMobile(otp1.getMobile());
33510 tejus.loha 181
        String ownerName = pod.getFirstName();
182
        String message = String.format(LOI_ACCEPTANCE_OTP_TEMPLATE, ownerName, otp);
183
        this.sendSms(LOI_ACCEPTANCE_OTP_TEMPLATE_ID, message, otp1.getMobile());
33577 tejus.loha 184
        String emailTo[] = {pod.getEmail()};
33867 tejus.loha 185
        try {
35963 amit 186
            mailOutboxService.queueMail(emailTo, null, "LOI ACCEPTANCE OTP", message, "OtpProcessor.sendOtpForLoiAcceptance");
33867 tejus.loha 187
        } catch (Exception e) {
188
            LOGGER.error("Loi OTP not send on email couse -  ", e);
189
        }
33577 tejus.loha 190
 
33510 tejus.loha 191
    }
192
 
193
    public String sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
31828 amit.gupta 194
        Map<String, String> queryParams = new HashMap<>();
36240 vikas 195
        queryParams.put("header", SENDER);
196
        queryParams.put("templateid", dltTemplateId);
197
        queryParams.put("entityid", DLT_PRINCIPLE_ENTITY_ID);
198
        queryParams.put("contactnumber", "91" + mobileNumber);
199
        queryParams.put("message", message);
200
        queryParams.put("messageType", "normal");
201
        queryParams.put("serviceType", "otp");
33577 tejus.loha 202
        queryParams.put("password", PASS);
33510 tejus.loha 203
        queryParams.put("username", USER);
31828 amit.gupta 204
        //OTP Message Template
26783 amit.gupta 205
 
36256 vikas 206
        String response = restClient.get(NEXG_API_ENDPOINT, queryParams, new HashMap<>());
33617 tejus.loha 207
        LOGGER.info("LOI_ACCEPTANCE_OTP_Response - " + response);
33510 tejus.loha 208
        return response;
26711 amit.gupta 209
 
31828 amit.gupta 210
    }
26783 amit.gupta 211
 
31828 amit.gupta 212
    class SmsMessage {
213
        private String sender = "SMTDKN";
214
        @JsonProperty(value = "templateid")
215
        private String templateId = "54";
26711 amit.gupta 216
 
31828 amit.gupta 217
        private List<Message> message;
26711 amit.gupta 218
 
31828 amit.gupta 219
        @JsonProperty(value = "messagetype")
220
        private String messageType = "TXT";
26711 amit.gupta 221
 
31828 amit.gupta 222
        @Override
223
        public String toString() {
224
            return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
225
                    + ", messageType=" + messageType + "]";
226
        }
26711 amit.gupta 227
 
31828 amit.gupta 228
        public String getMessageType() {
229
            return messageType;
230
        }
26711 amit.gupta 231
 
31828 amit.gupta 232
        public void setMessageType(String messageType) {
233
            this.messageType = messageType;
234
        }
26711 amit.gupta 235
 
31828 amit.gupta 236
        public String getSender() {
237
            return sender;
238
        }
26711 amit.gupta 239
 
31828 amit.gupta 240
        public void setSender(String sender) {
241
            this.sender = sender;
242
        }
26711 amit.gupta 243
 
31828 amit.gupta 244
        public String getTemplateId() {
245
            return templateId;
246
        }
26711 amit.gupta 247
 
31828 amit.gupta 248
        public void setTemplateId(String templateId) {
249
            this.templateId = templateId;
250
        }
26711 amit.gupta 251
 
31828 amit.gupta 252
        public List<Message> getMessage() {
253
            return message;
254
        }
26711 amit.gupta 255
 
31828 amit.gupta 256
        public void setMessage(List<Message> message) {
257
            this.message = message;
258
        }
26711 amit.gupta 259
 
31828 amit.gupta 260
    }
26711 amit.gupta 261
 
31828 amit.gupta 262
    class Message {
263
        private String number;
33510 tejus.loha 264
        private Map<String, String> text;
26711 amit.gupta 265
 
31828 amit.gupta 266
        public String getNumber() {
267
            return number;
268
        }
26711 amit.gupta 269
 
31828 amit.gupta 270
        public void setNumber(String number) {
271
            this.number = number;
272
        }
26711 amit.gupta 273
 
31828 amit.gupta 274
        @Override
275
        public String toString() {
276
            return "Message [number=" + number + ", text=" + text + "]";
277
        }
26711 amit.gupta 278
 
31828 amit.gupta 279
        public Map<String, String> getText() {
280
            return text;
281
        }
26711 amit.gupta 282
 
31828 amit.gupta 283
        public void setText(Map<String, String> text) {
284
            this.text = text;
285
        }
21285 kshitij.so 286
 
31828 amit.gupta 287
    }
288
 
21285 kshitij.so 289
}