Subversion Repositories SmartDukaan

Rev

Rev 3956 | Rev 4141 | 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
 
3578 mandeep.dh 3
import in.shop2020.payment.domain.Refund;
4
import in.shop2020.payment.handler.PaymentGatewayHandler;
5
import in.shop2020.payment.handler.PaymentHandler;
4008 mandeep.dh 6
import in.shop2020.payment.handler.PaymentRequiringExtraProcessingHandler;
3578 mandeep.dh 7
import in.shop2020.payment.handler.RefundHandler;
8
import in.shop2020.payments.Attribute;
4008 mandeep.dh 9
import in.shop2020.payments.ExtraPaymentProcessingType;
3578 mandeep.dh 10
import in.shop2020.payments.Payment;
11
import in.shop2020.payments.PaymentException;
12
import in.shop2020.payments.PaymentGateway;
13
import in.shop2020.payments.PaymentService.Iface;
14
import in.shop2020.payments.PaymentStatus;
15
 
3010 chandransh 16
import java.text.SimpleDateFormat;
1946 chandransh 17
import java.util.ArrayList;
18
import java.util.Date;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
 
23
import org.apache.thrift.TException;
3010 chandransh 24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
1946 chandransh 26
import org.springframework.context.ApplicationContext;
27
import org.springframework.context.support.ClassPathXmlApplicationContext;
28
 
29
public class PaymentServiceHandler implements Iface {
3010 chandransh 30
 
31
    private static Logger logger = LoggerFactory.getLogger(PaymentServiceHandler.class);
32
 
33
    /**
34
     * Enum of all statuses that can be returned by the HDFC gateway
35
     * 
36
     * @author Chandranshu
37
     * 
38
     */
39
    private enum HdfcPaymentReturnStatus{
40
        APPROVED("APPROVED"),
41
        NOT_APPROVED("NOT APPROVED"),
42
        CAPTURED("CAPTURED"),
43
        NOT_CAPTURED ("NOT CAPTURED"),
44
        CANCELLED ("CANCELLED"),
45
        DENIED_BY_RISK("DENIED BY RISK"),
46
        HOST_TIMEOUT("HOST TIMEOUT");
47
        private String value;
48
        HdfcPaymentReturnStatus(String value) {
49
            this.value = value;
50
        }
51
        public String value(){
52
            return this.value;
53
        }
54
    }
55
 
2391 chandransh 56
	public static final long PAYMENT_NOT_CREATED = -1;
57
 
3010 chandransh 58
	private static final long HDFC_GATEWAY_ID = 1;
59
	private static final long EBS_GATEWAY_ID = 2;
3583 chandransh 60
	private static final long HDFC_EMI_GATEWAY_ID = 5;
3010 chandransh 61
 
1946 chandransh 62
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
63
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
4008 mandeep.dh 64
    PaymentRequiringExtraProcessingHandler paymentRequiringExtraProcessingHandler =
65
        (PaymentRequiringExtraProcessingHandler) context.getBean("paymentRequiringExtraProcessingHandler");
66
 
67
    PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
2747 chandransh 68
	RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
1946 chandransh 69
 
70
	@Override
71
	public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
3010 chandransh 72
	    logger.info("Creating payment corresponding to our txn id:" + txnId);
1946 chandransh 73
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
74
		payment.setUserId(userId);
75
		payment.setAmount(amount);
76
		payment.setGatewayId(gatewayId);
77
		payment.setMerchantTxnId(txnId);
78
		payment.setStatus(PaymentStatus.INIT.getValue());
79
 
80
		return paymentHandler.insertPayment(payment);
81
	}
82
 
83
	@Override
84
	public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
3010 chandransh 85
	    logger.info("Getting payments from " + fromTime + " to " + toTime + " for user: " + userId);
2291 chandransh 86
		int statusValue = -1;
87
		if(status != null)
88
			statusValue = status.getValue();
89
		else
90
			statusValue = -1;
91
		return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
1946 chandransh 92
	}
93
 
94
	@Override
95
	public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,	TException {
3010 chandransh 96
	    logger.info("Getting payments from " + fromTime + " to " + toTime);
2291 chandransh 97
		int statusValue = -1;
98
		if(status != null)
99
			statusValue = status.getValue();
100
		else
101
			statusValue = -1;
102
		return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
1946 chandransh 103
	}
104
 
105
	@Override
106
	public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
3010 chandransh 107
	    logger.info("Getting payment gateway with id:" + id);
2291 chandransh 108
		return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
