Subversion Repositories SmartDukaan

Rev

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