Subversion Repositories SmartDukaan

Rev

Rev 36240 | 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
54
    JavaMailSender mailSender;
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);
33510 tejus.loha 115
        } else if (otpType.equals(OtpType.LOI_ACCEPTANCE)) {
116
            sendOtpForLoiAcceptance(otp);
31828 amit.gupta 117
        } else {
118
            otpResponse.setOtp(otp.getOtp());
33510 tejus.loha 119
 
31828 amit.gupta 120
        }
26087 tejbeer 121
 
33510 tejus.loha 122
 
31828 amit.gupta 123
        otpResponse.setReference_id(otp.getId());
124
        otpResponse.setMessage("OTP generated successfully");
125
        otpResponse.setResult(true);
126
        return otpResponse;
127
    }
26087 tejbeer 128
 
31828 amit.gupta 129
    public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
130
            throws Exception, ProfitMandiBusinessException {
33510 tejus.loha 131
        LOGGER.info("validate_call");
31828 amit.gupta 132
        OTPResponse otpResponse = new OTPResponse();
133
        Otp otp = otpRepository.selectById(referenceId);
134
        otp.setTryCount(otp.getTryCount() + 1);
135
        otpResponse.setReference_id(referenceId);
136
        if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
137
            otpResponse.setMessage("Invalid otp");
138
            otpResponse.setResult(false);
33510 tejus.loha 139
            LOGGER.info("Invalid otp");
31828 amit.gupta 140
            return otpResponse;
141
        }
142
        if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
143
            otpResponse.setMessage("OTP expired");
144
            otpResponse.setResult(false);
33510 tejus.loha 145
            LOGGER.info("OTP expired");
31828 amit.gupta 146
            return otpResponse;
147
        }
148
        if (otp.getTryCount() > 5) {
149
            otpResponse.setMessage("Maximum try count reached");
150
            otpResponse.setResult(false);
33510 tejus.loha 151
            LOGGER.info("Maximum try count reached");
31828 amit.gupta 152
            return otpResponse;
153
        }
154
        otp.setExpired(true);
155
        otp.setVerified(true);
156
        otpRepository.persist(otp);
157
        otpResponse.setMessage("OTP validated successfully");
158
        otpResponse.setResult(true);
33510 tejus.loha 159
        LOGGER.info("OTP validated successfully");
31828 amit.gupta 160
        return otpResponse;
161
    }
26711 amit.gupta 162
 
31828 amit.gupta 163
    public void sendOtp(Otp otp) throws Exception {
164
        // In case of Cant receive SMS?
165
        // String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
166
        String otpString = otp.getOtp();
167
        try {
168
            this.sendSms(OTP_TEMPLATE_ID, String.format(OTP_TEMPLATE, otpString), otp.getMobile());
169
        } catch (Exception e) {
170
            e.printStackTrace();
171
        }
28305 amit.gupta 172
 
31828 amit.gupta 173
    }
26711 amit.gupta 174
 
33510 tejus.loha 175
    //todo
176
    public void sendOtpForLoiAcceptance(Otp otp1) throws Exception {
177
        String otp = otp1.getOtp();
33867 tejus.loha 178
        LoiForm pod = loiFormRepository.selectByEmailOrMobile(otp1.getMobile());
33510 tejus.loha 179
        String ownerName = pod.getFirstName();
180
        String message = String.format(LOI_ACCEPTANCE_OTP_TEMPLATE, ownerName, otp);
181
        this.sendSms(LOI_ACCEPTANCE_OTP_TEMPLATE_ID, message, otp1.getMobile());
33577 tejus.loha 182
        String emailTo[] = {pod.getEmail()};
33867 tejus.loha 183
        try {
35963 amit 184
            mailOutboxService.queueMail(emailTo, null, "LOI ACCEPTANCE OTP", message, "OtpProcessor.sendOtpForLoiAcceptance");
33867 tejus.loha 185
        } catch (Exception e) {
186
            LOGGER.error("Loi OTP not send on email couse -  ", e);
187
        }
33577 tejus.loha 188
 
33510 tejus.loha 189
    }
