Subversion Repositories SmartDukaan

Rev

Rev 23273 | Rev 23585 | 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;
4
import java.util.List;
5
import java.util.Map;
21464 kshitij.so 6
 
7
import javax.servlet.http.HttpServletRequest;
21505 kshitij.so 8
import org.apache.thrift.TException;
9
import org.apache.thrift.transport.TTransportException;
23568 govind 10
import org.apache.logging.log4j.Logger;
11
import org.apache.logging.log4j.LogManager;
21505 kshitij.so 12
import org.springframework.beans.factory.annotation.Autowired;
21464 kshitij.so 13
import org.springframework.http.MediaType;
14
import org.springframework.http.ResponseEntity;
15
import org.springframework.stereotype.Controller;
21702 ashik.ali 16
import org.springframework.transaction.annotation.Transactional;
21514 kshitij.so 17
import org.springframework.web.bind.annotation.RequestBody;
21464 kshitij.so 18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
20
import org.springframework.web.bind.annotation.RequestParam;
21
 
21505 kshitij.so 22
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21464 kshitij.so 23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21468 kshitij.so 24
import com.spice.profitmandi.common.util.Utils;
22931 ashik.ali 25
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 26
import com.spice.profitmandi.dao.entity.dtr.User;
23273 ashik.ali 27
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
21735 ashik.ali 28
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
29
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
30
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
23115 ashik.ali 31
import com.spice.profitmandi.dao.repository.transaction.PendingRechargeCommissionRepository;
21735 ashik.ali 32
import com.spice.profitmandi.thrift.clients.PaymentClient;
21464 kshitij.so 33
import com.spice.profitmandi.thrift.clients.TransactionClient;
21514 kshitij.so 34
import com.spice.profitmandi.web.req.CreateRechargeRequest;
21505 kshitij.so 35
import com.spice.profitmandi.web.res.ConfirmRechargeResponse;
21514 kshitij.so 36
import com.spice.profitmandi.web.res.CreateRechargeResponse;
21534 kshitij.so 37
import com.spice.profitmandi.web.res.MyRechargesResponse;
21517 kshitij.so 38
import com.spice.profitmandi.web.res.RechargeResultPojo;
21468 kshitij.so 39
import in.shop2020.model.v1.order.DenominationType;
21464 kshitij.so 40
import in.shop2020.model.v1.order.DeviceNumberInfo;
21514 kshitij.so 41
import in.shop2020.model.v1.order.OrderType;
21468 kshitij.so 42
import in.shop2020.model.v1.order.RechargeDenomination;
21514 kshitij.so 43
import in.shop2020.model.v1.order.RechargeOrder;
44
import in.shop2020.model.v1.order.RechargeOrderStatus;
21464 kshitij.so 45
import in.shop2020.model.v1.order.RechargeType;
21514 kshitij.so 46
import in.shop2020.model.v1.order.TransactionServiceException;
21505 kshitij.so 47
import in.shop2020.model.v1.order.UserWallet;
21514 kshitij.so 48
import in.shop2020.payments.Payment;
49
import in.shop2020.payments.PaymentException;
50
import in.shop2020.payments.PaymentStatus;
21464 kshitij.so 51
import io.swagger.annotations.ApiImplicitParam;
52
import io.swagger.annotations.ApiImplicitParams;
53
import io.swagger.annotations.ApiOperation;
54
 
