Subversion Repositories SmartDukaan

Rev

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