1946 chandransh 109
	}
110
 
111
	@Override
112
	public Payment getPayment(long id) throws PaymentException, TException {
3010 chandransh 113
	    logger.info("Getting payment with id: " + id);
1946 chandransh 114
		return paymentHandler.getPayment(id).getThriftPayment();
115
	}
116
 
117
	@Override
118
	public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
3010 chandransh 119
	    logger.info("Getting payment for the txn id: " + txnId);
1946 chandransh 120
		return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
121
	}
122
 
123
	@Override
124
	public boolean updatePaymentDetails(long id, String gatewayPaymentId,
125
			String sessionId, String gatewayTxnStatus, String description,
126
			String gatewayTxnId, String authCode, String referenceCode,
127
			String errorCode, PaymentStatus status, String gatewayTxnDate,
128
			List<Attribute> attributes) throws PaymentException, TException {
3010 chandransh 129
	    logger.info("Updating details of payment id: " + id);
1946 chandransh 130
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
131
		payment.setGatewayPaymentId(gatewayPaymentId);
132
		payment.setSessionId(sessionId);
133
		payment.setGatewayTxnStatus(gatewayTxnStatus);
134
		payment.setDescription(description);
135
		payment.setGatewayTxnId(gatewayTxnId);
136
		payment.setAuthCode(authCode);
137
		payment.setReferenceCode(referenceCode);
138
		payment.setErrorCode(errorCode);
139
		if(status!=null){
140
			payment.setStatus(status.getValue());
141
			if(status.equals(PaymentStatus.SUCCESS))
142
				payment.setSuccessTimestamp(new Date());
3578 mandeep.dh 143
			else if(status.equals(PaymentStatus.FAILED)) {
144
			    payment.setErrorTimestamp(new Date());
4008 mandeep.dh 145
			    persistPaymentRequiringExtraProcessing(payment);
3578 mandeep.dh 146
			}
1946 chandransh 147
		}
148
 
149
		payment.setGatewayTxnDate(gatewayTxnDate);
150
 
151
		Map<String, String> attrMap = new HashMap<String, String>();
2272 rajveer 152
		if(attributes != null){
153
			for(Attribute attribute : attributes){
154
				attrMap.put(attribute.getName(), attribute.getValue());
155
			}
1946 chandransh 156
		}
157
 
158
		paymentHandler.updatePayment(payment, attrMap);
159
		return true;
160
	}
161
 
3649 mandeep.dh 162
	/**
4008 mandeep.dh 163
	 * Persists a given payment Id in another table for future processing by CRM etc.
164
	 * Failed payments generally require a follow-up to help customers through our
165
	 * CRM-Outbound team.
3649 mandeep.dh 166
	 *
167
	 * @param payment  the payment object that failed.
168
	 */
4008 mandeep.dh 169
	private void persistPaymentRequiringExtraProcessing(in.shop2020.payment.domain.Payment payment) {
170
	    try {
171
            paymentRequiringExtraProcessingHandler.insert(payment.getId(), ExtraPaymentProcessingType.FAILED_PAYMENTS);
172
        } catch (Exception e) {
173
            logger.error("Could not persist payment: " + payment.getId(), e);
3578 mandeep.dh 174
        }
175
    }
176
 
177
    @Override
1946 chandransh 178
	public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
3010 chandransh 179
	    logger.info("Getting the range of successful payments.");
1946 chandransh 180
		List<Double> minMaxAmounts = new ArrayList<Double>();
181
		Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
182
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
183
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
184
		return minMaxAmounts;
185
	}
186
 
2391 chandransh 187
	@Override
188
	public String initializeHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
3010 chandransh 189
	    logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
2391 chandransh 190
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
191
		String redirectURL;
192
		try {
193
			redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this);
194
		} catch (Exception e) {
195
			throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
196
		}
197
		return redirectURL;
198
	}
199
 
200
	@Override
3616 chandransh 201
    public String initializeHdfcEmiPayment(long merchantPaymentId) throws PaymentException, TException {
202
        logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
203
        in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
204
        String redirectURL;
205
        try {
206
            redirectURL = HdfcEmiPaymentHandler.initializeHdfcPayment(payment, this);
207
        } catch (Exception e) {
208
            throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
209
        }
210
        return redirectURL;
211
    }
212
 
213
	@Override
