Subversion Repositories SmartDukaan

Rev

Rev 10968 | Rev 13352 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1946 chandransh 1
package in.shop2020.payment.service.handler;
2
 
6050 anupam.sin 3
import in.shop2020.model.v1.order.RechargeOrder;
3578 mandeep.dh 4
import in.shop2020.payment.domain.Refund;
5
import in.shop2020.payment.handler.PaymentGatewayHandler;
6
import in.shop2020.payment.handler.PaymentHandler;
4008 mandeep.dh 7
import in.shop2020.payment.handler.PaymentRequiringExtraProcessingHandler;
3578 mandeep.dh 8
import in.shop2020.payment.handler.RefundHandler;
9
import in.shop2020.payments.Attribute;
4008 mandeep.dh 10
import in.shop2020.payments.ExtraPaymentProcessingType;
3578 mandeep.dh 11
import in.shop2020.payments.Payment;
12
import in.shop2020.payments.PaymentException;
13
import in.shop2020.payments.PaymentGateway;
14
import in.shop2020.payments.PaymentService.Iface;
15
import in.shop2020.payments.PaymentStatus;
6050 anupam.sin 16
import in.shop2020.thrift.clients.TransactionClient;
3578 mandeep.dh 17
 
3010 chandransh 18
import java.text.SimpleDateFormat;
1946 chandransh 19
import java.util.ArrayList;
6448 rajveer 20
import java.util.Arrays;
1946 chandransh 21
import java.util.Date;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map;
25
 
8618 rajveer 26
import org.apache.commons.logging.Log;
27
import org.apache.commons.logging.LogFactory;
1946 chandransh 28
import org.apache.thrift.TException;
29
import org.springframework.context.ApplicationContext;
30
import org.springframework.context.support.ClassPathXmlApplicationContext;
31
 
32
public class PaymentServiceHandler implements Iface {
3010 chandransh 33
 
8618 rajveer 34
    private static final Log logger = LogFactory.getLog(PaymentServiceHandler.class);
35
 
3010 chandransh 36
 
37
    /**
38
     * Enum of all statuses that can be returned by the HDFC gateway
39
     * 
40
     * @author Chandranshu
41
     * 
42
     */
8907 rajveer 43
    public enum HdfcPaymentReturnStatus{
3010 chandransh 44
        APPROVED("APPROVED"),
45
        NOT_APPROVED("NOT APPROVED"),
46
        CAPTURED("CAPTURED"),
47
        NOT_CAPTURED ("NOT CAPTURED"),
48
        CANCELLED ("CANCELLED"),
49
        DENIED_BY_RISK("DENIED BY RISK"),
8907 rajveer 50
        HOST_TIMEOUT("HOST TIMEOUT"),
51
        SUCCESS("SUCCESS"),
52
        FAILURE("FAILURE");
3010 chandransh 53
        private String value;
54
        HdfcPaymentReturnStatus(String value) {
55
            this.value = value;
56
        }
57
        public String value(){
58
            return this.value;
59
        }
60
    }
61
 
2391 chandransh 62
	public static final long PAYMENT_NOT_CREATED = -1;
63
 
3010 chandransh 64
	private static final long HDFC_GATEWAY_ID = 1;
65
	private static final long EBS_GATEWAY_ID = 2;
13286 amit.gupta 66
	private static final long PAYU_GATEWAY_ID = 19;
8208 amar.kumar 67
	private static final long EBAY_GATEWAY_ID = 16;
8488 amar.kumar 68
	private static final long SNAPDEAL_GATEWAY_ID = 18;
7042 amar.kumar 69
	private static final List<Long> HDFC_EMI_GATEWAY_IDS = Arrays.asList(5L,10L,11L,12L,14L);
3010 chandransh 70
 
4651 rajveer 71
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
1946 chandransh 72
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
4008 mandeep.dh 73
    PaymentRequiringExtraProcessingHandler paymentRequiringExtraProcessingHandler =
74
        (PaymentRequiringExtraProcessingHandler) context.getBean("paymentRequiringExtraProcessingHandler");
75
 
76
    PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
2747 chandransh 77
	RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
1946 chandransh 78
 
4651 rajveer 79
	public void setDataSourceUrl(String dbHost){
80
		org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
81
		ds.setUrl(dbHost);
82
	}
83
 
84
	public String getDataSourceUrl(){
85
		org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
86
		return ds.getUrl();
87
	}
88
 
1946 chandransh 89
	@Override
6050 anupam.sin 90
	public long createPayment(long userId, double amount, long gatewayId, long txnId, boolean isDigital) throws PaymentException, TException {
3010 chandransh 91
	    logger.info("Creating payment corresponding to our txn id:" + txnId);
1946 chandransh 92
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
93
		payment.setUserId(userId);
94
		payment.setAmount(amount);
95
		payment.setGatewayId(gatewayId);
96
		payment.setMerchantTxnId(txnId);
97
		payment.setStatus(PaymentStatus.INIT.getValue());
6050 anupam.sin 98
		payment.setDigital(isDigital);
1946 chandransh 99
 
100
		return paymentHandler.insertPayment(payment);
101
	}
102
 
103
	@Override
104
	public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
3010 chandransh 105
	    logger.info("Getting payments from " + fromTime + " to " + toTime + " for user: " + userId);
2291 chandransh 106
		int statusValue = -1;
107
		if(status != null)
108
			statusValue = status.getValue();
109
		else
110
			statusValue = -1;
111
		return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
1946 chandransh 112
	}
113
 
114
	@Override
115
	public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,	TException {
3010 chandransh 116
	    logger.info("Getting payments from " + fromTime + " to " + toTime);
2291 chandransh 117
		int statusValue = -1;
118
		if(status != null)
119
			statusValue = status.getValue();
120
		else
121
			statusValue = -1;
122
		return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
1946 chandransh 123
	}