190
 
191
    public String sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
31828 amit.gupta 192
        Map<String, String> queryParams = new HashMap<>();
36240 vikas 193
        queryParams.put("header", SENDER);
194
        queryParams.put("templateid", dltTemplateId);
195
        queryParams.put("entityid", DLT_PRINCIPLE_ENTITY_ID);
196
        queryParams.put("contactnumber", "91" + mobileNumber);
197
        queryParams.put("message", message);
198
        queryParams.put("messageType", "normal");
199
        queryParams.put("serviceType", "otp");
33577 tejus.loha 200
        queryParams.put("password", PASS);
33510 tejus.loha 201
        queryParams.put("username", USER);
31828 amit.gupta 202
        //OTP Message Template
26783 amit.gupta 203
 
36256 vikas 204
        String response = restClient.get(NEXG_API_ENDPOINT, queryParams, new HashMap<>());
33617 tejus.loha 205
        LOGGER.info("LOI_ACCEPTANCE_OTP_Response - " + response);
33510 tejus.loha 206
        return response;
26711 amit.gupta 207
 
31828 amit.gupta 208
    }
26783 amit.gupta 209
 
31828 amit.gupta 210
    class SmsMessage {
211
        private String sender = "SMTDKN";
212
        @JsonProperty(value = "templateid")
213
        private String templateId = "54";
26711 amit.gupta 214
 
31828 amit.gupta 215
        private List<Message> message;
26711 amit.gupta 216
 
31828 amit.gupta 217
        @JsonProperty(value = "messagetype")
218
        private String messageType = "TXT";
26711 amit.gupta 219
 
31828 amit.gupta 220
        @Override
221
        public String toString() {
222
            return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
223
                    + ", messageType=" + messageType + "]";
224
        }
26711 amit.gupta 225
 
31828 amit.gupta 226
        public String getMessageType() {
227
            return messageType;
228
        }
26711 amit.gupta 229
 
31828 amit.gupta 230
        public void setMessageType(String messageType) {
231
            this.messageType = messageType;
232
        }
26711 amit.gupta 233
 
31828 amit.gupta 234
        public String getSender() {
235
            return sender;
236
        }
26711 amit.gupta 237
 
31828 amit.gupta 238
        public void setSender(String sender) {
239
            this.sender = sender;
240
        }
26711 amit.gupta 241
 
31828 amit.gupta 242
        public String getTemplateId() {
243
            return templateId;
244
        }
26711 amit.gupta 245
 
31828 amit.gupta 246
        public void setTemplateId(String templateId) {
247
            this.templateId = templateId;
248
        }
26711 amit.gupta 249
 
31828 amit.gupta 250
        public List<Message> getMessage() {
251
            return message;
252
        }
26711 amit.gupta 253
 
31828 amit.gupta 254
        public void setMessage(List<Message> message) {
255
            this.message = message;
256
        }
26711 amit.gupta 257
 
31828 amit.gupta 258
    }
26711 amit.gupta 259
 
31828 amit.gupta 260
    class Message {
261
        private String number;
33510 tejus.loha 262
        private Map<String, String> text;
26711 amit.gupta 263
 
31828 amit.gupta 264
        public String getNumber() {
265
            return number;
266
        }
26711 amit.gupta 267
 
31828 amit.gupta 268
        public void setNumber(String number) {
269
            this.number = number;
270
        }
26711 amit.gupta 271
 
31828 amit.gupta 272
        @Override
273
        public String toString() {
274
            return "Message [number=" + number + ", text=" + text + "]";
275
        }
26711 amit.gupta 276
 
31828 amit.gupta 277
        public Map<String, String> getText() {
278
            return text;
279
        }
26711 amit.gupta 280
 
31828 amit.gupta 281
        public void setText(Map<String, String> text) {
282
            this.text = text;
283
        }
21285 kshitij.so 284
 
31828 amit.gupta 285
    }
286
 
21285 kshitij.so 287
}