Subversion Repositories SmartDukaan

Rev

Rev 2291 | Rev 2689 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.payment.service.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import in.shop2020.payment.handler.PaymentGatewayHandler;
import in.shop2020.payment.handler.PaymentHandler;
import in.shop2020.payments.Attribute;
import in.shop2020.payments.Payment;
import in.shop2020.payments.PaymentException;
import in.shop2020.payments.PaymentGateway;
import in.shop2020.payments.PaymentService.Iface;
import in.shop2020.payments.PaymentStatus;

public class PaymentServiceHandler implements Iface {
        public static final long PAYMENT_NOT_CREATED = -1;
        private static final String FLAG_KEY = "IsFlagged";
        private static final String TXN_KEY = "TransactionID";
        private static final String AUTH_TXN_ID = "AuthTxnId";
        private static final String CAPTURE_TXN_ID = "CaptureTxnId";
        private static final String CAPTURE_TIME = "CaptureTime";
        
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
        PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
        
        @Override
        public void closeSession() throws TException {
                // TODO Auto-generated method stub              
        }

        @Override
        public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
                in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
                payment.setUserId(userId);
                payment.setAmount(amount);
                payment.setGatewayId(gatewayId);
                payment.setMerchantTxnId(txnId);
                payment.setStatus(PaymentStatus.INIT.getValue());

                return paymentHandler.insertPayment(payment);
        }

        @Override
        public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
                int statusValue = -1;
                if(status != null)
                        statusValue = status.getValue();
                else
                        statusValue = -1;
                return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
        }

        @Override
        public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,     TException {
                int statusValue = -1;
                if(status != null)
                        statusValue = status.getValue();
                else
                        statusValue = -1;
                return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
        }

        @Override
        public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
                return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
        }

        @Override
        public Payment getPayment(long id) throws PaymentException, TException {
                return paymentHandler.getPayment(id).getThriftPayment();
        }

        @Override
        public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
                return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
        }

        @Override
        public boolean updatePaymentDetails(long id, String gatewayPaymentId,
                        String sessionId, String gatewayTxnStatus, String description,
                        String gatewayTxnId, String authCode, String referenceCode,
                        String errorCode, PaymentStatus status, String gatewayTxnDate,
                        List<Attribute> attributes) throws PaymentException, TException {
                in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
                payment.setGatewayPaymentId(gatewayPaymentId);
                payment.setSessionId(sessionId);
                payment.setGatewayTxnStatus(gatewayTxnStatus);
                payment.setDescription(description);
                payment.setGatewayTxnId(gatewayTxnId);
                payment.setAuthCode(authCode);
                payment.setReferenceCode(referenceCode);
                payment.setErrorCode(errorCode);
                if(status!=null){
                        payment.setStatus(status.getValue());
                        if(status.equals(PaymentStatus.SUCCESS))
                                payment.setSuccessTimestamp(new Date());
                        else if(status.equals(PaymentStatus.FAILED))
                                payment.setErrorTimestamp(new Date());
                }
                
                payment.setGatewayTxnDate(gatewayTxnDate);
                
                Map<String, String> attrMap = new HashMap<String, String>();
                if(attributes != null){
                        for(Attribute attribute : attributes){
                                attrMap.put(attribute.getName(), attribute.getValue());
                        }
                }
                
                paymentHandler.updatePayment(payment, attrMap);
                return true;
        }

        @Override
        public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
                List<Double> minMaxAmounts = new ArrayList<Double>();
                Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
                minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
                minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
                return minMaxAmounts;
        }

        private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
                List<Payment> payments = new ArrayList<Payment>();
                for(in.shop2020.payment.domain.Payment payment : daoPayments){
                        payments.add(payment.getThriftPayment());
                }
                return payments;
        }

        @Override
        public Payment updateAndCaptureEbsPayment(Map<String, String> paymentParams) throws PaymentException, TException {
                long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
                String gatewayPaymentId = paymentParams.get("PaymentID");
                double amount = Double.parseDouble(paymentParams.get("Amount"));
                String isFlagged = paymentParams.get(FLAG_KEY);
                String gatewayTxnStatus = paymentParams.get("ResponseCode");
                String description = paymentParams.get("ResponseMessage");
                String authTxnId = paymentParams.get(TXN_KEY);
                
                
                List<Attribute> attributes = new ArrayList<Attribute>();
                attributes.add(new Attribute(FLAG_KEY, isFlagged));
                attributes.add(new Attribute(AUTH_TXN_ID, authTxnId));
                
                Payment payment = null;
                try {
                        payment = getPayment(merchantPaymentId);
                } catch (PaymentException e1) {
                        throw new PaymentException(e1);
                }
                
                if(!validatePaymentParams(amount, payment)){
                        throw new PaymentException(102, "Checks and balance failed on returned data");
                }
                
                if(gatewayTxnStatus.equals("0")){
                        //Update payment status as authorized
                        updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);

                        Map<String, String> captureResult = EbsPaymentHandler.capturePayment(amount, "" + gatewayPaymentId);
                        
                        String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
                        
                        if("".equals(captureStatus)){
                                //Failure
                                description = captureResult.get(EbsPaymentHandler.ERROR);
                                String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
                                
                                updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", errorCode, PaymentStatus.FAILED, "", attributes);
                        }else{
                                //Success
                                attributes.add(new Attribute(CAPTURE_TXN_ID, captureResult.get(EbsPaymentHandler.TXN_ID)));
                                attributes.add(new Attribute(CAPTURE_TIME, captureResult.get(EbsPaymentHandler.DATE_TIME)));
                                
                                updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", captureStatus, description, "", "", "", "", PaymentStatus.SUCCESS, "", attributes);
                        }
                        

                }else{
                        updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.FAILED, "", attributes);             
                }
                
                payment = getPayment(merchantPaymentId);
                
                return payment;
        }
        
        @Override
        public String initializeHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
                in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
                String redirectURL;
                try {
                        redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this);
                } catch (Exception e) {
                        throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
                }
                return redirectURL;
        }
        
        @Override
        public Map<String, String> captureHdfcPayment(long merchantPaymentId){
                in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
                return HdfcPaymentHandler.capturePayment(payment);
        }
        
        private boolean validatePaymentParams(double amount, Payment payment){
                if(payment==null || payment.getAmount()!= amount){
                        // We did not request this payment or the authorised amount is different.
                        return false;
                }
                return true;
        }
}