Subversion Repositories SmartDukaan

Rev

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