4141 chandransh 124
 
125
	@Override
126
	public List<Payment> getPaymentsByCapturedDate(long fromTime, long toTime, long gatewayId) throws PaymentException, TException {
127
		logger.info("Getting payments from " + fromTime + " to " + toTime + " for " + gatewayId);
128
		return getThriftPayments(paymentHandler.getPaymentsByCapturedDate(fromTime, toTime, gatewayId));
129
	}
1946 chandransh 130
 
131
	@Override
132
	public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
3010 chandransh 133
	    logger.info("Getting payment gateway with id:" + id);
2291 chandransh 134
		return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
1946 chandransh 135
	}
4600 varun.gupt 136
 
1946 chandransh 137
	@Override
4600 varun.gupt 138
	public List<PaymentGateway> getActivePaymentGateways() throws PaymentException, TException {
139
	    logger.info("Getting all active payment gateways");
140
	    return getThriftPaymentGateways(paymentGatewayHandler.getActivePaymentGateways());
141
	}
142
 
143
	@Override
1946 chandransh 144
	public Payment getPayment(long id) throws PaymentException, TException {
3010 chandransh 145
	    logger.info("Getting payment with id: " + id);
1946 chandransh 146
		return paymentHandler.getPayment(id).getThriftPayment();
147
	}
148
 
149
	@Override
150
	public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
3010 chandransh 151
	    logger.info("Getting payment for the txn id: " + txnId);
1946 chandransh 152
		return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
153
	}
4600 varun.gupt 154
 
155
	@Override
7049 anupam.sin 156
    public List<Payment> getPaymentForRechargeTxnId(long txnId) throws PaymentException, TException {
157
        logger.info("Getting payment for the txn id: " + txnId);
158
        return getThriftPayments(paymentHandler.getPaymentForRechargeTxn(txnId));
159
    }
160
 
161
	@Override
4600 varun.gupt 162
	public Payment getSuccessfulPaymentForTxnId(long txnId) throws PaymentException, TException {
163
 
164
		for (Payment payment: getPaymentForTxnId(txnId))	{
165
			if (payment.getStatus() == PaymentStatus.SUCCESS || payment.getStatus() == PaymentStatus.PARTIALLY_CAPTURED)	{
166
				return payment;
167
			}
168
		}
169
		return null;
170
	}
1946 chandransh 171
 
172
	@Override
173
	public boolean updatePaymentDetails(long id, String gatewayPaymentId,
174
			String sessionId, String gatewayTxnStatus, String description,
175
			String gatewayTxnId, String authCode, String referenceCode,
176
			String errorCode, PaymentStatus status, String gatewayTxnDate,
177
			List<Attribute> attributes) throws PaymentException, TException {
3010 chandransh 178
	    logger.info("Updating details of payment id: " + id);
1946 chandransh 179
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
180
		payment.setGatewayPaymentId(gatewayPaymentId);
181
		payment.setSessionId(sessionId);
182
		payment.setGatewayTxnStatus(gatewayTxnStatus);
183
		payment.setDescription(description);
184
		payment.setGatewayTxnId(gatewayTxnId);
185
		payment.setAuthCode(authCode);
186
		payment.setReferenceCode(referenceCode);
187
		payment.setErrorCode(errorCode);
188
		if(status!=null){
189
			payment.setStatus(status.getValue());
190
			if(status.equals(PaymentStatus.SUCCESS))
191
				payment.setSuccessTimestamp(new Date());
3578 mandeep.dh 192
			else if(status.equals(PaymentStatus.FAILED)) {
193
			    payment.setErrorTimestamp(new Date());
4421 mandeep.dh 194
			    persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
3578 mandeep.dh 195
			}
4421 mandeep.dh 196
			else if (status.equals(PaymentStatus.PROVISIONALLY_CAPTURED)) {
197
			    // FIXME different column to note provisional capture timestamp
198
			    // FIXME Requires extra processing at Crm end for actual manual capture
199
			    payment.setProvisionalCaptureTimestamp(new Date());
200
			    persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.PENDING_CAPTURE);
201
			}
1946 chandransh 202
		}
203
 
204
		payment.setGatewayTxnDate(gatewayTxnDate);
205
 
206
		Map<String, String> attrMap = new HashMap<String, String>();
2272 rajveer 207
		if(attributes != null){
208
			for(Attribute attribute : attributes){
209
				attrMap.put(attribute.getName(), attribute.getValue());
210
			}
1946 chandransh 211
		}
212
 
213
		paymentHandler.updatePayment(payment, attrMap);
214
		return true;
215
	}
216
 
3649 mandeep.dh 217
	/**
4008 mandeep.dh 218
	 * Persists a given payment Id in another table for future processing by CRM etc.
219
	 * Failed payments generally require a follow-up to help customers through our
220
	 * CRM-Outbound team.
3649 mandeep.dh 221
	 *
222
	 * @param payment  the payment object that failed.
4421 mandeep.dh 223
	 * @param type TODO
3649 mandeep.dh 224
	 */
4421 mandeep.dh 225
	private void persistPaymentRequiringExtraProcessing(in.shop2020.payment.domain.Payment payment, ExtraPaymentProcessingType type) {
4008 mandeep.dh 226
	    try {
4421 mandeep.dh 227
            paymentRequiringExtraProcessingHandler.insert(payment.getId(), type);
4008 mandeep.dh 228
        } catch (Exception e) {
229
            logger.error("Could not persist payment: " + payment.getId(), e);
3578 mandeep.dh 230
        }
231
    }
232
 
233
    @Override