55
@Controller
22040 amit.gupta 56
@Transactional(rollbackFor=Throwable.class)
21464 kshitij.so 57
public class RechargeController {
58
 
23568 govind 59
	private static final Logger log=LogManager.getLogger(RechargeController.class);
21514 kshitij.so 60
	private static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR";
23101 amit.gupta 61
	private static final String X_REAL_IP = "X-Real-IP";
21464 kshitij.so 62
 
21505 kshitij.so 63
	@Autowired
22931 ashik.ali 64
	private UserAccountRepository userAccountRepository;
21514 kshitij.so 65
 
66
	@Autowired
22931 ashik.ali 67
	private UserRepository userRepository;
68
 
69
	@Autowired
23115 ashik.ali 70
	private PendingRechargeCommissionRepository pendingRechargeCommissionRepository;
71
 
72
	@Autowired
22931 ashik.ali 73
	private ResponseSender<?> responseSender;
21514 kshitij.so 74
 
21464 kshitij.so 75
	@RequestMapping(value = ProfitMandiConstants.URL_GET_SERVICE_PROVIDER, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
76
	@ApiImplicitParams({
77
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
78
				required = true, dataType = "string", paramType = "header")
79
	})
80
	@ApiOperation(value = "")
81
	public ResponseEntity<?> getServiceProvider(HttpServletRequest request, @RequestParam(value="deviceNumber") String deviceNumber, @RequestParam(value="rechargeType") String rechargeType){
82
		DeviceNumberInfo deviceInfo = null;
83
		TransactionClient tcl;
84
		try {
85
			tcl = new TransactionClient();
86
			deviceInfo = tcl.getClient().getServiceProviderForDevice(RechargeType.valueOf(rechargeType), deviceNumber.substring(0,4));
87
		} catch (Exception e) {
88
			log.error("Unable to get service provider for Device number " + deviceNumber + " and rechargeType : " +  rechargeType, e);
89
		}
22931 ashik.ali 90
		return responseSender.ok(deviceInfo);
21464 kshitij.so 91
	}
21505 kshitij.so 92
 
21468 kshitij.so 93
	@RequestMapping(value = ProfitMandiConstants.URL_GET_ALL_DENOMINATIONS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
94
	@ApiImplicitParams({
95
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
96
				required = true, dataType = "string", paramType = "header")
97
	})
98
	@ApiOperation(value = "")
99
	public ResponseEntity<?> getAllDenominations(HttpServletRequest request, @RequestParam(value="operatorId") long operatorId, @RequestParam(value="circleCode") String circleCode, @RequestParam(value="denominationType") String denominationType){
100
		List<RechargeDenomination> rechargeDenominations = new ArrayList<RechargeDenomination>();
101
		TransactionClient tcl;
102
		try {
103
			tcl = new TransactionClient();
104
			rechargeDenominations =  tcl.getClient().getRechargeDenominations(operatorId, circleCode, DenominationType.valueOf(denominationType));
105
		} catch (Exception e) {
106
			log.error("Unable to get rechargeDenominations for operatorId " + operatorId + " and circleCode : " +  circleCode + " and DenominationType : " + denominationType, e);
107
		}
22931 ashik.ali 108
		return responseSender.ok(rechargeDenominations);
21468 kshitij.so 109
	}
21505 kshitij.so 110
 
21468 kshitij.so 111
	@RequestMapping(value = ProfitMandiConstants.URL_MOBILE_OPERATORS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
112
	@ApiImplicitParams({
113
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
114
				required = true, dataType = "string", paramType = "header")
115
	})
116
	@ApiOperation(value = "")
117
	public ResponseEntity<?> getAllMobileOperators(HttpServletRequest request){
118
		Map<Long, String> mobileProviderMap = Utils.getMobileProvidersMap();
22931 ashik.ali 119
		return responseSender.ok(mobileProviderMap);
21468 kshitij.so 120
	}
21505 kshitij.so 121
 
21468 kshitij.so 122
	@RequestMapping(value = ProfitMandiConstants.URL_DTH_OPERATORS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
123
	@ApiImplicitParams({
124
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
125
				required = true, dataType = "string", paramType = "header")
126
	})
127
	@ApiOperation(value = "")
128
	public ResponseEntity<?> getAllDTHOperators(HttpServletRequest request){
129
		Map<Long, String> dthProviderMap = Utils.getDthProvidersMap();
22931 ashik.ali 130
		return responseSender.ok(dthProviderMap);
21468 kshitij.so 131
	}
132
 
21505 kshitij.so 133
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_CONFIRM, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
134
	@ApiImplicitParams({
135
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
136
				required = true, dataType = "string", paramType = "header")
137
	})
138
	@ApiOperation(value = "")
22931 ashik.ali 139
	public ResponseEntity<?> confirmRecharge(HttpServletRequest request, @RequestParam(value="rechargeAmount") long rechargeAmount)
