Subversion Repositories SmartDukaan

Rev

Rev 29813 | Rev 29928 | 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
 
29812 tejbeer 3
import java.time.LocalDateTime;
4
 
29577 amit.gupta 5
import javax.servlet.http.HttpServletRequest;
6
 
7
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Qualifier;
11
import org.springframework.http.MediaType;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.mail.javamail.JavaMailSender;
14
import org.springframework.stereotype.Controller;
15
import org.springframework.transaction.annotation.Transactional;
16
import org.springframework.web.bind.annotation.PathVariable;
17
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestMethod;
19
 
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29812 tejbeer 22
import com.spice.profitmandi.common.services.mandii.AccountStatusResponseOut;
23
import com.spice.profitmandi.common.services.mandii.EligibilityStatusEnum;
29577 amit.gupta 24
import com.spice.profitmandi.common.services.mandii.MandiiService;
25
import com.spice.profitmandi.common.web.util.ResponseSender;
29812 tejbeer 26
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
27
import com.spice.profitmandi.dao.entity.dtr.CreditStatus;
29577 amit.gupta 28
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
29
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
30
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
29812 tejbeer 31
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
29577 amit.gupta 32
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
33
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
35
import com.spice.profitmandi.dao.repository.transaction.PaymentRepository;
36
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
37
import com.spice.profitmandi.service.wallet.WalletService;
38
 
39
@Controller
40
@Transactional(rollbackFor = Throwable.class)
41
public class GatewayController {
42
 
43
	private static final Logger log = LogManager.getLogger(GatewayController.class);
44
 
45
	@Autowired
46
	UserWalletRepository userWalletRepository;
29812 tejbeer 47
 
29577 amit.gupta 48
	@Autowired
49
	PaymentRepository paymentRepository;
50
 
51
	@Autowired
52
	AddWalletRequestRepository addWalletRequestRepository;
53
 
54
	@Autowired
55
	WalletService walletService;
56
 
57
	@Autowired
58
	FofoStoreRepository fofoStoreRepository;
29812 tejbeer 59
 
29577 amit.gupta 60
	@Autowired
61
	JavaMailSender mailSender;
62
 
63
	@Autowired
64
	@Qualifier("userRepository")
65
	private UserRepository userRepository;
66
 
67
	@Autowired
68
	private UserAccountRepository userAccountRepository;
69
 
70
	@Autowired
71
	private MandiiService mandiiService;
72
 
73
	@Autowired
74
	ResponseSender<?> responseSender;
75
 
29812 tejbeer 76
	@Autowired
77
	CreditAccountRepository creditAccountRepository;
29577 amit.gupta 78
 
79
	@RequestMapping(value = "payment/gateway/status/{gateway}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
29812 tejbeer 80
	public ResponseEntity<?> getLoanStatus(HttpServletRequest request, @PathVariable Gateway gateway)
81
			throws ProfitMandiBusinessException {
29577 amit.gupta 82
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
83
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
84
		FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
29812 tejbeer 85
		if (gateway != null && gateway.equals(Gateway.MANDII)) {
29577 amit.gupta 86
			try {
29812 tejbeer 87
 
88
				AccountStatusResponseOut accountStatusResponseOut = mandiiService.getStatus(fs.getPan());
29834 tejbeer 89
 
29813 tejbeer 90
				CreditAccount creditAccount = creditAccountRepository.selectByFofoIdAndGateway(retailerId,
91
						Gateway.MANDII);
29812 tejbeer 92
 
93
				if (creditAccount == null) {
94
 
95
					creditAccount = new CreditAccount();
96
 
97
					creditAccount.setFofoId(retailerId);
98
					creditAccount.setGateway(Gateway.MANDII);
99
 
100
				}
29834 tejbeer 101
 
102
				if (accountStatusResponseOut == null) {
103
					creditAccount.setCreditStatus(CreditStatus.UNKNOWN);
104
					creditAccount.setDescription("User company not found");
105
 
29812 tejbeer 106
				} else {
29834 tejbeer 107
					creditAccount.setSanctionedAmount(accountStatusResponseOut.getSanctionLimit().floatValue());
108
					creditAccount.setInterestRate(accountStatusResponseOut.getRateOfInterest());
109
					creditAccount.setAvailableAmount(accountStatusResponseOut.getBalanceAmount().floatValue());
110
					creditAccount.setDescription(accountStatusResponseOut.getCurrentStage().toString());
111
					if (accountStatusResponseOut.getStatus().equals(EligibilityStatusEnum.SANCTION_AVAILABLE)) {
112
						creditAccount.setCreditStatus(CreditStatus.SANCTIONED);
113
					} else if (accountStatusResponseOut.getStatus().equals(EligibilityStatusEnum.IN_ELIGIBLE)) {
114
						creditAccount.setCreditStatus(CreditStatus.INELIGIBLE);
115
					} else {
29812 tejbeer 116
 
29834 tejbeer 117
						creditAccount.setCreditStatus(CreditStatus.TO_BE_EVALUATED);
118
					}
29812 tejbeer 119
				}
29834 tejbeer 120
 
121
				creditAccount.setUpdatedOn(LocalDateTime.now());
122
 
29812 tejbeer 123
				creditAccountRepository.persist(creditAccount);
124
				return responseSender.ok(accountStatusResponseOut);
29577 amit.gupta 125
			} catch (Exception e) {
126
				throw new ProfitMandiBusinessException("Loan Provider", gateway, "Cant fetch details for provider");
127
			}
128
		} else {
129
			throw new ProfitMandiBusinessException("Provider", "Empty", "Provider cant be empty");
130
		}
131
	}
132
}