1946 chandransh 234
	public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
3010 chandransh 235
	    logger.info("Getting the range of successful payments.");
1946 chandransh 236
		List<Double> minMaxAmounts = new ArrayList<Double>();
237
		Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
238
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
239
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
240
		return minMaxAmounts;
241
	}
242
 
6050 anupam.sin 243
    @Override
10269 amit.gupta 244
    public String initializeHdfcPayment(long merchantPaymentId, boolean isMobile) throws PaymentException, TException {
6050 anupam.sin 245
        logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
246
        in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
247
        String redirectURL;
248
        try {
10269 amit.gupta 249
            redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this, isMobile);
6050 anupam.sin 250
        } catch (Exception e) {
251
            throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
252
        }
253
        return redirectURL;
254
    }
255
 
256
    @Override
10269 amit.gupta 257
	public String doHdfcPaymentForDigitalOrder(long merchantPaymentId, long rechargeOrderId, String phone, boolean isMobile) throws PaymentException, TException {
6050 anupam.sin 258
        logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
10968 amit.gupta 259
        logger.info("doHdfcPaymentForDigitalOrder phone---- " + phone);
260
 
6050 anupam.sin 261
        in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
262
        TransactionClient tc = new TransactionClient();
263
        String redirectURL;
264
        RechargeOrder rechargeOrder;
265
        try {
266
            rechargeOrder = tc.getClient().getRechargeOrder(rechargeOrderId);
10269 amit.gupta 267
            redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, rechargeOrder, phone, this, isMobile);
6050 anupam.sin 268
        } catch (Exception e) {
269
            throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
270
        }
271
 
272
        return redirectURL;
273
    }
2391 chandransh 274
 
275
	@Override
10269 amit.gupta 276
    public String initializeHdfcEmiPayment(long merchantPaymentId, boolean isMobile) throws PaymentException, TException {
3616 chandransh 277
        logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
278
        in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
279
        String redirectURL;
280
        try {
10470 amit.gupta 281
            redirectURL = HdfcEmiPaymentHandler.initializeHdfcPayment(payment, this, isMobile);
3616 chandransh 282
        } catch (Exception e) {
283
            throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
284
        }
285
        return redirectURL;
286
    }
287
 
6486 rajveer 288
	@Override
289
	public synchronized boolean refundPayment(long merchantTxnId, double amount, boolean isDigital) throws PaymentException, TException {
6482 rajveer 290
        logger.info("Attempting to refund payment of amount " + amount + " corresponding to our transaction " + merchantTxnId);
8409 rajveer 291
        List<in.shop2020.payment.domain.Payment> payments;
292
        if(isDigital){
293
        	payments = paymentHandler.getPaymentForRechargeTxn(merchantTxnId);
294
        }else{
295
        	payments = paymentHandler.getPaymentForTxn(merchantTxnId);
296
        }
6482 rajveer 297
        if(payments ==null || payments.isEmpty())
298
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
299
 
6486 rajveer 300
        if(payments ==null || payments.isEmpty())
301
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
302
 
6482 rajveer 303
        in.shop2020.payment.domain.Payment payment = payments.get(0);
6486 rajveer 304
 
305
        if(payment.getAmount() < amount){
306
        	logger.warn("Refund amount is more than payment amount.");
307
            return false;
308
        }
309
 
6482 rajveer 310
        switch(PaymentStatus.findByValue(payment.getStatus())){
311
        case PENDING:
312
            logger.error("Attempt to refund a non-authorized payment");
313
            return false;
314
        case INIT:
315
            logger.warn("Attempt to refund a non-authorized payment");
316
            return false;
317
        case AUTHORIZED:
318
        	logger.warn("Attempt to refund a non-captured payment");
319
            return false;
320
        case CAPTURE_IN_PROCESS:
321
        	logger.warn("Attempt to refund a non-captured payment");
322
            return false;
323
        case PROVISIONALLY_CAPTURED:
324
        	logger.warn("Attempt to refund a non-captured payment");
325
            return false;
6486 rajveer 326
        case REFUNDED:
327
        	logger.warn("Attempt to refund a refunded payment");
328
            return false;
6482 rajveer 329
        case SUCCESS:
330
            break;
331
        case FAILED:
332
            logger.error("Attempting to capture a failed payment");
333
            return false;
334
        }
335
 
336
        long gatewayId = payment.getGatewayId();
337
 
338
        if(gatewayId == HDFC_GATEWAY_ID){
6491 rajveer 339
            //Refund the HDFC payment
6482 rajveer 340
            return refundHdfcPayment(payment, amount);
6491 rajveer 341
        }else if (gatewayId == EBS_GATEWAY_ID){
342
            //Refund the EBS payment
343
            return refundEbsPayment(payment, amount);
6482 rajveer 344
        } 
6491 rajveer 345
        else if (HDFC_EMI_GATEWAY_IDS.contains(gatewayId)){
346
             //Capture and update the HDFC EMI payment
347
            return refundHdfcEmiPayment(payment, amount);
348
        }
6482 rajveer 349
 
350
        logger.error("We have an captured payment from unknown gateway: " + gatewayId);
351
        return false;
352
    }
353
 
354
 
3616 chandransh 355
	@Override
