Subversion Repositories SmartDukaan

Rev

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