Subversion Repositories SmartDukaan

Rev

Rev 31828 | Rev 33568 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31828 Rev 33510
Line 1... Line 1...
1
package com.spice.profitmandi.web.processor;
1
package com.spice.profitmandi.dao.service;
2
 
-
 
3
import java.time.LocalDateTime;
-
 
4
import java.util.HashMap;
-
 
5
import java.util.List;
-
 
6
import java.util.Map;
-
 
7
import java.util.Random;
-
 
8
 
2
 
-
 
3
import com.fasterxml.jackson.annotation.JsonProperty;
-
 
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
5
import com.spice.profitmandi.common.web.client.RestClient;
-
 
6
import com.spice.profitmandi.dao.entity.dtr.Otp;
-
 
7
import com.spice.profitmandi.dao.entity.onBoarding.LoiForm;
-
 
8
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
-
 
9
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
-
 
10
import com.spice.profitmandi.dao.repository.user.LoiFormRepository;
9
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
12
import org.apache.logging.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.mail.javamail.JavaMailSender;
15
import org.springframework.mail.javamail.JavaMailSender;
14
import org.springframework.stereotype.Component;
16
import org.springframework.stereotype.Component;
15
 
17
 
16
import com.fasterxml.jackson.annotation.JsonProperty;
18
import java.time.LocalDateTime;
17
import com.google.common.collect.ImmutableMap;
19
import java.util.HashMap;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
19
import com.spice.profitmandi.common.web.client.RestClient;
-
 
20
import com.spice.profitmandi.dao.entity.dtr.Otp;
20
import java.util.List;
21
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
21
import java.util.Map;
22
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
-
 
23
import com.spice.profitmandi.service.scheme.SchemeServiceImpl;
-
 
24
import com.spice.profitmandi.web.res.OTPResponse;
22
import java.util.Random;
25
 
23
 