2689 chandransh 356
    public long createRefund(long orderId, long merchantTxnId, double amount) throws PaymentException, TException{
6482 rajveer 357
		logger.info("Attempting to create a refund for order: " + orderId);
6488 rajveer 358
//		if(!refundPayment(merchantTxnId, amount, false)){
359
//			logger.warn("Not able to refund corresponding to the merchant txn " + merchantTxnId);
360
//		}
2689 chandransh 361
		List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
362
		if(payments ==null || payments.isEmpty())
363
			throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
364
 
3010 chandransh 365
		in.shop2020.payment.domain.Payment payment = payments.get(0);
2689 chandransh 366
		if(payment.getStatus() != PaymentStatus.SUCCESS.getValue())
367
			throw new PaymentException(104, "No successful payments found corresponding to the merchant txn " + merchantTxnId);
368
 
2747 chandransh 369
		Refund refund = new Refund();
370
		refund.setOrderId(orderId);
371
		refund.setPaymentId(payment.getId());
372
		refund.setGatewayId(payment.getGatewayId());
373
		refund.setAmount(amount);
374
		refund.setAttempts(0);
375
		return refundHandler.createRefund(refund);
2689 chandransh 376
    }
377
 
3010 chandransh 378
    @Override
8618 rajveer 379
    public synchronized boolean capturePayment(long merchantTxnId, boolean isDigital) throws PaymentException, TException {
380
        logger.info("Attempting to capture payment corresponding to our transaction " + merchantTxnId + " and Is Digital is:" + isDigital);
381
        List<in.shop2020.payment.domain.Payment> payments;
382
        if(isDigital){
383
        	payments = paymentHandler.getPaymentForRechargeTxn(merchantTxnId);
384
        }else{
385
        	payments = paymentHandler.getPaymentForTxn(merchantTxnId);
386
        }
3010 chandransh 387
        if(payments ==null || payments.isEmpty())
388
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
389
 
390
        in.shop2020.payment.domain.Payment payment = payments.get(0);
391
        switch(PaymentStatus.findByValue(payment.getStatus())){
392
        case PENDING:
393
            logger.error("Attempt to capture a non-authorized payment");
394
            return false;
395
        case INIT:
396
            logger.warn("Attempt to capture a non-authorized payment");
397
            return false;
398
        case AUTHORIZED:
4421 mandeep.dh 399
        case CAPTURE_IN_PROCESS:
3010 chandransh 400
            //Actual work to be done in this case. Let the call proceed.
401
            break;
4421 mandeep.dh 402
        case PROVISIONALLY_CAPTURED:
403
            logger.info("Attempting to capture a payment that is provisonally captured but we can let the client proceed.");
404
            return true;
3010 chandransh 405
        case SUCCESS:
406
            logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
407
            return true;
408
        case FAILED:
409
            logger.error("Attempting to capture a failed payment");
410
            return false;
411
        }
412
 
413
        long gatewayId = payment.getGatewayId();
414
 
415
        if(gatewayId == HDFC_GATEWAY_ID){
416
            //Capture and update the HDFC payment
417
            return captureAndUpdateHdfcPayment(payment);
418
        } else if (gatewayId == EBS_GATEWAY_ID){
419
            //Capture and update the EBS payment
420
            return captureAndUpdateEbsPayment(payment);
6449 rajveer 421
        } else if (HDFC_EMI_GATEWAY_IDS.contains(gatewayId)){
3583 chandransh 422
            //Capture and update the HDFC EMI payment
423
            return captureAndUpdateHdfcEmiPayment(payment);
8488 amar.kumar 424
        } else if (gatewayId == EBAY_GATEWAY_ID || gatewayId == SNAPDEAL_GATEWAY_ID) {
8208 amar.kumar 425
        	return true;
13286 amit.gupta 426
        } else if (gatewayId == PAYU_GATEWAY_ID) {
427
        	return catpureAndUpdatePayuPayment(payment);
3010 chandransh 428
        }
429
 
430
        logger.error("We have an authorized payment from unknown gateway: " + gatewayId);
431
        return false;
432
    }
433
 
13286 amit.gupta 434
    private boolean catpureAndUpdatePayuPayment(
435
			in.shop2020.payment.domain.Payment payment) {
436
		return false;
437
	}
438
 
439
	@Override
3956 chandransh 440
    public boolean partiallyCapturePayment(long merchantTxnId, double amount, String xferBy, String xferTxnId, long xferDate) throws PaymentException, TException {
441
        logger.info("Attempting to partially capture payment corresponding to our transaction " + merchantTxnId);
442
        List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
443
        if(payments ==null || payments.isEmpty())
444
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
445
 
446
        in.shop2020.payment.domain.Payment payment = payments.get(0);
447
        switch(PaymentStatus.findByValue(payment.getStatus())){
448
        case PENDING:
449
            // COD payments lie in this state before settlement.
450
        case INIT:
451
        case PARTIALLY_CAPTURED:
452
        case AUTHORIZED:
453
            // COD payments would not be in this state but we are processing
454
            // payments in this state as well for forward compatibility since
455
            // someday we'd want to be able to capture authorized CC payments
456
            // partially.
457
            break;
458
        case SUCCESS:
459
            logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
460
            return true;
461
        case FAILED:
462
            logger.error("Attempting to capture a failed payment");
463
            return false;
464
        }
465
        SimpleDateFormat mysqlDateFormatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
466
        String xferDateStr = mysqlDateFormatter.format(new Date(xferDate));
467
 
468
        return settleAndUpdateCodPayment(payment, amount, xferBy, xferTxnId, xferDateStr);
469
    }
470
 
3010 chandransh 471
    /**
472
     * Capture the HDFC payment represented by the given payment object. If the
473
     * capture attempt is not successful, we mark this payment as failed. We
474
     * don't retry or anything. We'll add the support of multiple attempts later
475
     * on.
476
     * 
477
     * @param payment The payment which has to be captured.
478
     * @return True if the payment attempt is successful, false if not.
4421 mandeep.dh 479
     * @throws PaymentException 
3010 chandransh 480
     */
