Subversion Repositories SmartDukaan

Rev

Rev 28257 | Rev 30700 | 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;
23942 tejbeer 5
import java.util.List;
28257 amit.gupta 6
import java.util.Map;
7
import java.util.Optional;
23936 tejbeer 8
 
9
import javax.mail.internet.InternetAddress;
10
import javax.mail.internet.MimeMessage;
21532 kshitij.so 11
import javax.servlet.http.HttpServletRequest;
12
 
23985 tejbeer 13
import org.apache.logging.log4j.LogManager;
23568 govind 14
import org.apache.logging.log4j.Logger;
21532 kshitij.so 15
import org.springframework.beans.factory.annotation.Autowired;
23936 tejbeer 16
import org.springframework.beans.factory.annotation.Qualifier;
21532 kshitij.so 17
import org.springframework.http.MediaType;
18
import org.springframework.http.ResponseEntity;
23936 tejbeer 19
import org.springframework.mail.javamail.JavaMailSender;
20
import org.springframework.mail.javamail.MimeMessageHelper;
21532 kshitij.so 21
import org.springframework.stereotype.Controller;
21702 ashik.ali 22
import org.springframework.transaction.annotation.Transactional;
28257 amit.gupta 23
import org.springframework.ui.Model;
24
import org.springframework.web.bind.annotation.PathVariable;
23936 tejbeer 25
import org.springframework.web.bind.annotation.RequestBody;
21532 kshitij.so 26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMethod;
23942 tejbeer 28
import org.springframework.web.bind.annotation.RequestParam;
21532 kshitij.so 29
 
30
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23942 tejbeer 31
import com.spice.profitmandi.common.model.CustomRetailer;
21532 kshitij.so 32
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22609 ashik.ali 33
import com.spice.profitmandi.common.web.util.ResponseSender;
29503 amit.gupta 34
import com.spice.profitmandi.dao.entity.fofo.FofoPayment;
23936 tejbeer 35
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
28257 amit.gupta 36
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
23936 tejbeer 37
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
28257 amit.gupta 38
import com.spice.profitmandi.dao.model.PaymentOptionModel;
23936 tejbeer 39
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
40
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
41
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
29503 amit.gupta 42
import com.spice.profitmandi.dao.repository.fofo.FofoPaymentRepository;
22555 amit.gupta 43
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
28257 amit.gupta 44
import com.spice.profitmandi.service.integrations.CCAvenuePaymentService;
23942 tejbeer 45
import com.spice.profitmandi.service.user.RetailerService;
22605 ashik.ali 46
import com.spice.profitmandi.service.wallet.WalletService;
23936 tejbeer 47
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
23985 tejbeer 48
 
21532 kshitij.so 49
import io.swagger.annotations.ApiImplicitParam;
50
import io.swagger.annotations.ApiImplicitParams;
51
import io.swagger.annotations.ApiOperation;
52
 
