Subversion Repositories SmartDukaan

Rev

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