Subversion Repositories SmartDukaan

Rev

Rev 21522 | Rev 21534 | 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;
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
58
public class RechargeController {
59
 
60
	private static final Logger log=LoggerFactory.getLogger(RechargeController.class);
21514 kshitij.so 61
	private static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR";
21464 kshitij.so 62
 
21505 kshitij.so 63
	@Autowired
64
	UserAccountRepository userAccountRepository;
21514 kshitij.so 65
 
66
	@Autowired
67
	UserRepository userRepository;
68
 
21464 kshitij.so 69
	@RequestMapping(value = ProfitMandiConstants.URL_GET_SERVICE_PROVIDER, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
70
	@ApiImplicitParams({
71
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
72
				required = true, dataType = "string", paramType = "header")
73
	})
74
	@ApiOperation(value = "")
75
	public ResponseEntity<?> getServiceProvider(HttpServletRequest request, @RequestParam(value="deviceNumber") String deviceNumber, @RequestParam(value="rechargeType") String rechargeType){
76
		DeviceNumberInfo deviceInfo = null;
77
		TransactionClient tcl;
78
		try {
79
			tcl = new TransactionClient();
80
			deviceInfo = tcl.getClient().getServiceProviderForDevice(RechargeType.valueOf(rechargeType), deviceNumber.substring(0,4));
81
		} catch (Exception e) {
82
			log.error("Unable to get service provider for Device number " + deviceNumber + " and rechargeType : " +  rechargeType, e);
83
		}
84
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, deviceInfo);
85
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
86
	}
21505 kshitij.so 87
 
21468 kshitij.so 88
	@RequestMapping(value = ProfitMandiConstants.URL_GET_ALL_DENOMINATIONS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
89
	@ApiImplicitParams({
90
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
91
				required = true, dataType = "string", paramType = "header")
92
	})
93
	@ApiOperation(value = "")
94
	public ResponseEntity<?> getAllDenominations(HttpServletRequest request, @RequestParam(value="operatorId") long operatorId, @RequestParam(value="circleCode") String circleCode, @RequestParam(value="denominationType") String denominationType){
95
		List<RechargeDenomination> rechargeDenominations = new ArrayList<RechargeDenomination>();
96
		TransactionClient tcl;
97
		try {
98
			tcl = new TransactionClient();
99
			rechargeDenominations =  tcl.getClient().getRechargeDenominations(operatorId, circleCode, DenominationType.valueOf(denominationType));
100
		} catch (Exception e) {
101
			log.error("Unable to get rechargeDenominations for operatorId " + operatorId + " and circleCode : " +  circleCode + " and DenominationType : " + denominationType, e);
102
		}
103
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, rechargeDenominations);
104
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
105
	}
21505 kshitij.so 106
 
21468 kshitij.so 107
	@RequestMapping(value = ProfitMandiConstants.URL_MOBILE_OPERATORS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
108
	@ApiImplicitParams({
109
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
110
				required = true, dataType = "string", paramType = "header")
111
	})
112
	@ApiOperation(value = "")
113
	public ResponseEntity<?> getAllMobileOperators(HttpServletRequest request){
114
		Map<Long, String> mobileProviderMap = Utils.getMobileProvidersMap();
115
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, mobileProviderMap);
116
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
117
	}
21505 kshitij.so 118
 
21468 kshitij.so 119
	@RequestMapping(value = ProfitMandiConstants.URL_DTH_OPERATORS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
120
	@ApiImplicitParams({
121
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
122
				required = true, dataType = "string", paramType = "header")
123
	})
124
	@ApiOperation(value = "")
125
	public ResponseEntity<?> getAllDTHOperators(HttpServletRequest request){
126
		Map<Long, String> dthProviderMap = Utils.getDthProvidersMap();
127
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dthProviderMap);
128
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
129
	}
130
 
21505 kshitij.so 131
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_CONFIRM, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
132
	@ApiImplicitParams({
133
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
134
				required = true, dataType = "string", paramType = "header")
135
	})
136
	@ApiOperation(value = "")
