| Line 1... |
Line 1... |
| 1 |
package in.shop2020.payment.service.handler;
|
1 |
package in.shop2020.payment.service.handler;
|
| 2 |
|
2 |
|
| 3 |
import in.shop2020.crm.CRMService.Client;
|
- |
|
| 4 |
import in.shop2020.payment.domain.Refund;
|
3 |
import in.shop2020.payment.domain.Refund;
|
| 5 |
import in.shop2020.payment.handler.PaymentGatewayHandler;
|
4 |
import in.shop2020.payment.handler.PaymentGatewayHandler;
|
| 6 |
import in.shop2020.payment.handler.PaymentHandler;
|
5 |
import in.shop2020.payment.handler.PaymentHandler;
|
| - |
|
6 |
import in.shop2020.payment.handler.PaymentRequiringExtraProcessingHandler;
|
| 7 |
import in.shop2020.payment.handler.RefundHandler;
|
7 |
import in.shop2020.payment.handler.RefundHandler;
|
| 8 |
import in.shop2020.payments.Attribute;
|
8 |
import in.shop2020.payments.Attribute;
|
| - |
|
9 |
import in.shop2020.payments.ExtraPaymentProcessingType;
|
| 9 |
import in.shop2020.payments.Payment;
|
10 |
import in.shop2020.payments.Payment;
|
| 10 |
import in.shop2020.payments.PaymentException;
|
11 |
import in.shop2020.payments.PaymentException;
|
| 11 |
import in.shop2020.payments.PaymentGateway;
|
12 |
import in.shop2020.payments.PaymentGateway;
|
| 12 |
import in.shop2020.payments.PaymentService.Iface;
|
13 |
import in.shop2020.payments.PaymentService.Iface;
|
| 13 |
import in.shop2020.payments.PaymentStatus;
|
14 |
import in.shop2020.payments.PaymentStatus;
|
| 14 |
import in.shop2020.thrift.clients.CRMClient;
|
- |
|
| 15 |
|
15 |
|
| 16 |
import java.text.SimpleDateFormat;
|
16 |
import java.text.SimpleDateFormat;
|
| 17 |
import java.util.ArrayList;
|
17 |
import java.util.ArrayList;
|
| 18 |
import java.util.Date;
|
18 |
import java.util.Date;
|
| 19 |
import java.util.HashMap;
|
19 |
import java.util.HashMap;
|
| 20 |
import java.util.List;
|
20 |
import java.util.List;
|
| 21 |
import java.util.Map;
|
21 |
import java.util.Map;
|
| 22 |
|
22 |
|
| 23 |
import org.apache.thrift.TException;
|
23 |
import org.apache.thrift.TException;
|
| 24 |
import org.apache.thrift.transport.TTransportException;
|
- |
|
| 25 |
import org.slf4j.Logger;
|
24 |
import org.slf4j.Logger;
|
| 26 |
import org.slf4j.LoggerFactory;
|
25 |
import org.slf4j.LoggerFactory;
|
| 27 |
import org.springframework.context.ApplicationContext;
|
26 |
import org.springframework.context.ApplicationContext;
|
| 28 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
27 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
| 29 |
|
28 |
|
| Line 60... |
Line 59... |
| 60 |
private static final long EBS_GATEWAY_ID = 2;
|
59 |
private static final long EBS_GATEWAY_ID = 2;
|
| 61 |
private static final long HDFC_EMI_GATEWAY_ID = 5;
|
60 |
private static final long HDFC_EMI_GATEWAY_ID = 5;
|
| 62 |
|
61 |
|
| 63 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
62 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
| 64 |
PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
|
63 |
PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
|
| - |
|
64 |
PaymentRequiringExtraProcessingHandler paymentRequiringExtraProcessingHandler =
|
| - |
|
65 |
(PaymentRequiringExtraProcessingHandler) context.getBean("paymentRequiringExtraProcessingHandler");
|
| - |
|
66 |
|
| 65 |
PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
|
67 |
PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
|
| 66 |
RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
|
68 |
RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
|
| 67 |
|
69 |
|
| 68 |
@Override
|
70 |
@Override
|
| 69 |
public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
|
71 |
public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
|
| 70 |
logger.info("Creating payment corresponding to our txn id:" + txnId);
|
72 |
logger.info("Creating payment corresponding to our txn id:" + txnId);
|
| Line 138... |
Line 140... |
| 138 |
payment.setStatus(status.getValue());
|
140 |
payment.setStatus(status.getValue());
|
| 139 |
if(status.equals(PaymentStatus.SUCCESS))
|
141 |
if(status.equals(PaymentStatus.SUCCESS))
|
| 140 |
payment.setSuccessTimestamp(new Date());
|
142 |
payment.setSuccessTimestamp(new Date());
|
| 141 |
else if(status.equals(PaymentStatus.FAILED)) {
|
143 |
else if(status.equals(PaymentStatus.FAILED)) {
|
| 142 |
payment.setErrorTimestamp(new Date());
|
144 |
payment.setErrorTimestamp(new Date());
|
| 143 |
createTicketForFailedPayment(payment);
|
145 |
persistPaymentRequiringExtraProcessing(payment);
|
| 144 |
}
|
146 |
}
|
| 145 |
}
|
147 |
}
|
| 146 |
|
148 |
|
| 147 |
payment.setGatewayTxnDate(gatewayTxnDate);
|
149 |
payment.setGatewayTxnDate(gatewayTxnDate);
|
| 148 |
|
150 |
|
| Line 156... |
Line 158... |
| 156 |
paymentHandler.updatePayment(payment, attrMap);
|
158 |
paymentHandler.updatePayment(payment, attrMap);
|
| 157 |
return true;
|
159 |
return true;
|
| 158 |
}
|
160 |
}
|
| 159 |
|
161 |
|
| 160 |
/**
|
162 |
/**
|
| 161 |
* Creates a ticket in CRM tool for each customer whose payment got failed.
|
163 |
* Persists a given payment Id in another table for future processing by CRM etc.
|
| 162 |
* Later, CRM agents follow-up with the users and try making a sale.
|
164 |
* Failed payments generally require a follow-up to help customers through our
|
| 163 |
*
|
- |
|
| 164 |
* We swallow the exceptions raised and just log them at error level. This is
|
- |
|
| 165 |
* done to not make Website unaffected by any CRM related issues.
|
165 |
* CRM-Outbound team.
|
| 166 |
*
|
166 |
*
|
| 167 |
* @param payment the payment object that failed.
|
167 |
* @param payment the payment object that failed.
|
| 168 |
*/
|
168 |
*/
|
| 169 |
private void createTicketForFailedPayment(in.shop2020.payment.domain.Payment payment) {
|
169 |
private void persistPaymentRequiringExtraProcessing(in.shop2020.payment.domain.Payment payment) {
|
| 170 |
try {
|
170 |
try {
|
| 171 |
Client crmClient = new CRMClient().getClient();
|
- |
|
| 172 |
|
- |
|
| 173 |
// This call is aync (oneway). This ensures that website response
|
- |
|
| 174 |
// is sent in time.
|
- |
|
| 175 |
crmClient.processPaymentFailure(payment.getUserId());
|
171 |
paymentRequiringExtraProcessingHandler.insert(payment.getId(), ExtraPaymentProcessingType.FAILED_PAYMENTS);
|
| 176 |
} catch (TTransportException e) {
|
- |
|
| 177 |
logger.error("Could not create CRM client", e);
|
- |
|
| 178 |
} catch (TException e) {
|
172 |
} catch (Exception e) {
|
| 179 |
logger.error("Could not process paymentId: " + payment.getId(), e);
|
173 |
logger.error("Could not persist payment: " + payment.getId(), e);
|
| 180 |
}
|
174 |
}
|
| 181 |
}
|
175 |
}
|
| 182 |
|
176 |
|
| 183 |
@Override
|
177 |
@Override
|
| 184 |
public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
|
178 |
public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
|
| Line 338... |
Line 332... |
| 338 |
payment.setDescription(description);
|
332 |
payment.setDescription(description);
|
| 339 |
payment.setErrorCode(errorCode);
|
333 |
payment.setErrorCode(errorCode);
|
| 340 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
334 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
| 341 |
payment.setErrorTimestamp(new Date());
|
335 |
payment.setErrorTimestamp(new Date());
|
| 342 |
paymentHandler.updatePayment(payment, attrMap);
|
336 |
paymentHandler.updatePayment(payment, attrMap);
|
| 343 |
createTicketForFailedPayment(payment);
|
337 |
persistPaymentRequiringExtraProcessing(payment);
|
| 344 |
return false;
|
338 |
return false;
|
| 345 |
} else {
|
339 |
} else {
|
| 346 |
// Success
|
340 |
// Success
|
| 347 |
logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
|
341 |
logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
|
| 348 |
payment.setDescription("Payment Captured");
|
342 |
payment.setDescription("Payment Captured");
|
| Line 390... |
Line 384... |
| 390 |
payment.setDescription(description);
|
384 |
payment.setDescription(description);
|
| 391 |
payment.setErrorCode(errorCode);
|
385 |
payment.setErrorCode(errorCode);
|
| 392 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
386 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
| 393 |
payment.setErrorTimestamp(new Date());
|
387 |
payment.setErrorTimestamp(new Date());
|
| 394 |
paymentHandler.updatePayment(payment, attrMap);
|
388 |
paymentHandler.updatePayment(payment, attrMap);
|
| 395 |
createTicketForFailedPayment(payment);
|
389 |
persistPaymentRequiringExtraProcessing(payment);
|
| 396 |
return false;
|
390 |
return false;
|
| 397 |
} else {
|
391 |
} else {
|
| 398 |
// Success
|
392 |
// Success
|
| 399 |
logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
|
393 |
logger.info("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
|
| 400 |
payment.setDescription("Payment Captured");
|
394 |
payment.setDescription("Payment Captured");
|
| Line 437... |
Line 431... |
| 437 |
payment.setDescription(description);
|
431 |
payment.setDescription(description);
|
| 438 |
payment.setErrorCode(errorCode);
|
432 |
payment.setErrorCode(errorCode);
|
| 439 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
433 |
payment.setStatus(PaymentStatus.FAILED.getValue());
|
| 440 |
payment.setErrorTimestamp(new Date());
|
434 |
payment.setErrorTimestamp(new Date());
|
| 441 |
paymentHandler.updatePayment(payment, attrMap);
|
435 |
paymentHandler.updatePayment(payment, attrMap);
|
| 442 |
createTicketForFailedPayment(payment);
|
436 |
persistPaymentRequiringExtraProcessing(payment);
|
| 443 |
return false;
|
437 |
return false;
|
| 444 |
}else{
|
438 |
}else{
|
| 445 |
//Success
|
439 |
//Success
|
| 446 |
logger.info("Capture attempt successful for EBS payment with id: " + payment.getId());
|
440 |
logger.info("Capture attempt successful for EBS payment with id: " + payment.getId());
|
| 447 |
payment.setGatewayTxnStatus(captureStatus);
|
441 |
payment.setGatewayTxnStatus(captureStatus);
|
| Line 527... |
Line 521... |
| 527 |
|
521 |
|
| 528 |
@Override
|
522 |
@Override
|
| 529 |
public void closeSession() throws TException {
|
523 |
public void closeSession() throws TException {
|
| 530 |
// TODO Auto-generated method stub
|
524 |
// TODO Auto-generated method stub
|
| 531 |
}
|
525 |
}
|
| - |
|
526 |
|
| - |
|
527 |
@Override
|
| - |
|
528 |
public List<Long> getPaymentsRequiringExtraProcessing (
|
| - |
|
529 |
ExtraPaymentProcessingType category) throws TException {
|
| - |
|
530 |
return paymentRequiringExtraProcessingHandler.getPaymentIds(category);
|
| - |
|
531 |
}
|
| - |
|
532 |
|
| - |
|
533 |
@Override
|
| - |
|
534 |
public void markPaymentAsProcessed(long paymentId,
|
| - |
|
535 |
ExtraPaymentProcessingType category) throws TException {
|
| - |
|
536 |
paymentRequiringExtraProcessingHandler.delete(paymentId, category);
|
| - |
|
537 |
}
|
| 532 |
}
|
538 |
}
|