Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21285 kshitij.so 1
package com.spice.profitmandi.web.processor;
2
 
3
import java.time.LocalDateTime;
26656 amit.gupta 4
import java.util.HashMap;
21285 kshitij.so 5
import java.util.List;
26656 amit.gupta 6
import java.util.Map;
21285 kshitij.so 7
import java.util.Random;
8
 
26712 amit.gupta 9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
21285 kshitij.so 11
import org.springframework.beans.factory.annotation.Autowired;
26649 amit.gupta 12
import org.springframework.beans.factory.annotation.Value;
26657 amit.gupta 13
import org.springframework.mail.javamail.JavaMailSender;
21285 kshitij.so 14
import org.springframework.stereotype.Component;
15
 
26709 amit.gupta 16
import com.fasterxml.jackson.annotation.JsonProperty;
28305 amit.gupta 17
import com.google.common.collect.ImmutableMap;
21285 kshitij.so 18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26656 amit.gupta 19
import com.spice.profitmandi.common.web.client.RestClient;
21735 ashik.ali 20
import com.spice.profitmandi.dao.entity.dtr.Otp;
21
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
22
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
26712 amit.gupta 23
import com.spice.profitmandi.service.scheme.SchemeServiceImpl;
21285 kshitij.so 24
import com.spice.profitmandi.web.res.OTPResponse;
25
 
26
@Component
26087 tejbeer 27
public class OtpProcessor {
21285 kshitij.so 28
 
31828 amit.gupta 29
    private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
30
    private static final String SMS_GATEWAY = "http://sms.speqtrainnov.com/api/v4/?api_key=A7897cd4627a8781e176fd31710b057a9";
31
    private static final int len = 5;
32
    private static final String SENDER = "SMTDKN";
28305 amit.gupta 33
 
21285 kshitij.so 34
 
31828 amit.gupta 35
    private static final String numbers = "0123456789";
36
    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";
37
    private static final String OTP_TEMPLATE_ID = "1507161889822750240";
38
    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";
39
    public static final String SELF_CANCELLED_TEMPLATE_ID = "1507161943392646481";
40
    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";
41
    public static final String TEMPLATE_ORDER_CREATED_ID = "1507162021138742118";
26711 amit.gupta 42
 
31828 amit.gupta 43
    @Autowired
44
    OtpRepository otpRepository;
26711 amit.gupta 45
 
31828 amit.gupta 46
    @Autowired
47
    JavaMailSender mailSender;
21285 kshitij.so 48
 
31828 amit.gupta 49
    @Value("${prod}")
50
    private boolean prodEnv;
26649 amit.gupta 51
 
31828 amit.gupta 52
    @Autowired
53
    RestClient restClient;
21285 kshitij.so 54
 
31828 amit.gupta 55
    private String getOtp() {
56
        Random rndm_method = new Random();
57
        char[] otp = new char[len];
21285 kshitij.so 58
 
31828 amit.gupta 59
        for (int i = 0; i < len; i++) {
60
            otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
61
        }
62
        return String.valueOf(otp);
63
    }
26087 tejbeer 64
 
31828 amit.gupta 65
    public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
66
        LOGGER.info("Try generating otp for mobile -- {}", mobile);
67
        OTPResponse otpResponse = new OTPResponse();
68
        List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
69
        String otpCode = null;
70
        if (otps.size() >= 5) {
71
            otpResponse.setReference_id(0);
72
            otpResponse.setResult(false);
73
            otpResponse.setMessage("Maximum limit reached for the day");
74
            return otpResponse;
75
        }
76
        if (!otps.isEmpty()) {
77
            if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
78
                otpResponse.setMessage("OTP generated less than 2 minutes ago");
79
                otpResponse.setReference_id(otps.get(0).getId());
80
                otpResponse.setResult(true);
81
                otpResponse.setOtp(otps.get(0).getOtp());
82
                return otpResponse;
83
            } else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
84
                otpCode = otps.get(0).getOtp();
85
            } else {
86
                ;
87
            }
88
        }
89
        if (otpCode == null || otpCode.isEmpty()) {
90
            otpCode = getOtp();
91
        }
26087 tejbeer 92
 
31828 amit.gupta 93
        Otp otp = new Otp();
94
        otp.setMobile(mobile);
95
        otp.setOtp(otpCode);
96
        otp.setOtpType(otpType);
97
        otp.setCreatedOn(LocalDateTime.now());
98
        otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
99
        otpRepository.persist(otp);
21285 kshitij.so 100
 
31828 amit.gupta 101
        //sendOtp(otp);
102
        if (prodEnv) {
103
            sendOtp(otp);
104
        } else {
105
            otpResponse.setOtp(otp.getOtp());
106
        }
26087 tejbeer 107
 
31828 amit.gupta 108
        otpResponse.setReference_id(otp.getId());
109
        otpResponse.setMessage("OTP generated successfully");
110
        otpResponse.setResult(true);
111
        return otpResponse;
112
    }
26087 tejbeer 113
 
31828 amit.gupta 114
    public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