21510 kshitij.so 137
	public ResponseEntity<?> confirmRecharge(HttpServletRequest request, @RequestParam(value="rechargeAmount") long rechargeAmount){
21505 kshitij.so 138
		int userId = (int)request.getAttribute("userId");
139
		UserAccounts userAccount = null;
140
		UserWallet wallet = null;
141
		if (rechargeAmount <=0){
142
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.SUCCESS, null);
143
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
144
		}
21514 kshitij.so 145
 
21505 kshitij.so 146
		try {
147
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
148
		} catch (ProfitMandiBusinessException e) {
149
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
150
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
151
		}
152
		try {
153
			TransactionClient tc = new TransactionClient();
154
			wallet = tc.getClient().getUserWallet(Long.valueOf(userAccount.getAccount_key()));
155
		} catch (NumberFormatException | TException e) {
156
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
157
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
158
		}
21514 kshitij.so 159
 
21505 kshitij.so 160
		ConfirmRechargeResponse crr = new ConfirmRechargeResponse();
161
		crr.setWalletAmount(wallet.getAmount());
162
		crr.setWalletAmountLeft(wallet.getAmount() - rechargeAmount);
163
		crr.setCanProceed(true);
164
		if (crr.getWalletAmountLeft() < 0){
165
			crr.setCanProceed(false);
166
			crr.setReason("You don't have sufficient wallet balance");
167
		}
168
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, crr);
169
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
170
	}
21514 kshitij.so 171
 
21505 kshitij.so 172
	@RequestMapping(value = ProfitMandiConstants.URL_CREATE_RECHARGE , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
173
	@ApiImplicitParams({
174
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
175
				required = true, dataType = "string", paramType = "header")
176
	})
21514 kshitij.so 177
	public ResponseEntity<?> createRecharge(HttpServletRequest request, @RequestBody CreateRechargeRequest createRechargeRequest){
178
		String ipAddress  = remoteAddr(request);
179
		String errorMessage = validateRecharge(RechargeType.valueOf(createRechargeRequest.getRechargeType()), createRechargeRequest.getNumber(), createRechargeRequest.getOperatorId(), ipAddress);
180
		CreateRechargeResponse crr =  new CreateRechargeResponse();
181
		if(!errorMessage.equals("SUCCESS")){
182
			crr.setReason(errorMessage);
183
			crr.setResult(false);
184
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.SUCCESS, crr);
185
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
186
		}
187
		int userId = (int)request.getAttribute("userId");
188
		UserAccounts userAccount = null;
189
		UserWallet wallet = null;
190
		if (createRechargeRequest.getRechargeAmount() <=0){
191
			crr.setReason("Illegal recharge amount");
192
			crr.setResult(false);
193
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.SUCCESS, crr);
194
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
195
		}
196
 
197
		try {
198
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
199
		} catch (ProfitMandiBusinessException e) {
200
			log.error("Unable to get user account ",e);
201
			crr.setReason("We are experiencing some problem right now.");
202
			crr.setResult(false);
203
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, crr);
204
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
205
		}
206
		try {
207
			TransactionClient tc = new TransactionClient();
208
			wallet = tc.getClient().getUserWallet(Long.valueOf(userAccount.getAccount_key()));
209
		} catch (NumberFormatException | TException e) {
210
			log.error("Unable to get user wallet ",e);
211
			crr.setReason("We are experiencing some problem right now.");
212
			crr.setResult(false);
213
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, crr);
214
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
215
		}
216
 
217
		if (wallet.getAmount() < createRechargeRequest.getRechargeAmount()){
218
			crr.setReason("You don't have sufficient wallet balance.");
219
			crr.setResult(false);
220
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.SUCCESS, crr);
221
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
222
		}
223
 
224
		User user = null;
225
		try {
226
			user = userRepository.selectById(userId);
227
		} catch (ProfitMandiBusinessException e) {
228
			log.error("Unable to get user",e);
229
			crr.setReason("We are experiencing some problem right now.");
230
			crr.setResult(false);
231
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, crr);
232
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
233
		}
234
 
235
 
236
		RechargeOrder t_rechargeOrder = new RechargeOrder();
