Subversion Repositories SmartDukaan

Rev

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