Subversion Repositories SmartDukaan

Rev

Rev 25007 | Rev 25100 | 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
 
25048 amit.gupta 80
	private List<String> UNBLOCKED_RECHARGE_EMAILS = Arrays.asList("pgautam84@gmail.com");
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);
25007 amit.gupta 322
		User user = null;
323
		try {
324
			user = userRepository.selectById(userId);
325
			if (UNBLOCKED_RECHARGE_EMAILS.contains(user.getEmailId())) {
326
				return createRecharge(request, createRechargeRequest);
327
			}
328
		} catch (ProfitMandiBusinessException e) {
329
			log.error("Unable to get user", e);
330
		}
331
 
332
 
24893 amit.gupta 333
		CreateRechargeResponse crr = new CreateRechargeResponse();
334
		String ipAddress = remoteAddr(request);
335
		log.info("IP Address is {}", ipAddress);
336
 
337
		crr.setResult(true);
338
		crr.setRequestId("RECHARGE00050");
339
		return responseSender.ok(crr);
340
 
341
	}
342
 
343
	public ResponseEntity<?> createRecharge(HttpServletRequest request,
344
			@RequestBody CreateRechargeRequest createRechargeRequest) throws ProfitMandiBusinessException {
345
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
346
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
25007 amit.gupta 347
 
24893 amit.gupta 348
		String ipAddress = remoteAddr(request);
349
 
350
		CreateRechargeResponse crr = new CreateRechargeResponse();
351
 
352
		UserAccount userAccount = null;
353
		UserWallet wallet = null;
23585 tejbeer 354
		wallet = userWalletRepository.selectByRetailerId(retailerId);
24893 amit.gupta 355
 
356
		if (createRechargeRequest.getRechargeAmount() <= 0) {
21514 kshitij.so 357
			crr.setReason("Illegal recharge amount");
358
			crr.setResult(false);
22931 ashik.ali 359
			return responseSender.badRequest(crr);
21514 kshitij.so 360
		}
361
 
24893 amit.gupta 362
		if (wallet.getAmount() < createRechargeRequest.getRechargeAmount()) {
21514 kshitij.so 363
			crr.setReason("You don't have sufficient wallet balance.");
364
			crr.setResult(false);
22931 ashik.ali 365
			return responseSender.badRequest(crr);
21514 kshitij.so 366
		}
367
 
368
		User user = null;
369
		try {
370
			user = userRepository.selectById(userId);
371
		} catch (ProfitMandiBusinessException e) {
24893 amit.gupta 372
			log.error("Unable to get user", e);
21514 kshitij.so 373
			crr.setReason("We are experiencing some problem right now.");
374
			crr.setResult(false);
22931 ashik.ali 375
			return responseSender.internalServerError(e);
21514 kshitij.so 376
		}
377
 
23585 tejbeer 378
		RechargeRequest rechargeRequest = convertRechargeRequest(createRechargeRequest);
25007 amit.gupta 379
		if (!UNBLOCKED_RECHARGE_EMAILS.contains(user.getEmailId())) {
24893 amit.gupta 380
			throw new ProfitMandiBusinessException("Recharge Provider", createRechargeRequest.getOperatorId(),
381
					"We are experiencing some problem right now.");
24260 amit.gupta 382
		}
24893 amit.gupta 383
		RechargeResponse rechargeResponse = rechargeService.doRecharge(oxigenRechargeTransactionUrl,
384
				oxigenRechargeAuthKey, oxigenRechargeValidationUrl, oxigenRechargeValidationAuthKey,
385
				rechargeTransactionMobileCredential, rechargeTransactionDthCredential, retailerId, rechargeRequest);
386
 
23585 tejbeer 387
		crr.setResult(rechargeResponse.getStatus().equals(RechargeStatus.SUCCESS));
388
		crr.setRequestId(rechargeResponse.getRequestId());
24893 amit.gupta 389
 
390
		if (RechargeType.valueOf(rechargeRequest.getRechargeType()) == RechargeType.MOBILE) {
391
			return responseSender.ok(crr);
392
		} else {
393
			return responseSender.ok(crr);
21514 kshitij.so 394
		}
23585 tejbeer 395
	}
24893 amit.gupta 396
 
