Subversion Repositories SmartDukaan

Rev

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

Rev 25249 Rev 28257
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.text.MessageFormat;
3
import java.text.MessageFormat;
-
 
4
import java.time.LocalDateTime;
-
 
5
import java.util.ArrayList;
-
 
6
import java.util.HashMap;
4
import java.util.List;
7
import java.util.List;
-
 
8
import java.util.Map;
-
 
9
import java.util.Optional;
5
 
10
 
6
import javax.mail.internet.InternetAddress;
11
import javax.mail.internet.InternetAddress;
7
import javax.mail.internet.MimeMessage;
12
import javax.mail.internet.MimeMessage;
8
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletRequest;
9
 
14
 
Line 15... Line 20...
15
import org.springframework.http.ResponseEntity;
20
import org.springframework.http.ResponseEntity;
16
import org.springframework.mail.javamail.JavaMailSender;
21
import org.springframework.mail.javamail.JavaMailSender;
17
import org.springframework.mail.javamail.MimeMessageHelper;
22
import org.springframework.mail.javamail.MimeMessageHelper;
18
import org.springframework.stereotype.Controller;
23
import org.springframework.stereotype.Controller;
19
import org.springframework.transaction.annotation.Transactional;
24
import org.springframework.transaction.annotation.Transactional;
-
 
25
import org.springframework.ui.Model;
-
 
26
import org.springframework.web.bind.annotation.PathVariable;
20
import org.springframework.web.bind.annotation.RequestBody;
27
import org.springframework.web.bind.annotation.RequestBody;
21
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
29
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestParam;
30
import org.springframework.web.bind.annotation.RequestParam;
24
 
31
 
25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26
import com.spice.profitmandi.common.model.CustomRetailer;
33
import com.spice.profitmandi.common.model.CustomRetailer;
27
import com.spice.profitmandi.common.model.ProfitMandiConstants;
34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
28
import com.spice.profitmandi.common.web.util.ResponseSender;
35
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
36
import com.spice.profitmandi.dao.entity.fofo.LoanPaymentRequest;
29
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
37
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
-
 
38
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
30
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
39
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
-
 
40
import com.spice.profitmandi.dao.model.PaymentOptionModel;
31
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
41
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
32
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
42
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
33
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
43
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
-
 
44
import com.spice.profitmandi.dao.repository.fofo.LoanPaymentRequestRepository;
34
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
45
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
-
 
46
import com.spice.profitmandi.service.integrations.CCAvenuePaymentService;
35
import com.spice.profitmandi.service.user.RetailerService;
47
import com.spice.profitmandi.service.user.RetailerService;
36
import com.spice.profitmandi.service.wallet.WalletService;
48
import com.spice.profitmandi.service.wallet.WalletService;
37
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
49
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
38
 
50
 
39
import io.swagger.annotations.ApiImplicitParam;
51
import io.swagger.annotations.ApiImplicitParam;
Line 46... Line 58...
46
 
58
 
47
	private static final Logger log = LogManager.getLogger(WalletController.class);
59
	private static final Logger log = LogManager.getLogger(WalletController.class);
48
 
60
 
49
	@Autowired
61
	@Autowired
50
	UserWalletRepository userWalletRepository;
62
	UserWalletRepository userWalletRepository;
-
 
63
	
-
 
64
	@Autowired
-
 
65
	LoanPaymentRequestRepository loanPaymentRequestRepository;
51
 
66
 
52
	@Autowired
67
	@Autowired
53
	AddWalletRequestRepository addWalletRequestRepository;
68
	AddWalletRequestRepository addWalletRequestRepository;
54
 
69
 
55
	@Autowired
70
	@Autowired
Line 59... Line 74...
59
	JavaMailSender mailSender;
74
	JavaMailSender mailSender;
60
 
75
 
61
	@Autowired
76
	@Autowired
62
	@Qualifier("userRepository")
77
	@Qualifier("userRepository")
63
	private UserRepository userRepository;
78
	private UserRepository userRepository;
-
 
79
	
-
 
80
	@Autowired
-
 
81
	private CCAvenuePaymentService ccAvenuePaymentService;
64
 
82
 
65
	@Autowired
83
	@Autowired
66
	private UserAccountRepository userAccountRepository;
84
	private UserAccountRepository userAccountRepository;
67
 
85
 
68
	@Autowired
86
	@Autowired