4421 mandeep.dh 481
    private boolean captureAndUpdateHdfcPayment(in.shop2020.payment.domain.Payment payment) throws PaymentException {
3010 chandransh 482
        long merchantPaymentId = payment.getId();
483
        logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
484
        Map<String, String> captureResult = HdfcPaymentHandler.capturePayment(payment);
485
        String captureStatus = captureResult.get(IPaymentHandler.STATUS);
486
        String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
487
 
488
        Map<String, String> attrMap = new HashMap<String, String>();
489
        if (!captureStatus.trim().equals("0") 
490
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
491
            // Failure
3616 chandransh 492
            logger.error("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
3010 chandransh 493
            String description = captureResult.get(IPaymentHandler.ERROR);
494
            String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
495
 
496
            payment.setDescription(description);
497
            payment.setErrorCode(errorCode);
498
            payment.setErrorTimestamp(new Date());
4421 mandeep.dh 499
 
500
            if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
501
                payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
502
                paymentHandler.updatePayment(payment, attrMap);
503
                throw new PaymentException(106, "Could not capture due to connection issue");
504
            }
505
            else {
506
                payment.setStatus(PaymentStatus.FAILED.getValue());
507
                paymentHandler.updatePayment(payment, attrMap);
508
                persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
509
            }
510
 
3010 chandransh 511
            return false;
512
        } else {
513
            // Success
3616 chandransh 514
            logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
3010 chandransh 515
            payment.setDescription("Payment Captured");
516
            payment.setGatewayTxnStatus(gatewayStatus);
517
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
518
            payment.setSuccessTimestamp(new Date());           
519
 
520
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
521
            attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
522
            attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
523
 
524
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
525
            attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
526
 
527
            paymentHandler.updatePayment(payment, attrMap);
528
            return true;
529
          }
530
    }
531
 
532
    /**
3583 chandransh 533
     * Capture the HDFC EMI payment represented by the given payment object. If
534
     * the capture attempt is not successful, we mark this payment as failed. We
535
     * don't retry or anything. We'll add the support of multiple attempts later
536
     * on.
537
     * 
538
     * @param payment
539
     *            The payment which has to be captured.
540
     * @return True if the payment attempt is successful, false if not.
4421 mandeep.dh 541
     * @throws PaymentException 
3583 chandransh 542
     */
4421 mandeep.dh 543
    private boolean captureAndUpdateHdfcEmiPayment(in.shop2020.payment.domain.Payment payment) throws PaymentException{
3583 chandransh 544
        long merchantPaymentId = payment.getId();
545
        logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
546
        Map<String, String> captureResult = HdfcEmiPaymentHandler.capturePayment(payment);
547
        String captureStatus = captureResult.get(IPaymentHandler.STATUS);
548
        String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
549
 
550
        Map<String, String> attrMap = new HashMap<String, String>();
551
        if (!captureStatus.trim().equals("0") 
552
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
553
            // Failure
3616 chandransh 554
            logger.error("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
3583 chandransh 555
            String description = captureResult.get(IPaymentHandler.ERROR);
556
            String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
557
 
558
            payment.setDescription(description);
559
            payment.setErrorCode(errorCode);
4421 mandeep.dh 560
            payment.setErrorTimestamp(new Date());                
561
 
562
            // Not marking payments as failed in case of connection issues
563
            if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
564
                payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
565
                paymentHandler.updatePayment(payment, attrMap);
566
                throw new PaymentException(106, "Could not capture due to connection issue");
567
            }
568
            else {
569
                payment.setStatus(PaymentStatus.FAILED.getValue());
570
                paymentHandler.updatePayment(payment, attrMap);
571
                persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
572
            }
573
 
3583 chandransh 574
            return false;
575
        } else {
576
            // Success
3616 chandransh 577
            logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
3583 chandransh 578
            payment.setDescription("Payment Captured");
579
            payment.setGatewayTxnStatus(gatewayStatus);
580
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
4421 mandeep.dh 581
            payment.setSuccessTimestamp(new Date());
3583 chandransh 582
 
583
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
584
            attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
585
            attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
586
 
587
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
588
            attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
589
 
590
            paymentHandler.updatePayment(payment, attrMap);
591
            return true;
592
          }
593
    }
594
 
595
    /**
3010 chandransh 596
     * Capture the EBS payment represented by the given payment object. If the
597
     * capture attempt is not successful, we mark this payment as failed. We
598
     * don't retry or anything. We'll add the support of multiple attempts later
599
     * on.
600
     * 
601
     * @param payment The payment which has to be captured.
602
     * @return True if the payment attempt is successful, false if not.
4421 mandeep.dh 603
     * @throws PaymentException 
3010 chandransh 604
     */
4421 mandeep.dh 605
    private boolean captureAndUpdateEbsPayment(in.shop2020.payment.domain.Payment payment) throws PaymentException{
3010 chandransh 606
        Map<String, String> captureResult = EbsPaymentHandler.capturePayment(payment);
607
        String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
8650 anupam.sin 608
        for(String key : captureResult.keySet()) {
609
            logger.info("key : " + key + " value : " + captureResult.get(key));
610
        }
3010 chandransh 611
 
8650 anupam.sin 612
        logger.info("capture status : " + captureStatus);
613
 
3010 chandransh 614
        Map<String, String> attrMap = new HashMap<String, String>();
615
        if("".equals(captureStatus)){
616
            //Failure
3616 chandransh 617
            logger.error("Capture attempt failed for EBS payment with id: " + payment.getId());
3010 chandransh 618
            String description = captureResult.get(EbsPaymentHandler.ERROR);
619
            String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
4421 mandeep.dh 620
 
3010 chandransh 621
            payment.setDescription(description);
622
            payment.setErrorCode(errorCode);
623
            payment.setErrorTimestamp(new Date());
4421 mandeep.dh 624
 
625
            if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
626
                payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());            
627
                paymentHandler.updatePayment(payment, attrMap);
628
                throw new PaymentException(106, "Could not capture due to connection issue");
629
            }