237
		t_rechargeOrder.setTotalAmount(createRechargeRequest.getRechargeAmount());
238
		t_rechargeOrder.setUserEmailId(user.getEmailId());
21519 kshitij.so 239
		t_rechargeOrder.setUserId(Long.valueOf(userAccount.getAccount_key()));
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);
259
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, crr);
260
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
261
		}
262
 
263
		log.info("Recharge Order:" + rechargeOrder);
264
		PaymentClient paymentServiceClient = null;
265
		try {
266
			paymentServiceClient = new PaymentClient();
267
		} catch (TTransportException e) {
268
			log.error("Unable to create payment client",e);
269
			crr.setReason("We are experiencing some problem right now.");
270
			crr.setResult(false);
271
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, crr);
272
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
273
		}
274
		try {
275
			RechargeOrder d_rechargeOrder = tc.getClient().getRechargeOrder(rechargeOrder.getId());
276
			List<Payment> payments = paymentServiceClient.getClient().getPaymentForRechargeTxnId(d_rechargeOrder.getTransactionId());
277
			if(payments.size() > 0) {
278
				throw new PaymentException(d_rechargeOrder.getId(), "Payment already exists for recharge");
279
			}
280
			Long merchantPaymentId = 0l;
281
			if (d_rechargeOrder.getWalletAmount() +  d_rechargeOrder.getCouponAmount() != d_rechargeOrder.getTotalAmount()) {
282
				log.error("Wallet amount : " + d_rechargeOrder.getWalletAmount() + ", coupon amount : " + d_rechargeOrder.getCouponAmount() + " and total amount : " + d_rechargeOrder.getTotalAmount());
283
				merchantPaymentId = paymentServiceClient.getClient().createPayment(d_rechargeOrder.getUserId(), d_rechargeOrder.getTotalAmount(), 8, d_rechargeOrder.getTransactionId(), true);
284
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, "","", "0", "", "", "", "", "", PaymentStatus.FAILED, "", null);
285
				tc.getClient().updateRechargeOrderStatus(d_rechargeOrder.getId(),  RechargeOrderStatus.PAYMENT_FAILED);
286
			} else {
287
				merchantPaymentId = paymentServiceClient.getClient().createPayment(d_rechargeOrder.getUserId(), d_rechargeOrder.getTotalAmount(), 8, d_rechargeOrder.getTransactionId(), true);
288
				//Update payment status as authorized
289
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, "","", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
290
				tc.getClient().updateRechargeOrderStatus(d_rechargeOrder.getId(),  RechargeOrderStatus.PAYMENT_SUCCESSFUL);
291
			}
292
			crr.setRechargeOrderId(d_rechargeOrder.getId());
293
			crr.setResult(true);
294
		} catch (Exception e) {
295
			log.error("Unable to mark the payment as authorized", e);
296
			crr.setReason("We are experiencing some problem right now.");
297
			crr.setResult(false);
298
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, crr);
299
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
300
		}
301
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, crr);
302
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21505 kshitij.so 303
	}
21514 kshitij.so 304
 
21517 kshitij.so 305
	@RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_RESULT , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
306
	@ApiImplicitParams({
307
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
308
				required = true, dataType = "string", paramType = "header")
309
	})
310
	public ResponseEntity<?> rechargeResult(HttpServletRequest request, @RequestParam(value="rechargeOrderId") long rechargeOrderId){
311
		TransactionClient tc=null;
312
		RechargeOrder rechargeOrder = null;
313
		RechargeResultPojo rrp = null;
314
		try {
315
			tc = new TransactionClient();
316
			rechargeOrder = tc.getClient().getRechargeOrder(rechargeOrderId);
317
		} catch (TransactionServiceException | TException e) {
318
			// return with internal server error
319
			e.printStackTrace();
320
			rrp = new RechargeResultPojo();
321
			rrp.setIsError(true);
322
			rrp.setDetailDisplayMessage("We are experiencing some problem right now.");
323
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, rrp);
324
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
325
		}
