Subversion Repositories SmartDukaan

Rev

Rev 35458 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21464 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
21468 kshitij.so 3
import java.util.ArrayList;
24260 amit.gupta 4
import java.util.Arrays;
23585 tejbeer 5
import java.util.HashMap;
6
import java.util.LinkedHashSet;
21468 kshitij.so 7
import java.util.List;
8
import java.util.Map;
23585 tejbeer 9
import java.util.Optional;
10
import java.util.Set;
11
import java.util.stream.Collectors;
21464 kshitij.so 12
 
23585 tejbeer 13
import javax.annotation.PostConstruct;
21464 kshitij.so 14
import javax.servlet.http.HttpServletRequest;
23585 tejbeer 15
 
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
21505 kshitij.so 18
import org.springframework.beans.factory.annotation.Autowired;
23585 tejbeer 19
import org.springframework.beans.factory.annotation.Value;
21464 kshitij.so 20
import org.springframework.http.MediaType;
21
import org.springframework.http.ResponseEntity;
22
import org.springframework.stereotype.Controller;
21702 ashik.ali 23
import org.springframework.transaction.annotation.Transactional;
21514 kshitij.so 24
import org.springframework.web.bind.annotation.RequestBody;
21464 kshitij.so 25
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestParam;
28
 
23585 tejbeer 29
import com.spice.profitmandi.common.enumuration.RechargeStatus;
21505 kshitij.so 30
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23585 tejbeer 31
import com.spice.profitmandi.common.model.CustomPaymentOption;
32
import com.spice.profitmandi.common.model.OperatorType;
21464 kshitij.so 33
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23585 tejbeer 34
import com.spice.profitmandi.common.model.RechargeCredential;
35
import com.spice.profitmandi.common.model.RechargeRequest;
36
import com.spice.profitmandi.common.model.RechargeResponse;
22931 ashik.ali 37
import com.spice.profitmandi.common.web.util.ResponseSender;
23585 tejbeer 38
import com.spice.profitmandi.dao.entity.dtr.RechargeCommission;
39
import com.spice.profitmandi.dao.entity.dtr.RechargeOperator;
40
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
21735 ashik.ali 41
import com.spice.profitmandi.dao.entity.dtr.User;
23273 ashik.ali 42
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
21735 ashik.ali 43
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
23585 tejbeer 44
import com.spice.profitmandi.dao.enumuration.dtr.RechargeType;
45
import com.spice.profitmandi.dao.repository.GenericRepository;
46
import com.spice.profitmandi.dao.repository.dtr.RechargeCommissionRepository;
47
import com.spice.profitmandi.dao.repository.dtr.RechargeOperatorRepository;
48
import com.spice.profitmandi.dao.repository.dtr.RechargeProviderRepository;
49
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
21735 ashik.ali 50
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
51
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
23115 ashik.ali 52
import com.spice.profitmandi.dao.repository.transaction.PendingRechargeCommissionRepository;
23585 tejbeer 53
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
54
import com.spice.profitmandi.service.recharge.RechargeService;
25547 amit.gupta 55
import com.spice.profitmandi.service.wallet.WalletService;
21514 kshitij.so 56
import com.spice.profitmandi.web.req.CreateRechargeRequest;
21505 kshitij.so 57
import com.spice.profitmandi.web.res.ConfirmRechargeResponse;
21514 kshitij.so 58
import com.spice.profitmandi.web.res.CreateRechargeResponse;
21517 kshitij.so 59
import com.spice.profitmandi.web.res.RechargeResultPojo;
23585 tejbeer 60
 
21464 kshitij.so 61
import io.swagger.annotations.ApiImplicitParam;
62
import io.swagger.annotations.ApiImplicitParams;
63
import io.swagger.annotations.ApiOperation;
64
 
