Subversion Repositories SmartDukaan

Rev

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