53
@Controller
23936 tejbeer 54
@Transactional(rollbackFor = Throwable.class)
21532 kshitij.so 55
public class WalletController {
56
 
23936 tejbeer 57
	private static final Logger log = LogManager.getLogger(WalletController.class);
21532 kshitij.so 58
 
59
	@Autowired
23037 ashik.ali 60
	UserWalletRepository userWalletRepository;
28257 amit.gupta 61
 
62
	@Autowired
29503 amit.gupta 63
	FofoPaymentRepository fofoPaymentRepository;
23936 tejbeer 64
 
21532 kshitij.so 65
	@Autowired
23936 tejbeer 66
	AddWalletRequestRepository addWalletRequestRepository;
67
 
68
	@Autowired
23037 ashik.ali 69
	WalletService walletService;
23936 tejbeer 70
 
23037 ashik.ali 71
	@Autowired
23936 tejbeer 72
	JavaMailSender mailSender;
73
 
74
	@Autowired
75
	@Qualifier("userRepository")
76
	private UserRepository userRepository;
28257 amit.gupta 77
 
78
	@Autowired
79
	private CCAvenuePaymentService ccAvenuePaymentService;
23936 tejbeer 80
 
81
	@Autowired
82
	private UserAccountRepository userAccountRepository;
25249 amit.gupta 83
 
23942 tejbeer 84
	@Autowired
85
	private RetailerService retailerService;
23936 tejbeer 86
 
87
	@Autowired
23037 ashik.ali 88
	ResponseSender<?> responseSender;
89
 
23936 tejbeer 90
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 91
	@ApiImplicitParams({
23936 tejbeer 92
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21532 kshitij.so 93
	@ApiOperation(value = "")
23936 tejbeer 94
	public ResponseEntity<?> getMyWallet(HttpServletRequest request) throws ProfitMandiBusinessException {
95
		int userId = (int) request.getAttribute("userId");
25249 amit.gupta 96
		return responseSender.ok(walletService.getUserWalletByUserId(userId));
21532 kshitij.so 97
	}
98
 
23936 tejbeer 99
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 100
	@ApiImplicitParams({
23936 tejbeer 101
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
102
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
103
		int userId = (int) request.getAttribute("userId");
28257 amit.gupta 104
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
23936 tejbeer 105
		try {
28257 amit.gupta 106
			return responseSender.ok(walletService.getPaginatedUserWalletHistoryByRetailerId(retailerId, null, null, 0, 150));
23936 tejbeer 107
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23037 ashik.ali 108
			return responseSender.badRequest(profitMandiBusinessException);
109
		}
21532 kshitij.so 110
	}
28257 amit.gupta 111
 
112
	@RequestMapping(value = "/wallet/add-via-loan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
113
	@ApiImplicitParams({
114
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
115
	public ResponseEntity<?> payViaLoan(HttpServletRequest request, @RequestParam double loanAmount, 
116
			@RequestParam double surcharge,
117
			@RequestParam String payMethod,  @RequestParam Gateway gateway) throws Exception {
118
		List<PaymentOptionModel> paymentOptionModels = ccAvenuePaymentService.getPaymentOptionModelMap().get(gateway);
119
		Optional<PaymentOptionModel> optPaymentOptionModel = paymentOptionModels.stream().filter(x->x.getPaymentMethod().equals(payMethod)).findFirst();
120
 
121
		if(!optPaymentOptionModel.isPresent()) {
122
			return responseSender.badRequest(new ProfitMandiBusinessException("", "", "Bad parameters in request"));
123
		} else {
124
			double calculatedSurcharge = Math.round(loanAmount*optPaymentOptionModel.get().getSurcharge()*1.18/100);
125
			if(Math.abs(calculatedSurcharge - surcharge) >= 1) {
126
				return responseSender.badRequest(new ProfitMandiBusinessException("", "", "Bad parameters in request"));
127
			}
128
		}
129
		int userId = (int) request.getAttribute("userId");
130
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
29503 amit.gupta 131
		FofoPayment fofoPayment = new FofoPayment();
132
		fofoPayment.setCreateTimestamp(LocalDateTime.now());
133
		fofoPayment.setFofoId(retailerId);
134
		fofoPayment.setAmount(loanAmount);
135
		fofoPayment.setGatewayFee(surcharge);
136
		fofoPayment.setGateway(gateway);
137
		fofoPayment.setPaymentMethod(optPaymentOptionModel.get().getPaymentMethodDesctiption());
138
		fofoPaymentRepository.persist(fofoPayment);
139
		Map<String, String> returnMap = ccAvenuePaymentService.getFormParams(fofoPayment, payMethod);
28257 amit.gupta 140
		returnMap.put("status", "pending");
141
		return responseSender.ok(returnMap);
142
 
143
	}
21532 kshitij.so 144
 
23936 tejbeer 145
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
146
	@ApiImplicitParams({
147
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
28257 amit.gupta 148
	public ResponseEntity<?> addMoneyToWallet(HttpServletRequest request,
23936 tejbeer 149
			@RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
150
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
25249 amit.gupta 151
		log.info("user_id" + userId);
23936 tejbeer 152
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
25249 amit.gupta 153
		CustomRetailer customRetailer = retailerService.getFofoRetailer(retailerId);
23936 tejbeer 154
		AddWalletRequest addWalletrequest = new AddWalletRequest();
155
		addWalletrequest.setRetailerId(retailerId);
156
		addWalletrequest.setAmount(createAddMoneyRequest.getAmount());
157
		addWalletrequest.setTransaction_reference(createAddMoneyRequest.getTransaction_reference());
24089 tejbeer 158
		addWalletrequest.setReference_date(createAddMoneyRequest.getReference_date());
24749 tejbeer 159
		addWalletrequest.setBank_name(createAddMoneyRequest.getBank_name());
23936 tejbeer 160
		addWalletrequest.setStatus(AddWalletRequestStatus.pending);
25249 amit.gupta 161
		log.info("info" + addWalletrequest);
23936 tejbeer 162
		addWalletRequestRepository.persist(addWalletrequest);
23942 tejbeer 163
		log.info("fofoinfo" + customRetailer);
23936 tejbeer 164
		String subject = "Add money to wallet request";
25249 amit.gupta 165
		String messageText = MessageFormat.format(
166
				"User Id - {0}\n Name -{1}\n Email -{2}\n Reference - {3}\n Amount - Rs.{4} \n link - {5}",
167
				new Integer(addWalletrequest.getRetailerId()), customRetailer.getBusinessName(),
168
				customRetailer.getEmail(), addWalletrequest.getTransaction_reference(),
169
				new Float(addWalletrequest.getAmount()), "http://partners.smartdukaan.com/dashboard");
23943 tejbeer 170
		String email = "neeraj.gupta@smartdukaan.com";
23936 tejbeer 171
		this.sendMailWithAttachments(email, subject, messageText);
172
 
173
		return responseSender.ok(true);
174
	}
28257 amit.gupta 175
 
176
	@RequestMapping(value = "/wallet/add-money/{loanId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
177
	@ApiImplicitParams({
178
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
179
	public ResponseEntity<?> addMoneyToWallet(HttpServletRequest request,@PathVariable int loanId) throws Exception {
180
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
181
		log.info("user_id" + userId);
182
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
183
 
29503 amit.gupta 184
		FofoPayment fofoPayment = fofoPaymentRepository.selectById(loanId);
185
		if(fofoPayment.getFofoId()!=retailerId) {
28257 amit.gupta 186
			return responseSender.badRequest(new ProfitMandiBusinessException("Invalid Add Money Id", "Invalid Add money id", "Invalid Add Money"));
187
		}
29503 amit.gupta 188
		return responseSender.ok(fofoPayment);
28257 amit.gupta 189
	}
23936 tejbeer 190
 
191
	private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
192
		log.info("message" + messageText);
193
		MimeMessage message = mailSender.createMimeMessage();
194
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
195
		helper.setSubject(subject);
196
		helper.setText(messageText);
197
		helper.setTo(email);
25249 amit.gupta 198
		// helper.setCc("neerajgupta2021@gmail.com");
23936 tejbeer 199
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
200
		helper.setFrom(senderAddress);
201
		mailSender.send(message);
202
 
203
	}
25249 amit.gupta 204
 
23942 tejbeer 205
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
206
	@ApiImplicitParams({
207
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
25249 amit.gupta 208
	public ResponseEntity<?> getAddMoneyToWalletHistory(HttpServletRequest request,
209
			@RequestParam(name = "offset", defaultValue = "0") int offset,
23942 tejbeer 210
			@RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
211
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
212
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
25249 amit.gupta 213
		List<AddWalletRequest> walletRequest = addWalletRequestRepository.selectByRetailerId(offset, limit, retailerId);
214
		return responseSender.ok(walletRequest);
23942 tejbeer 215
	}
28257 amit.gupta 216
 
217
 
218
 
219
	@RequestMapping(value = "/getPaymentOptions", method = RequestMethod.GET)
220
	public ResponseEntity<?> getPaymentOptions(HttpServletRequest request, Model model) throws Exception {
221
		return responseSender.ok(ccAvenuePaymentService.getPaymentOptionModelMap());
222
	}
25249 amit.gupta 223
 
21532 kshitij.so 224
}