140
		throws ProfitMandiBusinessException{
23115 ashik.ali 141
		int userId = (int)request.getAttribute(ProfitMandiConstants.USER_ID);
23273 ashik.ali 142
		UserAccount userAccount = null;
21505 kshitij.so 143
		UserWallet wallet = null;
144
		if (rechargeAmount <=0){
22931 ashik.ali 145
			return responseSender.badRequest(null);
21505 kshitij.so 146
		}
21514 kshitij.so 147
 
23273 ashik.ali 148
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
22931 ashik.ali 149
 
21505 kshitij.so 150
		try {
151
			TransactionClient tc = new TransactionClient();
23273 ashik.ali 152
			wallet = tc.getClient().getUserWallet(Long.valueOf(userAccount.getAccountKey()));
21505 kshitij.so 153
		} catch (NumberFormatException | TException e) {
22931 ashik.ali 154
			return responseSender.internalServerError(e);		
21505 kshitij.so 155
		}
21514 kshitij.so 156
 
21505 kshitij.so 157
		ConfirmRechargeResponse crr = new ConfirmRechargeResponse();
158
		crr.setWalletAmount(wallet.getAmount());
159
		crr.setWalletAmountLeft(wallet.getAmount() - rechargeAmount);
160
		crr.setCanProceed(true);
161
		if (crr.getWalletAmountLeft() < 0){
162
			crr.setCanProceed(false);
163
			crr.setReason("You don't have sufficient wallet balance");
164
		}
22931 ashik.ali 165
		return responseSender.ok(crr);
21505 kshitij.so 166
	}
23115 ashik.ali 167
 
168
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_PENDING_COMMISSION, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
169
	@ApiImplicitParams({
170
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
171
				required = true, dataType = "string", paramType = "header")
172
	})
173
	@ApiOperation(value = "")
174
	public ResponseEntity<?> getPendingRechargeCommission(HttpServletRequest request)
175
		throws ProfitMandiBusinessException{
176
		int userId = (int)request.getAttribute(ProfitMandiConstants.USER_ID);
177
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
178
		return responseSender.ok(pendingRechargeCommissionRepository.selectByRetailerId(retailerId));
179
	}
21514 kshitij.so 180
 
21505 kshitij.so 181
	@RequestMapping(value = ProfitMandiConstants.URL_CREATE_RECHARGE , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
182
	@ApiImplicitParams({
183
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
184
				required = true, dataType = "string", paramType = "header")
185
	})
22931 ashik.ali 186
	public ResponseEntity<?> createRecharge(HttpServletRequest request, @RequestBody CreateRechargeRequest createRechargeRequest)
