| 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 {
|
| 2391 |
chandransh |
23 |
public static final long PAYMENT_NOT_CREATED = -1;
|
|
|
24 |
private static final String FLAG_KEY = "IsFlagged";
|
|
|
25 |
private static final String TXN_KEY = "TransactionID";
|
|
|
26 |
private static final String AUTH_TXN_ID = "AuthTxnId";
|
|
|
27 |
private static final String CAPTURE_TXN_ID = "CaptureTxnId";
|
|
|
28 |
private static final String CAPTURE_TIME = "CaptureTime";
|
|
|
29 |
|
| 1946 |
chandransh |
30 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
|
|
31 |
PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
|
|
|
32 |
PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
|
|
|
33 |
|
|
|
34 |
@Override
|
|
|
35 |
public void closeSession() throws TException {
|
|
|
36 |
// TODO Auto-generated method stub
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
@Override
|
|
|
40 |
public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
|
|
|
41 |
in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
|
|
|
42 |
payment.setUserId(userId);
|
|
|
43 |
payment.setAmount(amount);
|
|
|
44 |
payment.setGatewayId(gatewayId);
|
|
|
45 |
payment.setMerchantTxnId(txnId);
|
|
|
46 |
payment.setStatus(PaymentStatus.INIT.getValue());
|
|
|
47 |
|
|
|
48 |
return paymentHandler.insertPayment(payment);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
@Override
|
|
|
52 |
public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
|
| 2291 |
chandransh |
53 |
int statusValue = -1;
|
|
|
54 |
if(status != null)
|
|
|
55 |
statusValue = status.getValue();
|
|
|
56 |
else
|
|
|
57 |
statusValue = -1;
|
|
|
58 |
return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
|
| 1946 |
chandransh |
59 |
}
|
|
|
60 |
|
|
|
61 |
@Override
|
|
|
62 |
public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
|
| 2291 |
chandransh |
63 |
int statusValue = -1;
|
|
|
64 |
if(status != null)
|
|
|
65 |
statusValue = status.getValue();
|
|
|
66 |
else
|
|
|
67 |
statusValue = -1;
|
|
|
68 |
return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
|
| 1946 |
chandransh |
69 |
}
|
|
|
70 |
|
|
|
71 |
@Override
|
|
|
72 |
public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
|
| 2291 |
chandransh |
73 |
return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
|
| 1946 |
chandransh |
74 |
}
|
|
|
75 |
|
|
|
76 |
@Override
|
|
|
77 |
public Payment getPayment(long id) throws PaymentException, TException {
|
|
|
78 |
return paymentHandler.getPayment(id).getThriftPayment();
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
@Override
|
|
|
82 |
public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
|
|
|
83 |
return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
@Override
|
|
|
87 |
public boolean updatePaymentDetails(long id, String gatewayPaymentId,
|
|
|
88 |
String sessionId, String gatewayTxnStatus, String description,
|
|
|
89 |
String gatewayTxnId, String authCode, String referenceCode,
|
|
|
90 |
String errorCode, PaymentStatus status, String gatewayTxnDate,
|
|
|
91 |
List<Attribute> attributes) throws PaymentException, TException {
|
|
|
92 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
|
|
|
93 |
payment.setGatewayPaymentId(gatewayPaymentId);
|
|
|
94 |
payment.setSessionId(sessionId);
|
|
|
95 |
payment.setGatewayTxnStatus(gatewayTxnStatus);
|
|
|
96 |
payment.setDescription(description);
|
|
|
97 |
payment.setGatewayTxnId(gatewayTxnId);
|
|
|
98 |
payment.setAuthCode(authCode);
|
|
|
99 |
payment.setReferenceCode(referenceCode);
|
|
|
100 |
payment.setErrorCode(errorCode);
|
|
|
101 |
if(status!=null){
|
|
|
102 |
payment.setStatus(status.getValue());
|
|
|
103 |
if(status.equals(PaymentStatus.SUCCESS))
|
|
|
104 |
payment.setSuccessTimestamp(new Date());
|
|
|
105 |
else if(status.equals(PaymentStatus.FAILED))
|
|
|
106 |
payment.setErrorTimestamp(new Date());
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
payment.setGatewayTxnDate(gatewayTxnDate);
|
|
|
110 |
|
|
|
111 |
Map<String, String> attrMap = new HashMap<String, String>();
|
| 2272 |
rajveer |
112 |
if(attributes != null){
|
|
|
113 |
for(Attribute attribute : attributes){
|
|
|
114 |
attrMap.put(attribute.getName(), attribute.getValue());
|
|
|
115 |
}
|
| 1946 |
chandransh |
116 |
}
|
|
|
117 |
|
|
|
118 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
119 |
return true;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
@Override
|
|
|
123 |
public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
|
|
|
124 |
List<Double> minMaxAmounts = new ArrayList<Double>();
|
|
|
125 |
Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
|
|
|
126 |
minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
|
|
|
127 |
minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
|
|
|
128 |
return minMaxAmounts;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
|
|
|
132 |
List<Payment> payments = new ArrayList<Payment>();
|
|
|
133 |
for(in.shop2020.payment.domain.Payment payment : daoPayments){
|
|
|
134 |
payments.add(payment.getThriftPayment());
|
|
|
135 |
}
|
|
|
136 |
return payments;
|
|
|
137 |
}
|
| 2391 |
chandransh |
138 |
|
|
|
139 |
@Override
|
|
|
140 |
public Payment updateAndCaptureEbsPayment(Map<String, String> paymentParams) throws PaymentException, TException {
|
|
|
141 |
long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
|
|
|
142 |
String gatewayPaymentId = paymentParams.get("PaymentID");
|
|
|
143 |
double amount = Double.parseDouble(paymentParams.get("Amount"));
|
|
|
144 |
String isFlagged = paymentParams.get(FLAG_KEY);
|
|
|
145 |
String gatewayTxnStatus = paymentParams.get("ResponseCode");
|
|
|
146 |
String description = paymentParams.get("ResponseMessage");
|
|
|
147 |
String authTxnId = paymentParams.get(TXN_KEY);
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
List<Attribute> attributes = new ArrayList<Attribute>();
|
|
|
151 |
attributes.add(new Attribute(FLAG_KEY, isFlagged));
|
|
|
152 |
attributes.add(new Attribute(AUTH_TXN_ID, authTxnId));
|
|
|
153 |
|
|
|
154 |
Payment payment = null;
|
|
|
155 |
try {
|
|
|
156 |
payment = getPayment(merchantPaymentId);
|
|
|
157 |
} catch (PaymentException e1) {
|
|
|
158 |
throw new PaymentException(e1);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
if(!validatePaymentParams(amount, payment)){
|
|
|
162 |
throw new PaymentException(102, "Checks and balance failed on returned data");
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if(gatewayTxnStatus.equals("0")){
|
|
|
166 |
//Update payment status as authorized
|
|
|
167 |
updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
|
|
|
168 |
|
|
|
169 |
Map<String, String> captureResult = EbsPaymentHandler.capturePayment(amount, "" + gatewayPaymentId);
|
|
|
170 |
|
|
|
171 |
String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
|
|
|
172 |
|
|
|
173 |
if("".equals(captureStatus)){
|
|
|
174 |
//Failure
|
|
|
175 |
description = captureResult.get(EbsPaymentHandler.ERROR);
|
|
|
176 |
String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
|
|
|
177 |
|
|
|
178 |
updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", errorCode, PaymentStatus.FAILED, "", attributes);
|
|
|
179 |
}else{
|
|
|
180 |
//Success
|
|
|
181 |
attributes.add(new Attribute(CAPTURE_TXN_ID, captureResult.get(EbsPaymentHandler.TXN_ID)));
|
|
|
182 |
attributes.add(new Attribute(CAPTURE_TIME, captureResult.get(EbsPaymentHandler.DATE_TIME)));
|
|
|
183 |
|
|
|
184 |
updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", captureStatus, description, "", "", "", "", PaymentStatus.SUCCESS, "", attributes);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
}else{
|
|
|
189 |
updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.FAILED, "", attributes);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
payment = getPayment(merchantPaymentId);
|
|
|
193 |
|
|
|
194 |
return payment;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
@Override
|
|
|
198 |
public String initializeHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
|
|
|
199 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
|
|
|
200 |
String redirectURL;
|
|
|
201 |
try {
|
|
|
202 |
redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this);
|
|
|
203 |
} catch (Exception e) {
|
|
|
204 |
throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
|
|
|
205 |
}
|
|
|
206 |
return redirectURL;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
@Override
|
|
|
210 |
public Map<String, String> captureHdfcPayment(long merchantPaymentId){
|
|
|
211 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
|
|
|
212 |
return HdfcPaymentHandler.capturePayment(payment);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
private boolean validatePaymentParams(double amount, Payment payment){
|
|
|
216 |
if(payment==null || payment.getAmount()!= amount){
|
|
|
217 |
// We did not request this payment or the authorised amount is different.
|
|
|
218 |
return false;
|
|
|
219 |
}
|
|
|
220 |
return true;
|
|
|
221 |
}
|
| 1946 |
chandransh |
222 |
}
|