Subversion Repositories SmartDukaan

Rev

Rev 2291 | 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 {

        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 {
                return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, status.getValue(), gatewayId));
        }

        @Override
        public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,     TException {
                return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, status.getValue(), gatewayId));
        }

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

        @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>();
                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;
        }
}