Subversion Repositories SmartDukaan

Rev

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