26
@Component
24
@Component
27
public class OtpProcessor {
25
public class OtpProcessor {
28
 
26
 
-
 
27
    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";
-
 
28
    public static final String SELF_CANCELLED_TEMPLATE_ID = "1507161943392646481";
-
 
29
    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";
-
 
30
    public static final String TEMPLATE_ORDER_CREATED_ID = "1507162021138742118";
-
 
31
    public static final String LOI_ACCEPTANCE_OTP_TEMPLATE_ID = "1707171687739961361";
-
 
32
    public static final String DLT_PRINCIPLE_ENTITY_ID = "1501589330000013091";
-
 
33
    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.";
29
    private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
34
    private static final Logger LOGGER = LogManager.getLogger(OtpProcessor.class);
30
    private static final String SMS_GATEWAY = "http://sms.speqtrainnov.com/api/v4/?api_key=A7897cd4627a8781e176fd31710b057a9";
35
    private static final String SMS_GATEWAY = "http://sms.speqtrainnov.com/api/v4/?api_key=A7897cd4627a8781e176fd31710b057a9";
31
    private static final int len = 5;
36
    private static final int len = 5;
32
    private static final String SENDER = "SMTDKN";
37
    private static final String SENDER = "SMTDKN";
33
 
-
 
34
 
-
 
35
    private static final String numbers = "0123456789";
38
    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";
39
    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";
40
    private static final String OTP_TEMPLATE_ID = "1507161889822750240";
-
 
41
    //TODO LOI OTP
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";
42
    private static final String USER = "smartdukaanOTP";
39
    public static final String SELF_CANCELLED_TEMPLATE_ID = "1507161943392646481";
43
    private static final String PASS1 = "Amit@Smart123";
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";
44
    private static String NEXG_API_ENDPOINT = "https://api2.nexgplatforms.com/sms/1/text/query";
42
 
45
 
43
    @Autowired
46
    @Autowired
44
    OtpRepository otpRepository;
47
    OtpRepository otpRepository;
45
 
48
 
46
    @Autowired
49
    @Autowired
47
    JavaMailSender mailSender;
50
    JavaMailSender mailSender;
48
 
-
 
49
    @Value("${prod}")
-
 
50
    private boolean prodEnv;
-
 
51
 
-
 
52
    @Autowired
51
    @Autowired
53
    RestClient restClient;
52
    RestClient restClient;
-
 
53
    @Autowired
-
 
54
    LoiFormRepository partnerOnBoardingDataRepository;
-
 
55
    @Value("${prod}")
-
 
56
    private boolean prodEnv;
54
 
57
 
55
    private String getOtp() {
58
    private String getOtp() {
56
        Random rndm_method = new Random();
59
        Random rndm_method = new Random();
57
        char[] otp = new char[len];
60
        char[] otp = new char[len];
58
 
61
 
Line 65... Line 68...
65
    public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
68
    public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
66
        LOGGER.info("Try generating otp for mobile -- {}", mobile);
69
        LOGGER.info("Try generating otp for mobile -- {}", mobile);
67
        OTPResponse otpResponse = new OTPResponse();
70
        OTPResponse otpResponse = new OTPResponse();
68
        List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
71
        List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
69
        String otpCode = null;
72
        String otpCode = null;
-
 
73
        LOGGER.info("Try to size of otps -- {}", otps.size());
70
        if (otps.size() >= 5) {
74
        if (otps.size() >= 5) {
71
            otpResponse.setReference_id(0);
75
            otpResponse.setReference_id(0);
72
            otpResponse.setResult(false);
76
            otpResponse.setResult(false);
73
            otpResponse.setMessage("Maximum limit reached for the day");
77
            otpResponse.setMessage("Maximum limit reached for the day");
74
            return otpResponse;
78
            return otpResponse;
75
        }
79
        }
-
 
80
        LOGGER.info("Try to check otps isEmpty", otps.isEmpty());
76
        if (!otps.isEmpty()) {
81
        if (!otps.isEmpty()) {
77
            if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
82
            if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
78
                otpResponse.setMessage("OTP generated less than 2 minutes ago");
83
                otpResponse.setMessage("OTP generated less than 2 minutes ago");
79
                otpResponse.setReference_id(otps.get(0).getId());
84
                otpResponse.setReference_id(otps.get(0).getId());
80
                otpResponse.setResult(true);
85
                otpResponse.setResult(true);
Line 84... Line 89...
84
                otpCode = otps.get(0).getOtp();
89
                otpCode = otps.get(0).getOtp();
85
            } else {
90
            } else {
86
                ;
91
                ;
87
            }
92
            }
88
        }
93
        }
-
 
94
        LOGGER.info("Try to check otp null-- {}", otpCode == null);
89
        if (otpCode == null || otpCode.isEmpty()) {
95
        if (otpCode == null || otpCode.isEmpty()) {
90
            otpCode = getOtp();
96
            otpCode = getOtp();
91
        }
97
        }
92
 
98
 
93
        Otp otp = new Otp();
99
        Otp otp = new Otp();
Line 95... Line 101...
95
        otp.setOtp(otpCode);
101
        otp.setOtp(otpCode);
96
        otp.setOtpType(otpType);
102
        otp.setOtpType(otpType);
97
        otp.setCreatedOn(LocalDateTime.now());
103
        otp.setCreatedOn(LocalDateTime.now());
98
        otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
104
        otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
99
        otpRepository.persist(otp);
105
        otpRepository.persist(otp);
100
 
-
 
101
        //sendOtp(otp);
106
        //sendOtp(otp);
102
        if (prodEnv) {
107
        if (otpType.equals(OtpType.REGISTRATION)) {
103
            sendOtp(otp);
108
            sendOtp(otp);
-
 
109
        } else if (otpType.equals(OtpType.LOI_ACCEPTANCE)) {
-
 
110
            sendOtpForLoiAcceptance(otp);
104
        } else {
111
        } else {
105
            otpResponse.setOtp(otp.getOtp());
112
            otpResponse.setOtp(otp.getOtp());
-
 
113
 
-
 
114
        }
-
 
115
 
-
 
116
        if (otpType.equals(OtpType.LOI_ACCEPTANCE)) {
-
 
117
            LOGGER.info("otp_type-" + otpType);
-
 
118
            sendOtpForLoiAcceptance(otp);
106
        }
119
        }
107
 
120
 
108
        otpResponse.setReference_id(otp.getId());
121
        otpResponse.setReference_id(otp.getId());
109
        otpResponse.setMessage("OTP generated successfully");
122
        otpResponse.setMessage("OTP generated successfully");
110
        otpResponse.setResult(true);
123
        otpResponse.setResult(true);
111
        return otpResponse;
124
        return otpResponse;
112
    }
125
    }
113
 
126
 
114
    public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
127
    public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
115
            throws Exception, ProfitMandiBusinessException {
128
            throws Exception, ProfitMandiBusinessException {
-
 
129
        LOGGER.info("validate_call");
116
        OTPResponse otpResponse = new OTPResponse();
130
        OTPResponse otpResponse = new OTPResponse();
117
        Otp otp = otpRepository.selectById(referenceId);
131
        Otp otp = otpRepository.selectById(referenceId);
118
        otp.setTryCount(otp.getTryCount() + 1);
132
        otp.setTryCount(otp.getTryCount() + 1);
119
        otpResponse.setReference_id(referenceId);
133
        otpResponse.setReference_id(referenceId);
120
        if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
134
        if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
121
            otpResponse.setMessage("Invalid otp");
135
            otpResponse.setMessage("Invalid otp");
122
            otpResponse.setResult(false);
136
            otpResponse.setResult(false);
-
 
137
            LOGGER.info("Invalid otp");
123
            return otpResponse;
138
            return otpResponse;
124
        }
139
        }