187
		throws ProfitMandiBusinessException{
21514 kshitij.so 188
		String ipAddress  = remoteAddr(request);
189
		String errorMessage = validateRecharge(RechargeType.valueOf(createRechargeRequest.getRechargeType()), createRechargeRequest.getNumber(), createRechargeRequest.getOperatorId(), ipAddress);
190
		CreateRechargeResponse crr =  new CreateRechargeResponse();
191
		if(!errorMessage.equals("SUCCESS")){
192
			crr.setReason(errorMessage);
193
			crr.setResult(false);
22931 ashik.ali 194
			return responseSender.badRequest(crr);
21514 kshitij.so 195
		}
196
		int userId = (int)request.getAttribute("userId");
23273 ashik.ali 197
		UserAccount userAccount = null;
21514 kshitij.so 198
		UserWallet wallet = null;
199
		if (createRechargeRequest.getRechargeAmount() <=0){
200
			crr.setReason("Illegal recharge amount");
201
			crr.setResult(false);
22931 ashik.ali 202
			return responseSender.badRequest(crr);
21514 kshitij.so 203
		}
204
 
23273 ashik.ali 205
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
22931 ashik.ali 206
 
21514 kshitij.so 207
		try {
208
			TransactionClient tc = new TransactionClient();
23273 ashik.ali 209
			wallet = tc.getClient().getUserWallet(Long.valueOf(userAccount.getAccountKey()));
21514 kshitij.so 210
		} catch (NumberFormatException | TException e) {
211
			log.error("Unable to get user wallet ",e);
212
			crr.setReason("We are experiencing some problem right now.");
213
			crr.setResult(false);
22931 ashik.ali 214
			return responseSender.internalServerError(e);
21514 kshitij.so 215
		}
216
 
217
		if (wallet.getAmount() < createRechargeRequest.getRechargeAmount()){
218
			crr.setReason("You don't have sufficient wallet balance.");
219
			crr.setResult(false);
22931 ashik.ali 220
			return responseSender.badRequest(crr);
21514 kshitij.so 221
		}
222
 
223
		User user = null;
224
		try {
225
			user = userRepository.selectById(userId);
226
		} catch (ProfitMandiBusinessException e) {
227
			log.error("Unable to get user",e);
228
			crr.setReason("We are experiencing some problem right now.");
229
			crr.setResult(false);
22931 ashik.ali 230
			return responseSender.internalServerError(e);
21514 kshitij.so 231
		}
232
 
233
 
234
		RechargeOrder t_rechargeOrder = new RechargeOrder();
235
		t_rechargeOrder.setTotalAmount(createRechargeRequest.getRechargeAmount());
236
		t_rechargeOrder.setUserEmailId(user.getEmailId());
23273 ashik.ali 237
		t_rechargeOrder.setUserId(Long.valueOf(userAccount.getAccountKey()));
21514 kshitij.so 238
		t_rechargeOrder.setDeviceNumber(createRechargeRequest.getNumber());
239
		t_rechargeOrder.setPlan(createRechargeRequest.getPlan()==null?"":createRechargeRequest.getPlan());
240
		t_rechargeOrder.setOperatorId(createRechargeRequest.getOperatorId());
241
		t_rechargeOrder.setRechargeType(RechargeType.valueOf(createRechargeRequest.getRechargeType()));
242
		t_rechargeOrder.setStatus(RechargeOrderStatus.PAYMENT_PENDING);
243
		t_rechargeOrder.setOrderType(OrderType.B2C);
244
		t_rechargeOrder.setWalletAmount(createRechargeRequest.getRechargeAmount());
245
		t_rechargeOrder.setCouponAmount(0);
246
		t_rechargeOrder.setCouponCode("");
247
		t_rechargeOrder.setIpAddress(ipAddress);
248
		TransactionClient tc = null;
249
		RechargeOrder rechargeOrder;
250
		try {
251
			tc = new TransactionClient();
252
			rechargeOrder = tc.getClient().createRechargeOrder(t_rechargeOrder);
253
		} catch (TransactionServiceException | TException e) {
254
			log.error("Unable to create recharge order",e);
255
			crr.setReason("We are experiencing some problem right now.");
256
			crr.setResult(false);
22931 ashik.ali 257
			return responseSender.internalServerError(e);
21514 kshitij.so 258
		}
259
 
260
		log.info("Recharge Order:" + rechargeOrder);
261
		PaymentClient paymentServiceClient = null;
262
		try {
263
			paymentServiceClient = new PaymentClient();
264
		} catch (TTransportException e) {
265
			log.error("Unable to create payment client",e);
266
			crr.setReason("We are experiencing some problem right now.");
267
			crr.setResult(false);
22931 ashik.ali 268
			return responseSender.internalServerError(e);
21514 kshitij.so 269
		}
270
		try {
271
			RechargeOrder d_rechargeOrder = tc.getClient().getRechargeOrder(rechargeOrder.getId());
272
			List<Payment> payments = paymentServiceClient.getClient().getPaymentForRechargeTxnId(d_rechargeOrder.getTransactionId());
273
			if(payments.size() > 0) {
274
				throw new PaymentException(d_rechargeOrder.getId(), "Payment already exists for recharge");
275
			}
276
			Long merchantPaymentId = 0l;
277
			if (d_rechargeOrder.getWalletAmount() +  d_rechargeOrder.getCouponAmount() != d_rechargeOrder.getTotalAmount()) {
278
				log.error("Wallet amount : " + d_rechargeOrder.getWalletAmount() + ", coupon amount : " + d_rechargeOrder.getCouponAmount() + " and total amount : " + d_rechargeOrder.getTotalAmount());
279
				merchantPaymentId = paymentServiceClient.getClient().createPayment(d_rechargeOrder.getUserId(), d_rechargeOrder.getTotalAmount(), 8, d_rechargeOrder.getTransactionId(), true);
280
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, "","", "0", "", "", "", "", "", PaymentStatus.FAILED, "", null);
281
				tc.getClient().updateRechargeOrderStatus(d_rechargeOrder.getId(),  RechargeOrderStatus.PAYMENT_FAILED);
282
			} else {
283
				merchantPaymentId = paymentServiceClient.getClient().createPayment(d_rechargeOrder.getUserId(), d_rechargeOrder.getTotalAmount(), 8, d_rechargeOrder.getTransactionId(), true);
284
				//Update payment status as authorized
285
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, "","", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
286
				tc.getClient().updateRechargeOrderStatus(d_rechargeOrder.getId(),  RechargeOrderStatus.PAYMENT_SUCCESSFUL);
287
			}
288
			crr.setRechargeOrderId(d_rechargeOrder.getId());
289
			crr.setResult(true);
290
		} catch (Exception e) {
291
			log.error("Unable to mark the payment as authorized", e);
292
			crr.setReason("We are experiencing some problem right now.");
293
			crr.setResult(false);
22931 ashik.ali 294
			return responseSender.internalServerError(e);
21514 kshitij.so 295
		}
