Subversion Repositories SmartDukaan

Rev

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