Subversion Repositories SmartDukaan

Rev

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