Subversion Repositories SmartDukaan

Rev

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