115
            throws Exception, ProfitMandiBusinessException {
116
        OTPResponse otpResponse = new OTPResponse();
117
        Otp otp = otpRepository.selectById(referenceId);
118
        otp.setTryCount(otp.getTryCount() + 1);
119
        otpResponse.setReference_id(referenceId);
120
        if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
121
            otpResponse.setMessage("Invalid otp");
122
            otpResponse.setResult(false);
123
            return otpResponse;
124
        }
125
        if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
126
            otpResponse.setMessage("OTP expired");
127
            otpResponse.setResult(false);
128
            return otpResponse;
129
        }
130
        if (otp.getTryCount() > 5) {
131
            otpResponse.setMessage("Maximum try count reached");
132
            otpResponse.setResult(false);
133
            return otpResponse;
134
        }
135
        otp.setExpired(true);
136
        otp.setVerified(true);
137
        otpRepository.persist(otp);
138
        otpResponse.setMessage("OTP validated successfully");
139
        otpResponse.setResult(true);
140
        return otpResponse;
141
    }
26711 amit.gupta 142
 
31828 amit.gupta 143
    public void sendOtp(Otp otp) throws Exception {
144
        // In case of Cant receive SMS?
145
        // String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
146
        String otpString = otp.getOtp();
147
        try {
148
            this.sendSms(OTP_TEMPLATE_ID, String.format(OTP_TEMPLATE, otpString), otp.getMobile());
149
        } catch (Exception e) {
150
            e.printStackTrace();
151
        }
28305 amit.gupta 152
 
31828 amit.gupta 153
    }
26711 amit.gupta 154
 
31828 amit.gupta 155
    public void sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
156
        Map<String, String> queryParams = new HashMap<>();
157
        queryParams.put("sender", SENDER);
158
        queryParams.put("method", "sms");
159
        queryParams.put("to", "91" + mobileNumber);
160
        queryParams.put("message", message);
161
        queryParams.put("format", "json");
162
        //OTP Message Template
26783 amit.gupta 163
 
31828 amit.gupta 164
        String response = restClient.post(SMS_GATEWAY, queryParams, new HashMap<>(), new HashMap<>());
165
        LOGGER.info(response);
26711 amit.gupta 166
 
31828 amit.gupta 167
    }
26783 amit.gupta 168
 
31828 amit.gupta 169
    class SmsMessage {
170
        private String sender = "SMTDKN";
171
        @JsonProperty(value = "templateid")
172
        private String templateId = "54";
26711 amit.gupta 173
 
31828 amit.gupta 174
        private List<Message> message;
26711 amit.gupta 175
 
31828 amit.gupta 176
        @JsonProperty(value = "messagetype")
177
        private String messageType = "TXT";
26711 amit.gupta 178
 
31828 amit.gupta 179
        @Override
180
        public String toString() {
181
            return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
182
                    + ", messageType=" + messageType + "]";
183
        }
26711 amit.gupta 184
 
31828 amit.gupta 185
        public String getMessageType() {
186
            return messageType;
187
        }
26711 amit.gupta 188
 
31828 amit.gupta 189
        public void setMessageType(String messageType) {
190
            this.messageType = messageType;
191
        }
26711 amit.gupta 192
 
31828 amit.gupta 193
        public String getSender() {
194
            return sender;
195
        }
26711 amit.gupta 196
 
31828 amit.gupta 197
        public void setSender(String sender) {
198
            this.sender = sender;
199
        }
26711 amit.gupta 200
 
31828 amit.gupta 201
        public String getTemplateId() {
202
            return templateId;
203
        }
26711 amit.gupta 204
 
31828 amit.gupta 205
        public void setTemplateId(String templateId) {
206
            this.templateId = templateId;
207
        }
26711 amit.gupta 208
 
31828 amit.gupta 209
        public List<Message> getMessage() {
210
            return message;
211
        }
26711 amit.gupta 212
 
31828 amit.gupta 213
        public void setMessage(List<Message> message) {
214
            this.message = message;
215
        }
26711 amit.gupta 216
 
31828 amit.gupta 217
    }
26711 amit.gupta 218
 
31828 amit.gupta 219
    class Message {
220
        private String number;
26711 amit.gupta 221
 
31828 amit.gupta 222
        public String getNumber() {
223
            return number;
224
        }
26711 amit.gupta 225
 
31828 amit.gupta 226
        public void setNumber(String number) {
227
            this.number = number;
228
        }
26711 amit.gupta 229
 
31828 amit.gupta 230
        private Map<String, String> text;
26711 amit.gupta 231
 
31828 amit.gupta 232
        @Override
233
        public String toString() {
234
            return "Message [number=" + number + ", text=" + text + "]";
235
        }
26711 amit.gupta 236
 
31828 amit.gupta 237
        public Map<String, String> getText() {
238
            return text;
239
        }
26711 amit.gupta 240
 
31828 amit.gupta 241
        public void setText(Map<String, String> text) {
242
            this.text = text;
243
        }
21285 kshitij.so 244
 
31828 amit.gupta 245
    }
246
 
21285 kshitij.so 247
}