23037 ashik.ali 296
		return responseSender.ok(crr);
21505 kshitij.so 297
	}
21514 kshitij.so 298
 
21517 kshitij.so 299
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_RESULT , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
300
	@ApiImplicitParams({
301
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
302
				required = true, dataType = "string", paramType = "header")
303
	})
304
	public ResponseEntity<?> rechargeResult(HttpServletRequest request, @RequestParam(value="rechargeOrderId") long rechargeOrderId){
305
		TransactionClient tc=null;
306
		RechargeOrder rechargeOrder = null;
307
		RechargeResultPojo rrp = null;
308
		try {
309
			tc = new TransactionClient();
310
			rechargeOrder = tc.getClient().getRechargeOrder(rechargeOrderId);
311
		} catch (TransactionServiceException | TException e) {
312
			// return with internal server error
313
			e.printStackTrace();
314
			rrp = new RechargeResultPojo();
315
			rrp.setIsError(true);
316
			rrp.setDetailDisplayMessage("We are experiencing some problem right now.");
22931 ashik.ali 317
			return responseSender.internalServerError(e);
21517 kshitij.so 318
		}
319
		if (rechargeOrder == null){
320
			rrp = new RechargeResultPojo();
321
			rrp.setIsError(true);
322
			rrp.setDetailDisplayMessage("Recharge order doesnot exist in our system.");
22931 ashik.ali 323
			return responseSender.badRequest(rrp);
21517 kshitij.so 324
		}
325
		String[] os = Utils.getOrderStatus(rechargeOrder.getStatus());
326
		rrp = new RechargeResultPojo();
327
		rrp.setRechargeDeviceNumber(rechargeOrder.getDeviceNumber());
328
		rrp.setRechargeDisplayId(rechargeOrder.getDisplayId());
329
		rrp.setTotalAmount(rechargeOrder.getTotalAmount() + "");
330
		rrp.setRechargeProvider(Utils.getProvider(rechargeOrder.getOperatorId()));
331
		rrp.setIsError(Boolean.parseBoolean(os[0]));
332
		rrp.setRechargeStatus(os[1]);
333
		rrp.setDetailDisplayMessage(os[2]);
21523 kshitij.so 334
		if (rechargeOrder.getStatus().equals(RechargeOrderStatus.RECHARGE_UNKNOWN)){
21517 kshitij.so 335
			rrp.setPoll(true);
336
		}
23098 amit.gupta 337
		return responseSender.ok(rrp);
21517 kshitij.so 338
	}
21514 kshitij.so 339
 
21517 kshitij.so 340
	@RequestMapping(value = ProfitMandiConstants.URL_POLL_RECHARGE , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
341
	@ApiImplicitParams({
342
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
343
				required = true, dataType = "string", paramType = "header")
344
	})
21519 kshitij.so 345
	public ResponseEntity<?> pollRechargeResult(HttpServletRequest request, @RequestParam(value="rechargeOrderId") long rechargeOrderId, @RequestParam(value="finalCall") boolean finalCall){
21517 kshitij.so 346
		TransactionClient transactionServiceClient = null;
347
		RechargeOrder t_rechargeOrder = null;
348
		RechargeResultPojo rrp = null;
349
		try{
350
			transactionServiceClient = new TransactionClient();
351
			t_rechargeOrder = transactionServiceClient.getClient().getRcgOrderStatus(rechargeOrderId, finalCall);
352
		}
353
		catch(Exception e){
354
			rrp = new RechargeResultPojo();
355
			rrp.setRechargeStatus("");
22931 ashik.ali 356
			return responseSender.internalServerError(e);
21517 kshitij.so 357
		}
358
		if (t_rechargeOrder == null){
359
			rrp = new RechargeResultPojo();
360
			rrp.setRechargeStatus("");
22931 ashik.ali 361
			return responseSender.badRequest(new RechargeResultPojo());
21517 kshitij.so 362
		}
363
		rrp = new RechargeResultPojo();
22411 amit.gupta 364
		String[] os = Utils.getOrderStatus(t_rechargeOrder.getStatus());
365
		rrp.setIsError(Boolean.parseBoolean(os[0]));
366
		rrp.setRechargeStatus(os[1]);
367
		rrp.setDetailDisplayMessage(os[2]);
22931 ashik.ali 368
		return responseSender.ok(rrp);
21517 kshitij.so 369
	}
21534 kshitij.so 370
 