630
            else {
631
                payment.setStatus(PaymentStatus.FAILED.getValue());            
632
                paymentHandler.updatePayment(payment, attrMap);
633
                persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
634
            }
635
 
3010 chandransh 636
            return false;
637
        }else{
638
            //Success
3616 chandransh 639
            logger.info("Capture attempt successful for EBS payment with id: " + payment.getId());
3010 chandransh 640
            payment.setGatewayTxnStatus(captureStatus);
641
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
642
            payment.setSuccessTimestamp(new Date());
643
 
644
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
645
            attrMap.put(IPaymentHandler.CAPTURE_TIME, captureResult.get(IPaymentHandler.CAPTURE_TIME));
646
            paymentHandler.updatePayment(payment, attrMap);
647
            return true;
648
        }
649
    }
650
 
6482 rajveer 651
 
3010 chandransh 652
    /**
6482 rajveer 653
     * Refund the HDFC payment represented by the given payment object. If the
654
     * refund attempt is not successful, will not any action.
655
     * 
656
     * @param payment The payment which has to be captured.
657
     * @amount amount to be refunded
658
     * @return True if the payment attempt is successful, false if not.
659
     * @throws PaymentException 
660
     */
661
    private boolean refundHdfcPayment(in.shop2020.payment.domain.Payment payment, double amount) throws PaymentException {
662
        long merchantPaymentId = payment.getId();
663
        logger.info("Refunding HDFC payment with id: " + merchantPaymentId);
664
        Map<String, String> refundResult = HdfcPaymentHandler.refundPayment(payment, amount);
6486 rajveer 665
        String refundStatus = refundResult.get(IPaymentHandler.STATUS);
6482 rajveer 666
        String gatewayStatus = refundResult.get(IPaymentHandler.GATEWAY_STATUS);
667
 
668
        Map<String, String> attrMap = new HashMap<String, String>();
6486 rajveer 669
        if (!refundStatus.trim().equals("0") 
6482 rajveer 670
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
6486 rajveer 671
 
672
        	logger.error("Refund attempt failed for HDFC payment with id: " + merchantPaymentId);
673
            String description = refundResult.get(IPaymentHandler.ERROR);
674
            String errorCode = refundResult.get(IPaymentHandler.ERR_CODE);
675
 
676
            payment.setDescription(description);
677
            payment.setErrorCode(errorCode);
678
            payment.setErrorTimestamp(new Date());
679
 
680
            if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
681
                //payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
682
                //paymentHandler.updatePayment(payment, attrMap);
683
                throw new PaymentException(106, "Could not capture due to connection issue. Try Later");
684
            }
685
            else {
6491 rajveer 686
//                payment.setStatus(PaymentStatus.FAILED.getValue());
687
//                paymentHandler.updatePayment(payment, attrMap);
6486 rajveer 688
                persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
689
            }
690
 
6482 rajveer 691
            return false;
692
        } else {
693
            // Success
694
            logger.info("Refund attempt successful for HDFC payment with id: " + merchantPaymentId);
695
            payment.setDescription("Payment Refunded");
696
            payment.setGatewayTxnStatus(gatewayStatus);
6503 rajveer 697
            payment.setStatus(PaymentStatus.REFUNDED.getValue());    
698
            payment.setRefundAmount(amount);
6482 rajveer 699
 
6486 rajveer 700
            attrMap.put(IPaymentHandler.REFUND_TXN_ID, refundResult.get(IPaymentHandler.REFUND_TXN_ID));
701
            attrMap.put(IPaymentHandler.REFUND_REF_ID, refundResult.get(IPaymentHandler.REFUND_REF_ID));
702
            attrMap.put(IPaymentHandler.REFUND_AUTH_ID, refundResult.get(IPaymentHandler.REFUND_AUTH_ID));
6503 rajveer 703
            attrMap.put(IPaymentHandler.REFUND_AMNT, refundResult.get(IPaymentHandler.REFUND_AMNT));
704
 
6482 rajveer 705
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
6486 rajveer 706
            attrMap.put(HdfcPaymentHandler.REFUND_TIME, captureTimeDateFormatter.format(new Date()));
6482 rajveer 707
 
708
            paymentHandler.updatePayment(payment, attrMap);
709
            return true;
710
          }
711
    }
712
 
713
 
6491 rajveer 714
    /**
715
     * Refund the HDFC EMI payment represented by the given payment object. If
716
     * the capture attempt is not successful, we will not do anything.
717
     * 
718
     * @param payment
719
     *            The payment which has to be captured.
720
     * @return True if the payment attempt is successful, false if not.
721
     * @throws PaymentException 
722
     */
