Rev 4449 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.payment.service;import in.shop2020.model.v1.order.AlertType;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.Transaction;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.payments.Payment;import in.shop2020.payments.PaymentException;import in.shop2020.payments.PaymentService.Client;import in.shop2020.payments.PaymentStatus;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import java.util.Calendar;import java.util.Date;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;/*** @author mandeep**/public class PaymentCapturer {private static final String SPACE = " ";private static final Log log = LogFactory.getLog(PaymentCapturer.class);private Client paymentClient;private in.shop2020.model.v1.order.TransactionService.Client transactionClient;private PaymentCapturer() {try {paymentClient = new PaymentClient().getClient();} catch (TTransportException e) {log.error("Could not create payment client", e);}}private void captureAndCreateAlert(long merchantTxnId) throws PaymentException, TException {paymentClient.capturePayment(merchantTxnId, false);}private void createAlertForCapture(long merchantTxnId) throws TransactionServiceException, TException {transactionClient = new TransactionClient().getClient();Transaction transaction = transactionClient.getTransaction(merchantTxnId);String orderString = SPACE;for (Order order : transaction.getOrders()) {orderString.concat(String.valueOf(order.getId()) + SPACE);}transactionClient.addAlert(AlertType.ORDER_READY_TO_ACCEPT.getValue(), transaction.getOrders().get(0).getWarehouse_id() , "Payment captured. Order" +orderString + "now ready to be accepted.");}private void captureAll() throws PaymentException, TException {for (Payment payment : fetchPaymentsToBeCaptured()) {try {captureAndCreateAlert(payment.getMerchantTxnId());createAlertForCapture(payment.getMerchantTxnId());} catch (PaymentException e) {log.error("Could not capture payment id: " + payment.getPaymentId(), e);} catch (TException e) {log.error("Could not capture payment id: " + payment.getPaymentId(), e);} catch (TransactionServiceException e) {log.error("Could not create alert for payment id: " + payment.getPaymentId(), e);}}}private List<Payment> fetchPaymentsToBeCaptured() throws PaymentException, TException {return paymentClient.getPayments(0, getTomorrow().getTime(), PaymentStatus.CAPTURE_IN_PROCESS, 0);}private Date getTomorrow() {Calendar calendar = Calendar.getInstance();calendar.setTime(new Date());calendar.add(Calendar.DAY_OF_MONTH, 1);return calendar.getTime();}/*** @param args* @throws TException* @throws PaymentException*/public static void main(String[] args) throws PaymentException, TException {PaymentCapturer paymentCapturer = new PaymentCapturer();if (args != null && args.length != 0) {for (String arg : args[0].split(",")) {try {long merchantTxnId = Long.parseLong(arg);paymentCapturer.captureAndCreateAlert(merchantTxnId);} catch (PaymentException e) {log.error("Could not capture payment for merchant txn id: " + arg, e);} catch (TException e) {log.error("Could not capture payment for merchant txn id: " + arg, e);}}}else {paymentCapturer.captureAll();}}}