Subversion Repositories SmartDukaan

Rev

Rev 25249 | Rev 29503 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21532 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
23936 tejbeer 3
import java.text.MessageFormat;
28257 amit.gupta 4
import java.time.LocalDateTime;
5
import java.util.ArrayList;
6
import java.util.HashMap;
23942 tejbeer 7
import java.util.List;
28257 amit.gupta 8
import java.util.Map;
9
import java.util.Optional;
23936 tejbeer 10
 
11
import javax.mail.internet.InternetAddress;
12
import javax.mail.internet.MimeMessage;
21532 kshitij.so 13
import javax.servlet.http.HttpServletRequest;
14
 
23985 tejbeer 15
import org.apache.logging.log4j.LogManager;
23568 govind 16
import org.apache.logging.log4j.Logger;
21532 kshitij.so 17
import org.springframework.beans.factory.annotation.Autowired;
23936 tejbeer 18
import org.springframework.beans.factory.annotation.Qualifier;
21532 kshitij.so 19
import org.springframework.http.MediaType;
20
import org.springframework.http.ResponseEntity;
23936 tejbeer 21
import org.springframework.mail.javamail.JavaMailSender;
22
import org.springframework.mail.javamail.MimeMessageHelper;
21532 kshitij.so 23
import org.springframework.stereotype.Controller;
21702 ashik.ali 24
import org.springframework.transaction.annotation.Transactional;
28257 amit.gupta 25
import org.springframework.ui.Model;
26
import org.springframework.web.bind.annotation.PathVariable;
23936 tejbeer 27
import org.springframework.web.bind.annotation.RequestBody;
21532 kshitij.so 28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMethod;
23942 tejbeer 30
import org.springframework.web.bind.annotation.RequestParam;
21532 kshitij.so 31
 
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23942 tejbeer 33
import com.spice.profitmandi.common.model.CustomRetailer;
21532 kshitij.so 34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22609 ashik.ali 35
import com.spice.profitmandi.common.web.util.ResponseSender;
28257 amit.gupta 36
import com.spice.profitmandi.dao.entity.fofo.LoanPaymentRequest;
23936 tejbeer 37
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
28257 amit.gupta 38
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
23936 tejbeer 39
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
28257 amit.gupta 40
import com.spice.profitmandi.dao.model.PaymentOptionModel;
23936 tejbeer 41
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
42
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
43
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
28257 amit.gupta 44
import com.spice.profitmandi.dao.repository.fofo.LoanPaymentRequestRepository;
22555 amit.gupta 45
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
28257 amit.gupta 46
import com.spice.profitmandi.service.integrations.CCAvenuePaymentService;
23942 tejbeer 47
import com.spice.profitmandi.service.user.RetailerService;
22605 ashik.ali 48
import com.spice.profitmandi.service.wallet.WalletService;
23936 tejbeer 49
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
23985 tejbeer 50
 
21532 kshitij.so 51
import io.swagger.annotations.ApiImplicitParam;
52
import io.swagger.annotations.ApiImplicitParams;
53
import io.swagger.annotations.ApiOperation;
54
 