723
    private boolean refundHdfcEmiPayment(in.shop2020.payment.domain.Payment payment, double amount) throws PaymentException{
724
        long merchantPaymentId = payment.getId();
725
        logger.info("Refunding HDFC payment with id: " + merchantPaymentId);
726
        Map<String, String> refundResult = HdfcEmiPaymentHandler.refundPayment(payment, amount);
727
        String refundStatus = refundResult.get(IPaymentHandler.STATUS);
728
        String gatewayStatus = refundResult.get(IPaymentHandler.GATEWAY_STATUS);
729
 
730
        Map<String, String> attrMap = new HashMap<String, String>();
731
        if (!refundStatus.trim().equals("0") 
732
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
733
            // Failure
734
            logger.error("Refund attempt failed for HDFC payment with id: " + merchantPaymentId);
735
            String description = refundResult.get(IPaymentHandler.ERROR);
736
            String errorCode = refundResult.get(IPaymentHandler.ERR_CODE);
737
 
738
            payment.setDescription(description);
739
            payment.setErrorCode(errorCode);
740
            payment.setErrorTimestamp(new Date());                
741
 
742
            // Not marking payments as failed in case of connection issues
743
            if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
744
             //   payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
745
             //   paymentHandler.updatePayment(payment, attrMap);
746
                throw new PaymentException(106, "Could not capture due to connection issue");
747
            }
748
            else {
749
              //  payment.setStatus(PaymentStatus.FAILED.getValue());
750
              //  paymentHandler.updatePayment(payment, attrMap);
751
                persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
752
            }
753
 
754
            return false;
755
        } else {
756
            // Success
757
            logger.info("Refund attempt successful for HDFC payment with id: " + merchantPaymentId);
758
            payment.setDescription("Payment Refunded");
759
            payment.setGatewayTxnStatus(gatewayStatus);
760
            payment.setStatus(PaymentStatus.REFUNDED.getValue());           
6503 rajveer 761
            payment.setRefundAmount(amount);
6491 rajveer 762
 
763
            attrMap.put(IPaymentHandler.REFUND_TXN_ID, refundResult.get(IPaymentHandler.REFUND_TXN_ID));
764
            attrMap.put(IPaymentHandler.REFUND_REF_ID, refundResult.get(IPaymentHandler.REFUND_REF_ID));
765
            attrMap.put(IPaymentHandler.REFUND_AUTH_ID, refundResult.get(IPaymentHandler.REFUND_AUTH_ID));
6503 rajveer 766
            attrMap.put(IPaymentHandler.REFUND_AMNT, refundResult.get(IPaymentHandler.REFUND_AMNT));
767
 
6491 rajveer 768
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
769
            attrMap.put(HdfcPaymentHandler.REFUND_TIME, captureTimeDateFormatter.format(new Date()));
770
 
771
            paymentHandler.updatePayment(payment, attrMap);
772
            return true;
773
          }
774
    }
6482 rajveer 775
 
776
    /**
6491 rajveer 777
     * Refund the EBS payment represented by the given payment object. If the
778
     * capture attempt is not successful, we will ignore. We don't retry or anything. 
779
     * We'll add the support of multiple attempts later on.
780
     * 
781
     * @param payment The payment which has to be captured.
782
     * @amount Amount to be refunded
783
     * @return True if the payment attempt is successful, false if not.
784
     * @throws PaymentException 
785
     */
786
    private boolean refundEbsPayment(in.shop2020.payment.domain.Payment payment, double amount) throws PaymentException{
787
        Map<String, String> refundResult = EbsPaymentHandler.refundPayment(payment, amount);
788
        String refundStatus = refundResult.get(EbsPaymentHandler.STATUS);
789
 
790
        Map<String, String> attrMap = new HashMap<String, String>();
791
        if("".equals(refundStatus)){
792
            //Failure
793
            logger.error("Refund attempt failed for EBS payment with id: " + payment.getId());
794
            String description = refundResult.get(EbsPaymentHandler.ERROR);
795
            String errorCode = refundResult.get(EbsPaymentHandler.ERR_CODE);
796
 
797
            payment.setDescription(description);
798
            payment.setErrorCode(errorCode);
799
            payment.setErrorTimestamp(new Date());
800
 
801
            if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
802
//                payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());            
803
//                paymentHandler.updatePayment(payment, attrMap);
804
                throw new PaymentException(106, "Could not capture due to connection issue");
805
            }
806
            else {
807
//                payment.setStatus(PaymentStatus.FAILED.getValue());            
808
//                paymentHandler.updatePayment(payment, attrMap);
809
                persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
810
            }
811
 
812
            return false;
813
        }else{
814
            //Success
815
            logger.info("Refund attempt successful for EBS payment with id: " + payment.getId());
816
            payment.setGatewayTxnStatus(refundStatus);
817
            payment.setStatus(PaymentStatus.REFUNDED.getValue());
6503 rajveer 818
            payment.setRefundAmount(amount);
6491 rajveer 819
 
820
            attrMap.put(IPaymentHandler.REFUND_TXN_ID, refundResult.get(IPaymentHandler.REFUND_TXN_ID));
821
            attrMap.put(IPaymentHandler.REFUND_TIME, refundResult.get(IPaymentHandler.REFUND_TIME));
822
            paymentHandler.updatePayment(payment, attrMap);
823
            return true;
824
        }
825
    }
826
 
827
 
828
    /**
3956 chandransh 829
     * Updates the settlement details of COD payments. Sets payment status as
830
     * either PARTIALLY CAPTURED or SUCCESS depending on whether the complete
831
     * amount has been captured. Other parameters are set as attributes.
832
     * 
833
     * @param payment
834
     *            The payment which needs to be updated.
835
     * @param amount
836
     *            Amount that has been captured.
837
     * @param xferBy
838
     *            Entity which transferred the money.
839
     * @param xferTxnId
840
     *            Transaction Id of the transfer.
841
     * @param xferDateStr
842
     *            Date on which the transfer took place.
843
     * @return true if the payment details were updated successfully.
844
     * @throws PaymentException
845
     *             if the captured amount will become more than the actual
846
     *             amount after this update.
847
     */
