Subversion Repositories SmartDukaan

Rev

Rev 3649 | Rev 4008 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3649 Rev 3956
Line 62... Line 62...
62
	
62
	
63
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
63
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
64
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
64
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
65
	PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
65
	PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
66
	RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
66
	RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
67
	
-
 
68
	@Override
-
 
69
	public void closeSession() throws TException {
-
 
70
		// TODO Auto-generated method stub		
-
 
71
	}
-
 
72
 
67
 
73
	@Override
68
	@Override
74
	public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
69
	public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
75
	    logger.info("Creating payment corresponding to our txn id:" + txnId);
70
	    logger.info("Creating payment corresponding to our txn id:" + txnId);
76
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
71
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
Line 282... Line 277...
282
        
277
        
283
        logger.error("We have an authorized payment from unknown gateway: " + gatewayId);
278
        logger.error("We have an authorized payment from unknown gateway: " + gatewayId);
284
        return false;
279
        return false;
285
    }
280
    }
286
    
281
    
-
 
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
    
287
    /**
314
    /**
288
     * Capture the HDFC payment represented by the given payment object. If the
315
     * Capture the HDFC payment represented by the given payment object. If the
289
     * capture attempt is not successful, we mark this payment as failed. We
316
     * capture attempt is not successful, we mark this payment as failed. We
290
     * don't retry or anything. We'll add the support of multiple attempts later
317
     * don't retry or anything. We'll add the support of multiple attempts later
291
     * on.
318
     * on.
Line 425... Line 452...
425
            attrMap.put(IPaymentHandler.CAPTURE_TIME, captureResult.get(IPaymentHandler.CAPTURE_TIME));
452
            attrMap.put(IPaymentHandler.CAPTURE_TIME, captureResult.get(IPaymentHandler.CAPTURE_TIME));
426
            paymentHandler.updatePayment(payment, attrMap);
453
            paymentHandler.updatePayment(payment, attrMap);
427
            return true;
454
            return true;
428
        }
455
        }
429
    }
456
    }
430
    
-
 
431
 
457
 
432
    /**
458
    /**
-
 
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
    /**
433
     * Creates a list of thrift payment objects corresponding to a list of
505
     * Creates a list of thrift payment objects corresponding to a list of
434
     * payment data objects.
506
     * payment data objects.
435
     * 
507
     * 
436
     * @param daoPayments
508
     * @param daoPayments
437
     *            A list of payment DAO.
509
     *            A list of payment DAO.
Line 449... Line 521...
449
	@Override
521
	@Override
450
	public boolean isAlive() throws TException {
522
	public boolean isAlive() throws TException {
451
		// TODO Auto-generated method stub
523
		// TODO Auto-generated method stub
452
		return true;
524
		return true;
453
	}
525
	}
-
 
526
	
-
 
527
	   
-
 
528
    @Override
-
 
529
    public void closeSession() throws TException {
-
 
530
        // TODO Auto-generated method stub      
-
 
531
    }
454
}
532
}