| 1946 |
chandransh |
1 |
package in.shop2020.payment.service.handler;
|
|
|
2 |
|
| 6050 |
anupam.sin |
3 |
import in.shop2020.model.v1.order.RechargeOrder;
|
| 3578 |
mandeep.dh |
4 |
import in.shop2020.payment.domain.Refund;
|
|
|
5 |
import in.shop2020.payment.handler.PaymentGatewayHandler;
|
|
|
6 |
import in.shop2020.payment.handler.PaymentHandler;
|
| 4008 |
mandeep.dh |
7 |
import in.shop2020.payment.handler.PaymentRequiringExtraProcessingHandler;
|
| 3578 |
mandeep.dh |
8 |
import in.shop2020.payment.handler.RefundHandler;
|
|
|
9 |
import in.shop2020.payments.Attribute;
|
| 4008 |
mandeep.dh |
10 |
import in.shop2020.payments.ExtraPaymentProcessingType;
|
| 3578 |
mandeep.dh |
11 |
import in.shop2020.payments.Payment;
|
|
|
12 |
import in.shop2020.payments.PaymentException;
|
|
|
13 |
import in.shop2020.payments.PaymentGateway;
|
|
|
14 |
import in.shop2020.payments.PaymentService.Iface;
|
|
|
15 |
import in.shop2020.payments.PaymentStatus;
|
| 6050 |
anupam.sin |
16 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 3578 |
mandeep.dh |
17 |
|
| 3010 |
chandransh |
18 |
import java.text.SimpleDateFormat;
|
| 1946 |
chandransh |
19 |
import java.util.ArrayList;
|
| 6448 |
rajveer |
20 |
import java.util.Arrays;
|
| 1946 |
chandransh |
21 |
import java.util.Date;
|
|
|
22 |
import java.util.HashMap;
|
|
|
23 |
import java.util.List;
|
|
|
24 |
import java.util.Map;
|
|
|
25 |
|
| 8618 |
rajveer |
26 |
import org.apache.commons.logging.Log;
|
|
|
27 |
import org.apache.commons.logging.LogFactory;
|
| 1946 |
chandransh |
28 |
import org.apache.thrift.TException;
|
|
|
29 |
import org.springframework.context.ApplicationContext;
|
|
|
30 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
|
31 |
|
|
|
32 |
public class PaymentServiceHandler implements Iface {
|
| 3010 |
chandransh |
33 |
|
| 8618 |
rajveer |
34 |
private static final Log logger = LogFactory.getLog(PaymentServiceHandler.class);
|
|
|
35 |
|
| 3010 |
chandransh |
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Enum of all statuses that can be returned by the HDFC gateway
|
|
|
39 |
*
|
|
|
40 |
* @author Chandranshu
|
|
|
41 |
*
|
|
|
42 |
*/
|
| 8907 |
rajveer |
43 |
public enum HdfcPaymentReturnStatus{
|
| 3010 |
chandransh |
44 |
APPROVED("APPROVED"),
|
|
|
45 |
NOT_APPROVED("NOT APPROVED"),
|
|
|
46 |
CAPTURED("CAPTURED"),
|
|
|
47 |
NOT_CAPTURED ("NOT CAPTURED"),
|
|
|
48 |
CANCELLED ("CANCELLED"),
|
|
|
49 |
DENIED_BY_RISK("DENIED BY RISK"),
|
| 8907 |
rajveer |
50 |
HOST_TIMEOUT("HOST TIMEOUT"),
|
|
|
51 |
SUCCESS("SUCCESS"),
|
|
|
52 |
FAILURE("FAILURE");
|
| 3010 |
chandransh |
53 |
private String value;
|
|
|
54 |
HdfcPaymentReturnStatus(String value) {
|
|
|
55 |
this.value = value;
|
|
|
56 |
}
|
|
|
57 |
public String value(){
|
|
|
58 |
return this.value;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
| 2391 |
chandransh |
62 |
public static final long PAYMENT_NOT_CREATED = -1;
|
|
|
63 |
|
| 3010 |
chandransh |
64 |
private static final long HDFC_GATEWAY_ID = 1;
|
|
|
65 |
private static final long EBS_GATEWAY_ID = 2;
|
| 8208 |
amar.kumar |
66 |
private static final long EBAY_GATEWAY_ID = 16;
|
| 8488 |
amar.kumar |
67 |
private static final long SNAPDEAL_GATEWAY_ID = 18;
|
| 7042 |
amar.kumar |
68 |
private static final List<Long> HDFC_EMI_GATEWAY_IDS = Arrays.asList(5L,10L,11L,12L,14L);
|
| 3010 |
chandransh |
69 |
|
| 4651 |
rajveer |
70 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
| 1946 |
chandransh |
71 |
PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
|
| 4008 |
mandeep.dh |
72 |
PaymentRequiringExtraProcessingHandler paymentRequiringExtraProcessingHandler =
|
|
|
73 |
(PaymentRequiringExtraProcessingHandler) context.getBean("paymentRequiringExtraProcessingHandler");
|
|
|
74 |
|
|
|
75 |
PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
|
| 2747 |
chandransh |
76 |
RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
|
| 1946 |
chandransh |
77 |
|
| 4651 |
rajveer |
78 |
public void setDataSourceUrl(String dbHost){
|
|
|
79 |
org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
|
|
|
80 |
ds.setUrl(dbHost);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public String getDataSourceUrl(){
|
|
|
84 |
org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
|
|
|
85 |
return ds.getUrl();
|
|
|
86 |
}
|
|
|
87 |
|
| 1946 |
chandransh |
88 |
@Override
|
| 6050 |
anupam.sin |
89 |
public long createPayment(long userId, double amount, long gatewayId, long txnId, boolean isDigital) throws PaymentException, TException {
|
| 3010 |
chandransh |
90 |
logger.info("Creating payment corresponding to our txn id:" + txnId);
|
| 1946 |
chandransh |
91 |
in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
|
|
|
92 |
payment.setUserId(userId);
|
|
|
93 |
payment.setAmount(amount);
|
|
|
94 |
payment.setGatewayId(gatewayId);
|
|
|
95 |
payment.setMerchantTxnId(txnId);
|
|
|
96 |
payment.setStatus(PaymentStatus.INIT.getValue());
|
| 6050 |
anupam.sin |
97 |
payment.setDigital(isDigital);
|
| 1946 |
chandransh |
98 |
|
|
|
99 |
return paymentHandler.insertPayment(payment);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
@Override
|
|
|
103 |
public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
|
| 3010 |
chandransh |
104 |
logger.info("Getting payments from " + fromTime + " to " + toTime + " for user: " + userId);
|
| 2291 |
chandransh |
105 |
int statusValue = -1;
|
|
|
106 |
if(status != null)
|
|
|
107 |
statusValue = status.getValue();
|
|
|
108 |
else
|
|
|
109 |
statusValue = -1;
|
|
|
110 |
return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
|
| 1946 |
chandransh |
111 |
}
|
|
|
112 |
|
|
|
113 |
@Override
|
|
|
114 |
public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
|
| 3010 |
chandransh |
115 |
logger.info("Getting payments from " + fromTime + " to " + toTime);
|
| 2291 |
chandransh |
116 |
int statusValue = -1;
|
|
|
117 |
if(status != null)
|
|
|
118 |
statusValue = status.getValue();
|
|
|
119 |
else
|
|
|
120 |
statusValue = -1;
|
|
|
121 |
return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
|
| 1946 |
chandransh |
122 |
}
|
| 4141 |
chandransh |
123 |
|
|
|
124 |
@Override
|
|
|
125 |
public List<Payment> getPaymentsByCapturedDate(long fromTime, long toTime, long gatewayId) throws PaymentException, TException {
|
|
|
126 |
logger.info("Getting payments from " + fromTime + " to " + toTime + " for " + gatewayId);
|
|
|
127 |
return getThriftPayments(paymentHandler.getPaymentsByCapturedDate(fromTime, toTime, gatewayId));
|
|
|
128 |
}
|
| 1946 |
chandransh |
129 |
|
|
|
130 |
@Override
|
|
|
131 |
public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
|
| 3010 |
chandransh |
132 |
logger.info("Getting payment gateway with id:" + id);
|
| 2291 |
chandransh |
133 |
return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
|
| 1946 |
chandransh |
134 |
}
|
| 4600 |
varun.gupt |
135 |
|
| 1946 |
chandransh |
136 |
@Override
|
| 4600 |
varun.gupt |
137 |
public List<PaymentGateway> getActivePaymentGateways() throws PaymentException, TException {
|
|
|
138 |
logger.info("Getting all active payment gateways");
|
|
|
139 |
return getThriftPaymentGateways(paymentGatewayHandler.getActivePaymentGateways());
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
@Override
|
| 1946 |
chandransh |
143 |
public Payment getPayment(long id) throws PaymentException, TException {
|
| 3010 |
chandransh |
144 |
logger.info("Getting payment with id: " + id);
|
| 1946 |
chandransh |
145 |
return paymentHandler.getPayment(id).getThriftPayment();
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
@Override
|
|
|
149 |
public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
|
| 3010 |
chandransh |
150 |
logger.info("Getting payment for the txn id: " + txnId);
|
| 1946 |
chandransh |
151 |
return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
|
|
|
152 |
}
|
| 4600 |
varun.gupt |
153 |
|
|
|
154 |
@Override
|
| 7049 |
anupam.sin |
155 |
public List<Payment> getPaymentForRechargeTxnId(long txnId) throws PaymentException, TException {
|
|
|
156 |
logger.info("Getting payment for the txn id: " + txnId);
|
|
|
157 |
return getThriftPayments(paymentHandler.getPaymentForRechargeTxn(txnId));
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
@Override
|
| 4600 |
varun.gupt |
161 |
public Payment getSuccessfulPaymentForTxnId(long txnId) throws PaymentException, TException {
|
|
|
162 |
|
|
|
163 |
for (Payment payment: getPaymentForTxnId(txnId)) {
|
|
|
164 |
if (payment.getStatus() == PaymentStatus.SUCCESS || payment.getStatus() == PaymentStatus.PARTIALLY_CAPTURED) {
|
|
|
165 |
return payment;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
return null;
|
|
|
169 |
}
|
| 1946 |
chandransh |
170 |
|
|
|
171 |
@Override
|
|
|
172 |
public boolean updatePaymentDetails(long id, String gatewayPaymentId,
|
|
|
173 |
String sessionId, String gatewayTxnStatus, String description,
|
|
|
174 |
String gatewayTxnId, String authCode, String referenceCode,
|
|
|
175 |
String errorCode, PaymentStatus status, String gatewayTxnDate,
|
|
|
176 |
List<Attribute> attributes) throws PaymentException, TException {
|
| 3010 |
chandransh |
177 |
logger.info("Updating details of payment id: " + id);
|
| 1946 |
chandransh |
178 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
|
|
|
179 |
payment.setGatewayPaymentId(gatewayPaymentId);
|
|
|
180 |
payment.setSessionId(sessionId);
|
|
|
181 |
payment.setGatewayTxnStatus(gatewayTxnStatus);
|
|
|
182 |
payment.setDescription(description);
|
|
|
183 |
payment.setGatewayTxnId(gatewayTxnId);
|
|
|
184 |
payment.setAuthCode(authCode);
|
|
|
185 |
payment.setReferenceCode(referenceCode);
|
|
|
186 |
payment.setErrorCode(errorCode);
|
|
|
187 |
if(status!=null){
|
|
|
188 |
payment.setStatus(status.getValue());
|
|
|
189 |
if(status.equals(PaymentStatus.SUCCESS))
|
|
|
190 |
payment.setSuccessTimestamp(new Date());
|
| 3578 |
mandeep.dh |
191 |
else if(status.equals(PaymentStatus.FAILED)) {
|
|
|
192 |
payment.setErrorTimestamp(new Date());
|
| 4421 |
mandeep.dh |
193 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
| 3578 |
mandeep.dh |
194 |
}
|
| 4421 |
mandeep.dh |
195 |
else if (status.equals(PaymentStatus.PROVISIONALLY_CAPTURED)) {
|
|
|
196 |
// FIXME different column to note provisional capture timestamp
|
|
|
197 |
// FIXME Requires extra processing at Crm end for actual manual capture
|
|
|
198 |
payment.setProvisionalCaptureTimestamp(new Date());
|
|
|
199 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.PENDING_CAPTURE);
|
|
|
200 |
}
|
| 1946 |
chandransh |
201 |
}
|
|
|
202 |
|
|
|
203 |
payment.setGatewayTxnDate(gatewayTxnDate);
|
|
|
204 |
|
|
|
205 |
Map<String, String> attrMap = new HashMap<String, String>();
|
| 2272 |
rajveer |
206 |
if(attributes != null){
|
|
|
207 |
for(Attribute attribute : attributes){
|
|
|
208 |
attrMap.put(attribute.getName(), attribute.getValue());
|
|
|
209 |
}
|
| 1946 |
chandransh |
210 |
}
|
|
|
211 |
|
|
|
212 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
213 |
return true;
|
|
|
214 |
}
|
|
|
215 |
|
| 3649 |
mandeep.dh |
216 |
/**
|
| 4008 |
mandeep.dh |
217 |
* Persists a given payment Id in another table for future processing by CRM etc.
|
|
|
218 |
* Failed payments generally require a follow-up to help customers through our
|
|
|
219 |
* CRM-Outbound team.
|
| 3649 |
mandeep.dh |
220 |
*
|
|
|
221 |
* @param payment the payment object that failed.
|
| 4421 |
mandeep.dh |
222 |
* @param type TODO
|
| 3649 |
mandeep.dh |
223 |
*/
|
| 4421 |
mandeep.dh |
224 |
private void persistPaymentRequiringExtraProcessing(in.shop2020.payment.domain.Payment payment, ExtraPaymentProcessingType type) {
|
| 4008 |
mandeep.dh |
225 |
try {
|
| 4421 |
mandeep.dh |
226 |
paymentRequiringExtraProcessingHandler.insert(payment.getId(), type);
|
| 4008 |
mandeep.dh |
227 |
} catch (Exception e) {
|
|
|
228 |
logger.error("Could not persist payment: " + payment.getId(), e);
|
| 3578 |
mandeep.dh |
229 |
}
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
@Override
|
| 1946 |
chandransh |
233 |
public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
|
| 3010 |
chandransh |
234 |
logger.info("Getting the range of successful payments.");
|
| 1946 |
chandransh |
235 |
List<Double> minMaxAmounts = new ArrayList<Double>();
|
|
|
236 |
Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
|
|
|
237 |
minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
|
|
|
238 |
minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
|
|
|
239 |
return minMaxAmounts;
|
|
|
240 |
}
|
|
|
241 |
|
| 6050 |
anupam.sin |
242 |
@Override
|
| 10269 |
amit.gupta |
243 |
public String initializeHdfcPayment(long merchantPaymentId, boolean isMobile) throws PaymentException, TException {
|
| 6050 |
anupam.sin |
244 |
logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
|
|
|
245 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
|
|
|
246 |
String redirectURL;
|
|
|
247 |
try {
|
| 10269 |
amit.gupta |
248 |
redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this, isMobile);
|
| 6050 |
anupam.sin |
249 |
} catch (Exception e) {
|
|
|
250 |
throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
|
|
|
251 |
}
|
|
|
252 |
return redirectURL;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
@Override
|
| 10269 |
amit.gupta |
256 |
public String doHdfcPaymentForDigitalOrder(long merchantPaymentId, long rechargeOrderId, String phone, boolean isMobile) throws PaymentException, TException {
|
| 6050 |
anupam.sin |
257 |
logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
|
| 10968 |
amit.gupta |
258 |
logger.info("doHdfcPaymentForDigitalOrder phone---- " + phone);
|
|
|
259 |
|
| 6050 |
anupam.sin |
260 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
|
|
|
261 |
TransactionClient tc = new TransactionClient();
|
|
|
262 |
String redirectURL;
|
|
|
263 |
RechargeOrder rechargeOrder;
|
|
|
264 |
try {
|
|
|
265 |
rechargeOrder = tc.getClient().getRechargeOrder(rechargeOrderId);
|
| 10269 |
amit.gupta |
266 |
redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, rechargeOrder, phone, this, isMobile);
|
| 6050 |
anupam.sin |
267 |
} catch (Exception e) {
|
|
|
268 |
throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
return redirectURL;
|
|
|
272 |
}
|
| 2391 |
chandransh |
273 |
|
|
|
274 |
@Override
|
| 10269 |
amit.gupta |
275 |
public String initializeHdfcEmiPayment(long merchantPaymentId, boolean isMobile) throws PaymentException, TException {
|
| 3616 |
chandransh |
276 |
logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
|
|
|
277 |
in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
|
|
|
278 |
String redirectURL;
|
|
|
279 |
try {
|
| 10470 |
amit.gupta |
280 |
redirectURL = HdfcEmiPaymentHandler.initializeHdfcPayment(payment, this, isMobile);
|
| 3616 |
chandransh |
281 |
} catch (Exception e) {
|
|
|
282 |
throw new PaymentException(102, "Error while initiliazing payment. Check service log for more details.");
|
|
|
283 |
}
|
|
|
284 |
return redirectURL;
|
|
|
285 |
}
|
|
|
286 |
|
| 6486 |
rajveer |
287 |
@Override
|
|
|
288 |
public synchronized boolean refundPayment(long merchantTxnId, double amount, boolean isDigital) throws PaymentException, TException {
|
| 6482 |
rajveer |
289 |
logger.info("Attempting to refund payment of amount " + amount + " corresponding to our transaction " + merchantTxnId);
|
| 8409 |
rajveer |
290 |
List<in.shop2020.payment.domain.Payment> payments;
|
|
|
291 |
if(isDigital){
|
|
|
292 |
payments = paymentHandler.getPaymentForRechargeTxn(merchantTxnId);
|
|
|
293 |
}else{
|
|
|
294 |
payments = paymentHandler.getPaymentForTxn(merchantTxnId);
|
|
|
295 |
}
|
| 6482 |
rajveer |
296 |
if(payments ==null || payments.isEmpty())
|
|
|
297 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
298 |
|
| 6486 |
rajveer |
299 |
if(payments ==null || payments.isEmpty())
|
|
|
300 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
301 |
|
| 6482 |
rajveer |
302 |
in.shop2020.payment.domain.Payment payment = payments.get(0);
|
| 6486 |
rajveer |
303 |
|
|
|
304 |
if(payment.getAmount() < amount){
|
|
|
305 |
logger.warn("Refund amount is more than payment amount.");
|
|
|
306 |
return false;
|
|
|
307 |
}
|
|
|
308 |
|
| 6482 |
rajveer |
309 |
switch(PaymentStatus.findByValue(payment.getStatus())){
|
|
|
310 |
case PENDING:
|
|
|
311 |
logger.error("Attempt to refund a non-authorized payment");
|
|
|
312 |
return false;
|
|
|
313 |
case INIT:
|
|
|
314 |
logger.warn("Attempt to refund a non-authorized payment");
|
|
|
315 |
return false;
|
|
|
316 |
case AUTHORIZED:
|
|
|
317 |
logger.warn("Attempt to refund a non-captured payment");
|
|
|
318 |
return false;
|
|
|
319 |
case CAPTURE_IN_PROCESS:
|
|
|
320 |
logger.warn("Attempt to refund a non-captured payment");
|
|
|
321 |
return false;
|
|
|
322 |
case PROVISIONALLY_CAPTURED:
|
|
|
323 |
logger.warn("Attempt to refund a non-captured payment");
|
|
|
324 |
return false;
|
| 6486 |
rajveer |
325 |
case REFUNDED:
|
|
|
326 |
logger.warn("Attempt to refund a refunded payment");
|
|
|
327 |
return false;
|
| 6482 |
rajveer |
328 |
case SUCCESS:
|
|
|
329 |
break;
|
|
|
330 |
case FAILED:
|
|
|
331 |
logger.error("Attempting to capture a failed payment");
|
|
|
332 |
return false;
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
long gatewayId = payment.getGatewayId();
|
|
|
336 |
|
|
|
337 |
if(gatewayId == HDFC_GATEWAY_ID){
|
| 6491 |
rajveer |
338 |
//Refund the HDFC payment
|
| 6482 |
rajveer |
339 |
return refundHdfcPayment(payment, amount);
|
| 6491 |
rajveer |
340 |
}else if (gatewayId == EBS_GATEWAY_ID){
|
|
|
341 |
//Refund the EBS payment
|
|
|
342 |
return refundEbsPayment(payment, amount);
|
| 6482 |
rajveer |
343 |
}
|
| 6491 |
rajveer |
344 |
else if (HDFC_EMI_GATEWAY_IDS.contains(gatewayId)){
|
|
|
345 |
//Capture and update the HDFC EMI payment
|
|
|
346 |
return refundHdfcEmiPayment(payment, amount);
|
|
|
347 |
}
|
| 6482 |
rajveer |
348 |
|
|
|
349 |
logger.error("We have an captured payment from unknown gateway: " + gatewayId);
|
|
|
350 |
return false;
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
|
| 3616 |
chandransh |
354 |
@Override
|
| 2689 |
chandransh |
355 |
public long createRefund(long orderId, long merchantTxnId, double amount) throws PaymentException, TException{
|
| 6482 |
rajveer |
356 |
logger.info("Attempting to create a refund for order: " + orderId);
|
| 6488 |
rajveer |
357 |
// if(!refundPayment(merchantTxnId, amount, false)){
|
|
|
358 |
// logger.warn("Not able to refund corresponding to the merchant txn " + merchantTxnId);
|
|
|
359 |
// }
|
| 2689 |
chandransh |
360 |
List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
|
|
|
361 |
if(payments ==null || payments.isEmpty())
|
|
|
362 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
363 |
|
| 3010 |
chandransh |
364 |
in.shop2020.payment.domain.Payment payment = payments.get(0);
|
| 2689 |
chandransh |
365 |
if(payment.getStatus() != PaymentStatus.SUCCESS.getValue())
|
|
|
366 |
throw new PaymentException(104, "No successful payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
367 |
|
| 2747 |
chandransh |
368 |
Refund refund = new Refund();
|
|
|
369 |
refund.setOrderId(orderId);
|
|
|
370 |
refund.setPaymentId(payment.getId());
|
|
|
371 |
refund.setGatewayId(payment.getGatewayId());
|
|
|
372 |
refund.setAmount(amount);
|
|
|
373 |
refund.setAttempts(0);
|
|
|
374 |
return refundHandler.createRefund(refund);
|
| 2689 |
chandransh |
375 |
}
|
|
|
376 |
|
| 3010 |
chandransh |
377 |
@Override
|
| 8618 |
rajveer |
378 |
public synchronized boolean capturePayment(long merchantTxnId, boolean isDigital) throws PaymentException, TException {
|
|
|
379 |
logger.info("Attempting to capture payment corresponding to our transaction " + merchantTxnId + " and Is Digital is:" + isDigital);
|
|
|
380 |
List<in.shop2020.payment.domain.Payment> payments;
|
|
|
381 |
if(isDigital){
|
|
|
382 |
payments = paymentHandler.getPaymentForRechargeTxn(merchantTxnId);
|
|
|
383 |
}else{
|
|
|
384 |
payments = paymentHandler.getPaymentForTxn(merchantTxnId);
|
|
|
385 |
}
|
| 3010 |
chandransh |
386 |
if(payments ==null || payments.isEmpty())
|
|
|
387 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
388 |
|
|
|
389 |
in.shop2020.payment.domain.Payment payment = payments.get(0);
|
|
|
390 |
switch(PaymentStatus.findByValue(payment.getStatus())){
|
|
|
391 |
case PENDING:
|
|
|
392 |
logger.error("Attempt to capture a non-authorized payment");
|
|
|
393 |
return false;
|
|
|
394 |
case INIT:
|
|
|
395 |
logger.warn("Attempt to capture a non-authorized payment");
|
|
|
396 |
return false;
|
|
|
397 |
case AUTHORIZED:
|
| 4421 |
mandeep.dh |
398 |
case CAPTURE_IN_PROCESS:
|
| 3010 |
chandransh |
399 |
//Actual work to be done in this case. Let the call proceed.
|
|
|
400 |
break;
|
| 4421 |
mandeep.dh |
401 |
case PROVISIONALLY_CAPTURED:
|
|
|
402 |
logger.info("Attempting to capture a payment that is provisonally captured but we can let the client proceed.");
|
|
|
403 |
return true;
|
| 3010 |
chandransh |
404 |
case SUCCESS:
|
|
|
405 |
logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
|
|
|
406 |
return true;
|
|
|
407 |
case FAILED:
|
|
|
408 |
logger.error("Attempting to capture a failed payment");
|
|
|
409 |
return false;
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
long gatewayId = payment.getGatewayId();
|
|
|
413 |
|
|
|
414 |
if(gatewayId == HDFC_GATEWAY_ID){
|
|
|
415 |
//Capture and update the HDFC payment
|
|
|
416 |
return captureAndUpdateHdfcPayment(payment);
|
|
|
417 |
} else if (gatewayId == EBS_GATEWAY_ID){
|
|
|
418 |
//Capture and update the EBS payment
|
|
|
419 |
return captureAndUpdateEbsPayment(payment);
|
| 6449 |
rajveer |
420 |
} else if (HDFC_EMI_GATEWAY_IDS.contains(gatewayId)){
|
| 3583 |
chandransh |
421 |
//Capture and update the HDFC EMI payment
|
|
|
422 |
return captureAndUpdateHdfcEmiPayment(payment);
|
| 8488 |
amar.kumar |
423 |
} else if (gatewayId == EBAY_GATEWAY_ID || gatewayId == SNAPDEAL_GATEWAY_ID) {
|
| 8208 |
amar.kumar |
424 |
return true;
|
| 3010 |
chandransh |
425 |
}
|
|
|
426 |
|
|
|
427 |
logger.error("We have an authorized payment from unknown gateway: " + gatewayId);
|
|
|
428 |
return false;
|
|
|
429 |
}
|
|
|
430 |
|
| 3956 |
chandransh |
431 |
@Override
|
|
|
432 |
public boolean partiallyCapturePayment(long merchantTxnId, double amount, String xferBy, String xferTxnId, long xferDate) throws PaymentException, TException {
|
|
|
433 |
logger.info("Attempting to partially capture payment corresponding to our transaction " + merchantTxnId);
|
|
|
434 |
List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
|
|
|
435 |
if(payments ==null || payments.isEmpty())
|
|
|
436 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
437 |
|
|
|
438 |
in.shop2020.payment.domain.Payment payment = payments.get(0);
|
|
|
439 |
switch(PaymentStatus.findByValue(payment.getStatus())){
|
|
|
440 |
case PENDING:
|
|
|
441 |
// COD payments lie in this state before settlement.
|
|
|
442 |
case INIT:
|
|
|
443 |
case PARTIALLY_CAPTURED:
|
|
|
444 |
case AUTHORIZED:
|
|
|
445 |
// COD payments would not be in this state but we are processing
|
|
|
446 |
// payments in this state as well for forward compatibility since
|
|
|
447 |
// someday we'd want to be able to capture authorized CC payments
|
|
|
448 |
// partially.
|
|
|
449 |
break;
|
|
|
450 |
case SUCCESS:
|
|
|
451 |
logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
|
|
|
452 |
return true;
|
|
|
453 |
case FAILED:
|
|
|
454 |
logger.error("Attempting to capture a failed payment");
|
|
|
455 |
return false;
|
|
|
456 |
}
|
|
|
457 |
SimpleDateFormat mysqlDateFormatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
458 |
String xferDateStr = mysqlDateFormatter.format(new Date(xferDate));
|
|
|
459 |
|
|
|
460 |
return settleAndUpdateCodPayment(payment, amount, xferBy, xferTxnId, xferDateStr);
|
|
|
461 |
}
|
|
|
462 |
|
| 3010 |
chandransh |
463 |
/**
|
|
|
464 |
* Capture the HDFC payment represented by the given payment object. If the
|
|
|
465 |
* capture attempt is not successful, we mark this payment as failed. We
|
|
|
466 |
* don't retry or anything. We'll add the support of multiple attempts later
|
|
|
467 |
* on.
|
|
|
468 |
*
|
|
|
469 |
* @param payment The payment which has to be captured.
|
|
|
470 |
* @return True if the payment attempt is successful, false if not.
|
| 4421 |
mandeep.dh |
471 |
* @throws PaymentException
|
| 3010 |
chandransh |
472 |
*/
|
| 4421 |
mandeep.dh |
473 |
private boolean captureAndUpdateHdfcPayment(in.shop2020.payment.domain.Payment payment) throws PaymentException {
|
| 3010 |
chandransh |
474 |
long merchantPaymentId = payment.getId();
|
|
|
475 |
logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
|
|
|
476 |
Map<String, String> captureResult = HdfcPaymentHandler.capturePayment(payment);
|
|
|
477 |
String captureStatus = captureResult.get(IPaymentHandler.STATUS);
|
|
|
478 |
String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
|
|
|
479 |
|
|
|
480 |
Map<String, String> attrMap = new HashMap<String, String>();
|
|
|
481 |
if (!captureStatus.trim().equals("0")
|
|
|
482 |
|| !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
|
|
|
483 |
// Failure
|
| 3616 |
chandransh |
484 |
logger.error("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
|
| 3010 |
chandransh |
485 |
String description = captureResult.get(IPaymentHandler.ERROR);
|
|
|
486 |
String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
|
|
|
487 |
|
|
|
488 |
payment.setDescription(description);
|
|
|
489 |
payment.setErrorCode(errorCode);
|
|
|
490 |
payment.setErrorTimestamp(new Date());
|
| 4421 |
mandeep.dh |
491 |
|
|
|
492 |
if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
|
|
|
493 |
payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
|
|
|
494 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
495 |
throw new PaymentException(106, "Could not capture due to connection issue");
|
|
|
496 |
}
|
|
|
497 |
else {
|
|
|
498 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
|
|
499 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
500 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
|
|
501 |
}
|
|
|
502 |
|
| 3010 |
chandransh |
503 |
return false;
|
|
|
504 |
} else {
|
|
|
505 |
// Success
|
| 3616 |
chandransh |
506 |
logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
|
| 3010 |
chandransh |
507 |
payment.setDescription("Payment Captured");
|
|
|
508 |
payment.setGatewayTxnStatus(gatewayStatus);
|
|
|
509 |
payment.setStatus(PaymentStatus.SUCCESS.getValue());
|
|
|
510 |
payment.setSuccessTimestamp(new Date());
|
|
|
511 |
|
|
|
512 |
attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
|
|
|
513 |
attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
|
|
|
514 |
attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
|
|
|
515 |
|
|
|
516 |
SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
517 |
attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
|
|
|
518 |
|
|
|
519 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
520 |
return true;
|
|
|
521 |
}
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
/**
|
| 3583 |
chandransh |
525 |
* Capture the HDFC EMI payment represented by the given payment object. If
|
|
|
526 |
* the capture attempt is not successful, we mark this payment as failed. We
|
|
|
527 |
* don't retry or anything. We'll add the support of multiple attempts later
|
|
|
528 |
* on.
|
|
|
529 |
*
|
|
|
530 |
* @param payment
|
|
|
531 |
* The payment which has to be captured.
|
|
|
532 |
* @return True if the payment attempt is successful, false if not.
|
| 4421 |
mandeep.dh |
533 |
* @throws PaymentException
|
| 3583 |
chandransh |
534 |
*/
|
| 4421 |
mandeep.dh |
535 |
private boolean captureAndUpdateHdfcEmiPayment(in.shop2020.payment.domain.Payment payment) throws PaymentException{
|
| 3583 |
chandransh |
536 |
long merchantPaymentId = payment.getId();
|
|
|
537 |
logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
|
|
|
538 |
Map<String, String> captureResult = HdfcEmiPaymentHandler.capturePayment(payment);
|
|
|
539 |
String captureStatus = captureResult.get(IPaymentHandler.STATUS);
|
|
|
540 |
String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
|
|
|
541 |
|
|
|
542 |
Map<String, String> attrMap = new HashMap<String, String>();
|
|
|
543 |
if (!captureStatus.trim().equals("0")
|
|
|
544 |
|| !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
|
|
|
545 |
// Failure
|
| 3616 |
chandransh |
546 |
logger.error("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
|
| 3583 |
chandransh |
547 |
String description = captureResult.get(IPaymentHandler.ERROR);
|
|
|
548 |
String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
|
|
|
549 |
|
|
|
550 |
payment.setDescription(description);
|
|
|
551 |
payment.setErrorCode(errorCode);
|
| 4421 |
mandeep.dh |
552 |
payment.setErrorTimestamp(new Date());
|
|
|
553 |
|
|
|
554 |
// Not marking payments as failed in case of connection issues
|
|
|
555 |
if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
|
|
|
556 |
payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
|
|
|
557 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
558 |
throw new PaymentException(106, "Could not capture due to connection issue");
|
|
|
559 |
}
|
|
|
560 |
else {
|
|
|
561 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
|
|
562 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
563 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
|
|
564 |
}
|
|
|
565 |
|
| 3583 |
chandransh |
566 |
return false;
|
|
|
567 |
} else {
|
|
|
568 |
// Success
|
| 3616 |
chandransh |
569 |
logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
|
| 3583 |
chandransh |
570 |
payment.setDescription("Payment Captured");
|
|
|
571 |
payment.setGatewayTxnStatus(gatewayStatus);
|
|
|
572 |
payment.setStatus(PaymentStatus.SUCCESS.getValue());
|
| 4421 |
mandeep.dh |
573 |
payment.setSuccessTimestamp(new Date());
|
| 3583 |
chandransh |
574 |
|
|
|
575 |
attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
|
|
|
576 |
attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
|
|
|
577 |
attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
|
|
|
578 |
|
|
|
579 |
SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
580 |
attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
|
|
|
581 |
|
|
|
582 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
583 |
return true;
|
|
|
584 |
}
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
/**
|
| 3010 |
chandransh |
588 |
* Capture the EBS payment represented by the given payment object. If the
|
|
|
589 |
* capture attempt is not successful, we mark this payment as failed. We
|
|
|
590 |
* don't retry or anything. We'll add the support of multiple attempts later
|
|
|
591 |
* on.
|
|
|
592 |
*
|
|
|
593 |
* @param payment The payment which has to be captured.
|
|
|
594 |
* @return True if the payment attempt is successful, false if not.
|
| 4421 |
mandeep.dh |
595 |
* @throws PaymentException
|
| 3010 |
chandransh |
596 |
*/
|
| 4421 |
mandeep.dh |
597 |
private boolean captureAndUpdateEbsPayment(in.shop2020.payment.domain.Payment payment) throws PaymentException{
|
| 3010 |
chandransh |
598 |
Map<String, String> captureResult = EbsPaymentHandler.capturePayment(payment);
|
|
|
599 |
String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
|
| 8650 |
anupam.sin |
600 |
for(String key : captureResult.keySet()) {
|
|
|
601 |
logger.info("key : " + key + " value : " + captureResult.get(key));
|
|
|
602 |
}
|
| 3010 |
chandransh |
603 |
|
| 8650 |
anupam.sin |
604 |
logger.info("capture status : " + captureStatus);
|
|
|
605 |
|
| 3010 |
chandransh |
606 |
Map<String, String> attrMap = new HashMap<String, String>();
|
|
|
607 |
if("".equals(captureStatus)){
|
|
|
608 |
//Failure
|
| 3616 |
chandransh |
609 |
logger.error("Capture attempt failed for EBS payment with id: " + payment.getId());
|
| 3010 |
chandransh |
610 |
String description = captureResult.get(EbsPaymentHandler.ERROR);
|
|
|
611 |
String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
|
| 4421 |
mandeep.dh |
612 |
|
| 3010 |
chandransh |
613 |
payment.setDescription(description);
|
|
|
614 |
payment.setErrorCode(errorCode);
|
|
|
615 |
payment.setErrorTimestamp(new Date());
|
| 4421 |
mandeep.dh |
616 |
|
|
|
617 |
if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
|
|
|
618 |
payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
|
|
|
619 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
620 |
throw new PaymentException(106, "Could not capture due to connection issue");
|
|
|
621 |
}
|
|
|
622 |
else {
|
|
|
623 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
|
|
624 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
625 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
|
|
626 |
}
|
|
|
627 |
|
| 3010 |
chandransh |
628 |
return false;
|
|
|
629 |
}else{
|
|
|
630 |
//Success
|
| 3616 |
chandransh |
631 |
logger.info("Capture attempt successful for EBS payment with id: " + payment.getId());
|
| 3010 |
chandransh |
632 |
payment.setGatewayTxnStatus(captureStatus);
|
|
|
633 |
payment.setStatus(PaymentStatus.SUCCESS.getValue());
|
|
|
634 |
payment.setSuccessTimestamp(new Date());
|
|
|
635 |
|
|
|
636 |
attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
|
|
|
637 |
attrMap.put(IPaymentHandler.CAPTURE_TIME, captureResult.get(IPaymentHandler.CAPTURE_TIME));
|
|
|
638 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
639 |
return true;
|
|
|
640 |
}
|
|
|
641 |
}
|
|
|
642 |
|
| 6482 |
rajveer |
643 |
|
| 3010 |
chandransh |
644 |
/**
|
| 6482 |
rajveer |
645 |
* Refund the HDFC payment represented by the given payment object. If the
|
|
|
646 |
* refund attempt is not successful, will not any action.
|
|
|
647 |
*
|
|
|
648 |
* @param payment The payment which has to be captured.
|
|
|
649 |
* @amount amount to be refunded
|
|
|
650 |
* @return True if the payment attempt is successful, false if not.
|
|
|
651 |
* @throws PaymentException
|
|
|
652 |
*/
|
|
|
653 |
private boolean refundHdfcPayment(in.shop2020.payment.domain.Payment payment, double amount) throws PaymentException {
|
|
|
654 |
long merchantPaymentId = payment.getId();
|
|
|
655 |
logger.info("Refunding HDFC payment with id: " + merchantPaymentId);
|
|
|
656 |
Map<String, String> refundResult = HdfcPaymentHandler.refundPayment(payment, amount);
|
| 6486 |
rajveer |
657 |
String refundStatus = refundResult.get(IPaymentHandler.STATUS);
|
| 6482 |
rajveer |
658 |
String gatewayStatus = refundResult.get(IPaymentHandler.GATEWAY_STATUS);
|
|
|
659 |
|
|
|
660 |
Map<String, String> attrMap = new HashMap<String, String>();
|
| 6486 |
rajveer |
661 |
if (!refundStatus.trim().equals("0")
|
| 6482 |
rajveer |
662 |
|| !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
|
| 6486 |
rajveer |
663 |
|
|
|
664 |
logger.error("Refund attempt failed for HDFC payment with id: " + merchantPaymentId);
|
|
|
665 |
String description = refundResult.get(IPaymentHandler.ERROR);
|
|
|
666 |
String errorCode = refundResult.get(IPaymentHandler.ERR_CODE);
|
|
|
667 |
|
|
|
668 |
payment.setDescription(description);
|
|
|
669 |
payment.setErrorCode(errorCode);
|
|
|
670 |
payment.setErrorTimestamp(new Date());
|
|
|
671 |
|
|
|
672 |
if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
|
|
|
673 |
//payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
|
|
|
674 |
//paymentHandler.updatePayment(payment, attrMap);
|
|
|
675 |
throw new PaymentException(106, "Could not capture due to connection issue. Try Later");
|
|
|
676 |
}
|
|
|
677 |
else {
|
| 6491 |
rajveer |
678 |
// payment.setStatus(PaymentStatus.FAILED.getValue());
|
|
|
679 |
// paymentHandler.updatePayment(payment, attrMap);
|
| 6486 |
rajveer |
680 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
|
|
681 |
}
|
|
|
682 |
|
| 6482 |
rajveer |
683 |
return false;
|
|
|
684 |
} else {
|
|
|
685 |
// Success
|
|
|
686 |
logger.info("Refund attempt successful for HDFC payment with id: " + merchantPaymentId);
|
|
|
687 |
payment.setDescription("Payment Refunded");
|
|
|
688 |
payment.setGatewayTxnStatus(gatewayStatus);
|
| 6503 |
rajveer |
689 |
payment.setStatus(PaymentStatus.REFUNDED.getValue());
|
|
|
690 |
payment.setRefundAmount(amount);
|
| 6482 |
rajveer |
691 |
|
| 6486 |
rajveer |
692 |
attrMap.put(IPaymentHandler.REFUND_TXN_ID, refundResult.get(IPaymentHandler.REFUND_TXN_ID));
|
|
|
693 |
attrMap.put(IPaymentHandler.REFUND_REF_ID, refundResult.get(IPaymentHandler.REFUND_REF_ID));
|
|
|
694 |
attrMap.put(IPaymentHandler.REFUND_AUTH_ID, refundResult.get(IPaymentHandler.REFUND_AUTH_ID));
|
| 6503 |
rajveer |
695 |
attrMap.put(IPaymentHandler.REFUND_AMNT, refundResult.get(IPaymentHandler.REFUND_AMNT));
|
|
|
696 |
|
| 6482 |
rajveer |
697 |
SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| 6486 |
rajveer |
698 |
attrMap.put(HdfcPaymentHandler.REFUND_TIME, captureTimeDateFormatter.format(new Date()));
|
| 6482 |
rajveer |
699 |
|
|
|
700 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
701 |
return true;
|
|
|
702 |
}
|
|
|
703 |
}
|
|
|
704 |
|
|
|
705 |
|
| 6491 |
rajveer |
706 |
/**
|
|
|
707 |
* Refund the HDFC EMI payment represented by the given payment object. If
|
|
|
708 |
* the capture attempt is not successful, we will not do anything.
|
|
|
709 |
*
|
|
|
710 |
* @param payment
|
|
|
711 |
* The payment which has to be captured.
|
|
|
712 |
* @return True if the payment attempt is successful, false if not.
|
|
|
713 |
* @throws PaymentException
|
|
|
714 |
*/
|
|
|
715 |
private boolean refundHdfcEmiPayment(in.shop2020.payment.domain.Payment payment, double amount) throws PaymentException{
|
|
|
716 |
long merchantPaymentId = payment.getId();
|
|
|
717 |
logger.info("Refunding HDFC payment with id: " + merchantPaymentId);
|
|
|
718 |
Map<String, String> refundResult = HdfcEmiPaymentHandler.refundPayment(payment, amount);
|
|
|
719 |
String refundStatus = refundResult.get(IPaymentHandler.STATUS);
|
|
|
720 |
String gatewayStatus = refundResult.get(IPaymentHandler.GATEWAY_STATUS);
|
|
|
721 |
|
|
|
722 |
Map<String, String> attrMap = new HashMap<String, String>();
|
|
|
723 |
if (!refundStatus.trim().equals("0")
|
|
|
724 |
|| !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
|
|
|
725 |
// Failure
|
|
|
726 |
logger.error("Refund attempt failed for HDFC payment with id: " + merchantPaymentId);
|
|
|
727 |
String description = refundResult.get(IPaymentHandler.ERROR);
|
|
|
728 |
String errorCode = refundResult.get(IPaymentHandler.ERR_CODE);
|
|
|
729 |
|
|
|
730 |
payment.setDescription(description);
|
|
|
731 |
payment.setErrorCode(errorCode);
|
|
|
732 |
payment.setErrorTimestamp(new Date());
|
|
|
733 |
|
|
|
734 |
// Not marking payments as failed in case of connection issues
|
|
|
735 |
if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
|
|
|
736 |
// payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
|
|
|
737 |
// paymentHandler.updatePayment(payment, attrMap);
|
|
|
738 |
throw new PaymentException(106, "Could not capture due to connection issue");
|
|
|
739 |
}
|
|
|
740 |
else {
|
|
|
741 |
// payment.setStatus(PaymentStatus.FAILED.getValue());
|
|
|
742 |
// paymentHandler.updatePayment(payment, attrMap);
|
|
|
743 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
return false;
|
|
|
747 |
} else {
|
|
|
748 |
// Success
|
|
|
749 |
logger.info("Refund attempt successful for HDFC payment with id: " + merchantPaymentId);
|
|
|
750 |
payment.setDescription("Payment Refunded");
|
|
|
751 |
payment.setGatewayTxnStatus(gatewayStatus);
|
|
|
752 |
payment.setStatus(PaymentStatus.REFUNDED.getValue());
|
| 6503 |
rajveer |
753 |
payment.setRefundAmount(amount);
|
| 6491 |
rajveer |
754 |
|
|
|
755 |
attrMap.put(IPaymentHandler.REFUND_TXN_ID, refundResult.get(IPaymentHandler.REFUND_TXN_ID));
|
|
|
756 |
attrMap.put(IPaymentHandler.REFUND_REF_ID, refundResult.get(IPaymentHandler.REFUND_REF_ID));
|
|
|
757 |
attrMap.put(IPaymentHandler.REFUND_AUTH_ID, refundResult.get(IPaymentHandler.REFUND_AUTH_ID));
|
| 6503 |
rajveer |
758 |
attrMap.put(IPaymentHandler.REFUND_AMNT, refundResult.get(IPaymentHandler.REFUND_AMNT));
|
|
|
759 |
|
| 6491 |
rajveer |
760 |
SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
761 |
attrMap.put(HdfcPaymentHandler.REFUND_TIME, captureTimeDateFormatter.format(new Date()));
|
|
|
762 |
|
|
|
763 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
764 |
return true;
|
|
|
765 |
}
|
|
|
766 |
}
|
| 6482 |
rajveer |
767 |
|
|
|
768 |
/**
|
| 6491 |
rajveer |
769 |
* Refund the EBS payment represented by the given payment object. If the
|
|
|
770 |
* capture attempt is not successful, we will ignore. We don't retry or anything.
|
|
|
771 |
* We'll add the support of multiple attempts later on.
|
|
|
772 |
*
|
|
|
773 |
* @param payment The payment which has to be captured.
|
|
|
774 |
* @amount Amount to be refunded
|
|
|
775 |
* @return True if the payment attempt is successful, false if not.
|
|
|
776 |
* @throws PaymentException
|
|
|
777 |
*/
|
|
|
778 |
private boolean refundEbsPayment(in.shop2020.payment.domain.Payment payment, double amount) throws PaymentException{
|
|
|
779 |
Map<String, String> refundResult = EbsPaymentHandler.refundPayment(payment, amount);
|
|
|
780 |
String refundStatus = refundResult.get(EbsPaymentHandler.STATUS);
|
|
|
781 |
|
|
|
782 |
Map<String, String> attrMap = new HashMap<String, String>();
|
|
|
783 |
if("".equals(refundStatus)){
|
|
|
784 |
//Failure
|
|
|
785 |
logger.error("Refund attempt failed for EBS payment with id: " + payment.getId());
|
|
|
786 |
String description = refundResult.get(EbsPaymentHandler.ERROR);
|
|
|
787 |
String errorCode = refundResult.get(EbsPaymentHandler.ERR_CODE);
|
|
|
788 |
|
|
|
789 |
payment.setDescription(description);
|
|
|
790 |
payment.setErrorCode(errorCode);
|
|
|
791 |
payment.setErrorTimestamp(new Date());
|
|
|
792 |
|
|
|
793 |
if (IPaymentHandler.Errors.CONN_FAILURE.code.equals(errorCode)) {
|
|
|
794 |
// payment.setStatus(PaymentStatus.CAPTURE_IN_PROCESS.getValue());
|
|
|
795 |
// paymentHandler.updatePayment(payment, attrMap);
|
|
|
796 |
throw new PaymentException(106, "Could not capture due to connection issue");
|
|
|
797 |
}
|
|
|
798 |
else {
|
|
|
799 |
// payment.setStatus(PaymentStatus.FAILED.getValue());
|
|
|
800 |
// paymentHandler.updatePayment(payment, attrMap);
|
|
|
801 |
persistPaymentRequiringExtraProcessing(payment, ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
|
|
802 |
}
|
|
|
803 |
|
|
|
804 |
return false;
|
|
|
805 |
}else{
|
|
|
806 |
//Success
|
|
|
807 |
logger.info("Refund attempt successful for EBS payment with id: " + payment.getId());
|
|
|
808 |
payment.setGatewayTxnStatus(refundStatus);
|
|
|
809 |
payment.setStatus(PaymentStatus.REFUNDED.getValue());
|
| 6503 |
rajveer |
810 |
payment.setRefundAmount(amount);
|
| 6491 |
rajveer |
811 |
|
|
|
812 |
attrMap.put(IPaymentHandler.REFUND_TXN_ID, refundResult.get(IPaymentHandler.REFUND_TXN_ID));
|
|
|
813 |
attrMap.put(IPaymentHandler.REFUND_TIME, refundResult.get(IPaymentHandler.REFUND_TIME));
|
|
|
814 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
815 |
return true;
|
|
|
816 |
}
|
|
|
817 |
}
|
|
|
818 |
|
|
|
819 |
|
|
|
820 |
/**
|
| 3956 |
chandransh |
821 |
* Updates the settlement details of COD payments. Sets payment status as
|
|
|
822 |
* either PARTIALLY CAPTURED or SUCCESS depending on whether the complete
|
|
|
823 |
* amount has been captured. Other parameters are set as attributes.
|
|
|
824 |
*
|
|
|
825 |
* @param payment
|
|
|
826 |
* The payment which needs to be updated.
|
|
|
827 |
* @param amount
|
|
|
828 |
* Amount that has been captured.
|
|
|
829 |
* @param xferBy
|
|
|
830 |
* Entity which transferred the money.
|
|
|
831 |
* @param xferTxnId
|
|
|
832 |
* Transaction Id of the transfer.
|
|
|
833 |
* @param xferDateStr
|
|
|
834 |
* Date on which the transfer took place.
|
|
|
835 |
* @return true if the payment details were updated successfully.
|
|
|
836 |
* @throws PaymentException
|
|
|
837 |
* if the captured amount will become more than the actual
|
|
|
838 |
* amount after this update.
|
|
|
839 |
*/
|
|
|
840 |
private boolean settleAndUpdateCodPayment(in.shop2020.payment.domain.Payment payment, double amount, String xferBy, String xferTxnId, String xferDateStr) throws PaymentException{
|
|
|
841 |
Map<String, String> attrMap = payment.getAttributeMap();
|
|
|
842 |
|
|
|
843 |
double captureAmount = 0;
|
|
|
844 |
String captureAmntStr = attrMap.get(IPaymentHandler.CAPTURE_AMNT);
|
|
|
845 |
if(captureAmntStr != null)
|
|
|
846 |
captureAmount = Double.parseDouble(captureAmntStr);
|
|
|
847 |
captureAmount += amount;
|
| 5051 |
rajveer |
848 |
// If capture amount higher than payment amount by more than 50 paisa,
|
|
|
849 |
// there is some issue and we should raise exception.
|
|
|
850 |
if(captureAmount - payment.getAmount() > 0.5){
|
|
|
851 |
throw new PaymentException(105, "We've got a settlement request for an amount which is more than the transaction value.");
|
|
|
852 |
}
|
| 3956 |
chandransh |
853 |
|
| 5051 |
rajveer |
854 |
// If capture amount differs from payment amount by less than 50 paisa, lets mark the payment as successful.
|
|
|
855 |
// Else we can safely assume there will be some more orders for the payment, leading to make the payment as partially captured.
|
|
|
856 |
if(Math.abs(captureAmount - payment.getAmount()) < 0.5){
|
|
|
857 |
payment.setStatus(PaymentStatus.SUCCESS.getValue());
|
|
|
858 |
}else {
|
|
|
859 |
payment.setStatus(PaymentStatus.PARTIALLY_CAPTURED.getValue());
|
| 3956 |
chandransh |
860 |
}
|
|
|
861 |
payment.setSuccessTimestamp(new Date());
|
|
|
862 |
attrMap.put(IPaymentHandler.CAPTURE_AMNT, captureAmount + "");
|
|
|
863 |
attrMap.put(IPaymentHandler.XFER_TXN_ID, xferTxnId);
|
|
|
864 |
attrMap.put(IPaymentHandler.XFER_TXN_DATE, xferDateStr);
|
|
|
865 |
attrMap.put(IPaymentHandler.XFER_BY, xferBy);
|
|
|
866 |
paymentHandler.updatePayment(payment, attrMap);
|
|
|
867 |
return true;
|
|
|
868 |
}
|
|
|
869 |
|
|
|
870 |
/**
|
| 3010 |
chandransh |
871 |
* Creates a list of thrift payment objects corresponding to a list of
|
|
|
872 |
* payment data objects.
|
|
|
873 |
*
|
|
|
874 |
* @param daoPayments
|
|
|
875 |
* A list of payment DAO.
|
|
|
876 |
* @return A list of Thrift payment objects.
|
|
|
877 |
*/
|
|
|
878 |
private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
|
|
|
879 |
|
|
|
880 |
List<Payment> payments = new ArrayList<Payment>();
|
|
|
881 |
for(in.shop2020.payment.domain.Payment payment : daoPayments){
|
|
|
882 |
payments.add(payment.getThriftPayment());
|
|
|
883 |
}
|
|
|
884 |
return payments;
|
|
|
885 |
}
|
| 3375 |
rajveer |
886 |
|
| 4600 |
varun.gupt |
887 |
/**
|
|
|
888 |
* Creates a list of thrift payment gateway objects corresponding to a list of
|
|
|
889 |
* payment gateway data objects.
|
|
|
890 |
*
|
|
|
891 |
* @param daoPaymentGateways
|
|
|
892 |
* A list of payment gateway DAO.
|
|
|
893 |
* @return A list of Thrift payment gateway objects.
|
|
|
894 |
*/
|
|
|
895 |
private List<PaymentGateway> getThriftPaymentGateways(List<in.shop2020.payment.domain.PaymentGateway> daoPaymentGateways){
|
|
|
896 |
|
|
|
897 |
List<PaymentGateway> paymentGateways = new ArrayList<PaymentGateway>();
|
|
|
898 |
for(in.shop2020.payment.domain.PaymentGateway paymentGateway : daoPaymentGateways){
|
|
|
899 |
paymentGateways.add(paymentGateway.getThriftPaymentGateway());
|
|
|
900 |
}
|
|
|
901 |
return paymentGateways;
|
|
|
902 |
}
|
| 4619 |
mandeep.dh |
903 |
|
| 3375 |
rajveer |
904 |
@Override
|
| 4619 |
mandeep.dh |
905 |
public boolean isAlive() {
|
|
|
906 |
try {
|
|
|
907 |
return !paymentGatewayHandler.getActivePaymentGateways().isEmpty();
|
|
|
908 |
} catch (Exception e) {
|
|
|
909 |
logger.error("Could not fetch payment gateways", e);
|
|
|
910 |
}
|
|
|
911 |
|
|
|
912 |
return false;
|
| 3375 |
rajveer |
913 |
}
|
| 3956 |
chandransh |
914 |
|
|
|
915 |
|
|
|
916 |
@Override
|
|
|
917 |
public void closeSession() throws TException {
|
|
|
918 |
// TODO Auto-generated method stub
|
|
|
919 |
}
|
| 4008 |
mandeep.dh |
920 |
|
|
|
921 |
@Override
|
|
|
922 |
public List<Long> getPaymentsRequiringExtraProcessing (
|
|
|
923 |
ExtraPaymentProcessingType category) throws TException {
|
|
|
924 |
return paymentRequiringExtraProcessingHandler.getPaymentIds(category);
|
|
|
925 |
}
|
|
|
926 |
|
|
|
927 |
@Override
|
|
|
928 |
public void markPaymentAsProcessed(long paymentId,
|
|
|
929 |
ExtraPaymentProcessingType category) throws TException {
|
|
|
930 |
paymentRequiringExtraProcessingHandler.delete(paymentId, category);
|
|
|
931 |
}
|
| 4141 |
chandransh |
932 |
|
| 8907 |
rajveer |
933 |
@Override
|
| 8914 |
rajveer |
934 |
public PaymentStatus getPaymentStatusAtGateway(long merchantTxnId, double amount, boolean isDigital) throws PaymentException, TException {
|
| 8907 |
rajveer |
935 |
logger.info("Attempting to get status of payment of amount " + amount + " corresponding to our transaction " + merchantTxnId);
|
|
|
936 |
List<in.shop2020.payment.domain.Payment> payments;
|
|
|
937 |
if(isDigital){
|
|
|
938 |
payments = paymentHandler.getPaymentForRechargeTxn(merchantTxnId);
|
|
|
939 |
}else{
|
|
|
940 |
payments = paymentHandler.getPaymentForTxn(merchantTxnId);
|
|
|
941 |
}
|
|
|
942 |
if(payments ==null || payments.isEmpty())
|
|
|
943 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
944 |
|
|
|
945 |
if(payments ==null || payments.isEmpty())
|
|
|
946 |
throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
|
|
|
947 |
|
|
|
948 |
in.shop2020.payment.domain.Payment payment = payments.get(0);
|
|
|
949 |
long gatewayId = payment.getGatewayId();
|
|
|
950 |
|
|
|
951 |
if(gatewayId == HDFC_GATEWAY_ID){
|
|
|
952 |
return HdfcPaymentHandler.validateHdfcPayment(payment, amount);
|
|
|
953 |
}else if (gatewayId == EBS_GATEWAY_ID){
|
|
|
954 |
//return validateEbsPayment(payment, amount);
|
|
|
955 |
}
|
|
|
956 |
else if (HDFC_EMI_GATEWAY_IDS.contains(gatewayId)){
|
|
|
957 |
//return validateHdfcEmiPayment(payment, amount);
|
|
|
958 |
}
|
|
|
959 |
|
|
|
960 |
logger.error("We have an payment from unknown gateway: " + gatewayId);
|
| 8914 |
rajveer |
961 |
return PaymentStatus.INIT;
|
| 8907 |
rajveer |
962 |
}
|
|
|
963 |
|
|
|
964 |
|
| 1946 |
chandransh |
965 |
}
|