2689 chandransh 214
    public long createRefund(long orderId, long merchantTxnId, double amount) throws PaymentException, TException{
3010 chandransh 215
	    logger.info("Attempting to create a refund for order: " + orderId);
2689 chandransh 216
		List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
217
		if(payments ==null || payments.isEmpty())
218
			throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
219
 
3010 chandransh 220
		in.shop2020.payment.domain.Payment payment = payments.get(0);
2689 chandransh 221
		if(payment.getStatus() != PaymentStatus.SUCCESS.getValue())
222
			throw new PaymentException(104, "No successful payments found corresponding to the merchant txn " + merchantTxnId);
223
 
2747 chandransh 224
		Refund refund = new Refund();
225
		refund.setOrderId(orderId);
226
		refund.setPaymentId(payment.getId());
227
		refund.setGatewayId(payment.getGatewayId());
228
		refund.setAmount(amount);
229
		refund.setAttempts(0);
230
		return refundHandler.createRefund(refund);
2689 chandransh 231
    }
232
 
3010 chandransh 233
    @Override
234
    public boolean capturePayment(long merchantTxnId) throws PaymentException, TException {
235
        logger.info("Attempting to capture payment corresponding to our transaction " + merchantTxnId);
236
        List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
237
        if(payments ==null || payments.isEmpty())
238
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
239
 
240
        in.shop2020.payment.domain.Payment payment = payments.get(0);
241
        switch(PaymentStatus.findByValue(payment.getStatus())){
242
        case PENDING:
243
            logger.error("Attempt to capture a non-authorized payment");
244
            return false;
245
        case INIT:
246
            logger.warn("Attempt to capture a non-authorized payment");
247
            return false;
248
        case AUTHORIZED:
249
            //Actual work to be done in this case. Let the call proceed.
250
            break;
251
        case SUCCESS:
252
            logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
253
            return true;
254
        case FAILED:
255
            logger.error("Attempting to capture a failed payment");
256
            return false;
257
        }
258
 
259
        long gatewayId = payment.getGatewayId();
260
 
261
        if(gatewayId == HDFC_GATEWAY_ID){
262
            //Capture and update the HDFC payment
263
            return captureAndUpdateHdfcPayment(payment);
264
        } else if (gatewayId == EBS_GATEWAY_ID){
265
            //Capture and update the EBS payment
266
            return captureAndUpdateEbsPayment(payment);
3583 chandransh 267
        } else if (gatewayId == HDFC_EMI_GATEWAY_ID){
268
            //Capture and update the HDFC EMI payment
269
            return captureAndUpdateHdfcEmiPayment(payment);
3010 chandransh 270
        }
271
 
272
        logger.error("We have an authorized payment from unknown gateway: " + gatewayId);
273
        return false;
274
    }
275
 
3956 chandransh 276
    @Override
277
    public boolean partiallyCapturePayment(long merchantTxnId, double amount, String xferBy, String xferTxnId, long xferDate) throws PaymentException, TException {
278
        logger.info("Attempting to partially capture payment corresponding to our transaction " + merchantTxnId);
279
        List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
280
        if(payments ==null || payments.isEmpty())
281
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
282
 
283
        in.shop2020.payment.domain.Payment payment = payments.get(0);
284
        switch(PaymentStatus.findByValue(payment.getStatus())){
285
        case PENDING:
286
            // COD payments lie in this state before settlement.
287
        case INIT:
288
        case PARTIALLY_CAPTURED:
289
        case AUTHORIZED:
290
            // COD payments would not be in this state but we are processing
291
            // payments in this state as well for forward compatibility since
292
            // someday we'd want to be able to capture authorized CC payments
293
            // partially.
294
            break;
295
        case SUCCESS:
296
            logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
297
            return true;
298
        case FAILED:
299
            logger.error("Attempting to capture a failed payment");
300
            return false;
301
        }
302
        SimpleDateFormat mysqlDateFormatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
303
        String xferDateStr = mysqlDateFormatter.format(new Date(xferDate));
304
 
305
        return settleAndUpdateCodPayment(payment, amount, xferBy, xferTxnId, xferDateStr);
306
    }
307
 
3010 chandransh 308
    /**
309
     * Capture the HDFC payment represented by the given payment object. If the
310
     * capture attempt is not successful, we mark this payment as failed. We
311
     * don't retry or anything. We'll add the support of multiple attempts later
312
     * on.
313
     * 
314
     * @param payment The payment which has to be captured.
315
     * @return True if the payment attempt is successful, false if not.
316
     */