848
    private boolean settleAndUpdateCodPayment(in.shop2020.payment.domain.Payment payment, double amount, String xferBy, String xferTxnId, String xferDateStr) throws PaymentException{
849
        Map<String, String> attrMap = payment.getAttributeMap();
850
 
851
        double captureAmount = 0;
852
        String captureAmntStr = attrMap.get(IPaymentHandler.CAPTURE_AMNT);
853
        if(captureAmntStr != null)
854
            captureAmount = Double.parseDouble(captureAmntStr);
855
        captureAmount += amount;
5051 rajveer 856
        // If capture amount higher than payment amount by more than 50 paisa,
857
        // there is some issue and we should raise exception.
858
        if(captureAmount - payment.getAmount() > 0.5){
859
        	throw new PaymentException(105, "We've got a settlement request for an amount which is more than the transaction value.");
860
        }
3956 chandransh 861
 
5051 rajveer 862
        // If capture amount differs from payment amount by less than 50 paisa, lets mark the payment as successful. 
863
        // Else we can safely assume there will be some more orders for the payment, leading to make the payment as partially captured.
864
        if(Math.abs(captureAmount - payment.getAmount()) < 0.5){
865
        	payment.setStatus(PaymentStatus.SUCCESS.getValue());
866
        }else {
867
        	payment.setStatus(PaymentStatus.PARTIALLY_CAPTURED.getValue());   
3956 chandransh 868
        }
869
        payment.setSuccessTimestamp(new Date());
870
        attrMap.put(IPaymentHandler.CAPTURE_AMNT, captureAmount + "");
871
        attrMap.put(IPaymentHandler.XFER_TXN_ID, xferTxnId);
872
        attrMap.put(IPaymentHandler.XFER_TXN_DATE, xferDateStr);
873
        attrMap.put(IPaymentHandler.XFER_BY, xferBy);
874
        paymentHandler.updatePayment(payment, attrMap);
875
        return true;
876
    }
877
 
878
    /**
3010 chandransh 879
     * Creates a list of thrift payment objects corresponding to a list of
880
     * payment data objects.
881
     * 
882
     * @param daoPayments
883
     *            A list of payment DAO.
884
     * @return A list of Thrift payment objects.
885
     */
886
    private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
887
 
888
        List<Payment> payments = new ArrayList<Payment>();
889
        for(in.shop2020.payment.domain.Payment payment : daoPayments){
890
            payments.add(payment.getThriftPayment());
891
        }
892
        return payments;
893
    }
3375 rajveer 894
 
4600 varun.gupt 895
    /**
896
     * Creates a list of thrift payment gateway objects corresponding to a list of
897
     * payment gateway data objects.
898
     * 
899
     * @param daoPaymentGateways
900
     *            A list of payment gateway DAO.
901
     * @return A list of Thrift payment gateway objects.
902
     */
903
    private List<PaymentGateway> getThriftPaymentGateways(List<in.shop2020.payment.domain.PaymentGateway> daoPaymentGateways){
904
 
905
        List<PaymentGateway> paymentGateways = new ArrayList<PaymentGateway>();
906
        for(in.shop2020.payment.domain.PaymentGateway paymentGateway : daoPaymentGateways){
907
            paymentGateways.add(paymentGateway.getThriftPaymentGateway());
908
        }
909
        return paymentGateways;
910
    }
4619 mandeep.dh 911
 
3375 rajveer 912
	@Override
4619 mandeep.dh 913
	public boolean isAlive() {
914
	    try {
915
            return !paymentGatewayHandler.getActivePaymentGateways().isEmpty();
916
        } catch (Exception e) {
917
            logger.error("Could not fetch payment gateways", e);
918
        }
919
 
920
        return false;
3375 rajveer 921
	}
3956 chandransh 922
 
923
 
924
    @Override
925
    public void closeSession() throws TException {
926
        // TODO Auto-generated method stub      
927
    }
4008 mandeep.dh 928
 
929
    @Override
930
    public List<Long> getPaymentsRequiringExtraProcessing (
931
            ExtraPaymentProcessingType category) throws TException {
932
        return paymentRequiringExtraProcessingHandler.getPaymentIds(category);
933
    }
934
 
935
    @Override
936
    public void markPaymentAsProcessed(long paymentId,
937
            ExtraPaymentProcessingType category) throws TException {
938
        paymentRequiringExtraProcessingHandler.delete(paymentId, category);
939
    }
4141 chandransh 940
 
8907 rajveer 941
	@Override
8914 rajveer 942
	public PaymentStatus getPaymentStatusAtGateway(long merchantTxnId, double amount, boolean isDigital) throws PaymentException, TException {
8907 rajveer 943
	        logger.info("Attempting to get status of payment of amount " + amount + " corresponding to our transaction " + merchantTxnId);
944
	        List<in.shop2020.payment.domain.Payment> payments;
945
	        if(isDigital){
946
	        	payments = paymentHandler.getPaymentForRechargeTxn(merchantTxnId);
947
	        }else{
948
	        	payments = paymentHandler.getPaymentForTxn(merchantTxnId);
949
	        }
950
	        if(payments ==null || payments.isEmpty())
951
	            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
952
 
953
	        if(payments ==null || payments.isEmpty())
954
	            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
955
 
956
	        in.shop2020.payment.domain.Payment payment = payments.get(0);
957
	        long gatewayId = payment.getGatewayId();
958
 
959
	        if(gatewayId == HDFC_GATEWAY_ID){
960
	            return HdfcPaymentHandler.validateHdfcPayment(payment, amount);
961
	        }else if (gatewayId == EBS_GATEWAY_ID){
962
	            //return validateEbsPayment(payment, amount);
963
	        } 
964
	        else if (HDFC_EMI_GATEWAY_IDS.contains(gatewayId)){
965
	            //return validateHdfcEmiPayment(payment, amount);
966
	        }
967
 
968
	        logger.error("We have an payment from unknown gateway: " + gatewayId);
8914 rajveer 969
	        return PaymentStatus.INIT;
8907 rajveer 970
	    }
971
 
972
 
1946 chandransh 973
}