55
@Controller
23936 tejbeer 56
@Transactional(rollbackFor = Throwable.class)
21532 kshitij.so 57
public class WalletController {
58
 
23936 tejbeer 59
	private static final Logger log = LogManager.getLogger(WalletController.class);
21532 kshitij.so 60
 
61
	@Autowired
23037 ashik.ali 62
	UserWalletRepository userWalletRepository;
28257 amit.gupta 63
 
64
	@Autowired
65
	LoanPaymentRequestRepository loanPaymentRequestRepository;
23936 tejbeer 66
 
21532 kshitij.so 67
	@Autowired
23936 tejbeer 68
	AddWalletRequestRepository addWalletRequestRepository;
69
 
70
	@Autowired
23037 ashik.ali 71
	WalletService walletService;
23936 tejbeer 72
 
23037 ashik.ali 73
	@Autowired
23936 tejbeer 74
	JavaMailSender mailSender;
75
 
76
	@Autowired
77
	@Qualifier("userRepository")
78
	private UserRepository userRepository;
28257 amit.gupta 79
 
80
	@Autowired
81
	private CCAvenuePaymentService ccAvenuePaymentService;
23936 tejbeer 82
 
83
	@Autowired
84
	private UserAccountRepository userAccountRepository;
25249 amit.gupta 85
 
23942 tejbeer 86
	@Autowired
87
	private RetailerService retailerService;
23936 tejbeer 88
 
89
	@Autowired
23037 ashik.ali 90
	ResponseSender<?> responseSender;
91
 
23936 tejbeer 92
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 93
	@ApiImplicitParams({
23936 tejbeer 94
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21532 kshitij.so 95
	@ApiOperation(value = "")
23936 tejbeer 96
	public ResponseEntity<?> getMyWallet(HttpServletRequest request) throws ProfitMandiBusinessException {
97
		int userId = (int) request.getAttribute("userId");
25249 amit.gupta 98
		return responseSender.ok(walletService.getUserWalletByUserId(userId));
21532 kshitij.so 99
	}
100
 
23936 tejbeer 101
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 102
	@ApiImplicitParams({
23936 tejbeer 103
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
104
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
105
		int userId = (int) request.getAttribute("userId");
28257 amit.gupta 106
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
23936 tejbeer 107
		try {
28257 amit.gupta 108
			return responseSender.ok(walletService.getPaginatedUserWalletHistoryByRetailerId(retailerId, null, null, 0, 150));
23936 tejbeer 109
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23037 ashik.ali 110
			return responseSender.badRequest(profitMandiBusinessException);
111
		}
21532 kshitij.so 112
	}
28257 amit.gupta 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
	}
21532 kshitij.so 146
 
23936 tejbeer 147
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
148
	@ApiImplicitParams({
149
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
28257 amit.gupta 150
	public ResponseEntity<?> addMoneyToWallet(HttpServletRequest request,
23936 tejbeer 151
			@RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
152
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
25249 amit.gupta 153
		log.info("user_id" + userId);
23936 tejbeer 154
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
25249 amit.gupta 155
		CustomRetailer customRetailer = retailerService.getFofoRetailer(retailerId);
23936 tejbeer 156
		AddWalletRequest addWalletrequest = new AddWalletRequest();
157
		addWalletrequest.setRetailerId(retailerId);
158
		addWalletrequest.setAmount(createAddMoneyRequest.getAmount());
159
		addWalletrequest.setTransaction_reference(createAddMoneyRequest.getTransaction_reference());
24089 tejbeer 160
		addWalletrequest.setReference_date(createAddMoneyRequest.getReference_date());
24749 tejbeer 161
		addWalletrequest.setBank_name(createAddMoneyRequest.getBank_name());
23936 tejbeer 162
		addWalletrequest.setStatus(AddWalletRequestStatus.pending);
25249 amit.gupta 163
		log.info("info" + addWalletrequest);
23936 tejbeer 164
		addWalletRequestRepository.persist(addWalletrequest);
23942 tejbeer 165
		log.info("fofoinfo" + customRetailer);
23936 tejbeer 166
		String subject = "Add money to wallet request";
25249 amit.gupta 167
		String messageText = MessageFormat.format(
168
				"User Id - {0}\n Name -{1}\n Email -{2}\n Reference - {3}\n Amount - Rs.{4} \n link - {5}",
169
				new Integer(addWalletrequest.getRetailerId()), customRetailer.getBusinessName(),
170
				customRetailer.getEmail(), addWalletrequest.getTransaction_reference(),
171
				new Float(addWalletrequest.getAmount()), "http://partners.smartdukaan.com/dashboard");
23943 tejbeer 172
		String email = "neeraj.gupta@smartdukaan.com";
23936 tejbeer 173
		this.sendMailWithAttachments(email, subject, messageText);
174
 
175
		return responseSender.ok(true);
176
	}
28257 amit.gupta 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
	}
23936 tejbeer 192
 
193
	private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
194
		log.info("message" + messageText);
195
		MimeMessage message = mailSender.createMimeMessage();
196
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
197
		helper.setSubject(subject);
198
		helper.setText(messageText);
199
		helper.setTo(email);
25249 amit.gupta 200
		// helper.setCc("neerajgupta2021@gmail.com");
23936 tejbeer 201
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
202
		helper.setFrom(senderAddress);
203
		mailSender.send(message);
204
 
205
	}
25249 amit.gupta 206
 
23942 tejbeer 207
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
208
	@ApiImplicitParams({
209
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
25249 amit.gupta 210
	public ResponseEntity<?> getAddMoneyToWalletHistory(HttpServletRequest request,
211
			@RequestParam(name = "offset", defaultValue = "0") int offset,
23942 tejbeer 212
			@RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
213
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
214
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
25249 amit.gupta 215
		List<AddWalletRequest> walletRequest = addWalletRequestRepository.selectByRetailerId(offset, limit, retailerId);
216
		return responseSender.ok(walletRequest);
23942 tejbeer 217
	}
28257 amit.gupta 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
	}
25249 amit.gupta 225
 
21532 kshitij.so 226
}