Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
29577 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
30014 tejbeer 3
import java.time.LocalDateTime;
4
import java.util.Comparator;
5
import java.util.List;
6
import java.util.stream.Collectors;
7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
10
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Qualifier;
14
import org.springframework.http.MediaType;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.mail.javamail.JavaMailSender;
17
import org.springframework.stereotype.Controller;
18
import org.springframework.transaction.annotation.Transactional;
19
import org.springframework.web.bind.annotation.PathVariable;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestMethod;
22
 
29577 amit.gupta 23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29812 tejbeer 25
import com.spice.profitmandi.common.services.mandii.AccountStatusResponseOut;
26
import com.spice.profitmandi.common.services.mandii.EligibilityStatusEnum;
29577 amit.gupta 27
import com.spice.profitmandi.common.services.mandii.MandiiService;
30014 tejbeer 28
import com.spice.profitmandi.common.services.mandii.TransactionSummaryResponseOut;
29577 amit.gupta 29
import com.spice.profitmandi.common.web.util.ResponseSender;
29812 tejbeer 30
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
31
import com.spice.profitmandi.dao.entity.dtr.CreditStatus;
29577 amit.gupta 32
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
33
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
34
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
29812 tejbeer 35
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
29577 amit.gupta 36
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
37
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
38
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
39
import com.spice.profitmandi.dao.repository.transaction.PaymentRepository;
40
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
41
import com.spice.profitmandi.service.wallet.WalletService;
42
 
30014 tejbeer 43
import io.swagger.annotations.ApiImplicitParam;
44
import io.swagger.annotations.ApiImplicitParams;
29928 amit.gupta 45
 
29577 amit.gupta 46
@Controller
47
@Transactional(rollbackFor = Throwable.class)
48
public class GatewayController {
49
 
50
	private static final Logger log = LogManager.getLogger(GatewayController.class);
51
 
52
	@Autowired
53
	UserWalletRepository userWalletRepository;
29812 tejbeer 54
 
29577 amit.gupta 55
	@Autowired
56
	PaymentRepository paymentRepository;
57
 
58
	@Autowired
59
	AddWalletRequestRepository addWalletRequestRepository;
60
 
61
	@Autowired
62
	WalletService walletService;
63
 
64
	@Autowired
65
	FofoStoreRepository fofoStoreRepository;
29812 tejbeer 66
 
29577 amit.gupta 67
	@Autowired
68
	JavaMailSender mailSender;
69
 
70
	@Autowired
71
	@Qualifier("userRepository")
72
	private UserRepository userRepository;
73
 
74
	@Autowired
75
	private UserAccountRepository userAccountRepository;
76
 
77
	@Autowired
78
	private MandiiService mandiiService;
79
 
80
	@Autowired
81
	ResponseSender<?> responseSender;
82
 
29812 tejbeer 83
	@Autowired
84
	CreditAccountRepository creditAccountRepository;
29577 amit.gupta 85
 
86
	@RequestMapping(value = "payment/gateway/status/{gateway}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
29812 tejbeer 87
	public ResponseEntity<?> getLoanStatus(HttpServletRequest request, @PathVariable Gateway gateway)
88
			throws ProfitMandiBusinessException {
29577 amit.gupta 89
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
90
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
91
		FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
29812 tejbeer 92
		if (gateway != null && gateway.equals(Gateway.MANDII)) {
29577 amit.gupta 93
			try {
29812 tejbeer 94
 
95
				AccountStatusResponseOut accountStatusResponseOut = mandiiService.getStatus(fs.getPan());
29834 tejbeer 96
 
29813 tejbeer 97
				CreditAccount creditAccount = creditAccountRepository.selectByFofoIdAndGateway(retailerId,
98
						Gateway.MANDII);
29812 tejbeer 99
 
100
				if (creditAccount == null) {
101
 
102
					creditAccount = new CreditAccount();
103
 
104
					creditAccount.setFofoId(retailerId);
105
					creditAccount.setGateway(Gateway.MANDII);
106
 
107
				}
29834 tejbeer 108
 
109
				if (accountStatusResponseOut == null) {
110
					creditAccount.setCreditStatus(CreditStatus.UNKNOWN);
111
					creditAccount.setDescription("User company not found");
112
 
29812 tejbeer 113
				} else {
29834 tejbeer 114
					creditAccount.setSanctionedAmount(accountStatusResponseOut.getSanctionLimit().floatValue());
115
					creditAccount.setInterestRate(accountStatusResponseOut.getRateOfInterest());
116
					creditAccount.setAvailableAmount(accountStatusResponseOut.getBalanceAmount().floatValue());
117
					creditAccount.setDescription(accountStatusResponseOut.getCurrentStage().toString());
118
					if (accountStatusResponseOut.getStatus().equals(EligibilityStatusEnum.SANCTION_AVAILABLE)) {
119
						creditAccount.setCreditStatus(CreditStatus.SANCTIONED);
120
					} else if (accountStatusResponseOut.getStatus().equals(EligibilityStatusEnum.IN_ELIGIBLE)) {
121
						creditAccount.setCreditStatus(CreditStatus.INELIGIBLE);
122
					} else {
29812 tejbeer 123
 
29834 tejbeer 124
						creditAccount.setCreditStatus(CreditStatus.TO_BE_EVALUATED);
125
					}
29812 tejbeer 126
				}
29834 tejbeer 127
 
128
				creditAccount.setUpdatedOn(LocalDateTime.now());
129
 
29812 tejbeer 130
				creditAccountRepository.persist(creditAccount);
131
				return responseSender.ok(accountStatusResponseOut);
29577 amit.gupta 132
			} catch (Exception e) {
29928 amit.gupta 133
				log.info("Trace {}\n{}", e.getMessage(), e);
30014 tejbeer 134
				throw new ProfitMandiBusinessException("Loan Provider", gateway, "Can't fetch details for provider");
29577 amit.gupta 135
			}
136
		}
30014 tejbeer 137
 
138
		else {
139
			throw new ProfitMandiBusinessException("Provider", "Empty", "Provider can't be empty");
140
		}
29577 amit.gupta 141
	}
30014 tejbeer 142
 
143
	@RequestMapping(value = "payment/gateway/transactionSummary", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
144
	@ApiImplicitParams({
145
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
146
	public ResponseEntity<?> getLoanStatus(HttpServletRequest request) throws Exception {
147
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
148
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
149
		FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
150
		List<TransactionSummaryResponseOut> tranactionSummary = mandiiService.getTransactionSummary(fs.getPan());
151
		tranactionSummary = tranactionSummary.stream()
152
				.sorted(Comparator.comparingInt(TransactionSummaryResponseOut::getMerchantOrderNumber).reversed())
153
				.collect(Collectors.toList());
154
		log.info("tranactionSummary" + tranactionSummary);
155
 
156
		return responseSender.ok(tranactionSummary.stream().limit(10).collect(Collectors.toList()));
157
	}
29577 amit.gupta 158
}