Subversion Repositories SmartDukaan

Rev

Rev 2272 | Rev 2391 | 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
 
3
import java.util.ArrayList;
4
import java.util.Date;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
 
9
import org.apache.thrift.TException;
10
import org.springframework.context.ApplicationContext;
11
import org.springframework.context.support.ClassPathXmlApplicationContext;
12
 
13
import in.shop2020.payment.handler.PaymentGatewayHandler;
14
import in.shop2020.payment.handler.PaymentHandler;
15
import in.shop2020.payments.Attribute;
16
import in.shop2020.payments.Payment;
17
import in.shop2020.payments.PaymentException;
18
import in.shop2020.payments.PaymentGateway;
19
import in.shop2020.payments.PaymentService.Iface;
20
import in.shop2020.payments.PaymentStatus;
21
 
22
public class PaymentServiceHandler implements Iface {
23
 
24
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
25
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
26
	PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
27
 
28
	@Override
29
	public void closeSession() throws TException {
30
		// TODO Auto-generated method stub		
31
	}
32
 
33
	@Override
34
	public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
35
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
36
		payment.setUserId(userId);
37
		payment.setAmount(amount);
38
		payment.setGatewayId(gatewayId);
39
		payment.setMerchantTxnId(txnId);
40
		payment.setStatus(PaymentStatus.INIT.getValue());
41
 
42
		return paymentHandler.insertPayment(payment);
43
	}
44
 
45
	@Override
46
	public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
2291 chandransh 47
		int statusValue = -1;
48
		if(status != null)
49
			statusValue = status.getValue();
50
		else
51
			statusValue = -1;
52
		return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
1946 chandransh 53
	}
54
 
55
	@Override
56
	public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,	TException {
2291 chandransh 57
		int statusValue = -1;
58
		if(status != null)
59
			statusValue = status.getValue();
60
		else
61
			statusValue = -1;
62
		return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
1946 chandransh 63
	}
64
 
65
	@Override
66
	public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
2291 chandransh 67
		return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
1946 chandransh 68
	}
69
 
70
	@Override
71
	public Payment getPayment(long id) throws PaymentException, TException {
72
		return paymentHandler.getPayment(id).getThriftPayment();
73
	}
74
 
75
	@Override
76
	public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
77
		return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
78
	}
79
 
80
	@Override
81
	public boolean updatePaymentDetails(long id, String gatewayPaymentId,
82
			String sessionId, String gatewayTxnStatus, String description,
83
			String gatewayTxnId, String authCode, String referenceCode,
84
			String errorCode, PaymentStatus status, String gatewayTxnDate,
85
			List<Attribute> attributes) throws PaymentException, TException {
86
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
87
		payment.setGatewayPaymentId(gatewayPaymentId);
88
		payment.setSessionId(sessionId);
89
		payment.setGatewayTxnStatus(gatewayTxnStatus);
90
		payment.setDescription(description);
91
		payment.setGatewayTxnId(gatewayTxnId);
92
		payment.setAuthCode(authCode);
93
		payment.setReferenceCode(referenceCode);
94
		payment.setErrorCode(errorCode);
95
		if(status!=null){
96
			payment.setStatus(status.getValue());
97
			if(status.equals(PaymentStatus.SUCCESS))
98
				payment.setSuccessTimestamp(new Date());
99
			else if(status.equals(PaymentStatus.FAILED))
100
				payment.setErrorTimestamp(new Date());
101
		}
102
 
103
		payment.setGatewayTxnDate(gatewayTxnDate);
104
 
105
		Map<String, String> attrMap = new HashMap<String, String>();
2272 rajveer 106
		if(attributes != null){
107
			for(Attribute attribute : attributes){
108
				attrMap.put(attribute.getName(), attribute.getValue());
109
			}
1946 chandransh 110
		}
111
 
112
		paymentHandler.updatePayment(payment, attrMap);
113
		return true;
114
	}
115
 
116
	@Override
117
	public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
118
		List<Double> minMaxAmounts = new ArrayList<Double>();
119
		Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
120
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
121
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
122
		return minMaxAmounts;
123
	}
124
 
125
	private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
126
		List<Payment> payments = new ArrayList<Payment>();
127
		for(in.shop2020.payment.domain.Payment payment : daoPayments){
128
			payments.add(payment.getThriftPayment());
129
		}
130
		return payments;
131
	}
132
}