Subversion Repositories SmartDukaan

Rev

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