397
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_SUMMARY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
23585 tejbeer 398
	@ApiImplicitParams({
24893 amit.gupta 399
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
400
	public ResponseEntity<?> rechargeSummary(HttpServletRequest request,
401
			@RequestParam(name = "offset", defaultValue = "0") int offset,
402
			@RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
403
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
23585 tejbeer 404
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
24893 amit.gupta 405
		// RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
406
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerId(retailerId,
407
				offset, limit);
23585 tejbeer 408
		for (RechargeTransaction rechargeTransaction : rechargeTransactions) {
24893 amit.gupta 409
			String operatorName = rechargeOperatorRepository.selectById(rechargeTransaction.getOperatorId()).getName();
23585 tejbeer 410
			rechargeTransaction.setOperatorName(operatorName);
21514 kshitij.so 411
		}
24893 amit.gupta 412
 
413
		return responseSender.ok(rechargeTransactions);
414
 
21505 kshitij.so 415
	}
24893 amit.gupta 416
 
417
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_COMMISSIONS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
23585 tejbeer 418
	@ApiImplicitParams({
24893 amit.gupta 419
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
420
	public ResponseEntity<?> rechargeCommissions(HttpServletRequest request,
421
			@RequestParam(name = "offset", defaultValue = "0") int offset,
422
			@RequestParam(name = "limit", defaultValue = "60") int limit) throws ProfitMandiBusinessException {
23585 tejbeer 423
		List<RechargeCommission> rechargeCommissions = rechargeCommissionRepository.selectAllPaginated(offset, limit);
424
		for (RechargeCommission rechargeCommission : rechargeCommissions) {
24893 amit.gupta 425
			String operatorName = rechargeOperatorRepository.selectById(rechargeCommission.getOperatorId()).getName();
426
			RechargeType rechargeType = rechargeOperatorRepository.selectById(rechargeCommission.getOperatorId())
427
					.getRechargeType();
428
			OperatorType operatorType = rechargeOperatorRepository.selectById(rechargeCommission.getOperatorId())
429
					.getOperatorType();
430
			rechargeCommission.setOperatorName(operatorName);
431
			rechargeCommission.setRechargeType(rechargeType);
432
			rechargeCommission.setOperatorType(operatorType);
23585 tejbeer 433
		}
24893 amit.gupta 434
		Map<Integer, RechargeCommission> commissionMap = rechargeCommissions.stream()
435
				.collect(
436
						Collectors
437
								.groupingBy(RechargeCommission::getOperatorId,
438
										Collectors
439
												.collectingAndThen(
440
														Collectors
441
																.reducing((RechargeCommission d1,
442
																		RechargeCommission d2) -> d1.getAmount() > d2
443
																				.getAmount() ? d1 : d2),
444
														Optional::get)));
445
 
23585 tejbeer 446
		return responseSender.ok(commissionMap.values());
447
	}
24893 amit.gupta 448
 
449
	@PostConstruct
23585 tejbeer 450
	private void setRechargeEnquiryCredentials() {
451
		thinkWalnutDigitalRechargeEnquiryCredential = new RechargeCredential();
452
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUrl(thinkWalnutDigitalRechargeEnquiryUrl);
453
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUserName(thinkWalnutDigitalRechargeEnquiryUserName);
454
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargePassword(thinkWalnutDigitalRechargeEnquiryPassword);
455
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
456
	}
24893 amit.gupta 457
 
458
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_RESULT, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
23587 tejbeer 459
	@ApiImplicitParams({
24893 amit.gupta 460
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
23587 tejbeer 461
 
24893 amit.gupta 462
	public ResponseEntity<?> rechargeResult(HttpServletRequest request,
463
			@RequestParam(value = "requestId") String requestId) throws ProfitMandiBusinessException {
21517 kshitij.so 464
		RechargeOrder rechargeOrder = null;
465
		RechargeResultPojo rrp = null;
24893 amit.gupta 466
 
467
		// RechargeResponse rr = new RechargeResponse();
468
		// String requestId = rr.getRequestId();
469
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
470
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
471
 
472
		RechargeResponse rechargeResponse = rechargeService.checkStatus(oxigenRechargeEnquiryUrl, oxigenRechargeAuthKey,
473
				thinkWalnutDigitalRechargeEnquiryCredential, retailerId, requestId);
23585 tejbeer 474
		return responseSender.ok(rechargeResponse);
21517 kshitij.so 475
	}
21514 kshitij.so 476
 
24893 amit.gupta 477
	@RequestMapping(value = ProfitMandiConstants.URL_POLL_RECHARGE, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21517 kshitij.so 478
	@ApiImplicitParams({
24893 amit.gupta 479
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
480
	public ResponseEntity<?> pollRechargeResult(HttpServletRequest request,
481
			@RequestParam(value = "rechargeOrderId") long rechargeOrderId,
482
			@RequestParam(value = "finalCall") boolean finalCall) {
21517 kshitij.so 483
		TransactionClient transactionServiceClient = null;
484
		RechargeOrder t_rechargeOrder = null;
485
		RechargeResultPojo rrp = null;
24893 amit.gupta 486
		try {
21517 kshitij.so 487
			transactionServiceClient = new TransactionClient();
488
			t_rechargeOrder = transactionServiceClient.getClient().getRcgOrderStatus(rechargeOrderId, finalCall);
24893 amit.gupta 489
		} catch (Exception e) {
21517 kshitij.so 490
			rrp = new RechargeResultPojo();
491
			rrp.setRechargeStatus("");
22931 ashik.ali 492
			return responseSender.internalServerError(e);
21517 kshitij.so 493
		}
24893 amit.gupta 494
		if (t_rechargeOrder == null) {
21517 kshitij.so 495
			rrp = new RechargeResultPojo();
496
			rrp.setRechargeStatus("");
22931 ashik.ali 497
			return responseSender.badRequest(new RechargeResultPojo());
21517 kshitij.so 498
		}
499
		rrp = new RechargeResultPojo();
22411 amit.gupta 500
		String[] os = Utils.getOrderStatus(t_rechargeOrder.getStatus());
501
		rrp.setIsError(Boolean.parseBoolean(os[0]));
502
		rrp.setRechargeStatus(os[1]);
503
		rrp.setDetailDisplayMessage(os[2]);
22931 ashik.ali 504
		return responseSender.ok(rrp);
21517 kshitij.so 505
	}
24893 amit.gupta 506
 
507
	@RequestMapping(value = ProfitMandiConstants.URL_MY_RECHARGES, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21534 kshitij.so 508
	@ApiImplicitParams({
24893 amit.gupta 509
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
510
	public ResponseEntity<?> myRecharges(HttpServletRequest request, @RequestParam(value = "offset") int offset,
511
			@RequestParam(value = "limit") int limit) throws ProfitMandiBusinessException {
512
		TransactionClient tc = null;
513
		int userId = (int) request.getAttribute("userId");
23273 ashik.ali 514
		UserAccount userAccount;
21534 kshitij.so 515
		List<RechargeOrder> rechargeOrders = null;
24893 amit.gupta 516
 
23273 ashik.ali 517
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
24893 amit.gupta 518
 
21534 kshitij.so 519
		try {
520
			tc = new TransactionClient();
24893 amit.gupta 521
			rechargeOrders = tc.getClient().getPaginatedRechargeOrders(Long.valueOf(userAccount.getAccountKey()),
522
					offset, limit);
21534 kshitij.so 523
		} catch (Exception e) {
24893 amit.gupta 524
			log.error("Unable to get recharge order list", e);
22931 ashik.ali 525
			return responseSender.internalServerError(e);
21534 kshitij.so 526
		}
527
		List<MyRechargesResponse> myRechargesList = new ArrayList<MyRechargesResponse>();
24893 amit.gupta 528
		for (RechargeOrder rechargeOrder : rechargeOrders) {
21534 kshitij.so 529
			MyRechargesResponse rp = new MyRechargesResponse();
24893 amit.gupta 530
			/*
531
			 * if(rechargeOrder.getRechargeType() == RechargeType.MOBILE){
532
			 * rp.setOperator(Utils.getProvider(rechargeOrder.getOperatorId()));
533
			 * rp.setOperatorType("MOBILE"); } if(rechargeOrder.getRechargeType() ==
534
			 * RechargeType.DTH){
535
			 * rp.setOperator(Utils.getProvider(rechargeOrder.getOperatorId()));
536
			 * rp.setOperatorType("DTH"); }
537
			 */
538
			rp.setOrderId(rechargeOrder.getId());
539
			rp.setOperatorId(rechargeOrder.getOperatorId());
540
			rp.setDate(rechargeOrder.getCreationTimestamp());
541
			rp.setNumber(rechargeOrder.getDeviceNumber());
542
			rp.setAmount(rechargeOrder.getTotalAmount());
543
			rp.setStatus(Utils.getRechargeDisplayStatus(rechargeOrder.getStatus()));
544
			rp.setDisplayOrderId(rechargeOrder.getDisplayId());
545
			myRechargesList.add(rp);
21534 kshitij.so 546
		}
22931 ashik.ali 547
		return responseSender.ok(myRechargesList);
21534 kshitij.so 548
	}
21517 kshitij.so 549
 
21514 kshitij.so 550
	private String remoteAddr(HttpServletRequest request) {
551
		String remoteAddr = "";
552
		String x;
23101 amit.gupta 553
		x = request.getHeader(X_REAL_IP);
554
		log.info("Value of X_REAL_IP is [{}]", x);
21514 kshitij.so 555
		if (x != null && !x.isEmpty()) {
556
			remoteAddr = x;
557
			int idx = remoteAddr.lastIndexOf(',');
558
			if (idx > -1) {
559
				remoteAddr = remoteAddr.substring(idx + 1).trim();
560
			}
561
		} else {
562
			remoteAddr = request.getRemoteAddr();
23100 amit.gupta 563
			log.info("Value of remoteAddr is [{}]", remoteAddr);
21514 kshitij.so 564
		}
565
		return remoteAddr;
566
	}
567
 
24893 amit.gupta 568
	/*
569
	 * private String validateRecharge(RechargeType rechargeType, String number,
570
	 * long operatorId, String ipAddress){ TransactionClient tcl; try { tcl = new
571
	 * TransactionClient(); // String result =
572
	 * tcl.getClient().validateRecharge(rechargeType, number, operatorId,
573
	 * ipAddress); //log.info("validateRecharge Called" + number +
574
	 * " and rechargeType : " + rechargeType + ", IP:" + ipAddress + ", Operator:" +
575
	 * operatorId + ", Result:" + result); //return result; } catch (Exception e) {
576
	 * log.error("Unable to get service provider for Device number " + number +
577
	 * " and rechargeType : " + rechargeType, e); } return
578
	 * "Oops! There seems to be a problem. Please try after some time"; }
579
	 */
21514 kshitij.so 580
 
21464 kshitij.so 581
}