Line 83... Line 101...
83
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
101
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
84
	@ApiImplicitParams({
102
	@ApiImplicitParams({
85
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
103
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
86
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
104
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
87
		int userId = (int) request.getAttribute("userId");
105
		int userId = (int) request.getAttribute("userId");
-
 
106
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
88
		try {
107
		try {
89
			return responseSender.ok(walletService.getUserWalletHistoryByUserId(userId));
108
			return responseSender.ok(walletService.getPaginatedUserWalletHistoryByRetailerId(retailerId, null, null, 0, 150));
90
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
109
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
91
			return responseSender.badRequest(profitMandiBusinessException);
110
			return responseSender.badRequest(profitMandiBusinessException);
92
		}
111
		}
93
	}
112
	}
-
 
113
	
-
 
114
	@RequestMapping(value = "/wallet/add-via-loan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
115
	@ApiImplicitParams({
-
 
116
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
-
 
117
	public ResponseEntity<?> payViaLoan(HttpServletRequest request, @RequestParam double loanAmount, 
-
 
118
			@RequestParam double surcharge,
-
 
119
			@RequestParam String payMethod,  @RequestParam Gateway gateway) throws Exception {
-
 
120
		List<PaymentOptionModel> paymentOptionModels = ccAvenuePaymentService.getPaymentOptionModelMap().get(gateway);
-
 
121
		Optional<PaymentOptionModel> optPaymentOptionModel = paymentOptionModels.stream().filter(x->x.getPaymentMethod().equals(payMethod)).findFirst();
-
 
122
		
-
 
123
		if(!optPaymentOptionModel.isPresent()) {
-
 
124
			return responseSender.badRequest(new ProfitMandiBusinessException("", "", "Bad parameters in request"));
-
 
125
		} else {
-
 
126
			double calculatedSurcharge = Math.round(loanAmount*optPaymentOptionModel.get().getSurcharge()*1.18/100);
-
 
127
			if(Math.abs(calculatedSurcharge - surcharge) >= 1) {
-
 
128
				return responseSender.badRequest(new ProfitMandiBusinessException("", "", "Bad parameters in request"));
-
 
129
			}
-
 
130
		}
-
 
131
		int userId = (int) request.getAttribute("userId");
-
 
132
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
-
 
133
		LoanPaymentRequest loanPaymentRequest = new LoanPaymentRequest();
-
 
134
		loanPaymentRequest.setCreateTimestamp(LocalDateTime.now());
-
 
135
		loanPaymentRequest.setFofoId(retailerId);
-
 
136
		loanPaymentRequest.setCreditAmount(loanAmount);
-
 
137
		loanPaymentRequest.setGatewayFee(surcharge);
-
 
138
		loanPaymentRequest.setGateway(gateway);
-
 
139
		loanPaymentRequest.setPaymentMethod(optPaymentOptionModel.get().getPaymentMethodDesctiption());
-
 
140
		loanPaymentRequestRepository.persist(loanPaymentRequest);
-
 
141
		Map<String, String> returnMap = ccAvenuePaymentService.getFormParams(loanPaymentRequest, payMethod);
-
 
142
		returnMap.put("status", "pending");
-
 
143
		return responseSender.ok(returnMap);
-
 
144
		
-
 
145
	}
94
 
146
 
95
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
147
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
96
	@ApiImplicitParams({
148
	@ApiImplicitParams({
97
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
149
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
98
	public ResponseEntity<?> AddMoneyToWallet(HttpServletRequest request,
150
	public ResponseEntity<?> addMoneyToWallet(HttpServletRequest request,
99
			@RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
151
			@RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
100
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
152
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
101
		log.info("user_id" + userId);
153
		log.info("user_id" + userId);
102
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
154
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
103
		CustomRetailer customRetailer = retailerService.getFofoRetailer(retailerId);
155
		CustomRetailer customRetailer = retailerService.getFofoRetailer(retailerId);
Line 120... Line 172...
120
		String email = "neeraj.gupta@smartdukaan.com";
172
		String email = "neeraj.gupta@smartdukaan.com";
121
		this.sendMailWithAttachments(email, subject, messageText);
173
		this.sendMailWithAttachments(email, subject, messageText);
122
 
174
 
123
		return responseSender.ok(true);
175
		return responseSender.ok(true);
124
	}
176
	}
-
 
177
	
-
 
178
	@RequestMapping(value = "/wallet/add-money/{loanId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
179
	@ApiImplicitParams({
-
 
180
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
-
 
181
	public ResponseEntity<?> addMoneyToWallet(HttpServletRequest request,@PathVariable int loanId) throws Exception {
-
 
182
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
-
 
183
		log.info("user_id" + userId);
-
 
184
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
-
 
185
		
-
 
186
		LoanPaymentRequest loanPaymentRequest = loanPaymentRequestRepository.selectById(loanId);
-
 
187
		if(loanPaymentRequest.getFofoId()!=retailerId) {
-
 
188
			return responseSender.badRequest(new ProfitMandiBusinessException("Invalid Add Money Id", "Invalid Add money id", "Invalid Add Money"));
-
 
189
		}
-
 
190
		return responseSender.ok(loanPaymentRequest);
-
 
191
	}
125
 
192
 
126
	private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
193
	private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
127
		log.info("message" + messageText);
194
		log.info("message" + messageText);
128
		MimeMessage message = mailSender.createMimeMessage();
195
		MimeMessage message = mailSender.createMimeMessage();
129
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
196
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
Line 146... Line 213...
146
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
213
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
147
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
214
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
148
		List<AddWalletRequest> walletRequest = addWalletRequestRepository.selectByRetailerId(offset, limit, retailerId);
215
		List<AddWalletRequest> walletRequest = addWalletRequestRepository.selectByRetailerId(offset, limit, retailerId);
149
		return responseSender.ok(walletRequest);
216
		return responseSender.ok(walletRequest);
150
	}
217
	}
-
 
218
	
-
 
219
	
-
 
220
	
-
 
221
	@RequestMapping(value = "/getPaymentOptions", method = RequestMethod.GET)
-
 
222
	public ResponseEntity<?> getPaymentOptions(HttpServletRequest request, Model model) throws Exception {
-
 
223
		return responseSender.ok(ccAvenuePaymentService.getPaymentOptionModelMap());
-
 
224
	}
151
 
225
 
152
}
226
}
153
227