Subversion Repositories SmartDukaan

Rev

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