317
    private boolean captureAndUpdateHdfcPayment(in.shop2020.payment.domain.Payment payment){
318
        long merchantPaymentId = payment.getId();
319
        logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
320
        Map<String, String> captureResult = HdfcPaymentHandler.capturePayment(payment);
321
        String captureStatus = captureResult.get(IPaymentHandler.STATUS);
322
        String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
323
 
324
        Map<String, String> attrMap = new HashMap<String, String>();
325
        if (!captureStatus.trim().equals("0") 
326
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
327
            // Failure
3616 chandransh 328
            logger.error("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
3010 chandransh 329
            String description = captureResult.get(IPaymentHandler.ERROR);
330
            String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
331
 
332
            payment.setDescription(description);
333
            payment.setErrorCode(errorCode);
334
            payment.setStatus(PaymentStatus.FAILED.getValue());
335
            payment.setErrorTimestamp(new Date());
336
            paymentHandler.updatePayment(payment, attrMap);
4008 mandeep.dh 337
            persistPaymentRequiringExtraProcessing(payment);
3010 chandransh 338
            return false;
339
        } else {
340
            // Success
3616 chandransh 341
            logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
3010 chandransh 342
            payment.setDescription("Payment Captured");
343
            payment.setGatewayTxnStatus(gatewayStatus);
344
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
345
            payment.setSuccessTimestamp(new Date());           
346
 
347
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
348
            attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
349
            attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
350
 
351
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
352
            attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
353
 
354
            paymentHandler.updatePayment(payment, attrMap);
355
            return true;
356
          }
357
    }
358
 
359
    /**
3583 chandransh 360
     * Capture the HDFC EMI payment represented by the given payment object. If
361
     * the capture attempt is not successful, we mark this payment as failed. We
362
     * don't retry or anything. We'll add the support of multiple attempts later
363
     * on.
364
     * 
365
     * @param payment
366
     *            The payment which has to be captured.
367
     * @return True if the payment attempt is successful, false if not.
368
     */
369
    private boolean captureAndUpdateHdfcEmiPayment(in.shop2020.payment.domain.Payment payment){
370
        long merchantPaymentId = payment.getId();
371
        logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
372
        Map<String, String> captureResult = HdfcEmiPaymentHandler.capturePayment(payment);
373
        String captureStatus = captureResult.get(IPaymentHandler.STATUS);
374
        String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
375
 
376
        Map<String, String> attrMap = new HashMap<String, String>();
377
        if (!captureStatus.trim().equals("0") 
378
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
379
            // Failure
3616 chandransh 380
            logger.error("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
3583 chandransh 381
            String description = captureResult.get(IPaymentHandler.ERROR);
382
            String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
383
 
384
            payment.setDescription(description);
385
            payment.setErrorCode(errorCode);
386
            payment.setStatus(PaymentStatus.FAILED.getValue());
387
            payment.setErrorTimestamp(new Date());
388
            paymentHandler.updatePayment(payment, attrMap);
4008 mandeep.dh 389
            persistPaymentRequiringExtraProcessing(payment);
3583 chandransh 390
            return false;
391
        } else {
392
            // Success
3616 chandransh 393
            logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
3583 chandransh 394
            payment.setDescription("Payment Captured");
395
            payment.setGatewayTxnStatus(gatewayStatus);
396
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
397
            payment.setSuccessTimestamp(new Date());           
398
 
399
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
400
            attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
401
            attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
402
 
403
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
404
            attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
405
 
406
            paymentHandler.updatePayment(payment, attrMap);
407
            return true;
408
          }
409
    }
410
 
411
    /**
3010 chandransh 412
     * Capture the EBS payment represented by the given payment object. If the
413
     * capture attempt is not successful, we mark this payment as failed. We
414
     * don't retry or anything. We'll add the support of multiple attempts later
415
     * on.
416
     * 
417
     * @param payment The payment which has to be captured.
418
     * @return True if the payment attempt is successful, false if not.
419
     */
420
    private boolean captureAndUpdateEbsPayment(in.shop2020.payment.domain.Payment payment){
421
        Map<String, String> captureResult = EbsPaymentHandler.capturePayment(payment);
422
        String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
423
 
424
        Map<String, String> attrMap = new HashMap<String, String>();
425
        if("".equals(captureStatus)){
426
            //Failure
3616 chandransh 427
            logger.error("Capture attempt failed for EBS payment with id: " + payment.getId());
3010 chandransh 428
            String description = captureResult.get(EbsPaymentHandler.ERROR);
429
            String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
430
 
431
            payment.setDescription(description);
432
            payment.setErrorCode(errorCode);
433
            payment.setStatus(PaymentStatus.FAILED.getValue());
434
            payment.setErrorTimestamp(new Date());
435
            paymentHandler.updatePayment(payment, attrMap);
4008 mandeep.dh 436
            persistPaymentRequiringExtraProcessing(payment);
3010 chandransh 437
            return false;
438
        }else{
439
            //Success
3616 chandransh 440
            logger.info("Capture attempt successful for EBS payment with id: " + payment.getId());
3010 chandransh 441
            payment.setGatewayTxnStatus(captureStatus);
442
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
443
            payment.setSuccessTimestamp(new Date());
444
 
445
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
446
            attrMap.put(IPaymentHandler.CAPTURE_TIME, captureResult.get(IPaymentHandler.CAPTURE_TIME));
447
            paymentHandler.updatePayment(payment, attrMap);
448
            return true;
449
        }
450
    }
451
 
452
    /**
3956 chandransh 453
     * Updates the settlement details of COD payments. Sets payment status as
454
     * either PARTIALLY CAPTURED or SUCCESS depending on whether the complete
455
     * amount has been captured. Other parameters are set as attributes.
456
     * 
457
     * @param payment
458
     *            The payment which needs to be updated.
459
     * @param amount
460
     *            Amount that has been captured.
461
     * @param xferBy
462
     *            Entity which transferred the money.
463
     * @param xferTxnId
464
     *            Transaction Id of the transfer.
465
     * @param xferDateStr
466
     *            Date on which the transfer took place.
467
     * @return true if the payment details were updated successfully.
468
     * @throws PaymentException
469
     *             if the captured amount will become more than the actual
470
     *             amount after this update.
471
     */
472
    private boolean settleAndUpdateCodPayment(in.shop2020.payment.domain.Payment payment, double amount, String xferBy, String xferTxnId, String xferDateStr) throws PaymentException{
473
        Map<String, String> attrMap = payment.getAttributeMap();
474
 
475
        double captureAmount = 0;
476
        String captureAmntStr = attrMap.get(IPaymentHandler.CAPTURE_AMNT);
477
        if(captureAmntStr != null)
478
            captureAmount = Double.parseDouble(captureAmntStr);
479
        captureAmount += amount;
480
        if(captureAmount > payment.getAmount())
481
            throw new PaymentException(105, "We've got a settlement request for an amount which is more than the transaction value.");
482
 
483
        if(captureAmount < payment.getAmount()){
484
            payment.setStatus(PaymentStatus.PARTIALLY_CAPTURED.getValue());
485
        } else {
486
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
487
        }
488
        payment.setSuccessTimestamp(new Date());
489
        attrMap.put(IPaymentHandler.CAPTURE_AMNT, captureAmount + "");
490
        attrMap.put(IPaymentHandler.XFER_TXN_ID, xferTxnId);
491
        attrMap.put(IPaymentHandler.XFER_TXN_DATE, xferDateStr);
492
        attrMap.put(IPaymentHandler.XFER_BY, xferBy);
493
        paymentHandler.updatePayment(payment, attrMap);
494
        return true;
495
    }
496
 
497
 
498
    /**
3010 chandransh 499
     * Creates a list of thrift payment objects corresponding to a list of
500
     * payment data objects.
501
     * 
502
     * @param daoPayments
503
     *            A list of payment DAO.
504
     * @return A list of Thrift payment objects.
505
     */
506
    private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
507
 
508
        List<Payment> payments = new ArrayList<Payment>();
509
        for(in.shop2020.payment.domain.Payment payment : daoPayments){
510
            payments.add(payment.getThriftPayment());
511
        }
512
        return payments;
513
    }
3375 rajveer 514
 
515
	@Override
516
	public boolean isAlive() throws TException {
517
		// TODO Auto-generated method stub
518
		return true;
519
	}
3956 chandransh 520
 
521
 
522
    @Override
523
    public void closeSession() throws TException {
524
        // TODO Auto-generated method stub      
525
    }
4008 mandeep.dh 526
 
527
    @Override
528
    public List<Long> getPaymentsRequiringExtraProcessing (
529
            ExtraPaymentProcessingType category) throws TException {
530
        return paymentRequiringExtraProcessingHandler.getPaymentIds(category);
531
    }
532
 
533
    @Override
534
    public void markPaymentAsProcessed(long paymentId,
535
            ExtraPaymentProcessingType category) throws TException {
536
        paymentRequiringExtraProcessingHandler.delete(paymentId, category);
537
    }
1946 chandransh 538
}