Subversion Repositories SmartDukaan

Rev

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