371
	@RequestMapping(value = ProfitMandiConstants.URL_MY_RECHARGES , method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
372
	@ApiImplicitParams({
373
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
374
				required = true, dataType = "string", paramType = "header")
375
	})
22931 ashik.ali 376
	public ResponseEntity<?> myRecharges(HttpServletRequest request, @RequestParam(value="offset") int offset, @RequestParam(value="limit") int limit) throws ProfitMandiBusinessException{
21534 kshitij.so 377
		TransactionClient tc=null;
378
		int userId = (int)request.getAttribute("userId");
23273 ashik.ali 379
		UserAccount userAccount;
21534 kshitij.so 380
		List<RechargeOrder> rechargeOrders = null;
22931 ashik.ali 381
 
23273 ashik.ali 382
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
22931 ashik.ali 383
 
21534 kshitij.so 384
		try {
385
			tc = new TransactionClient();
23273 ashik.ali 386
			rechargeOrders = tc.getClient().getPaginatedRechargeOrders(Long.valueOf(userAccount.getAccountKey()), offset, limit);
21534 kshitij.so 387
		} catch (Exception e) {
388
			log.error("Unable to get recharge order list",e);
22931 ashik.ali 389
			return responseSender.internalServerError(e);
21534 kshitij.so 390
		}
391
		List<MyRechargesResponse> myRechargesList = new ArrayList<MyRechargesResponse>();
392
		for (RechargeOrder rechargeOrder : rechargeOrders){
393
			MyRechargesResponse rp = new MyRechargesResponse();
394
			if(rechargeOrder.getRechargeType() == RechargeType.MOBILE){
395
        		rp.setOperator(Utils.getProvider(rechargeOrder.getOperatorId()));
396
        		rp.setOperatorType("MOBILE");
397
        	}
398
        	if(rechargeOrder.getRechargeType() == RechargeType.DTH){
399
        		rp.setOperator(Utils.getProvider(rechargeOrder.getOperatorId()));
400
        		rp.setOperatorType("DTH");
401
        	}
402
        	rp.setOrderId(rechargeOrder.getId());
403
        	rp.setOperatorId(rechargeOrder.getOperatorId());
404
        	rp.setDate(rechargeOrder.getCreationTimestamp());
405
        	rp.setNumber(rechargeOrder.getDeviceNumber());
406
        	rp.setAmount(rechargeOrder.getTotalAmount());
407
        	rp.setStatus(Utils.getRechargeDisplayStatus(rechargeOrder.getStatus()));
408
        	rp.setDisplayOrderId(rechargeOrder.getDisplayId());
409
        	myRechargesList.add(rp);
410
		}
22931 ashik.ali 411
		return responseSender.ok(myRechargesList);
21534 kshitij.so 412
	}
21517 kshitij.so 413
 
21534 kshitij.so 414
 
21514 kshitij.so 415
	private String remoteAddr(HttpServletRequest request) {
416
		String remoteAddr = "";
417
		String x;
23101 amit.gupta 418
		x = request.getHeader(X_REAL_IP);
419
		log.info("Value of X_REAL_IP is [{}]", x);
21514 kshitij.so 420
		if (x != null && !x.isEmpty()) {
421
			remoteAddr = x;
422
			int idx = remoteAddr.lastIndexOf(',');
423
			if (idx > -1) {
424
				remoteAddr = remoteAddr.substring(idx + 1).trim();
425
			}
426
		} else {
427
			remoteAddr = request.getRemoteAddr();
23100 amit.gupta 428
			log.info("Value of remoteAddr is [{}]", remoteAddr);
21514 kshitij.so 429
		}
430
		return remoteAddr;
431
	}
432
 
433
	private String validateRecharge(RechargeType rechargeType, String number, long operatorId, String ipAddress){
434
		TransactionClient tcl;
435
		try {
436
			tcl = new TransactionClient();
437
			String result = tcl.getClient().validateRecharge(rechargeType, number, operatorId, ipAddress);
438
			log.info("validateRecharge Called" + number + " and rechargeType : " +  rechargeType + ", IP:" + ipAddress + ", Operator:" + operatorId + ", Result:" + result);
439
			return result;
440
		} catch (Exception e) {
441
			log.error("Unable to get service provider for Device number " + number + " and rechargeType : " +  rechargeType, e);
442
		}
443
		return "Oops! There seems to be a problem. Please try after some time";
444
	}
445
 
446
 
21464 kshitij.so 447
}
21505 kshitij.so 448
 
449
 
450