65
@Controller
24893 amit.gupta 66
@Transactional(rollbackFor = Throwable.class)
21464 kshitij.so 67
public class RechargeController {
68
 
24893 amit.gupta 69
	private static final Logger log = LoggerFactory.getLogger(RechargeController.class);
21514 kshitij.so 70
	private static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR";
24893 amit.gupta 71
 
25048 amit.gupta 72
	private List<String> UNBLOCKED_RECHARGE_EMAILS = Arrays.asList("pgautam84@gmail.com");
23101 amit.gupta 73
	private static final String X_REAL_IP = "X-Real-IP";
24893 amit.gupta 74
 
23585 tejbeer 75
	@Value("${think.walnut.digital.recharge.transaction.mobile.url}")
76
	private String thinkWalnutDigitalRechargeTransactionMobileUrl;
24893 amit.gupta 77
 
23585 tejbeer 78
	@Value("${think.walnut.digital.recharge.transaction.mobile.username}")
79
	private String thinkWalnutDigitalRechargeTransactionMobileUserName;
24893 amit.gupta 80
 
23585 tejbeer 81
	@Value("${think.walnut.digital.recharge.transaction.mobile.password}")
82
	private String thinkWalnutDigitalRechargeTransactionMobilePassword;
24893 amit.gupta 83
 
23585 tejbeer 84
	@Value("${think.walnut.digital.recharge.transaction.dth.url}")
85
	private String thinkWalnutDigitalRechargeTransactionDthUrl;
24893 amit.gupta 86
 
23585 tejbeer 87
	@Value("${think.walnut.digital.recharge.transaction.dth.username}")
88
	private String thinkWalnutDigitalRechargeTransactionDthUserName;
24893 amit.gupta 89
 
23585 tejbeer 90
	@Value("${think.walnut.digital.recharge.transaction.dth.password}")
91
	private String thinkWalnutDigitalRechargeTransactionDthPassword;
24893 amit.gupta 92
 
23585 tejbeer 93
	@Value("${think.walnut.digital.recharge.enquiry.url}")
94
	private String thinkWalnutDigitalRechargeEnquiryUrl;
24893 amit.gupta 95
 
23585 tejbeer 96
	@Value("${think.walnut.digital.recharge.enquiry.username}")
97
	private String thinkWalnutDigitalRechargeEnquiryUserName;
24893 amit.gupta 98
 
23585 tejbeer 99
	@Value("${think.walnut.digital.recharge.enquiry.password}")
100
	private String thinkWalnutDigitalRechargeEnquiryPassword;
24893 amit.gupta 101
 
23585 tejbeer 102
	@Value("${think.walnut.digital.recharge.auth.key}")
103
	private String thinkWalnutDigitalRechargeAuthKey;
24893 amit.gupta 104
 
21505 kshitij.so 105
	@Autowired
23585 tejbeer 106
	UserWalletRepository userWalletRepository;
25703 amit.gupta 107
 
25547 amit.gupta 108
	@Autowired
109
	WalletService walletService;
24893 amit.gupta 110
 
23585 tejbeer 111
	@Autowired
112
	private GenericRepository genericRepository;
24893 amit.gupta 113
 
23585 tejbeer 114
	@Autowired
115
	private RechargeCommissionRepository rechargeCommissionRepository;
24893 amit.gupta 116
 
23585 tejbeer 117
	@Autowired
22931 ashik.ali 118
	private UserAccountRepository userAccountRepository;
24893 amit.gupta 119
 
23585 tejbeer 120
	@Autowired
121
	private RechargeTransactionRepository rechargeTransactionRepository;
21514 kshitij.so 122
 
123
	@Autowired
23585 tejbeer 124
	private RechargeProviderRepository rechargeProviderRepository;
24893 amit.gupta 125
 
23585 tejbeer 126
	@Autowired
22931 ashik.ali 127
	private UserRepository userRepository;
24893 amit.gupta 128
 
22931 ashik.ali 129
	@Autowired
23585 tejbeer 130
	private RechargeOperatorRepository rechargeOperatorRepository;
24893 amit.gupta 131
 
23585 tejbeer 132
	@Autowired
133
	private RechargeService rechargeService;
24893 amit.gupta 134
 
23585 tejbeer 135
	@Autowired
23115 ashik.ali 136
	private PendingRechargeCommissionRepository pendingRechargeCommissionRepository;
24893 amit.gupta 137
 
23115 ashik.ali 138
	@Autowired
22931 ashik.ali 139
	private ResponseSender<?> responseSender;
24893 amit.gupta 140
 
23585 tejbeer 141
	private RechargeCredential rechargeTransactionMobileCredential;
142
	private RechargeCredential rechargeTransactionDthCredential;
143
	private RechargeCredential thinkWalnutDigitalRechargeEnquiryCredential;
21514 kshitij.so 144
 
24893 amit.gupta 145
	@RequestMapping(value = ProfitMandiConstants.URL_GET_SERVICE_PROVIDER, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21464 kshitij.so 146
	@ApiImplicitParams({
24893 amit.gupta 147
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21464 kshitij.so 148
	@ApiOperation(value = "")
24893 amit.gupta 149
	public ResponseEntity<?> getServiceProvider(HttpServletRequest request,
150
			@RequestParam(value = "deviceNumber") String deviceNumber,
151
			@RequestParam(value = "rechargeType") String rechargeType) {
36620 amit 152
		return responseSender.ok(null);
21464 kshitij.so 153
	}
21505 kshitij.so 154
 
24893 amit.gupta 155
	@RequestMapping(value = ProfitMandiConstants.URL_GET_ALL_DENOMINATIONS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21468 kshitij.so 156
	@ApiImplicitParams({
24893 amit.gupta 157
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21468 kshitij.so 158
	@ApiOperation(value = "")
24893 amit.gupta 159
	public ResponseEntity<?> getAllDenominations(HttpServletRequest request,
160
			@RequestParam(value = "operatorId") long operatorId, @RequestParam(value = "circleCode") String circleCode,
161
			@RequestParam(value = "denominationType") String denominationType) {
36620 amit 162
		return responseSender.ok(new ArrayList<>());
21468 kshitij.so 163
	}
21505 kshitij.so 164
 
24893 amit.gupta 165
	@RequestMapping(value = ProfitMandiConstants.URL_MOBILE_OPERATORS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21468 kshitij.so 166
	@ApiImplicitParams({
24893 amit.gupta 167
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
168
 
21468 kshitij.so 169
	@ApiOperation(value = "")
24893 amit.gupta 170
	public ResponseEntity<?> getAllMobileOperators(HttpServletRequest request,
171
			@RequestParam(name = ProfitMandiConstants.OPERATOR_TYPE) OperatorType operatorType) {
23585 tejbeer 172
		Map<Integer, String> mobileProviderMap = new HashMap<>();
173
		List<RechargeOperator> rechargeOperators = rechargeOperatorRepository.selectAllByOperatorType(operatorType);
24893 amit.gupta 174
		// List<RechargeOperator> rechargeOperators =
175
		// rechargeOperatorRepository.selectAllByRechargeType(RechargeType.MOBILE);
176
		for (RechargeOperator rechargeOperator : rechargeOperators) {
177
			mobileProviderMap.put(rechargeOperator.getId(), rechargeOperator.getName());
178
		}
22931 ashik.ali 179
		return responseSender.ok(mobileProviderMap);
21468 kshitij.so 180
	}
21505 kshitij.so 181
 
24893 amit.gupta 182
	@RequestMapping(value = ProfitMandiConstants.URL_DTH_OPERATORS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21468 kshitij.so 183
	@ApiImplicitParams({
24893 amit.gupta 184
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21468 kshitij.so 185
	@ApiOperation(value = "")
24893 amit.gupta 186
	public ResponseEntity<?> getAllDTHOperators(HttpServletRequest request) {
23585 tejbeer 187
		Map<Integer, String> dthProviderMap = new HashMap<>();
188
		List<RechargeOperator> dthOperators = rechargeOperatorRepository.selectAllByRechargeType(RechargeType.DTH);
24893 amit.gupta 189
		for (RechargeOperator dthOperator : dthOperators) {
23585 tejbeer 190
			dthProviderMap.put(dthOperator.getId(), dthOperator.getName());
191
		}
22931 ashik.ali 192
		return responseSender.ok(dthProviderMap);
24893 amit.gupta 193
 
21468 kshitij.so 194
	}
195
 
24893 amit.gupta 196
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_CONFIRM, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21505 kshitij.so 197
	@ApiImplicitParams({
24893 amit.gupta 198
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21505 kshitij.so 199
	@ApiOperation(value = "")
24893 amit.gupta 200
	public ResponseEntity<?> confirmRecharge(HttpServletRequest request,
201
			@RequestParam(value = "rechargeAmount") long rechargeAmount) throws ProfitMandiBusinessException {
202
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
23273 ashik.ali 203
		UserAccount userAccount = null;
24893 amit.gupta 204
		if (rechargeAmount <= 0) {
22931 ashik.ali 205
			return responseSender.badRequest(null);
21505 kshitij.so 206
		}
21514 kshitij.so 207
 
23273 ashik.ali 208
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
24893 amit.gupta 209
 
25547 amit.gupta 210
		long walletAmount = walletService.getWalletAmount(userAccount.getAccountKey());
21514 kshitij.so 211
 
21505 kshitij.so 212
		ConfirmRechargeResponse crr = new ConfirmRechargeResponse();
24893 amit.gupta 213
		// crr.setWalletAmount(wallet.getAmount());
25547 amit.gupta 214
		crr.setWalletAmountLeft(walletAmount - rechargeAmount);
21505 kshitij.so 215
		crr.setCanProceed(true);
24893 amit.gupta 216
		if (crr.getWalletAmountLeft() < 0) {
21505 kshitij.so 217
			crr.setCanProceed(false);
218
			crr.setReason("You don't have sufficient wallet balance");
219
		}
22931 ashik.ali 220
		return responseSender.ok(crr);
21505 kshitij.so 221
	}
24893 amit.gupta 222
 
223
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_PENDING_COMMISSION, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
23115 ashik.ali 224
	@ApiImplicitParams({
24893 amit.gupta 225
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
23115 ashik.ali 226
	@ApiOperation(value = "")
227
	public ResponseEntity<?> getPendingRechargeCommission(HttpServletRequest request)
24893 amit.gupta 228
			throws ProfitMandiBusinessException {
229
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
23115 ashik.ali 230
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
231
		return responseSender.ok(pendingRechargeCommissionRepository.selectByRetailerId(retailerId));
232
	}
24893 amit.gupta 233
 
234
	private RechargeRequest convertRechargeRequest(CreateRechargeRequest createRechargeRequest) {
235
		RechargeRequest rr = new RechargeRequest();
23585 tejbeer 236
		rr.setAmount(createRechargeRequest.getRechargeAmount());
237
		rr.setOperatorId((int) createRechargeRequest.getOperatorId());
238
		rr.setOperatorType(createRechargeRequest.getPlan());
239
		rr.setReferenceNumber(createRechargeRequest.getNumber());
240
		rr.setRechargeType(createRechargeRequest.getRechargeType());
24893 amit.gupta 241
 
23585 tejbeer 242
		Set<CustomPaymentOption> pp = new LinkedHashSet<>();
243
		CustomPaymentOption cpo = new CustomPaymentOption();
244
		cpo.setPaymentOption("cash");
245
		cpo.setPaymentOptionId(1);
246
		cpo.setAmount(createRechargeRequest.getRechargeAmount());
247
		pp.add(cpo);
248
		rr.setPaymentOptions(pp);
24893 amit.gupta 249
		return rr;
23585 tejbeer 250
	}
24893 amit.gupta 251
 
252
	@PostConstruct
23585 tejbeer 253
	private void setRechargeCredentials() {
254
		rechargeTransactionMobileCredential = new RechargeCredential();
255
		rechargeTransactionMobileCredential.setRechargeUrl(thinkWalnutDigitalRechargeTransactionMobileUrl);
256
		rechargeTransactionMobileCredential.setRechargeUserName(thinkWalnutDigitalRechargeTransactionMobileUserName);
257
		rechargeTransactionMobileCredential.setRechargePassword(thinkWalnutDigitalRechargeTransactionMobilePassword);
258
		rechargeTransactionMobileCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
24893 amit.gupta 259
 
23585 tejbeer 260
		rechargeTransactionDthCredential = new RechargeCredential();
261
		rechargeTransactionDthCredential.setRechargeUrl(thinkWalnutDigitalRechargeTransactionDthUrl);
262
		rechargeTransactionDthCredential.setRechargeUserName(thinkWalnutDigitalRechargeTransactionDthUserName);
263
		rechargeTransactionDthCredential.setRechargePassword(thinkWalnutDigitalRechargeTransactionDthPassword);
264
		rechargeTransactionDthCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
265
	}
24893 amit.gupta 266
 
267
	@RequestMapping(value = ProfitMandiConstants.URL_CREATE_RECHARGE, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
23587 tejbeer 268
	@ApiImplicitParams({
24893 amit.gupta 269
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
270
 
271
	public ResponseEntity<?> createRechargeDown(HttpServletRequest request,
272
			@RequestBody CreateRechargeRequest createRechargeRequest) throws ProfitMandiBusinessException {
273
 
274
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
25100 amit.gupta 275
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
276
		User saholicUser = null;
25007 amit.gupta 277
		try {
25100 amit.gupta 278
			saholicUser = userRepository.selectById(retailerId);
279
			if (UNBLOCKED_RECHARGE_EMAILS.contains(saholicUser.getEmailId())) {
25007 amit.gupta 280
				return createRecharge(request, createRechargeRequest);
281
			}
282
		} catch (ProfitMandiBusinessException e) {
283
			log.error("Unable to get user", e);
284
		}
25703 amit.gupta 285
 
24893 amit.gupta 286
		CreateRechargeResponse crr = new CreateRechargeResponse();
287
		String ipAddress = remoteAddr(request);
288
		log.info("IP Address is {}", ipAddress);
289
 
290
		crr.setResult(true);
291
		crr.setRequestId("RECHARGE00050");
292
		return responseSender.ok(crr);
293
 
294
	}
295
 
296
	public ResponseEntity<?> createRecharge(HttpServletRequest request,
297
			@RequestBody CreateRechargeRequest createRechargeRequest) throws ProfitMandiBusinessException {
298
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
299
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
26228 amit.gupta 300
 
24893 amit.gupta 301
		String ipAddress = remoteAddr(request);
302
 
303
		CreateRechargeResponse crr = new CreateRechargeResponse();
304
 
305
		UserAccount userAccount = null;
25547 amit.gupta 306
		long walletAmount = walletService.getWalletAmount(retailerId);
24893 amit.gupta 307
 
308
		if (createRechargeRequest.getRechargeAmount() <= 0) {
21514 kshitij.so 309
			crr.setReason("Illegal recharge amount");
310
			crr.setResult(false);
22931 ashik.ali 311
			return responseSender.badRequest(crr);
21514 kshitij.so 312
		}
313
 
25547 amit.gupta 314
		if (walletAmount < createRechargeRequest.getRechargeAmount()) {
21514 kshitij.so 315
			crr.setReason("You don't have sufficient wallet balance.");
316
			crr.setResult(false);
22931 ashik.ali 317
			return responseSender.badRequest(crr);
21514 kshitij.so 318
		}
319
 
25100 amit.gupta 320
		User saholicUser = null;
21514 kshitij.so 321
		try {
25100 amit.gupta 322
			saholicUser = userRepository.selectById(retailerId);
21514 kshitij.so 323
		} catch (ProfitMandiBusinessException e) {
24893 amit.gupta 324
			log.error("Unable to get user", e);
21514 kshitij.so 325
			crr.setReason("We are experiencing some problem right now.");
326
			crr.setResult(false);
22931 ashik.ali 327
			return responseSender.internalServerError(e);
21514 kshitij.so 328
		}
329
 
23585 tejbeer 330
		RechargeRequest rechargeRequest = convertRechargeRequest(createRechargeRequest);
25100 amit.gupta 331
		if (!UNBLOCKED_RECHARGE_EMAILS.contains(saholicUser.getEmailId())) {
24893 amit.gupta 332
			throw new ProfitMandiBusinessException("Recharge Provider", createRechargeRequest.getOperatorId(),
333
					"We are experiencing some problem right now.");
24260 amit.gupta 334
		}
26228 amit.gupta 335
		RechargeResponse rechargeResponse = rechargeService.doRecharge(rechargeTransactionMobileCredential,
336
				rechargeTransactionDthCredential, retailerId, rechargeRequest);
24893 amit.gupta 337
 
23585 tejbeer 338
		crr.setResult(rechargeResponse.getStatus().equals(RechargeStatus.SUCCESS));
339
		crr.setRequestId(rechargeResponse.getRequestId());
24893 amit.gupta 340
 
341
		if (RechargeType.valueOf(rechargeRequest.getRechargeType()) == RechargeType.MOBILE) {
342
			return responseSender.ok(crr);
343
		} else {
344
			return responseSender.ok(crr);
21514 kshitij.so 345
		}
23585 tejbeer 346
	}
24893 amit.gupta 347
 
348
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_SUMMARY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
23585 tejbeer 349
	@ApiImplicitParams({
24893 amit.gupta 350
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
351
	public ResponseEntity<?> rechargeSummary(HttpServletRequest request,
352
			@RequestParam(name = "offset", defaultValue = "0") int offset,
353
			@RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
354
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
23585 tejbeer 355
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
24893 amit.gupta 356
		// RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
357
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerId(retailerId,
358
				offset, limit);
23585 tejbeer 359
		for (RechargeTransaction rechargeTransaction : rechargeTransactions) {
24893 amit.gupta 360
			String operatorName = rechargeOperatorRepository.selectById(rechargeTransaction.getOperatorId()).getName();
23585 tejbeer 361
			rechargeTransaction.setOperatorName(operatorName);
21514 kshitij.so 362
		}
24893 amit.gupta 363
 
364
		return responseSender.ok(rechargeTransactions);
365
 
21505 kshitij.so 366
	}
24893 amit.gupta 367
 
368
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_COMMISSIONS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
23585 tejbeer 369
	@ApiImplicitParams({
24893 amit.gupta 370
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
371
	public ResponseEntity<?> rechargeCommissions(HttpServletRequest request,
372
			@RequestParam(name = "offset", defaultValue = "0") int offset,
373
			@RequestParam(name = "limit", defaultValue = "60") int limit) throws ProfitMandiBusinessException {
23585 tejbeer 374
		List<RechargeCommission> rechargeCommissions = rechargeCommissionRepository.selectAllPaginated(offset, limit);
375
		for (RechargeCommission rechargeCommission : rechargeCommissions) {
24893 amit.gupta 376
			String operatorName = rechargeOperatorRepository.selectById(rechargeCommission.getOperatorId()).getName();
377
			RechargeType rechargeType = rechargeOperatorRepository.selectById(rechargeCommission.getOperatorId())
378
					.getRechargeType();
379
			OperatorType operatorType = rechargeOperatorRepository.selectById(rechargeCommission.getOperatorId())
380
					.getOperatorType();
381
			rechargeCommission.setOperatorName(operatorName);
382
			rechargeCommission.setRechargeType(rechargeType);
383
			rechargeCommission.setOperatorType(operatorType);
23585 tejbeer 384
		}
24893 amit.gupta 385
		Map<Integer, RechargeCommission> commissionMap = rechargeCommissions.stream()
386
				.collect(
387
						Collectors
388
								.groupingBy(RechargeCommission::getOperatorId,
389
										Collectors
390
												.collectingAndThen(
391
														Collectors
392
																.reducing((RechargeCommission d1,
393
																		RechargeCommission d2) -> d1.getAmount() > d2
394
																				.getAmount() ? d1 : d2),
395
														Optional::get)));
396
 
23585 tejbeer 397
		return responseSender.ok(commissionMap.values());
398
	}
24893 amit.gupta 399
 
400
	@PostConstruct
23585 tejbeer 401
	private void setRechargeEnquiryCredentials() {
402
		thinkWalnutDigitalRechargeEnquiryCredential = new RechargeCredential();
403
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUrl(thinkWalnutDigitalRechargeEnquiryUrl);
404
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUserName(thinkWalnutDigitalRechargeEnquiryUserName);
405
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargePassword(thinkWalnutDigitalRechargeEnquiryPassword);
406
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
407
	}
24893 amit.gupta 408
 
409
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_RESULT, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
23587 tejbeer 410
	@ApiImplicitParams({
24893 amit.gupta 411
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
23587 tejbeer 412
 
24893 amit.gupta 413
	public ResponseEntity<?> rechargeResult(HttpServletRequest request,
414
			@RequestParam(value = "requestId") String requestId) throws ProfitMandiBusinessException {
415
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
416
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
417
 
26228 amit.gupta 418
		RechargeResponse rechargeResponse = rechargeService.checkStatus(thinkWalnutDigitalRechargeEnquiryCredential,
419
				retailerId, requestId);
23585 tejbeer 420
		return responseSender.ok(rechargeResponse);
21517 kshitij.so 421
	}
21514 kshitij.so 422
 
24893 amit.gupta 423
	@RequestMapping(value = ProfitMandiConstants.URL_POLL_RECHARGE, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21517 kshitij.so 424
	@ApiImplicitParams({
24893 amit.gupta 425
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
426
	public ResponseEntity<?> pollRechargeResult(HttpServletRequest request,
427
			@RequestParam(value = "rechargeOrderId") long rechargeOrderId,
428
			@RequestParam(value = "finalCall") boolean finalCall) {
36620 amit 429
		RechargeResultPojo rrp = new RechargeResultPojo();
24893 amit.gupta 430
		try {
36620 amit 431
			RechargeTransaction rechargeTransaction = rechargeTransactionRepository.selectById((int) rechargeOrderId);
432
			RechargeStatus status = rechargeTransaction.getStatus();
433
			if (status == RechargeStatus.SUCCESS) {
434
				rrp.setIsError(false);
435
				rrp.setRechargeStatus("SUCCESS");
436
				rrp.setDetailDisplayMessage("Congratulations! Your device is successfully recharged.");
437
			} else if (status == RechargeStatus.FAILED) {
438
				rrp.setIsError(false);
439
				rrp.setRechargeStatus("RECHARGE FAILED");
440
				rrp.setDetailDisplayMessage("Your Payment was successful but unfortunately the recharge failed. The amount has been refunded to your wallet.");
441
			} else {
442
				rrp.setIsError(false);
443
				rrp.setRechargeStatus("RECHARGE IN PROCESS");
444
				rrp.setDetailDisplayMessage("Your Payment is successful. Please wait while we check with the operator.");
445
			}
24893 amit.gupta 446
		} catch (Exception e) {
36620 amit 447
			log.error("Unable to poll recharge status for rechargeOrderId: " + rechargeOrderId, e);
21517 kshitij.so 448
			rrp.setRechargeStatus("");
22931 ashik.ali 449
			return responseSender.internalServerError(e);
21517 kshitij.so 450
		}
22931 ashik.ali 451
		return responseSender.ok(rrp);
21517 kshitij.so 452
	}
24893 amit.gupta 453
 
454
	@RequestMapping(value = ProfitMandiConstants.URL_MY_RECHARGES, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21534 kshitij.so 455
	@ApiImplicitParams({
24893 amit.gupta 456
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
457
	public ResponseEntity<?> myRecharges(HttpServletRequest request, @RequestParam(value = "offset") int offset,
458
			@RequestParam(value = "limit") int limit) throws ProfitMandiBusinessException {
36620 amit 459
		return responseSender.ok(new ArrayList<>());
21534 kshitij.so 460
	}
21517 kshitij.so 461
 
21514 kshitij.so 462
	private String remoteAddr(HttpServletRequest request) {
463
		String remoteAddr = "";
464
		String x;
23101 amit.gupta 465
		x = request.getHeader(X_REAL_IP);
466
		log.info("Value of X_REAL_IP is [{}]", x);
21514 kshitij.so 467
		if (x != null && !x.isEmpty()) {
468
			remoteAddr = x;
469
			int idx = remoteAddr.lastIndexOf(',');
470
			if (idx > -1) {
471
				remoteAddr = remoteAddr.substring(idx + 1).trim();
472
			}
473
		} else {
474
			remoteAddr = request.getRemoteAddr();
23100 amit.gupta 475
			log.info("Value of remoteAddr is [{}]", remoteAddr);
21514 kshitij.so 476
		}
477
		return remoteAddr;
478
	}
479
 
24893 amit.gupta 480
	/*
481
	 * private String validateRecharge(RechargeType rechargeType, String number,
482
	 * long operatorId, String ipAddress){ TransactionClient tcl; try { tcl = new
483
	 * TransactionClient(); // String result =
484
	 * tcl.getClient().validateRecharge(rechargeType, number, operatorId,
485
	 * ipAddress); //log.info("validateRecharge Called" + number +
486
	 * " and rechargeType : " + rechargeType + ", IP:" + ipAddress + ", Operator:" +
487
	 * operatorId + ", Result:" + result); //return result; } catch (Exception e) {
488
	 * log.error("Unable to get service provider for Device number " + number +
489
	 * " and rechargeType : " + rechargeType, e); } return
490
	 * "Oops! There seems to be a problem. Please try after some time"; }
491
	 */
21514 kshitij.so 492
 
21464 kshitij.so 493
}