125
        if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
140
        if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
126
            otpResponse.setMessage("OTP expired");
141
            otpResponse.setMessage("OTP expired");
127
            otpResponse.setResult(false);
142
            otpResponse.setResult(false);
-
 
143
            LOGGER.info("OTP expired");
128
            return otpResponse;
144
            return otpResponse;
129
        }
145
        }
130
        if (otp.getTryCount() > 5) {
146
        if (otp.getTryCount() > 5) {
131
            otpResponse.setMessage("Maximum try count reached");
147
            otpResponse.setMessage("Maximum try count reached");
132
            otpResponse.setResult(false);
148
            otpResponse.setResult(false);
-
 
149
            LOGGER.info("Maximum try count reached");
133
            return otpResponse;
150
            return otpResponse;
134
        }
151
        }
135
        otp.setExpired(true);
152
        otp.setExpired(true);
136
        otp.setVerified(true);
153
        otp.setVerified(true);
137
        otpRepository.persist(otp);
154
        otpRepository.persist(otp);
138
        otpResponse.setMessage("OTP validated successfully");
155
        otpResponse.setMessage("OTP validated successfully");
139
        otpResponse.setResult(true);
156
        otpResponse.setResult(true);
-
 
157
        LOGGER.info("OTP validated successfully");
140
        return otpResponse;
158
        return otpResponse;
141
    }
159
    }
142
 
160
 
143
    public void sendOtp(Otp otp) throws Exception {
161
    public void sendOtp(Otp otp) throws Exception {
144
        // In case of Cant receive SMS?
162
        // In case of Cant receive SMS?
Line 150... Line 168...
150
            e.printStackTrace();
168
            e.printStackTrace();
151
        }
169
        }
152
 
170
 
153
    }
171
    }
154
 
172
 
-
 
173
    //todo
-
 
174
    public void sendOtpForLoiAcceptance(Otp otp1) throws Exception {
-
 
175
        String otp = otp1.getOtp();
-
 
176
        LoiForm pod = partnerOnBoardingDataRepository.selectByEmailOrMobile(otp1.getMobile());
-
 
177
        String ownerName = pod.getFirstName();
-
 
178
        String message = String.format(LOI_ACCEPTANCE_OTP_TEMPLATE, ownerName, otp);
-
 
179
        this.sendSms(LOI_ACCEPTANCE_OTP_TEMPLATE_ID, message, otp1.getMobile());
-
 
180
    }
-
 
181
 
155
    public void sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
182
    public String sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
156
        Map<String, String> queryParams = new HashMap<>();
183
        Map<String, String> queryParams = new HashMap<>();
157
        queryParams.put("sender", SENDER);
184
        queryParams.put("from", SENDER);
158
        queryParams.put("method", "sms");
185
        queryParams.put("indiaDltContentTemplateId", dltTemplateId);
-
 
186
        queryParams.put("indiaDltPrincipalEntityId", DLT_PRINCIPLE_ENTITY_ID);
159
        queryParams.put("to", "91" + mobileNumber);
187
        queryParams.put("to", "91" + mobileNumber);
160
        queryParams.put("message", message);
188
        queryParams.put("text", message);
-
 
189
        queryParams.put("password", PASS1);
161
        queryParams.put("format", "json");
190
        queryParams.put("username", USER);
162
        //OTP Message Template
191
        //OTP Message Template
163
 
192
 
164
        String response = restClient.post(SMS_GATEWAY, queryParams, new HashMap<>(), new HashMap<>());
193
        String response = restClient.post(NEXG_API_ENDPOINT, queryParams, new HashMap<>(), new HashMap<>());
165
        LOGGER.info(response);
194
        LOGGER.info(response);
-
 
195
        return response;
166
 
196
 
167
    }
197
    }
168
 
198
 
169
    class SmsMessage {
199
    class SmsMessage {
170
        private String sender = "SMTDKN";
200
        private String sender = "SMTDKN";
Line 216... Line 246...
216
 
246
 
217
    }
247
    }
218
 
248
 
219
    class Message {
249
    class Message {
220
        private String number;
250
        private String number;
-
 
251
        private Map<String, String> text;
221
 
252
 
222
        public String getNumber() {
253
        public String getNumber() {
223
            return number;
254
            return number;
224
        }
255
        }
225
 
256
 
226
        public void setNumber(String number) {
257
        public void setNumber(String number) {
227
            this.number = number;
258
            this.number = number;
228
        }
259
        }
229
 
260
 
230
        private Map<String, String> text;
-
 
231
 
-
 
232
        @Override
261
        @Override
233
        public String toString() {
262
        public String toString() {
234
            return "Message [number=" + number + ", text=" + text + "]";
263
            return "Message [number=" + number + ", text=" + text + "]";
235
        }
264
        }
236
 
265