Subversion Repositories SmartDukaan

Rev

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