326
		if (rechargeOrder == null){
327
			rrp = new RechargeResultPojo();
328
			rrp.setIsError(true);
329
			rrp.setDetailDisplayMessage("Recharge order doesnot exist in our system.");
330
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.SUCCESS, rrp);
331
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
332
		}
333
		String[] os = Utils.getOrderStatus(rechargeOrder.getStatus());
334
		rrp = new RechargeResultPojo();
335
		rrp.setRechargeDeviceNumber(rechargeOrder.getDeviceNumber());
336
		rrp.setRechargeDisplayId(rechargeOrder.getDisplayId());
337
		rrp.setTotalAmount(rechargeOrder.getTotalAmount() + "");
338
		rrp.setRechargeProvider(Utils.getProvider(rechargeOrder.getOperatorId()));
339
		rrp.setIsError(Boolean.parseBoolean(os[0]));
340
		rrp.setRechargeStatus(os[1]);
341
		rrp.setDetailDisplayMessage(os[2]);
21523 kshitij.so 342
		if (rechargeOrder.getStatus().equals(RechargeOrderStatus.RECHARGE_UNKNOWN)){
21517 kshitij.so 343
			rrp.setPoll(true);
344
		}
345
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, rrp);
346
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
347
	}
21514 kshitij.so 348
 
21517 kshitij.so 349
	@RequestMapping(value = ProfitMandiConstants.URL_POLL_RECHARGE , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
350
	@ApiImplicitParams({
351
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
352
				required = true, dataType = "string", paramType = "header")
353
	})
21519 kshitij.so 354
	public ResponseEntity<?> pollRechargeResult(HttpServletRequest request, @RequestParam(value="rechargeOrderId") long rechargeOrderId, @RequestParam(value="finalCall") boolean finalCall){
21517 kshitij.so 355
		TransactionClient transactionServiceClient = null;
356
		RechargeOrder t_rechargeOrder = null;
357
		RechargeResultPojo rrp = null;
358
		try{
359
			transactionServiceClient = new TransactionClient();
360
			t_rechargeOrder = transactionServiceClient.getClient().getRcgOrderStatus(rechargeOrderId, finalCall);
361
		}
362
		catch(Exception e){
363
			rrp = new RechargeResultPojo();
364
			rrp.setRechargeStatus("");
365
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, rrp);
366
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);	
367
		}
368
		if (t_rechargeOrder == null){
369
			rrp = new RechargeResultPojo();
370
			rrp.setRechargeStatus("");
371
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, new RechargeResultPojo());
372
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);	
373
		}
374
		rrp = new RechargeResultPojo();
375
		rrp.setRechargeStatus(t_rechargeOrder.getStatus().name());
376
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, rrp);
377
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
378
	}
379
 
380
 
381
 
21514 kshitij.so 382
	private String remoteAddr(HttpServletRequest request) {
383
		String remoteAddr = "";
384
		String x;
385
		x = request.getHeader(HEADER_X_FORWARDED_FOR);
386
		if (x != null && !x.isEmpty()) {
387
			remoteAddr = x;
388
			int idx = remoteAddr.lastIndexOf(',');
389
			if (idx > -1) {
390
				remoteAddr = remoteAddr.substring(idx + 1).trim();
391
			}
392
		} else {
393
			remoteAddr = request.getRemoteAddr();
394
		}
395
		return remoteAddr;
396
	}
397
 
398
	private String validateRecharge(RechargeType rechargeType, String number, long operatorId, String ipAddress){
399
		TransactionClient tcl;
400
		try {
401
			tcl = new TransactionClient();
402
			String result = tcl.getClient().validateRecharge(rechargeType, number, operatorId, ipAddress);
403
			log.info("validateRecharge Called" + number + " and rechargeType : " +  rechargeType + ", IP:" + ipAddress + ", Operator:" + operatorId + ", Result:" + result);
404
			return result;
405
		} catch (Exception e) {
406
			log.error("Unable to get service provider for Device number " + number + " and rechargeType : " +  rechargeType, e);
407
		}
408
		return "Oops! There seems to be a problem. Please try after some time";
409
	}
410
 
411
 
21464 kshitij.so 412
}
21505 kshitij.so 413
 
414
 
415